#pragma once #include #include #include #include #include #include #include "google/protobuf/util/json_util.h" #include "tool/error_code.hpp" using google::protobuf::io::FileInputStream; using google::protobuf::io::FileOutputStream; using google::protobuf::io::ZeroCopyInputStream; using google::protobuf::io::CodedInputStream; using google::protobuf::io::ZeroCopyOutputStream; using google::protobuf::io::CodedOutputStream; using google::protobuf::Message; static std::string getFileExtension(const std::string& file_path) { size_t dot_pos = file_path.find_last_of('.'); if (dot_pos != std::string::npos && dot_pos < file_path.length() - 1) { return file_path.substr(dot_pos + 1); } return ""; } static bool readTxtProtobufFile(const std::string &file_path, ::google::protobuf::Message& message) { int fd = open(file_path.c_str(), O_RDONLY); if (fd == -1) return false; auto* input = new FileInputStream(fd); bool success = google::protobuf::TextFormat::Parse(input, &message); delete input; close(fd); return success; } static bool readJsonProtobufFile(const std::string& file_path, ::google::protobuf::Message& message) { std::ifstream file(file_path); std::string json_data; if (file.is_open()) { // 将文件内容读取到字符串 std::string line; while (std::getline(file, line)) { json_data += line; } file.close(); } else { return false; } return google::protobuf::util::JsonStringToMessage(json_data, &message) == absl::OkStatus(); } static Error_manager loadProtobufFile(const std::string &file, ::google::protobuf::Message &message) { std::string ext = getFileExtension(file); if (ext.empty()) { return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s ext failed.", file.c_str()}; } bool ret = false; if (ext == "prototxt") { ret = readTxtProtobufFile(file, message); } else if (ext == "json") { ret = readJsonProtobufFile(file, message); } else { return {PARSE_FAILED, MINOR_ERROR, "protobuf file %s type error.", file.c_str()}; } if (ret) { return {SUCCESS, NORMAL, "prase protobuf file %s success.", file.c_str()}; } else { return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s failed.", file.c_str()}; } } static bool writeTxtProtobufFile(const std::string &file, const google::protobuf::Message &message) { int fd = open(file.c_str(), std::ios::out); FileOutputStream * output = new FileOutputStream(fd); google::protobuf::TextFormat::Print(message, output); delete output; close(fd); return true; } static bool writeJsonProtobufFile(const std::string &file, const google::protobuf::Message &message) { std::string str; google::protobuf::util::JsonPrintOptions options; options.add_whitespace = true; options.always_print_primitive_fields = true; options.preserve_proto_field_names = true; if (google::protobuf::util::MessageToJsonString(message, &str, options) != absl::OkStatus()) { return false; } //输出到文件 std::ofstream os; os.open(file, std::ios::out); if (!os.is_open()) std::cout << "error:can not find or create the file which named \" demo.json\"." << std::endl; os << str; os.close(); return true; } static Error_manager writeProtobufFile(const std::string &file, ::google::protobuf::Message &message) { std::string ext = getFileExtension(file); if (ext.empty()) { return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s ext failed.", file.c_str()}; } bool ret = false; if (ext == "prototxt") { ret = writeTxtProtobufFile(file, message); } else if (ext == "json") { ret = writeJsonProtobufFile(file, message); } else { return {PARSE_FAILED, MINOR_ERROR, "protobuf file %s type error.", file.c_str()}; } if (ret) { return {SUCCESS, NORMAL, "prase protobuf file %s success.", file.c_str()}; } else { return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s failed.", file.c_str()}; } }