123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #pragma once
- #include <fcntl.h>
- #include <unistd.h>
- #include <iostream>
- #include <fstream>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/text_format.h>
- #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()};
- }
- }
|