#include "../pch/pch.h" #include "CConfigReader.h" std::map CConfigReader::m_mapConfigInfo; CConfigReader::CConfigReader() { } CConfigReader::~CConfigReader() { } /* *读取配置文件内容,以#注释的忽略掉 **/ void CConfigReader::ReadConfigFile() { ifstream configFile; string path = "/usr/local/lewaimai_pos_windows_server/conf/lewaimai_pos_windows_server.conf"; configFile.open(path.c_str()); string str_line; if (configFile.is_open()) { while (!configFile.eof()) { getline(configFile, str_line); //过滤掉注释信息,即如果首个字符为#就过滤掉这一行 if (str_line.find('#') == 0) { continue; } size_t pos = str_line.find('='); string str_key = str_line.substr(0, pos); string str_value = str_line.substr(pos + 1); //去掉首尾的空格 CLewaimaiString::trim(str_key); CLewaimaiString::trim(str_value); CConfigReader::m_mapConfigInfo[str_key] = str_value; } } else { cout << "找不到配置文件,启动失败!"; LOG_INFO("找不到配置文件,启动失败!"); exit(-1); } } std::string CConfigReader::getConfigValue(std::string key) { if (CConfigReader::m_mapConfigInfo.find(key) == CConfigReader::m_mapConfigInfo.end()) { cout << "找不到配置文件参数:" << key.c_str() << endl; LOG_INFO("找不到配置文件参数:" << key.c_str()); //找不到就返回空字符串 return ""; } return CConfigReader::m_mapConfigInfo[key]; } bool CConfigReader::IsDebugMode() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return true; } return false; } std::string CConfigReader::getPathPlanningHost() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_pathplanning_host"); } else { return CConfigReader::getConfigValue("release_pathplanning_host"); } } std::string CConfigReader::getMysqlHost() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_host"); } else { return CConfigReader::getConfigValue("release_host"); } } std::string CConfigReader::getMysqlPort() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_port"); } else { return CConfigReader::getConfigValue("release_port"); } } std::string CConfigReader::getMysqlUsername() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_username"); } else { return CConfigReader::getConfigValue("release_username"); } } std::string CConfigReader::getMysqlPassword() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_password"); } else { return CConfigReader::getConfigValue("release_password"); } } std::string CConfigReader::getDBName() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_dbname"); } else { return CConfigReader::getConfigValue("release_dbname"); } } std::string CConfigReader::getTaskQueue() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_pos_task_queue"); } else { return CConfigReader::getConfigValue("release_pos_task_queue"); } } std::string CConfigReader::getResultQueue() { std::string mode = CConfigReader::getConfigValue("mode"); if (mode == "debug") { return CConfigReader::getConfigValue("debug_dispatch_result_queue"); } else { return CConfigReader::getConfigValue("release_dispatch_result_queue"); } } int CConfigReader::getDeliverymanMaxOrderNum() { std::string deliveryman_max_order_num = CConfigReader::getConfigValue("deliveryman_max_order_num"); int n_deliveryman_max_order_num = stoi(deliveryman_max_order_num); //最大不能超过10 if (n_deliveryman_max_order_num > 10) { n_deliveryman_max_order_num = 10; } if (n_deliveryman_max_order_num < 1) { n_deliveryman_max_order_num = 1; } return n_deliveryman_max_order_num; }