随笔 - 171  文章 - 257  trackbacks - 0
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(33)

随笔分类(225)

随笔档案(171)

相册

技术

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 441255
  • 排名 - 48

最新随笔

最新评论

阅读排行榜

评论排行榜

//ini文件操作类
//write by Khan
//最开头的注释默认的 section为 "###"
//普通的注释和空行 key为"##"

//所以 大家不要让配置文件的key = "##"
支持的注释格式 "#注释"


//LIniFile.h
#ifndef __LINI_FILE_H__
#define __LINI_FILE_H__

#include <string>
#include <vector>
#include <stdio.h>
#include <utility>

using namespace std;





class CLogFileException{
    string m_strError;

public:
    inline CLogFileException(string p_strError){ m_strError = m_strError; }
    inline string OnMessage(){ return this->m_strError; }
};






class LIniFile
{
public:
    LIniFile(const string p_strFileName);
    ~LIniFile(void);

public:
    string GetValue(const string p_strSection, const string p_strKey);
    int SetValue(const string p_strSection, const string p_strKey, const string p_strValue);
    void Print();

    int OpenIni(const string p_strFileName);
    int OpenIni();
    void CloseIni();
protected:


private:
    vector<pair<string, pair<string,string> > >   m_szStrItem;
    string m_strFileName;
    FILE*  m_fileHandle;



  string TrimL(string p_strValue);
  string TrimR(string p_strValue);
  string Trim(string p_strValue);
  void LIniFile::DeleteEnter(char *p_str, int size);
};

#endif //__LINI_FILE_H__











//LIniFile.cpp
#include "LIniFile.h"
#include <algorithm>


LIniFile::LIniFile(const string p_strFileName)
{
  this->m_strFileName = p_strFileName;
  this->m_fileHandle = NULL;
  //this->OpenIni(p_strFileName);
}

LIniFile::~LIniFile(void){
  //this->CloseIni();
}

int LIniFile::OpenIni(){
  return OpenIni(m_strFileName);
}

int  LIniFile::OpenIni(const string p_strFileName){
  this->m_fileHandle = fopen(p_strFileName.c_str(),"rt+");
  if (NULL == this->m_fileHandle) {
    return 0;
  }

  fseek(this->m_fileHandle, 0, SEEK_SET); //将文件指针指向文件开头
  char cTemp[512];
  memset(cTemp, 0, sizeof(cTemp));

  vector<string> vecStrItem;
  int iLen = 0;

  while(NULL != fgets(cTemp, sizeof(cTemp), this->m_fileHandle)){
    DeleteEnter(cTemp, sizeof(cTemp));//去结尾回车符
    Trim(cTemp); //去前后空格
    string str = cTemp;
    vecStrItem.push_back(str);
  }

  this->CloseIni();
 
  pair<string, string> pKVItem;
  pair<string, pair<string, string> > pItem;
  string strSection = "";
  string strCurrentSection = "";

  for (unsigned int i = 0; i < vecStrItem.size(); i++) {
    string strLine = ((string)vecStrItem[i]);

    if( '[' == strLine[0] ){
      strSection = strLine;

      strSection = strSection.erase(0,1);
      if(']' == strSection[strSection.size() -1 ])
        strSection = strSection.erase(strSection.size() - 1 );
      if (strCurrentSection != strSection)
        strCurrentSection = strSection;
     
  }else if( string::npos != strLine.find('=') ){
      if ("" != strCurrentSection ) {
        string strKey;
        string strValue;
        strKey = TrimR( strLine.substr(0, strLine.find('=')) );
        strValue = TrimL( strLine.substr(strLine.find('=') + 1, strLine.size() - strLine.find('=')  ) );
        pKVItem.first = strKey;
        pKVItem.second = strValue;
        pItem.first = strCurrentSection;
        pItem.second = pKVItem;
        m_szStrItem.push_back(pItem);
      }
  }else{
      if ("" != strCurrentSection ) {
          string strKey = "##";
          string strValue = TrimL( strLine );
          pKVItem.first = strKey;
          pKVItem.second = strValue;
          pItem.first = strCurrentSection;
          pItem.second = pKVItem;
          m_szStrItem.push_back(pItem);
    }else{
          string strKey = "##";
          string strValue = TrimL( strLine );
          pKVItem.first = strKey;
          pKVItem.second = strValue;
      pItem.first = "###";
          pItem.second = pKVItem;
          m_szStrItem.push_back(pItem);
    }
  }
  }

  return 1;
}

string LIniFile::GetValue(const string p_strSection, const string p_strKey){
  string strValue;
  for (unsigned i = 0; i< m_szStrItem.size(); i++) {
    if( p_strSection == (string) ( ((pair<string, pair<string, string> >)m_szStrItem[i]).first ) ){
      pair<string, string> pKVItem = ((pair<string, pair<string, string> >)m_szStrItem[i]).second;
      if ( p_strKey == (string)(pKVItem.first)) {
        strValue = (string)(pKVItem.second);
        break;
      }
    }
  }
  return strValue;
}


void LIniFile::Print(){
  string strValue;
  string strKey;
  string strSection;
  for (unsigned i = 0; i< m_szStrItem.size(); i++) {
    strSection = (string) ( ((pair<string, pair<string, string> >)m_szStrItem[i]).first );
    pair<string, string> pKVItem = ((pair<string, pair<string, string> >)m_szStrItem[i]).second;
    strKey = (string)(pKVItem.first);
    strValue = (string)(pKVItem.second);
  //if((strSection != "###") && (strKey != "##") )
    printf("[%s]:%s=%s\n", strSection.c_str(), strKey.c_str(), strValue.c_str());
  }
}


void LIniFile::CloseIni(){
  if(NULL != this->m_fileHandle){
    fclose(this->m_fileHandle);
    this->m_fileHandle = NULL;
  }
}


string LIniFile::TrimL(string p_strValue){
  int i = 0 ;
  if ((i=(int)(p_strValue.length())) < 1)
    return "";

  i = 0;
  while(isspace(p_strValue[i])){
    i++ ;
  }
  p_strValue.erase(0,i);

  return p_strValue;
}

string LIniFile::TrimR(string p_strValue){
  int i = 0;
  if ( (i = (int)(p_strValue.length()-1)) < 1 )
    return "";

  while( ( p_strValue[i] ) == ' ' ){
    i--;
  }
  p_strValue.erase(i + 1);
  return p_strValue;
}


string LIniFile::Trim(string p_strValue){
  return TrimL(TrimR(p_strValue));
}

int LIniFile::SetValue(const string p_strSection, const string p_strKey, const string p_strValue){
  pair<string, string> pKVItem;
  pair<string, pair<string, string> > pItem;

  pKVItem.first = p_strKey;
  pKVItem.second = p_strValue;
  pItem.first = p_strSection;
  pItem.second = pKVItem;

  int bFind = 0;

  for(int i = (int)(m_szStrItem.size())-1; i >= 0; i--){ //找到已有的section, 并在末尾添加
    string bb = m_szStrItem[i].second.first;
    if((m_szStrItem[i].first == p_strSection)  && (m_szStrItem[i].second.first != "##")/* && (m_szStrItem[i].second.second != "")*/){
      m_szStrItem.insert( m_szStrItem.begin()+ i+1, pItem );
      bFind = 1;
      break;
    }
  }

  if(bFind != 1) //如果是新增的section
    m_szStrItem.push_back(pItem);

  this->m_fileHandle = fopen(this->m_strFileName.c_str(),"wt+");
  //this->m_fileHandle = fopen("c:\\aaa.ini","wt+");
  if (NULL == this->m_fileHandle) {
    return 0;
  }

  string strCurSection = "";
  string strTmp = "";

  if((int)(m_szStrItem.size()) > 0){

    strCurSection = m_szStrItem[0].first;
    if(strCurSection != "###"){
      strTmp = "[" + strCurSection + "]" + "\n";
      fputs(strTmp.c_str(), this->m_fileHandle);
    }

    for(int i=0; i<(int)(m_szStrItem.size()); i++){
      if( strCurSection ==  m_szStrItem[i].first ){

        if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second ==""))
          strTmp = "\n";
        else if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second !=""))
          strTmp = m_szStrItem[i].second.second+ "\n";
        else
          strTmp = m_szStrItem[i].second.first + " = " + m_szStrItem[i].second.second+ "\n";

        fputs(strTmp.c_str(), this->m_fileHandle);
      }else{

        strCurSection = m_szStrItem[i].first;
        if(strCurSection != "###"){
          string strTmp = "[" + strCurSection + "]"+ "\n";
          fputs(strTmp.c_str(), this->m_fileHandle);
        }

        if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second ==""))
          strTmp = "\n";
        else if((m_szStrItem[i].second.first == "##") && (m_szStrItem[i].second.second !=""))
          strTmp = m_szStrItem[i].second.second+ "\n";
        else
          strTmp = m_szStrItem[i].second.first + " = " + m_szStrItem[i].second.second+ "\n";

        fputs(strTmp.c_str(), this->m_fileHandle);
      }
    }
  }
  this->CloseIni();
  return 1;
}




void LIniFile::DeleteEnter(char *p_str, int size){
  int i_len = (int)(strlen(p_str));
  if(p_str[i_len-1] == 0x0d || p_str[i_len-1] == 0x0a)
    p_str[i_len-1] = 0x00;

  i_len =  (int)(strlen(p_str));
  if(p_str[i_len-1] == 0x0d || p_str[i_len-1] == 0x0a)
    p_str[i_len-1] = 0x00;
}




调用方式

#include "LIniFile.h"


int main(int argc, char** argv)
{
    LIniFile ini("d:\\My Documents\\Visual Studio 2005\\Projects\\inifile\\cbm.ini");
    ini.OpenIni();
    ini.SetValue("cbm","a", "b");
    ini.SetValue("aaa","a", "b");

    ini.Print();
    ini.CloseIni();

    getchar();
    return 0;
}


处理前的日志文件内容
 1 ## cbm配置文件
 2 
 3 [cbm]
 4 
 5 #sp端口号
 6 sp_port = 07554400001
 7 
 8 # 开通的频道列表
 9 channels = 2001,2002,2003,2004
10 
11 #rsa私钥
12 rsa_key1 = 0004b831414e0b4613922bd35b4b36802bc1e1e81c95a27c958f5382003df646154ca92fc1ce02c3be047a45e9b02a9089b4b90278237c965192a0fcc86bb49bc82ae6
13 rsa_key2 = fdc2de709006b86c7676efdf597626fad633a4f7dc48c445d37eb55fcb3b1abb95baaa826d5390e15fd14ed403fa2d0cb841c650609524ec555e3bc56ca95700000000
14 rsa_key3 = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 rsa_key4 = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001
16 
17 
18 [2001]
19 flag = 1
20 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
21 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
22 
23 [2002]
24 flag = 1
25 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
26 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
27 
28 [2003]
29 flag = 1
30 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
31 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
32 
33 [2004]
34 flag = 1
35 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
36 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b


处理后的ini内容
 1 ## cbm配置文件
 2 
 3 [cbm]
 4 
 5 #sp端口号
 6 sp_port = 07554400001
 7 
 8 # 开通的频道列表
 9 channels = 2001,2002,2003,2004
10 
11 #rsa私钥
12 rsa_key1 = 0004b831414e0b4613922bd35b4b36802bc1e1e81c95a27c958f5382003df646154ca92fc1ce02c3be047a45e9b02a9089b4b90278237c965192a0fcc86bb49bc82ae6
13 rsa_key2 = fdc2de709006b86c7676efdf597626fad633a4f7dc48c445d37eb55fcb3b1abb95baaa826d5390e15fd14ed403fa2d0cb841c650609524ec555e3bc56ca95700000000
14 rsa_key3 = 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 rsa_key4 = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001
16 
17 
18 [2001]
19 flag = 1
20 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
21 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
22 
23 [2002]
24 flag = 1
25 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
26 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
27 = b
28 
29 [2003]
30 flag = 1
31 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
32 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
33 
34 [2004]
35 flag = 1
36 des3_key1 = 3e072bb9698439377cea73f33e0d5c5af1f5ce581f8e585ac4df2e98c8627e84fea8dbfc2ad4e66375187ab021aedc094c37196534c41611262419bfd07822a3ee
37 des3_key2 = a2bd32dd13ffc59535d99e2ae79c866f308fbd1de7466034f4ed3742651731d307e95a1e9e838e5c77f135124c0714efe06cb7c596a6c35e64eb7572b1737b
38 
39 [aaa]
40 = b
41 


posted on 2007-08-07 10:06 Khan 阅读(4546) 评论(12)  编辑 收藏 引用 所属分类: GCC/G++跨平台开发

FeedBack:
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-07 13:26 金庆
我使用CIniFile, 是用stl实现的,应该能用于wince。好像是从CodeProject下载的。  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-07 19:59 罗宾李
strKey = (string)(pKVItem.first);
完全的java风格嘛。。。  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-09 09:35 Khan's Notebook
to 金庆
之前写过一个纯c的版本..但是由于写ini实在太复杂了..所以就废了..

to 罗宾李
对..我是遵照java的ini文件格式来实现的
  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-09 09:45 Khan's Notebook
这段代码没有实现修改对应key的value. 如果需要完整版本.请向我索取  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-25 21:12 peng.jun
wince 不支持STL,这个代码需要改造  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-08-27 10:02 Khan's Notebook
sorry, 我没说太清楚, 我对应的系统是win mobile 5, 代码经过测试..是可以正常运行的..   回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-09-16 17:14 Jim.Liang
测试了一下发现
如果你在同一个小节下面写10个相同名称的键(Key),并分别设置不同的键值,那么在这个小节下会出现10个键名相同但键值不同的项目, 那么在读取这个键的值的时候,因为存在10个相同键名的项,应该返回哪个值呢??????
上面的源码中SetValue函数好像并不会去检测小节中是否已经存在这个键名(Key).  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-09-16 17:16 Jim.Liang
正确的方法应该是在写入一个Key的值的时候先检测一下小节下是否存在Key,如果存在,则用传入的Key的值更新已有的Key的值,如果不存在Key,则添加Key和对应的值.  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-09-18 16:14 Khan's Notebook
sorry. 我再仔细查查..谢谢楼上达人指出  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2007-09-18 16:15 Khan's Notebook
这个是最开始的版本.刚刚想起来.我发布后不久就调试出这个问题.并进行了改进..我迟一点会更新为最新的版本  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2008-07-11 10:16 EUREKA
不大懂...wince下不是UNICODE环境么,可以直接用string读到ini的值么  回复  更多评论
  
# re: 为wince项目写的一个可移植的ini文件读写的class[原创] 2008-07-15 16:42 Khan's Notebook
@EUREKA
win98是ansi环境, 并不表示win98下不能写unicode的文件..  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理