• <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>
            #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__ */
            /********************************************************************
                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;
            }

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


            posts - 94, comments - 138, trackbacks - 0, articles - 94

            Copyright © RichardHe

            精品久久人人妻人人做精品| 无码任你躁久久久久久| 久久久久青草线蕉综合超碰| 国产精品免费看久久久香蕉| 久久99精品国产99久久| 99国产欧美精品久久久蜜芽| 久久丫精品国产亚洲av不卡 | 欧美喷潮久久久XXXXx| 人人妻久久人人澡人人爽人人精品| 欧美精品丝袜久久久中文字幕 | 国产精品激情综合久久| 大美女久久久久久j久久| 狠狠精品久久久无码中文字幕 | 69久久夜色精品国产69| 国产精品久久自在自线观看| 久久香蕉综合色一综合色88| 国产精品伦理久久久久久| 久久久久久久综合日本| 久久毛片一区二区| 日本强好片久久久久久AAA| 国产精品久久精品| 久久久久无码精品| 久久SE精品一区二区| 久久Av无码精品人妻系列| 九九99精品久久久久久| 久久久久国产一区二区三区| 亚洲国产成人精品女人久久久| 99久久这里只精品国产免费| 久久久久AV综合网成人| 国产午夜精品久久久久九九| 久久婷婷色综合一区二区| 中文字幕久久波多野结衣av| 久久福利青草精品资源站| 久久国产综合精品五月天| 国产精品99久久久精品无码| 99久久99久久久精品齐齐| 亚洲精品美女久久久久99小说| 久久久无码人妻精品无码| 久久久久亚洲av毛片大| 久久久久久毛片免费播放| 欧美激情精品久久久久久久|