wince下INI的操作更新

wince虽然不是完全支持STL,但是很多功能都可以使用,在网上找到一个源码!很好用,借鉴了一下。具体在哪儿找到的我也记不大清除了,我在他上面稍微做了一些修改!
 
支持:

//读取参数
 BOOL GetValueString(const PCHAR pSection, const PCHAR pKey, PCHAR pReturnValue);
 BOOL GetValueInteger(const PCHAR pSection, const PCHAR pKey, DWORD& dwReturnValue);
 //设置参数
 BOOL SetValueString(const PCHAR pSection, const PCHAR pKey, const PCHAR pSetValue);
 BOOL SetValueInteger(const PCHAR pSection, const PCHAR pKey, const DWORD dwSetValue);

在网上找到的代码我发现他只能增加section和key但是不能修改已经存在的key

 

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

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

}

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

int CIniFileOP::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 CIniFileOP::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 CIniFileOP::CloseIni()
{
    if(NULL != this->m_fileHandle)
    {
        fclose(this->m_fileHandle);
        this->m_fileHandle = NULL;
    }
}


string CIniFileOP::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 CIniFileOP::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 CIniFileOP::Trim(string p_strValue)
{
    return TrimL(TrimR(p_strValue));
}

int CIniFileOP::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;
    int SectionP = 0;
    //找到已有的section,和key

    for(int i = (int)(m_szStrItem.size())-1; i >= 0; i--)
    {
        if (m_szStrItem[i].first == p_strSection)//找到section

        {
            if (0 == SectionP)
            {
                SectionP = i;
                bFind = 1;
            }
            if (m_szStrItem[i].second.first == p_strKey)//找到key

            {
                m_szStrItem[i].second.second = "";
                m_szStrItem[i].second.second = p_strValue;
                bFind = 2;
                break;
            }
        }
    }
    //找到已有的section,但是没有key

    if (1 == bFind)
    {
        m_szStrItem.insert( m_szStrItem.begin() + SectionP + 1, pItem );
        bFind = 1;
    }


    //如果是新增的section

    if(0 == bFind)
        m_szStrItem.push_back(pItem);

    this->SaveNewFile();

    return 1;
}

BOOL CIniFileOP::SaveNewFile()
{
    this->m_fileHandle = fopen(this->m_strFileName.c_str(),"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 TRUE;
}

void CIniFileOP::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;
}

//读取选项

BOOL CIniFileOP::GetValueString(const PCHAR pSection, const PCHAR pKey, PCHAR pReturnValue)
{
    string strReturn = GetValue(pSection, pKey);
    if (TRUE == strReturn.empty())
        return FALSE;
    else
        strcpy(pReturnValue, strReturn.data());
    return TRUE;
}

BOOL CIniFileOP::GetValueInteger(const PCHAR pSection, const PCHAR pKey, DWORD& dwReturnValue)
{
    string strReturn = GetValue(pSection, pKey);
    if (TRUE == strReturn.empty())
        return FALSE;
    else
        dwReturnValue = atoi(strReturn.data());
    return TRUE;
}


//设置选项

BOOL CIniFileOP::SetValueString(const PCHAR pSection, const PCHAR pKey, const PCHAR pSetValue)
{
    return SetValue(pSection, pKey, pSetValue);
}

BOOL CIniFileOP::SetValueInteger(const PCHAR pSection, const PCHAR pKey, const DWORD dwSetValue)
{
    CHAR cSetV[12] = {0};
    string strSetV = _itoa(dwSetValue, cSetV, 10);

    return SetValue(pSection, pKey, strSetV);
}

 

这个ini文件操作类非常的好用,感谢网上这位仁兄为我们提供了源码,这个源码有点模仿java,不提倡使用太多的STL,我认为在嵌入式系统中内存和CPU资源都是有限的,如果STL使用太多,难免会影像工作效率!

呵呵,个人理解是这样的,如果在使用中还有BUG我会及时的贴出来,欢迎大家一起讨论!

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(1053) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

so9272008-08-20 19:22:58

好像没有删除键值和块的功能哦!慢慢添加吧 o(∩_∩)o...

评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值