• <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>

            那誰的技術(shù)博客

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

            服務(wù)器公共庫開發(fā)--讀取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;
            }

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



            posted on 2008-07-29 00:57 那誰 閱讀(4452) 評論(7)  編輯 收藏 引用 所屬分類: C\C++ 、服務(wù)器設(shè)計

            評論

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

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

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

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

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

            一個問題:
            你使用map來存儲,一來無法保持用戶key的次序,二來無法設(shè)置相同的key
            2008-07-29 16:39 | 小明

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

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

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

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

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

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

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

            ini文件中有注釋怎么辦?

            linux中主要采用ini來保存配置,不知其中有沒有實現(xiàn)一個公用的類,還是每個軟件中都去實現(xiàn)一遍?
            2009-05-07 16:41 | someone
            国产精品伊人久久伊人电影| 人妻丰满?V无码久久不卡| 久久精品国产亚洲AV香蕉| 狠狠色丁香久久婷婷综合_中| 97精品国产97久久久久久免费 | 久久综合一区二区无码| 97视频久久久| 久久久精品一区二区三区| 亚洲国产精品无码久久久久久曰| 久久午夜羞羞影院免费观看| 国产视频久久| 人妻无码αv中文字幕久久琪琪布| 久久这里只精品国产99热| 一97日本道伊人久久综合影院| 精品综合久久久久久97超人| 性做久久久久久免费观看| 夜夜亚洲天天久久| 久久天天躁狠狠躁夜夜不卡| 国内精品免费久久影院| 久久国产乱子伦免费精品| 亚洲精品乱码久久久久久蜜桃| 麻豆精品久久久一区二区| 亚洲AV无码久久| 区久久AAA片69亚洲| 精品久久久久久无码免费| 国产精品毛片久久久久久久| 久久精品中文无码资源站| 久久久久香蕉视频| 国产精品99久久久久久董美香| 无码国内精品久久人妻蜜桃 | 久久综合九色综合欧美就去吻| AV无码久久久久不卡蜜桃| 国产精品99久久久精品无码| 欧美激情精品久久久久久久| 久久99精品久久久久久秒播| 久久精品国产精品青草app| 无码日韩人妻精品久久蜜桃 | 99久久精品无码一区二区毛片 | 色综合久久久久综合体桃花网| 久久亚洲2019中文字幕| 久久久久久国产精品免费免费 |