load_protobuf.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <google/protobuf/io/zero_copy_stream_impl.h>
  7. #include <google/protobuf/text_format.h>
  8. #include "google/protobuf/util/json_util.h"
  9. #include "tool/error_code.hpp"
  10. using google::protobuf::io::FileInputStream;
  11. using google::protobuf::io::FileOutputStream;
  12. using google::protobuf::io::ZeroCopyInputStream;
  13. using google::protobuf::io::CodedInputStream;
  14. using google::protobuf::io::ZeroCopyOutputStream;
  15. using google::protobuf::io::CodedOutputStream;
  16. using google::protobuf::Message;
  17. static std::string getFileExtension(const std::string& file_path) {
  18. size_t dot_pos = file_path.find_last_of('.');
  19. if (dot_pos != std::string::npos && dot_pos < file_path.length() - 1) {
  20. return file_path.substr(dot_pos + 1);
  21. }
  22. return "";
  23. }
  24. static bool readTxtProtobufFile(const std::string &file_path, ::google::protobuf::Message& message)
  25. {
  26. int fd = open(file_path.c_str(), O_RDONLY);
  27. if (fd == -1) return false;
  28. auto* input = new FileInputStream(fd);
  29. bool success = google::protobuf::TextFormat::Parse(input, &message);
  30. delete input;
  31. close(fd);
  32. return success;
  33. }
  34. static bool readJsonProtobufFile(const std::string& file_path, ::google::protobuf::Message& message) {
  35. std::ifstream file(file_path);
  36. std::string json_data;
  37. if (file.is_open()) {
  38. // 将文件内容读取到字符串
  39. std::string line;
  40. while (std::getline(file, line)) {
  41. json_data += line;
  42. }
  43. file.close();
  44. } else {
  45. return false;
  46. }
  47. return google::protobuf::util::JsonStringToMessage(json_data, &message) == absl::OkStatus();
  48. }
  49. static Error_manager loadProtobufFile(const std::string &file, ::google::protobuf::Message &message) {
  50. std::string ext = getFileExtension(file);
  51. if (ext.empty()) {
  52. return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s ext failed.", file.c_str()};
  53. }
  54. bool ret = false;
  55. if (ext == "prototxt") {
  56. ret = readTxtProtobufFile(file, message);
  57. } else if (ext == "json") {
  58. ret = readJsonProtobufFile(file, message);
  59. } else {
  60. return {PARSE_FAILED, MINOR_ERROR, "protobuf file %s type error.", file.c_str()};
  61. }
  62. if (ret) {
  63. return {SUCCESS, NORMAL, "prase protobuf file %s success.", file.c_str()};
  64. } else {
  65. return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s failed.", file.c_str()};
  66. }
  67. }
  68. static bool writeTxtProtobufFile(const std::string &file, const google::protobuf::Message &message) {
  69. int fd = open(file.c_str(), std::ios::out);
  70. FileOutputStream * output = new FileOutputStream(fd);
  71. google::protobuf::TextFormat::Print(message, output);
  72. delete output;
  73. close(fd);
  74. return true;
  75. }
  76. static bool writeJsonProtobufFile(const std::string &file, const google::protobuf::Message &message) {
  77. std::string str;
  78. google::protobuf::util::JsonPrintOptions options;
  79. options.add_whitespace = true;
  80. options.always_print_primitive_fields = true;
  81. options.preserve_proto_field_names = true;
  82. if (google::protobuf::util::MessageToJsonString(message, &str, options) != absl::OkStatus()) {
  83. return false;
  84. }
  85. //输出到文件
  86. std::ofstream os;
  87. os.open(file, std::ios::out);
  88. if (!os.is_open())
  89. std::cout << "error:can not find or create the file which named \" demo.json\"." << std::endl;
  90. os << str;
  91. os.close();
  92. return true;
  93. }
  94. static Error_manager writeProtobufFile(const std::string &file, ::google::protobuf::Message &message) {
  95. std::string ext = getFileExtension(file);
  96. if (ext.empty()) {
  97. return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s ext failed.", file.c_str()};
  98. }
  99. bool ret = false;
  100. if (ext == "prototxt") {
  101. ret = writeTxtProtobufFile(file, message);
  102. } else if (ext == "json") {
  103. ret = writeJsonProtobufFile(file, message);
  104. } else {
  105. return {PARSE_FAILED, MINOR_ERROR, "protobuf file %s type error.", file.c_str()};
  106. }
  107. if (ret) {
  108. return {SUCCESS, NORMAL, "prase protobuf file %s success.", file.c_str()};
  109. } else {
  110. return {PARSE_FAILED, MINOR_ERROR, "prase protobuf file %s failed.", file.c_str()};
  111. }
  112. }