linux版本中间件

Config.cpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "Config.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #define CFGFILE "cfg.json"
  5. CConfig::CConfig()
  6. {
  7. }
  8. CConfig::~CConfig()
  9. {
  10. }
  11. bool CConfig::load()
  12. {
  13. std::ifstream ifs;
  14. ifs.open(CFGPATH);
  15. Json::CharReaderBuilder builder;
  16. builder["collectComments"] = true;
  17. JSONCPP_STRING errs;
  18. if (!parseFromStream(builder, ifs, &root, &errs)) {
  19. std::cout << errs << std::endl;
  20. //write();
  21. return false;
  22. }
  23. if (ifs.is_open())
  24. ifs.close();
  25. // FS
  26. m_fsIp = root["fsAddr"].asString();
  27. m_fsPort = (int16_t)root["fsPort"].asInt();
  28. m_fsPwd = root["fsPwd"].asString();
  29. // WEBSOCKET
  30. m_wsPort = (int16_t)root["wsPort"].asInt();
  31. m_wssPort = (int16_t)root["wssPort"].asInt();
  32. m_sslFilePath = root["sslFile"].asString();
  33. m_sslPwd = root["sslPwd"].asString();
  34. // db
  35. m_DBhost = root["dbHost"].asString();
  36. m_DBusername = root["dbUser"].asString();
  37. m_DBpasswd = root["dbPwd"].asString();
  38. m_DBdatabase = root["dbName"].asString();
  39. return true;
  40. }
  41. bool CConfig::write()
  42. {
  43. //Json::Value root;
  44. Json::StreamWriterBuilder builder;
  45. // FS
  46. root["fsAddr"] = "192.168.1.10";
  47. root["fsPort"] = 8021;
  48. root["fsPwd"] = "123456";
  49. // WEBSOCKET
  50. root["wsPort"] = 9002;
  51. root["wssPort"] = 9003;
  52. root["sslFile"] = "wss.pem";
  53. root["sslPwd"] = "123456";
  54. // db
  55. m_DBhost = root["dbHost"].asString();
  56. m_DBusername = root["dbUser"].asString();
  57. m_DBpasswd = root["dbPwd"].asString();
  58. m_DBdatabase = root["dbName"].asString();
  59. // db
  60. root["dbHost"] = "tcp://192.168.8.6:3306";
  61. root["dbUser"] = "root";
  62. root["dbPwd"] = "123456";
  63. root["dbName"] = "asr";
  64. const std::string json_file = Json::writeString(builder, root);
  65. std::ofstream ofs;
  66. ofs.open(CFGPATH);
  67. ofs.write(json_file.c_str(), json_file.length());
  68. ofs.close();
  69. return true;
  70. }
  71. std::shared_ptr<CConfig> CConfig::pInstance(new CConfig);
  72. //{
  73. // "":
  74. //}