• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            那誰(shuí)的技術(shù)博客

            感興趣領(lǐng)域:高性能服務(wù)器編程,存儲(chǔ),算法,Linux內(nèi)核
            隨筆 - 210, 文章 - 0, 評(píng)論 - 1183, 引用 - 0
            數(shù)據(jù)加載中……

            服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)

            頭文件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;
            }

            實(shí)現(xiàn)主要采用STL, 實(shí)現(xiàn)了ini格式文件的讀,寫(xiě),已經(jīng)打印文件信息等功能,暫時(shí)覺(jué)得這些功能已經(jīng)夠用了,以后有需要再進(jìn)行添加.應(yīng)該還少了一個(gè)宏BUFFER_LEN的定義,這個(gè)宏在一個(gè)公用的頭文件中定義,如果你想試用這個(gè)類(lèi),可以自行進(jìn)行定義,我默認(rèn)定義為1024字節(jié)大小.



            posted on 2008-07-29 00:57 那誰(shuí) 閱讀(4450) 評(píng)論(7)  編輯 收藏 引用 所屬分類(lèi): C\C++服務(wù)器設(shè)計(jì)

            評(píng)論

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)[未登錄](méi)  回復(fù)  更多評(píng)論   

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

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)[未登錄](méi)  回復(fù)  更多評(píng)論   

            @noname
            我不用boost,也不會(huì)用.
            2008-07-29 16:36 | 創(chuàng)

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)  回復(fù)  更多評(píng)論   

            一個(gè)問(wèn)題:
            你使用map來(lái)存儲(chǔ),一來(lái)無(wú)法保持用戶(hù)key的次序,二來(lái)無(wú)法設(shè)置相同的key
            2008-07-29 16:39 | 小明

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)[未登錄](méi)  回復(fù)  更多評(píng)論   

            @小明
            呃..貌似我還沒(méi)有遇到過(guò)用重復(fù)KEY的情況,這個(gè)類(lèi)對(duì)我而言,目前為止是夠用的.
            2008-07-29 16:52 | 創(chuàng)

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)[未登錄](méi)  回復(fù)  更多評(píng)論   

            @小明
            另外,key的順序確實(shí)是按照默認(rèn)的字典排序,不過(guò)我想,也不打緊吧:)
            2008-07-29 16:54 | 創(chuàng)

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)  回復(fù)  更多評(píng)論   

            ini文件操作屬于通用的非關(guān)鍵性能的組件,新的實(shí)現(xiàn)不知道有什么特別的優(yōu)點(diǎn)。
            2008-07-29 17:06 | x-matrix

            # re: 服務(wù)器公共庫(kù)開(kāi)發(fā)--讀取ini文件格式的類(lèi)  回復(fù)  更多評(píng)論   

            ini文件中有注釋怎么辦?

            linux中主要采用ini來(lái)保存配置,不知其中有沒(méi)有實(shí)現(xiàn)一個(gè)公用的類(lèi),還是每個(gè)軟件中都去實(shí)現(xiàn)一遍?
            2009-05-07 16:41 | someone
            亚洲精品乱码久久久久66| 国产午夜精品理论片久久| 国产精品美女久久久久AV福利| 久久精品无码一区二区三区| 久久久久免费精品国产| 蜜臀久久99精品久久久久久| 久久精品这里热有精品| 精品国产乱码久久久久久1区2区| 国内精品久久久久国产盗摄| 久久国产精品99久久久久久老狼 | 香蕉久久一区二区不卡无毒影院| 无码人妻久久一区二区三区 | 欧美亚洲国产精品久久高清| 91精品国产91久久| 国产一区二区精品久久 | 久久精品国产精品亜洲毛片| 久久精品成人影院| 久久人人爽人人爽人人片AV麻豆 | 国产精品对白刺激久久久| 亚洲综合伊人久久大杳蕉| 新狼窝色AV性久久久久久| 久久亚洲AV成人无码国产| 久久精品国产秦先生| 色成年激情久久综合| 久久久久久A亚洲欧洲AV冫| 久久久这里有精品| 无码久久精品国产亚洲Av影片 | 91精品国产91久久久久久蜜臀| 久久国产高清一区二区三区| 久久精品无码av| 国产精品久久久久久久久软件| 久久久久久久亚洲Av无码| 91精品国产综合久久婷婷| 国产精品无码久久综合网| 伊人久久大香线蕉亚洲 | 久久久久久久亚洲精品| 久久只这里是精品66| 久久国产亚洲精品无码| 99热热久久这里只有精品68| 欧美午夜A∨大片久久| 久久AV高清无码|