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

            STL容器實(shí)現(xiàn)IniFileParser

            IniFileParser.h

            /*************************************************************************/
            /* FileName:IniFileParser.cpp                                                                                                   */
            /* Describe:IniFile@read、write                                                                                               */
            /* Author  :Kagayaku                                                                                                               */
            /* Date    :3.22.2010                                                                                                                */
            /************************************************************************/

            #ifndef _INIFILEPARSER_H_
            #define _INIFILEPARSER_H_

            #include <string>
            #include <vector>
            using namespace std;

             

            struct CIniEntry
            {
             CIniEntry(){}
             CIniEntry(char *szName,char *szValue):m_strIEName(szName),m_strIEValue(szValue){}
             string m_strIEName;
             string m_strIEValue;
            };

            struct CIniComment
            {
             CIniComment(){}
             CIniComment(char *szIC):m_strIC(szIC){}
             string m_strIC;
            };

            struct CIniSection
            {
             vector<CIniEntry>     m_vecIE;
             vector<CIniComment>   m_vecIC;
             string                m_strISName;
            };


            class CIniFile
            {
            public:
             CIniFile(const char *szIniFileFullPath);
             ~CIniFile();
             bool ReadIniFile(const char *szinifile);
             bool WriteIniFile(const char *szinifile);
             void TrimIniFile(char* &szinifile) const;
             void RemoveComment(char *szinifile) const;
             bool SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname);
             bool SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue);
               
            private:
             vector<CIniSection> m_vecIS;
             string              m_strBufIEValue;
             string              m_strIniFileName;


            };
            #endif

            IniFileParser.cpp

            #include "IniFileParser.h"
            #include <fstream>

            CIniFile::CIniFile(const char *szIniFileFullPath):m_strIniFileName(szIniFileFullPath)
            {
             ReadIniFile(szIniFileFullPath);
            }

            CIniFile::~CIniFile()
            {

            }

            bool CIniFile::ReadIniFile(const char *szinifile)
            {
             if (NULL==szinifile)
             {
              return false;
             }
             ifstream inifile(szinifile);
             if (NULL==inifile)
             {
              return false;
             }

             const int MAX_ROW_LENGTH=200;
             char chLineBufArray[MAX_ROW_LENGTH]={0};
             while(inifile.getline(chLineBufArray,MAX_ROW_LENGTH))
             {
              char *p=chLineBufArray;
              TrimIniFile(p);

              if (*p=='[')
              {
               RemoveComment(p);
               char *pEnd=strchr(p,']');
               if (NULL==pEnd || pEnd==p+1)
               {
                continue;
               }
               *pEnd = 0;
               CIniSection is;
               is.m_strISName=string(p+1);
               m_vecIS.push_back(is);
               continue;
              }

              if (m_vecIS.size()<1)
              {
               continue;
              }
              

              if (*p==';')
              {
               if (NULL==*(p+1))
               {
                continue;
               }
               else
               {
                CIniComment ic(p+1);
                m_vecIS[m_vecIS.size()-1].m_vecIC.push_back(ic);
                continue;
               }
               
              }
              
              char *pFlag=strchr(p,'=');
              if (NULL==pFlag || pFlag==p || NULL==*(pFlag+1))
              {
               continue;
              }
              else
              {
               *pFlag=0;
               CIniEntry ie;
               ie.m_strIEName=string(p);
               ie.m_strIEValue=string(pFlag+1);
               m_vecIS[m_vecIS.size()-1].m_vecIE.push_back(ie);
               continue;

              }

             }
             inifile.close();
             return true;

            }

            bool CIniFile::WriteIniFile(const char *szinifile)
            {
             if (NULL==szinifile || m_strIniFileName!=szinifile)
             {
              return false;
             }

             ofstream inifile(szinifile);
             if (NULL==inifile)
             {
              return false;
             }

             for (int i=0;i!=m_vecIS.size();++i)
             {
              inifile.write("[",1);
              inifile.write(m_vecIS[i].m_strISName.c_str(),m_vecIS[i].m_strISName.size());
              inifile.write("]",1);
              inifile << endl;
              for (int j=0;j!=m_vecIS[i].m_vecIE.size();++j)
              {
               inifile.write(m_vecIS[i].m_vecIE[j].m_strIEName.c_str(),m_vecIS[i].m_vecIE[j].m_strIEName.size());
               inifile.write("=",1);
               inifile.write(m_vecIS[i].m_vecIE[j].m_strIEValue.c_str(),m_vecIS[i].m_vecIE[j].m_strIEValue.size());
               inifile << endl;
              }
             }
             inifile.close();
             return true;
            }

            void CIniFile::TrimIniFile(char* &szinifile) const
            {
             if (NULL==szinifile)
             {
              return;
             }

             char *p=szinifile;

             while(*p==' ' || *p=='\t')
             {
              ++p;
             }

             szinifile=p;
             p=szinifile+strlen(szinifile)-1;

             while(*p==' ' || *p=='\t'|| *p=='\r'|| *p=='\n')
             {
              *p=0;
              --p;
             }

            }

            void CIniFile::RemoveComment(char *szinifile) const
            {
             if (NULL==szinifile)
             {
              return;
             }
             char *p=strchr(szinifile,';');
             *p = 0;

            }

            bool CIniFile::SearchMatchingIniFileSectionGetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname)
            {
             if (NULL==szinifile || NULL==szsectionname || NULL==szentryname)
             {
              return false;
             }

             if (m_strIniFileName!=szinifile)
             {
              return false;
             }

                bool temp=false;

             for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
             {
              if ((*iterIS).m_strISName==szsectionname)
              {
               for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
               {
                if ((*iterIE).m_strIEName==szentryname)
                {
                 m_strBufIEValue=(*iterIE).m_strIEValue;
                 temp=true;
                }
               }
              }
             }
             return temp;

            }

            bool CIniFile::SearchMatchingIniFileSectionSetEntryValue(const char *szinifile,const char *szsectionname,const char *szentryname,const char *szentryvalue)
            {
             if (NULL==szinifile || NULL==szsectionname || NULL==szentryname|| NULL==szentryvalue)
             {
              return false;
             }

             if (m_strIniFileName!=szinifile)
             {
              return false;
             }

             bool temp=false;

             for (vector<CIniSection>::iterator iterIS=m_vecIS.begin();iterIS!=m_vecIS.end();++iterIS)
             {
              if ((*iterIS).m_strISName==szsectionname)
              {
               for (vector<CIniEntry>::iterator iterIE=(*iterIS).m_vecIE.begin();iterIE!=(*iterIS).m_vecIE.end();++iterIE)
               {
                if ((*iterIE).m_strIEName==szentryname)
                {
                 (*iterIE).m_strIEValue=szentryvalue;
                 temp=true;
                }
               }
              }
             }
               
             
             return temp?WriteIniFile(szinifile):false;

            }

            posted on 2010-03-22 23:01 avatar 閱讀(1800) 評(píng)論(5)  編輯 收藏 引用

            評(píng)論

            # re: STL容器實(shí)現(xiàn)IniFileParser 2010-03-23 11:23 陳梓瀚(vczh)

            http://www.shnenglu.com/vczh/archive/2010/03/07/109103.html  回復(fù)  更多評(píng)論   

            # re: STL容器實(shí)現(xiàn)IniFileParser 2010-03-23 20:03 萌萌

            你不寫提要 怎么看啊  回復(fù)  更多評(píng)論   

            # re: STL容器實(shí)現(xiàn)IniFileParser 2010-03-29 16:29 淡月清風(fēng)

            哇,完全沒(méi)有注釋  回復(fù)  更多評(píng)論   

            # re: STL容器實(shí)現(xiàn)IniFileParser 2010-04-08 12:12 jmchxy

            #ifndef __JFILECONFIG_H__
            #define __JFILECONFIG_H__
            /////////////////////////////////////////////
            /// JFileConfig ini文件操作類
            /////////////////////////////////////////////
            #include "jconfig.h"
            #include <map>
            #include <vector>

            namespace jlib
            {

            // ini 文件中名字不分大小寫
            struct SectionLess
            {
            bool operator() (const JString& Key1, const JString& Key2)const
            {
            return Key1.compareNoCase(Key2) < 0;
            }
            };
            //-------------------------------------------
            // 定義foreach 函數(shù)需要使用的函數(shù)類型
            // pArg 為傳遞給 函數(shù)的數(shù)據(jù)
            //-------------------------------------------
            typedef bool (*FOREACHFUNC)(const JString& section, const JString& name, JConfigVal& value, LPVOID pArg);
            class JLIBAPI JFileConfig: JConfigBase
            {
            public:
            // 構(gòu)造析構(gòu)函數(shù)
            JFileConfig();
            virtual ~JFileConfig();
            public:
            // 裝載配置信息, 從文件或其他媒介
            virtual bool load(LPCTSTR pszFilename);
            // 保存配置到文件
            virtual bool save(LPCTSTR pszFilename)const;
            // 獲取配置信息, key, name, 獲得value
            // 讀取失敗返回 false
            // 獲取通用類型的值
            virtual bool getValue(LPCTSTR sectionName, LPCTSTR name, JConfigVal& jRval)const;
            // 獲取整數(shù)值
            virtual bool getInt(LPCTSTR sectionName, LPCTSTR name, int& iRval)const;
            // 獲取字符串
            virtual bool getString(LPCTSTR sectionName, LPCTSTR name, JString& szRval)const;
            // 設(shè)置配置, 如果沒(méi)有則創(chuàng)建指定名/值
            // 設(shè)置通用類型的值
            virtual bool setValue(LPCTSTR sectionName, LPCTSTR name, const JConfigVal& jVal);
            // 設(shè)置整型的值
            virtual bool setInt(LPCTSTR sectionName, LPCTSTR name, int iVal);
            // 設(shè)置字符串值, 如果沒(méi)有則創(chuàng)建指定名/值
            virtual bool setString(LPCTSTR sectionName, LPCTSTR name, LPCTSTR szVal);
            //--------------------------------------------------
            // 遍歷函數(shù)
            // 如果用戶定義的遍歷函數(shù)返回了 false, 結(jié)束遍歷
            bool foreach(FOREACHFUNC fpForeach, LPVOID pArgs);
            //========================================
            #ifdef _UNIT_TEST_ //單元測(cè)試, 遍歷一個(gè)ini文件
            static void unitTest(LPCTSTR filename);
            #endif //_UNIT_TEST_
            //========================================
            public:
            /////////////////////////////////////////////
            /// 配置節(jié)信息
            /////////////////////////////////////////////
            typedef std::map<JString, JConfigVal, SectionLess> Dict;
            typedef std::map<JString, JConfigVal, SectionLess>::iterator DictIterator;
            typedef std::map<JString, JConfigVal, SectionLess>::const_iterator ConstDictIterator;
            // 結(jié)點(diǎn)信息
            struct JConfigSection
            {
            JString m_strSectionName; //節(jié)名
            Dict m_Items; //名/值對(duì)
            public:
            JConfigSection(): m_strSectionName(), m_Items() { }
            ~JConfigSection(){ m_Items.clear(); }
            // 加入一個(gè) 名/值 對(duì)
            bool set(const JString& name, const JConfigVal& value)
            {
            // 查找內(nèi)容
            DictIterator iter = m_Items.find( name );
            if(iter == m_Items.end())
            {
            // 沒(méi)找到, 插入
            m_Items.insert( Dict::value_type( name, value));
            }
            // 如果已存在, 修改
            m_Items[name] = value;
            return true;
            }
            bool get(JString& name, JConfigVal& value)const
            {
            // 查找內(nèi)容
            ConstDictIterator iter = m_Items.find( name );
            if(iter == m_Items.end())
            {
            // 沒(méi)找到,
            return false;
            }
            // 如果已存在, 返回
            value = iter->second;
            return true;
            }
            };
            private:
            //
            typedef std::vector<JConfigSection>::iterator SectionIter;
            typedef std::vector<JConfigSection>::const_iterator ConstSectionIter;
            // 創(chuàng)建一個(gè)節(jié)
            bool createSection(const JString& sectionName);
            // 查找節(jié), 返回對(duì)應(yīng)的索引
            // 如果不存在, 返回 end(),
            ConstSectionIter findSection(const JString& sectionName)const;
            SectionIter findSection(const JString& sectionName);
            // const static int MAX_SECTION = 10; //最多的節(jié)數(shù)
            private:
            std::vector<JConfigSection> m_Sections;
            // 不允許拷貝對(duì)象
            DECLARE_NO_COPY_CLASS(JFileConfig);
            };

            } //end namespace

            #endif //__JFILECONFIG_H__  回復(fù)  更多評(píng)論   

            # re: STL容器實(shí)現(xiàn)IniFileParser 2010-04-08 12:17 jmchxy

            //===============================================
            bool JFileConfig::getValue(LPCTSTR sectionName, LPCTSTR name, JConfigVal& rval)const
            {
            // 查找節(jié)
            JString jstrSection(sectionName);
            JString jstrName(name);
            bool bRet = false;
            // 查看是否已經(jīng)存在
            ConstSectionIter iter = findSection( jstrSection );
            // 存在節(jié)
            if(iter == m_Sections.end())
            {
            return false;
            }
            return iter->get(jstrName, rval);
            }

            /////////////////////////////////////////////
            // 裝載配置信息, 從文件或其他媒介
            /////////////////////////////////////////////
            #ifdef _DEBUG
            #define _OUTPUT_STATE
            #endif
            bool JFileConfig::load(LPCTSTR pszFilename)
            {
            JStdioFile inifile(pszFilename, JFileBase::modeRead);
            JString CurSection;

            while(!inifile.eof())
            {
            //逐行分析數(shù)據(jù)
            JString line = inifile.getline();
            // 去掉末尾的行計(jì)數(shù)符
            line.chomp();
            if(line.length() == 0)
            {
            continue; //跳過(guò)空行
            }
            // 如果是注釋行, 跳過(guò)
            // ; 和 # 開始的行是注釋
            if( (line[0] == _T(';'))||(line[0] == _T('#')))
            {
            continue;
            }
            if(line[0] == _T('['))
            {
            //是節(jié)名?
            int i = line.find( _T(']') );
            if(i > 0)
            {// 節(jié)名, 取出節(jié)的名字, 創(chuàng)建節(jié)
            CurSection = line.substr(1, i-1);
            createSection(CurSection);
            #ifdef _OUTPUT_STATE //調(diào)試用
            _tprintf( _T("create seciton: %s\n"), CurSection.c_str());
            #endif
            }
            continue; //錯(cuò)誤的行直接跳過(guò)
            }
            else
            { // 名/值 對(duì)定義的行, 查找 '=' 字符
            int i = line.find( _T('='));
            if(i <= 0)
            {
            continue; //沒(méi)有 = 字符, 跳過(guò)
            }
            JString strName = line.substr(0, i);
            strName.TrimRight();
            JString strValue = line.substr(i + 1, -1);
            strValue.TrimLeft();
            // 插入值
            setString(CurSection, strName, strValue);
            }
            }
            return true;
            }

            //-------------------------------------
            // 保存配置到文件
            //-------------------------------------
            bool JFileConfig::save(LPCTSTR pszFilename)const
            {
            JFile inifile(pszFilename, JFileBase::modeWrite|JFileBase::modeCreate);
            JString strLine(128);

            for(ConstSectionIter sectIter = m_Sections.begin();
            sectIter != m_Sections.end();
            ++sectIter)
            {
            // 當(dāng)前節(jié)的名字
            const JString& sectionNmae = sectIter->m_strSectionName;
            // 寫入節(jié)名
            strLine.clear();
            strLine += _T("[");
            strLine += sectionNmae;
            strLine += _T("]\r\n");
            inifile.write(strLine.c_str(), strLine.length());
            // 當(dāng)前節(jié)對(duì)應(yīng)的map
            const Dict& CurSection = sectIter->m_Items;
            // 遍歷名字/值
            ConstDictIterator dictIter = CurSection.begin();
            while(dictIter != CurSection.end())
            {
            const JString& name = dictIter->first;
            const JConfigVal& value = dictIter->second;
            strLine.clear();
            strLine += name;
            strLine += _T("=");
            strLine += value.toString();
            strLine += _T("\r\n");
            inifile.write(strLine.c_str(), strLine.length());
            ++dictIter;
            }
            }
            return true;
            }

            回復(fù)不能太長(zhǎng), 這是我的庫(kù)中定義的 inifile 處理類  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2010年4月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久线看观看精品香蕉国产| 欧美久久久久久精选9999| 人妻无码久久精品| 国内精品久久久久久中文字幕| 亚洲午夜久久久精品影院 | 久久精品人人槡人妻人人玩AV| 久久天天躁夜夜躁狠狠| 女人高潮久久久叫人喷水| 久久午夜免费视频| 中文字幕久久精品无码| 亚洲AV日韩精品久久久久| 日日狠狠久久偷偷色综合免费| 99久久伊人精品综合观看| 久久久精品久久久久特色影视| 久久人人爽人人爽人人片AV麻烦| 久久国产欧美日韩精品免费| 亚洲综合伊人久久大杳蕉| 大伊人青草狠狠久久| 91久久精品视频| 一本大道久久东京热无码AV| 久久综合九色综合网站| 久久久久亚洲av无码专区| 国产一区二区三区久久| 久久久久国产一区二区| 亚洲成色www久久网站夜月| 一本伊大人香蕉久久网手机| 无码8090精品久久一区| 婷婷伊人久久大香线蕉AV| 国产高清美女一级a毛片久久w| 亚洲国产精品嫩草影院久久 | 久久99国产精一区二区三区| 91精品国产高清久久久久久91| 伊人伊成久久人综合网777| 性欧美大战久久久久久久久| 99久久亚洲综合精品成人| A级毛片无码久久精品免费| 久久成人精品| 97精品国产91久久久久久| 99久久香蕉国产线看观香| 亚洲成色999久久网站| 欧美大香线蕉线伊人久久|