那谁的技术博客

感兴趣领域:高性能服务器编程,存储,算法,Linux内核
随笔 - 210, 文章 - 0, 评论 - 1183, 引用 - 0
数据加载中……

服务器公共库开发--读取ini文件格式的类

头文件config.h
/********************************************************************
    created:    2008/07/28
    filename:     config.h
    author:        Lichuang
                
    purpose:    封装读取ini格式的配置文件操作
********************************************************************
*/

#ifndef __CONFIG_H__
#define __CONFIG_H__

#include 
<string>
#include 
<map>

using namespace std;

class CConfig
{
public:
    CConfig();
    CConfig(
const char* pFileName);
    CConfig(
const string& strFileName);
    
~CConfig();

    
int Init();
    
int Init(const char* pFileName);
    
int Init(const string& strFileName);
    
int Dump();

    
int ReadItem(const string& strSection, const string& strKey, string& strValue);
    
int WriteItem(const string& strSection, const string& strKey, const string& strValue);

private:
    
int LoadFile();
    
int WriteFile();

    
int ReadLine(FILE* pFile, string& strLine);
    
int TrimString(string& strToken);
private:
    
string m_strFileName;

    typedef map
<stringstring> ConfigType;
    map
<string, ConfigType> m_mSec2Config;
};

#endif /* __CONFIG_H__ */

cpp文件config.cpp:
/********************************************************************
    created:    2008/07/28
    filename:     config.h
    author:        Lichuang
                
    purpose:    封装读取ini格式的配置文件操作
********************************************************************
*/

#include 
"config.h"
#include 
"comdef.h"
#include 
<stdio.h>
#include 
<iostream>

CConfig::CConfig()
{
}

CConfig::CConfig(
const char* pFile)
    : m_strFileName(pFile)
{
}

CConfig::CConfig(
const string& strFile)
    : m_strFileName(strFile)
{
}

CConfig::
~CConfig()
{
}

int CConfig::Init(const char* pFileName)
{
    m_strFileName 
= pFileName;
    
return LoadFile();
}

int CConfig::Init(const string& strFileName)
{
    m_strFileName 
= strFileName;
    
return LoadFile();
}
int CConfig::Init()
{
    
return LoadFile();
}

int CConfig::Dump()
{
    map
<string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
    ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;

    
while (tSecIter1 != tSecIter2)
    {
        cout 
<< "[" << tSecIter1->first << "]" << endl;
        tConfigTypeIter1 
= tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
        
while (tConfigTypeIter1 != tConfigTypeIter2)
        {
            cout 
<< tConfigTypeIter1->first << "=" << tConfigTypeIter1->second << endl;

            
++tConfigTypeIter1;
        }
        cout 
<< endl;
        
++tSecIter1;
    }

    
return 0;
}

int CConfig::ReadItem(const string& strSection, const string& strKey, string& strValue)
{
    
if (!m_mSec2Config.count(strSection))
    {
        
return -1;
    }

    ConfigType
& tConfigType = m_mSec2Config[strSection];
    strValue 
= tConfigType[strKey];

    
return (strValue.empty()) ? -1 : 0;
}

int CConfig::WriteItem(const string& strSection, const string& strKey, const string& strValue)
{
    ConfigType
& tConfigType = m_mSec2Config[strSection];
    
if (tConfigType.count(strKey))
    {
        
return -1;
    }
    tConfigType[strKey] 
= strValue;

    
return WriteFile();
}

int CConfig::LoadFile()
{
    FILE
* pFile;

    
if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "r")))
    {
        
return -1;
    }

    
string strLine, strSection;
    
string strKey, strValue;
    size_t nPos, nEndPos;
    ConfigType tConfigType;
    
while (0 == ReadLine(pFile, strLine))
    {
        
if (string::npos != (nPos = strLine.find_first_of("[")))
        {
            
if (string::npos == (nEndPos = strLine.find_first_of("]")))
            {
                ::fclose(pFile);
                
return -1;
            }

            strSection 
= strLine.substr(nPos + 1, nEndPos - nPos - 1);
            
if (0 > TrimString(strSection))
            {
                ::fclose(pFile);
                
return -1;
            }
        }
        
else if (string::npos != (nPos = strLine.find_first_of("=")))
        {
            strKey 
= strLine.substr(0, nPos);
            strValue 
= strLine.substr(nPos + 1);

            
if (0 > TrimString(strKey) || 0 > TrimString(strValue) || strSection.empty())
            {
                ::fclose(pFile);
                
return -1;
            }

            m_mSec2Config[strSection][strKey] 
= strValue;
        }
    } 

    
return ::fclose(pFile);
}

int CConfig::WriteFile()
{
    FILE
* pFile;

    
if (NULL == (pFile = ::fopen(m_strFileName.c_str(), "w")))
    {
        
return -1;
    }

    map
<string, ConfigType>::iterator tSecIter1 = m_mSec2Config.begin(), tSecIter2 = m_mSec2Config.end();
    ConfigType::iterator tConfigTypeIter1, tConfigTypeIter2;

    
string strSection, strConfig;
    
while (tSecIter1 != tSecIter2)
    {
        strSection 
= string("["+ tSecIter1->first + string("]\n");
        ::fwrite(strSection.c_str(), 
sizeof(char), strSection.length(), pFile);
        tConfigTypeIter1 
= tSecIter1->second.begin(), tConfigTypeIter2 = tSecIter1->second.end();
        
while (tConfigTypeIter1 != tConfigTypeIter2)
        {
            strConfig 
= tConfigTypeIter1->first + string("="+ tConfigTypeIter1->second + string("\n");
            ::fwrite(strConfig.c_str(), 
sizeof(char), strConfig.length(), pFile);

            
++tConfigTypeIter1;
        }
        ::fwrite(
"\n"sizeof(char), 1, pFile);
        
++tSecIter1;
    }

    
return ::fclose(pFile);
}

int CConfig::ReadLine(FILE* pFile, string& strLine)
{
    
char szBuff[BUFFER_LEN];
    
int nLen;

    
do 
    {
        
if (NULL == ::fgets(szBuff, BUFFER_LEN, pFile))
        {
            
return -1;
        }
        
if (0 < (nLen = ::strlen(szBuff)))
        {
            
break;
        }
    } 
while (true);

    szBuff[nLen 
- 1= '\0';

    strLine 
= szBuff;

    
return 0;
}

int CConfig::TrimString(string& strToken)
{
    
if (strToken.empty())
    {
        
return -1;
    }

    size_t nPos    
= strToken.find_first_not_of(" \t");
    size_t nEndPos 
= strToken.find_last_not_of(" \t");

    strToken 
= strToken.substr(nPos, nEndPos - nPos + 1);

    
return (strToken.empty()) ? -1 : 0;
}

实现主要采用STL, 实现了ini格式文件的读,写,已经打印文件信息等功能,暂时觉得这些功能已经够用了,以后有需要再进行添加.应该还少了一个宏BUFFER_LEN的定义,这个宏在一个公用的头文件中定义,如果你想试用这个类,可以自行进行定义,我默认定义为1024字节大小.



posted on 2008-07-29 00:57 那谁 阅读(4395) 评论(7)  编辑 收藏 引用 所属分类: C\C++服务器设计

评论

# re: 服务器公共库开发--读取ini文件格式的类[未登录]  回复  更多评论   

为什么不用 boost::program_options
2008-07-29 16:27 | noname

# re: 服务器公共库开发--读取ini文件格式的类[未登录]  回复  更多评论   

@noname
我不用boost,也不会用.
2008-07-29 16:36 |

# re: 服务器公共库开发--读取ini文件格式的类  回复  更多评论   

一个问题:
你使用map来存储,一来无法保持用户key的次序,二来无法设置相同的key
2008-07-29 16:39 | 小明

# re: 服务器公共库开发--读取ini文件格式的类[未登录]  回复  更多评论   

@小明
呃..貌似我还没有遇到过用重复KEY的情况,这个类对我而言,目前为止是够用的.
2008-07-29 16:52 |

# re: 服务器公共库开发--读取ini文件格式的类[未登录]  回复  更多评论   

@小明
另外,key的顺序确实是按照默认的字典排序,不过我想,也不打紧吧:)
2008-07-29 16:54 |

# re: 服务器公共库开发--读取ini文件格式的类  回复  更多评论   

ini文件操作属于通用的非关键性能的组件,新的实现不知道有什么特别的优点。
2008-07-29 17:06 | x-matrix

# re: 服务器公共库开发--读取ini文件格式的类  回复  更多评论   

ini文件中有注释怎么办?

linux中主要采用ini来保存配置,不知其中有没有实现一个公用的类,还是每个软件中都去实现一遍?
2009-05-07 16:41 | someone

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