bool CSearch_func :: x_StringMayContainPlural(const string& str) const { char last_letter, second_to_last_letter, next_letter; bool may_contain_plural = false; string word_skip = " ,"; unsigned len; if (str.empty()) { return false; } vector <string> arr; arr = NStr::Tokenize(str, " ,", arr, NStr::eMergeDelims); if (arr.size() == 1) { // doesn't have ', ', or the last char is ', ' len = arr[0].size(); if (len == 1) { return false; } last_letter = arr[0][len-1]; second_to_last_letter = arr[0][len-2]; next_letter = (len == str.size()) ? ',' : str[len]; may_contain_plural = x_DoesStrContainPlural(arr[0], last_letter, second_to_last_letter, next_letter); } else { string strtmp(str); size_t pos; vector <string>::const_iterator jt; ITERATE (vector <string>, it, arr) { pos = strtmp.find(*it); len = (*it).size(); if (len == 1) { strtmp = CTempString(strtmp).substr(pos+len); strtmp = NStr::TruncateSpaces(strtmp, NStr::eTrunc_Begin); continue; } last_letter = (*it)[len-1]; second_to_last_letter = (*it)[len-2]; next_letter = (len == strtmp.size()) ? ',' : strtmp[pos+len]; may_contain_plural = x_DoesStrContainPlural(*it, last_letter, second_to_last_letter, next_letter); if (may_contain_plural) { break; } jt = it; if (++jt != arr.end()) { // not jt++ strtmp = CTempString(strtmp).substr(strtmp.find(*jt)); } } }
bool ZCE_INI_Pt::write_privateprofile_section( const char *sec_name, const char *write_string, const char *file_name) { if (sec_name == NULL || write_string == NULL || write_string == NULL) { return false; } FILE *pfile = fopen(file_name, "r+t"); //文件打不开, if (pfile == NULL) { return false; } //8*1024,1行的最大值 char choneline[LINE_BUFFER_LEN + 1], line[LINE_BUFFER_LEN + 1], *read_ret = NULL; //新文件保存数据区 std::vector<std::string> strarytmp; line[0] = '\0'; //表示是否找到App,key bool bApp = false; while (!feof(pfile)) { //填写到临时数据区 if (line[0] != '\0') { std::string strtmp(line); strarytmp.push_back(line); } read_ret = fgets(choneline, LINE_BUFFER_LEN, pfile); if (NULL == read_ret) { return false; } memmove(line, choneline, strlen(choneline) + 1); //整理 ZCE_LIB::strtrim(choneline); //注释行,空行 if (choneline[0] == ';' || choneline[0] == '#' || choneline[0] == '\0') { continue; } //[Section] if (choneline[0] == '[' && choneline[strlen(choneline) - 1] == ']') { //去掉'[',']' memmove(choneline, choneline + 1, strlen(choneline) - 1); choneline[strlen(choneline) - 2] = '\0'; //规整 ZCE_LIB::strtrim(choneline); //比较Section部分 if (ZCE_LIB::strcasecmp(choneline, sec_name) == 0) { bApp = true; //要修改这个App snprintf(line, LINE_BUFFER_LEN, "[%s]\n", write_string); break; } } } // if (line[0] != '\0') { std::string strtmp(line); strarytmp.push_back(line); } //如果找到App ,并且找到Key或者key == NUL ,表示工作已经完成, if (bApp == true) { while (!feof(pfile)) { read_ret = fgets(line, LINE_BUFFER_LEN, pfile); if (NULL == read_ret) { return false; } std::string strtmp(line); strarytmp.push_back(line); } fclose(pfile); //将信息写会文件 pfile = fopen(file_name, "w+t"); for (size_t j = 0; j < strarytmp.size(); ++j) { fputs(strarytmp[j].c_str(), pfile); } //关闭文件 fclose(pfile); return true; } return false; }
//写INI文件的谋个字段, bool ZCE_INI_Pt::write_privateprofile_string( const char *sec_name, const char *key_name, const char *write_string, const char *file_name) { int operate(0); if (sec_name == NULL && key_name == NULL && write_string == NULL) { return false; } if (sec_name == NULL) { operate = 1; } else { operate = 2; } FILE *pfile; pfile = fopen(file_name, "r+t"); //文件打不开, if (pfile == NULL) { pfile = fopen(file_name, "w+t"); if (pfile == NULL) { return false; } if (sec_name != NULL) { fprintf(pfile, "[%s]\n", sec_name); } fprintf(pfile, "%s=%s", key_name, write_string); fclose(pfile); return true; } //1行的最大值 char choneline[LINE_BUFFER_LEN + 1], line[LINE_BUFFER_LEN + 1], chkey[LINE_BUFFER_LEN + 1], chstring[LINE_BUFFER_LEN + 1]; if (operate == 1) { //如果没有相关的App,将section=string 写在末尾 fseek(pfile, 0, SEEK_END); fprintf(pfile, "%s", "\n"); fprintf(pfile, "%s=%s", key_name, write_string); return true; } if (operate == 2) { //新文件保存数据区 std::vector<std::string> strarytmp; line[0] = '\0'; //表示是否找到App,key bool bApp = false, bkey = false; while (!feof(pfile)) { //填写到临时文件 if (line[0] != '\0') { std::string strtmp(line); strarytmp.push_back(line); } if (fgets(choneline, LINE_BUFFER_LEN, pfile) == NULL) { break; } memmove(line, choneline, strlen(choneline) + 1); //整理 ZCE_LIB::strtrim(choneline); //注释行,空行 if (choneline[0] == ';' || choneline[0] == '#' || choneline[0] == '\0') { continue; } //[Section] if (choneline[0] == '[' && choneline[strlen(choneline) - 1] == ']') { //已经找到下一个Section,没有发现相关的Key,返回默认值 if (bApp == true) { if (key_name != NULL && write_string != NULL) { snprintf(choneline, LINE_BUFFER_LEN, "%s=%s\n", key_name, write_string); choneline[LINE_BUFFER_LEN] = '\0'; std::string strtmp(choneline); strarytmp.push_back(choneline); } break; } choneline[strlen(choneline) - 1] = '\0'; //规整 ZCE_LIB::strtrimleft(choneline); //去掉'[',']' memmove(choneline, choneline + 1, strlen(choneline)); ZCE_LIB::strtrimright(choneline); //比较Section部分 if (ZCE_LIB::strcasecmp(choneline, sec_name) == 0) { bApp = true; //要删除这个App if (key_name == NULL) { line[0] = '\0'; } continue; } } //如果[Section] 已经找到 if (bApp == true) { //如果Key==NULL,不用查询,继续 if (key_name == NULL) { line[0] = '\0'; continue; } //找到第一个'=' char *str = strstr(choneline, "="); //如果找到 if (str != NULL) { char *snext = str + 1; *str = '\0'; strncpy(chkey, choneline, LINE_BUFFER_LEN); strncpy(chstring, snext, LINE_BUFFER_LEN); //规整 ZCE_LIB::strtrim(chkey); ZCE_LIB::strtrim(chstring); //找到对应的Key if (ZCE_LIB::strcasecmp(chkey, key_name) == 0) { //表示找到键值 bkey = true; //如果Keystring==NULL则删除Key,如果Keystring!=NULL,则写入。 if (write_string != NULL) { snprintf(line, LINE_BUFFER_LEN, "%s=%s\n", key_name, write_string); break; } else { line[0] = '\0'; break; } } } } } // if (line[0] != '\0') { std::string strtmp(line); strarytmp.push_back(line); } //如果找到App ,并且找到Key或者key == NUL ,表示工作已经完成, if (bApp == true) { if (key_name != NULL && write_string != NULL && feof(pfile) && bkey != true) { snprintf(line, LINE_BUFFER_LEN, "\n%s=%s", key_name, write_string); std::string strtmp(line); strarytmp.push_back(line); } while (!feof(pfile)) { if (fgets(line, LINE_BUFFER_LEN, pfile) == NULL) { break; } std::string strtmp(line); strarytmp.push_back(line); } fclose(pfile); //将信息写会文件 pfile = fopen(file_name, "w+t"); for (size_t j = 0; j < strarytmp.size(); ++j) { fputs(strarytmp[j].c_str(), pfile); } //关闭文件 fclose(pfile); return true; } else if (bApp == false) { //将App,Key String全部写入 if (bkey == false && key_name != NULL && write_string != NULL) { fseek(pfile, 0, SEEK_END); fprintf(pfile, "\n[%s]", sec_name); fprintf(pfile, "\n%s=%s", key_name, write_string); fclose(pfile); return true; } else { fclose(pfile); return false; } } fclose(pfile); return false; } //No rearch return false; }