1 class ErrorCodeConfig{ 2 public: 3 ErrorCodeConfig(){} 4 ErrorCodeConfig(const ErrorCodeConfig&)=delete; 5 ErrorCodeConfig &operator=(const ErrorCodeConfig&) = delete; 6 ~ErrorCodeConfig(){} 7 bool FindErrorCodeStr(/*out*/string &strError,int error_code, int Module,string file_path); 8 private: 9 bool ReadMsgFromjson(string path); 10 string strErrorMsg_; 11 }; 12 bool ErrorCodeConfig::ReadMsgFromjson(string path) 13 { 14 ifstream read_file(path.c_str(), ios::binary); 15 if (read_file.is_open()){ 16 /*send_data = csv_data;*/ 17 istream::pos_type current_pos1 = read_file.tellg();//记录下当前位置 18 read_file.seekg(0, ios_base::end);//移动到文件尾 19 istream::pos_type file_size1 = read_file.tellg();//取得当前位置的指针长度->即文件长度 20 read_file.seekg(0, ios_base::beg);//移动到原来的位置 21 if (file_size1 > 0) 22 { 23 char* data1 = new char[file_size1]; 24 memset(data1, 0, file_size1); 25 read_file.read(data1, file_size1 * sizeof(char)); 26 strErrorMsg_ =std::move(string(data1, file_size1)); 27 read_file.close(); 28 delete[] data1; 29 data1 = nullptr; 30 return true; 31 } 32 read_file.close(); 33 } 34 return false; 35 } 36 bool ErrorCodeConfig::FindErrorCodeStr(string &strError, int error_code, int Module, string file_path) 37 { 38 if (0 == strErrorMsg_.size()) 39 { 40 if (!ReadMsgFromjson(file_path)) 41 { 42 return false; 43 } 44 } 45 string str_err; 46 switch (Module) 47 { 48 case MODULE3: 49 str_err = "MODULE3" + std::to_string(error_code); 50 break; 51 case MODULE2: 52 str_err = "MODULE2" + std::to_string(error_code); 53 break; 54 case MODULE1: 55 str_err = "MODULE1" + std::to_string(error_code); 56 break; 57 default: 58 str_err = error_code; 59 break; 60 61 } 62 string::size_type p = strErrorMsg_.find(str_err); 63 if (p == string::npos) 64 { 65 return false; 66 } 67 string::size_type maohao = strErrorMsg_.find(":", p); 68 if (maohao == string::npos) 69 { 70 return false; 71 } 72 string::size_type left_fen = strErrorMsg_.find(""", maohao + 1); 73 if (left_fen == string::npos) 74 { 75 return false; 76 } 77 string::size_type right_fen = strErrorMsg_.find(""", left_fen + 1); 78 if (right_fen == string::npos) 79 { 80 return false; 81 } 82 strError = strErrorMsg_.substr(left_fen + 1, right_fen - left_fen-1); 83 if (0==strError.size()) 84 { 85 return false; 86 } 87 88 //查找错误码大于一个的情况 89 string::size_type pos_last = right_fen; 90 string::size_type pos_now = string::npos; 91 while (-1!=(pos_now = strErrorMsg_.find(str_err, pos_last))) 92 { 93 //此处阈值给100,防止error_1下面出现k_error_1,一行数据不会超过100字节 94 if (pos_now - pos_last > 100) 95 { 96 //走到此处也算找到错误码 97 return true; 98 } 99 100 maohao = strErrorMsg_.find(":", pos_now); 101 if (maohao == string::npos) 102 { 103 return false; 104 } 105 left_fen = strErrorMsg_.find(""", maohao + 1); 106 if (left_fen == string::npos) 107 { 108 return false; 109 } 110 right_fen = strErrorMsg_.find(""", left_fen + 1); 111 if (right_fen == string::npos) 112 { 113 return false; 114 } 115 strError += strErrorMsg_.substr(left_fen + 1, right_fen - left_fen - 1); 116 pos_last = right_fen; 117 } 118 if (0 == strError.size()) 119 { 120 return false; 121 } 122 return true; 123 }