读写init文件

#ifndef __CINI_FILE__
#define __CINI_FILE__
#include <string>
#include <vector>

using namespace std;
/*
程序说明
1.从INI文件中读取参数
2.将参数写入INI格式文件
3.[sectin]必须第一个字符为'[',并以']'结束
4.';'后的字符串全部为注释
5.参数以name=value的方式表示,每一行只能设置一个参数,'='不能含有空格
6.没有对程序中注释做额外的处理,在写入文件时,没有考虑将注释写入INI文件
7.关于INI文件格式定义请参考:
[url]http://www.microsoft.com/technet/archive/wfw/0_welcom.mspx?mfr=true[/url]
*/
//参数行

struct CIniEntry
{
    CIniEntry(){}
    CIniEntry(char* szName,char* szValue):m_strName(szName),m_strValue(szValue){}
    string m_strName;
    string m_strValue;
};
//注释行

struct CIniComment
{
    CIniComment(){}
    CIniComment(char* strCmt):m_strComment(strCmt){}
    string m_strComment;

};
//Section

struct CIniSection 
{
    string m_isName;
    vector<CIniEntry> m_ieEntry;
    vector<CIniComment> m_icComment;
};

class CIniFile
{
    public:
    //从INI文件中读取参数

    int ReadIniFile(char* szIniFile);
    //将参数写入INI文件

    int WriteIniFile(char* szIniFile);
    //删除字符串两端的空格

    int Trim(char* &szString);
    //去除字符串中的注释

    int RemoveComment(char* szLine);

    //根据节名、键名 得到对应的键值
    string GetKeyValue(string section,string keyname);

    //根据节名、键名、键值 添加相应的行
    void SetKeyValue(string section,string keyname,string keyvalue);

private:
        vector<CIniSection> m


_isSection;
};
#endif
#include "IniFile.h"
#include <fstream>
using namespace std;

//一行最大字符数为260`这里写代码片`

#define MAX_COLS 260

#define E_OK                0x01L
#define E_OPEN_FILE_FAILED    0x00L

/*
功能
    读取INI文件
参数
    szIniFile    in    读入的INI文件名称
返回值
    E_OK                调用成功
    E_OPEN_FILE_FAILED    打开文件错我
*/
int CIniFile::ReadIniFile(char* szIniFile)
{
    ifstream fIniFile(szIniFile);
    if(fIniFile == NULL)
    {
        return E_OPEN_FILE_FAILED;
    }
    char szLine[MAX_COLS] = {0};
    while (fIniFile.getline(szLine,MAX_COLS))
    {
        char* p = szLine;
        //是否为[]

        if( *p == '[')
        {
            RemoveComment( p );
            char* pEnd = strchr( p ,']');
            if( pEnd == NULL)
                continue;
            *pEnd = 0;
            CIniSection is;
            is.m_isName = string( p + 1 );
            m_isSection.push_back(is);
            continue;
        }
        //是否为;

        Trim( p );
        if( *p == ';')
        {
            m_isSection[m_isSection.size() - 1].m_icComment.push_back(p + 1);
            continue;
        }
        //否则视为entry

        //p = szLine;

        char* pTemp = strchr( p,'=');
        if(pTemp == NULL)
        {
            continue;
        }
        *pTemp = 0;
        //创建一个Entry

        CIniEntry ie;
        ie.m_strName = p ;
        ie.m_strValue = pTemp + 1;
        //将Entry加入到响应的Section

        m_isSection[m_isSection.size() - 1 ].m_ieEntry.push_back(ie);

        memset(szLine,0,MAX_COLS);
    }
    fIniFile.close();

    return E_OK;
}
/*
功能
    将CIniFile中的内容写入文件
参数
    szIniFile    in    生成的INI文件名称
返回值
    E_OK        调用成功
    E_OPEN_FILE_FAILED    打开文件错误
*/
int CIniFile::WriteIniFile(char* szIniFile)
{
    ofstream fIniFile(szIniFile);
    if(fIniFile == NULL)
        return E_OPEN_FILE_FAILED;
    for (size_t i = 0; i < m_isSection.size();++i)
    {
        fIniFile.write("[",1);
        fIniFile.write(m_isSection[i].m_isName.c_str(),m_isSection[i].m_isName.length());
        fIniFile.write("]",1);
        fIniFile << endl;
        for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
        {
            fIniFile.write(m_isSection[i].m_ieEntry[j].m_strName.c_str(),m_isSection[i].m_ieEntry[j].m_strName.length());
            fIniFile.write("=",1);
            fIniFile.write(m_isSection[i].m_ieEntry[j].m_strValue.c_str(),m_isSection[i].m_ieEntry[j].m_strValue.length());
            fIniFile << endl;
        }
    }
    fIniFile.close();
    return E_OK;
}
/*
功能
    删除前后的空格(' ','/t','/r','/n')
参数
    szString        in    传入的字符串
                    out 去除空格后的字符串
返回值
    E_OK    调用成功
*/
int CIniFile::Trim(char* &szString)
{
    char* p = szString;
    while (*p == ' ' || *p == '/t')
    {
        p ++;
    }
    szString = p;

    p = szString + strlen(szString) - 1;
    while ( *p == ' ' || *p == '/t' || *p == '/r' || *p == '/n')
    {
        -- p;
    }
    *( p + 1 ) = 0;


    return E_OK;
}
/*
功能
    删除注释
参数
    szLine    in    传入的字符串
            out 删除注释后的字符串
返回值
    E_OK    调用成功
*/
int CIniFile::RemoveComment(char* szLine)
{
    char* p = strchr(szLine,';');
    if( p == NULL)
        return 0;
    *p = 0;
    return 0;
}

string CIniFile::GetKeyValue(string section,string keyname)
{
    //CFile file(L"log.txt", CFile::modeWrite); //打开日志文件
    //CString str;
    //str.Format(_T("CIniFile::GetKeyValue--%s,%s\n"), section,keyname);
    //file.SeekToEnd();
    //file.Write(str, str.GetLength()*2); //写入文件
    //file.Close();
     for (size_t i = 0; i < m_isSection.size();++i)
    {
        if(!section.compare(m_isSection[i].m_isName))  //equal  return 0
        {
            for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
            {
                if(!m_isSection[i].m_ieEntry[j].m_strName.compare(keyname))
                    return m_isSection[i].m_ieEntry[j].m_strValue;
            }
        }
    }
     return "";
}

void CIniFile::SetKeyValue(string section,string keyname,string keyvalue)
{
    for (size_t i = 0; i < m_isSection.size();++i)
    {
        if(!section.compare(m_isSection[i].m_isName))  //equal  return 0
        {
            for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
            {
                if(!m_isSection[i].m_ieEntry[j].m_strName.compare(keyname))  
                {
                    m_isSection[i].m_ieEntry[j].m_strValue=keyvalue;
                    return ;
                }
            }
            //有节 没有相应的键
            CIniEntry ie;
            ie.m_strName=keyname;
            ie.m_strValue=keyvalue;
            m_isSection[i].m_ieEntry.push_back(ie);
            return ;
        }
    }
    //没有找到相应的节
    CIniSection is;
    is.m_isName = section;
    CIniEntry ie;
    ie.m_strName=keyname;
    ie.m_strValue=keyvalue;
    is.m_ieEntry.push_back(ie);
    m_isSection.push_back(is);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值