c语言内存配置文件,c++实现逐行读取配置文件写入内存的示例

//

//#include "stdafx.h"

#include

#include "ReadProtocol.h"

//去掉字符串首尾的空格

static string strTrim(string aStr)

{

string s = aStr;

unsigned int first, last;

if (string::npos != (first = s.find_first_not_of(' ') ))

s = s.substr(first, s.length()-first);

if (string::npos != (last = s.find_last_not_of(' ') ))

s = s.substr(0, last+1);

return s;

}

///=====================================================================================

ReadProtocol::ReadProtocol(char* FilePath)

{

int iLen = (strlen(FilePath) > MAX_FILEPATH) ? MAX_FILEPATH : strlen(FilePath);

memset(m_IniFile, 0, MAX_FILEPATH);

memcpy(m_IniFile, FilePath, iLen);

}

ReadProtocol::~ReadProtocol()

{

m_SectionMap.clear();

m_StrVect.clear();

}

int ReadProtocol::GetOneSection(string Section, list &Protocol)

{

unsigned int Start = 0;

// 注意这里不能使用 [] 运算符

map::iterator itr = m_SectionMap.find(Section);

if (m_SectionMap.end() == itr)

{

m_ErrPos = Section;

return -5;   // Unknown Section!!

}

else

{

Start = itr->second;

}

vector::iterator it = m_StrVect.begin() + Start + 1;

for (; it!=m_StrVect.end(); ++it)

{

unsigned int First, Last;

First = it->find_first_of ( SECTION_BEGIN_FLG );

Last = it->find_last_of ( SECTION_END_FLG );

// stop when the next Section

if( string::npos != First && string::npos != Last)

{

break;

}

Protocol.push_back(*it);

}

return (int)Protocol.size();

}

int ReadProtocol::ReadIniFile()

{

ifstream fin(m_IniFile);

if (!fin.is_open())

{

return -1; //can'topen file

}

string strLine;

unsigned int Last;

while (std::getline(fin, strLine).good())

{

if ( string::npos !=(Last = strLine.find_last_not_of('\r') ))

{

//delete \r

strLine = strLine.substr(0, Last + 1);

}

PushBackToVector(strLine);

}

fin.close();

if (m_StrVect.empty())

{

return -2; //get noting from file

}

return 0;

}

void ReadProtocol::PushBackToVector(string oneLine)

{

unsigned int uPos;

//去掉行尾注释

if ( string::npos != (uPos = oneLine.find_first_of( COMMENT_FLG ) ) )

{

oneLine = oneLine.substr(0, uPos + 1);

}

//去首尾空格

oneLine = strTrim(oneLine);

if (oneLine.empty() || oneLine.length() < 2) return;

//一行只能有一条记录

unsigned int First, Last;

First = oneLine.find_first_of(SECTION_BEGIN_FLG);

Last = oneLine.find_last_of(SECTION_END_FLG);

// is Section

if( string::npos != First && string::npos != Last)

{

m_SectionMap[ oneLine.substr(First + 1, Last - First - 1) ] = m_StrVect.size();

}

m_StrVect.push_back(oneLine);

}

char* ReadProtocol::GetErrInfo(char* errMsg, int errNo)

{

string errInfo;

switch (errNo)

{

case 0:

{

errInfo = "Success!";

break;

}

case -1:

{

char Path[1024] = {0};

int pLength = 1024;

GetCurrentDirectory(pLength, Path);

errInfo.append("Can't open file. The file name is:==>\"");

errInfo.append( m_IniFile);

errInfo.append("\"\r\nMaybe no such file in Path:");

errInfo.append(Path);

break;

}

case -2:

{

errInfo = "Get noting from file: ";

errInfo.append(m_IniFile);

break;

}

case -3:

{

errInfo = "Analyze file failed. In ==> ";

errInfo.append(m_ErrPos);

break;

}

case -5:

{

errInfo = "\r\nUnknown Section!! ==> \"[";

errInfo.append(m_ErrPos);

errInfo.append("]\"\r\n请检查配置文件中是否有遗漏。");

break;

}

default:

{

errInfo = "请按照正确步骤使用";

}

}

memcpy(errMsg, errInfo.c_str(), errInfo.length());

return errMsg;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值