在牛人的指导下,和有了较大改变。
逐行读取配置文件,然后逐行解析~
读取一次之后,将键值对存入map,之后都从map中去取,减少读取文件次数
主要代码如下:
/*** * read config file, addinto map.* @param filepath (in)line text* @param return * -1:error,invalid line* 0:success* */int INIReader::readFile(const wstring &filename) { std::string strFilename(filename.begin(), filename.end()); wifstream infile(strFilename.c_str()); wstring buffer; while (getline(infile, buffer)) { parseContentLine(buffer); } return 0;}/*** * handle single line text,then add into map.* @param filepath (in)line text* @param return * -1:error,invalid line* 0:success* */int INIReader::parseContentLine(wstring &contentLine) { contentLine = trim(contentLine); if (contentLine.size() < 1) { return 0; // blank line } if (contentLine.substr(0, 1) == ANNOTATION_SYMBOL1 || contentLine.substr(0, 1) == ANNOTATION_SYMBOL2) { return 0; // comment } wstring::size_type equalPos = contentLine.find_first_of(L"="); wstring::size_type startPos = 0; wstring::size_type endPos = contentLine.size() - 1; if (equalPos <= startPos || equalPos > endPos) { return -1; // invalid line } wstring key = rtrim(contentLine.substr(startPos, equalPos )); wstring value = ltrim(contentLine.substr(equalPos + 1, endPos)); paramMap.insert(std::make_pair(key, value)); //std::wcout < <<"\t" << value << std::endl; return 0;}
点此整个代码工程