Chinaunix首页 | 论坛 | 博客
  • 博客访问: 431722
  • 博文数量: 56
  • 博客积分: 2262
  • 博客等级: 大尉
  • 技术积分: 711
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-08 20:04
文章分类

全部博文(56)

文章存档

2013年(1)

2012年(9)

2011年(10)

2010年(7)

2009年(7)

2008年(22)

我的朋友

分类: WINDOWS

2008-08-20 19:21:22

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我会及时的贴出来,欢迎大家一起讨论!

阅读(2340) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

so9272008-08-20 19:22:58

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