| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "Config.h"
- #include <fstream>
- #include <iostream>
- #define CFGFILE "cfg.json"
- CConfig::CConfig()
- {
- }
- CConfig::~CConfig()
- {
- }
- bool CConfig::load()
- {
- std::ifstream ifs;
- ifs.open(CFGPATH);
- Json::CharReaderBuilder builder;
- builder["collectComments"] = true;
- JSONCPP_STRING errs;
- if (!parseFromStream(builder, ifs, &root, &errs)) {
- std::cout << errs << std::endl;
- //write();
- return false;
- }
- if (ifs.is_open())
- ifs.close();
- // FS
- m_fsIp = root["fsAddr"].asString();
- m_fsPort = (int16_t)root["fsPort"].asInt();
- m_fsPwd = root["fsPwd"].asString();
- // WEBSOCKET
- m_wsPort = (int16_t)root["wsPort"].asInt();
- m_wssPort = (int16_t)root["wssPort"].asInt();
- m_sslFilePath = root["sslFile"].asString();
- m_sslPwd = root["sslPwd"].asString();
- // db
- m_DBhost = root["dbHost"].asString();
- m_DBusername = root["dbUser"].asString();
- m_DBpasswd = root["dbPwd"].asString();
- m_DBdatabase = root["dbName"].asString();
- return true;
- }
- bool CConfig::write()
- {
- //Json::Value root;
- Json::StreamWriterBuilder builder;
- // FS
- root["fsAddr"] = "192.168.1.10";
- root["fsPort"] = 8021;
- root["fsPwd"] = "123456";
- // WEBSOCKET
- root["wsPort"] = 9002;
- root["wssPort"] = 9003;
- root["sslFile"] = "wss.pem";
- root["sslPwd"] = "123456";
- // db
- m_DBhost = root["dbHost"].asString();
- m_DBusername = root["dbUser"].asString();
- m_DBpasswd = root["dbPwd"].asString();
- m_DBdatabase = root["dbName"].asString();
- // db
- root["dbHost"] = "tcp://192.168.8.6:3306";
- root["dbUser"] = "root";
- root["dbPwd"] = "123456";
- root["dbName"] = "asr";
- const std::string json_file = Json::writeString(builder, root);
- std::ofstream ofs;
- ofs.open(CFGPATH);
- ofs.write(json_file.c_str(), json_file.length());
- ofs.close();
- return true;
- }
- std::shared_ptr<CConfig> CConfig::pInstance(new CConfig);
- //{
- // "":
- //}
|