std::string ConfigFile::_ReadString(std::string section,std::string entry,std::string defaultString) { /* 功能:读取指定的section的指定entry的值到buffer中,未找到则使用缺省配置 参数: section[IN]:指定的section entry[IN]:指定的entry defaultstring[IN]:缺省配置 buffer[IN]:存放entry值的缓冲区 bufLen[IN]:缓冲区长度 返回值: 成功返回buffer中的值的长度 失败返回-1 */ int len = -1; fseek(fin,0,SEEK_SET); char buffer[1024]; memset(buffer,0,1024); int bufLen = sizeof(buffer) -1; if( gotoSection( section.c_str() ) ) { len = _ReadEntry(entry.c_str(), buffer, bufLen, true); } //后面的空格也要去。前面的空格等于没去. //hauk 2013-10-29 modified //percolate(buffer); char *szTmp = percolate(buffer); if(szTmp && strlen(szTmp) != strlen(buffer)) UTILITY::Snprintf(buffer, sizeof(buffer), "%s", szTmp); //end of modification len = strlen(buffer); if( len <= 0 ) //can not read entry, use default string { strncpy( buffer, defaultString.c_str(), bufLen-1 ); buffer[bufLen-1] = 0; len = strlen(buffer); } /** * 支持配置文件值中环境变量扩展 * 环境变量格式 ${环境变量} */ string str(buffer); return _expand_vars(str); // return buffer; }
int ConfigFile::ReadInt(std::string section,std::string entry,int defaultInt) { /* 功能:读取指定的section的指定entry的值以整数形式返回,未找到则使用缺省配置 参数: section[IN]:指定的section entry[IN]:指定的entry defaultInt[IN]:缺省配置 返回值: 没有找到指定entry则返回缺省值 否则返回相应的配置值 */ fseek(fin,0,SEEK_SET); if( gotoSection( section.c_str() ) ) { string tmp = ReadString( section.c_str(), entry.c_str() , "-"); if (tmp != "-") defaultInt = atoi(tmp.c_str()); } return defaultInt; }
//--------------------------------------------------------------------------- // readSection: position to BOF for a new section, // do continuous reads until len < 0 //--------------------------------------------------------------------------- Int32 UdrCfgParser::readSection( const char *section, char *buf, Int32 bufLen , NAString &errorText ) { Int32 len = -1; static NABoolean newSection = TRUE; if (cfgFile) { if (!newSection) len = readPair(cfgFile, buf, bufLen, errorText); else { fseek(cfgFile, 0, SEEK_SET); if( gotoSection( cfgFile, section, errorText ) ) len = readPair(cfgFile, buf, bufLen, errorText); newSection = FALSE; } } if (len < 0) newSection = TRUE; return len; }