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

            高性能服務(wù)器開(kāi)發(fā);C++深探

            單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map

            由于項(xiàng)目中經(jīng)常遇到讀取配置文件的地方,為方便調(diào)用雖寫了個(gè)功能簡(jiǎn)單,使用方便的配置文件類,基本思路是將配置文件緩沖到map當(dāng)中,目前只支持一級(jí)配置,多級(jí)別的樹(shù)形配置暫不支持,有需要的兄弟請(qǐng)完善。
            為單實(shí)例模式。
            config2map.h
            // config2map.h
            // 配置文件到map的映射類,單實(shí)例模式
            // 創(chuàng)建: 2010-09-13 changym, changup@qq.com
            // 修改: 
            //       
            //////////////////////////////////////////////////////////////////////
            #ifndef _CONFIG2MAP_H_
            #define _CONFIG2MAP_H_
            #include 
            <map>
            using namespace std;

            /*僅僅是為了能讓編譯通過(guò)添加日志相關(guān)部分*/
            #define LOG_DBG  1
            #define LOG_INFO 2
            #define LOG_ERR  3
            class CLog
            {
            public:
                
            int log(int,const char*,)
                
            {
                    
            return 0;
                }

            }


            typedef 
            struct tagNode
            {
                tagNode(
            const char* pname,const char* pvalue)
                
            {
                    name
            =0;
                    value
            =0;
                    
            if(pname)
                    
            {
                        name 
            = new char[strlen(pname)+1];
                        strcpy(name,pname);
                        name[strlen(pname)]
            ='\0';
                    }

                    
            if(pvalue)
                    
            {
                        value 
            = new char[strlen(pvalue)+1];
                        strcpy(value,pvalue);
                        value[strlen(pvalue)]
            ='\0';
                    }

                }

                
            ~tagNode()
                
            {
                    
            if(name)
                    
            {
                        delete [] name;
                        name
            =0;
                    }

                    
            if(value)
                    
            {
                        delete [] value;
                        value
            =0;
                    }

                }

                
            void print(CLog* plog)
                
            {
                    plog
            ->log(LOG_DBG,"\t%s=%s",name,value);
                }

                
            char  *name;
                
            char  *value;
            }
            NODE,*PNODE;

            class CNodeList : public list<PNODE>
            {
            public:
                
            virtual ~CNodeList()
                
            {
                    PNODE pnode 
            = NULL;
                    list
            <PNODE>::iterator iter = begin();
                    
            while(!empty())
                    
            {
                        pnode 
            = front();
                        delete pnode;
                        pop_front();
                    }

                    clear();
                }

                
            void print(CLog* plog)
                
            {
                    list
            <PNODE>::iterator iter = begin();
                    
            while(iter != end())
                    
            {
                        (
            *iter++)->print(plog); 
                    }

                }

            }
            ;
            typedef CNodeList
            * PNODELIST;
            typedef map
            <const char*,PNODELIST,eq_str_map> CIniMap;

            class CConfig2Map : public CBase,public CIniMap
            {
            public:
                
            ~CConfig2Map()
                
            {
                    PNODELIST pnodelist 
            = NULL;
                    PNODE pnode 
            = NULL;
                    
            char* pszname = NULL;
                    
            char* pszvalue = NULL;
                    CIniMap::iterator iter 
            = begin();
                    
            while(iter!=end())
                    
            {
                        pszname 
            = (char*)((*iter).first);
                        delete [] pszname;
                        delete (
            *iter).second;
                        iter
            ++;
                    }
                    
                    clear();
                }

                
            public:
                
            static CConfig2Map* Instance(const char* pszinifilename)
                
            {
                    
            if(!_instance)
                        _instance 
            = new CConfig2Map(pszinifilename);
                    
            return _instance;
                }


                
            int getstring(const char* pszsection,const char* pszname,char* pszvalue,int buflen)
                
            {
                    #ifdef _DEBUG
                    CLog log;
                    
            #endif
                    
                    PNODELIST pnodelist 
            = (*this)[pszsection];
                    
            if(pnodelist==NULL)
                    
            {
                        #ifdef _DEBUG
                        log.log(LOG_DBG,
            "section:%s not found",pszsection);
                        
            #endif
                        
            return 0;
                    }

                    #ifdef _DEBUG
                    log.log(LOG_DBG,
            "section:%s found",pszsection);
                    
            #endif
                    CNodeList::iterator iter 
            = pnodelist->begin();
                    PNODE pnode 
            = NULL;
                    
            int bfound = 0;
                    
            while(iter!=pnodelist->end())
                    
            {
                        #ifdef _DEBUG
                        log.log(LOG_DBG,
            "name=%s,value=%s",(*iter)->name,(*iter)->value);
                        
            #endif
                        
            if(!strcmp(pszname,(*iter)->name))
                        
            {
                            strncpy(pszvalue,(
            *iter)->value,buflen);
                            bfound 
            = 1;
                            
            break;
                        }

                        iter
            ++;
                    }


                    
            return bfound;
                }


                
            void print(CLog* plog)
                
            {
                    
            const char* pszkey = NULL;
                    PNODELIST pnodelist 
            = NULL;
                    CIniMap::iterator iter 
            = begin();
                    plog
            ->log(LOG_DBG,"++++++++++++++++begin print mapconfig+++++++++++++++++");
                    
            while(iter!=end())
                    
            {
                        plog
            ->log(LOG_DBG,"section=%s",(*iter).first);
                        pnodelist 
            = (*iter).second;
                        pnodelist
            ->print(plog);
                        iter
            ++;
                    }

                    plog
            ->log(LOG_DBG,"++++++++++++++++print mapconfig end ++++++++++++++++++");    
                }

                
            private:
                CConfig2Map(
            const char* pszinifilename) throw(const char*);
                
            private:
                
            static CConfig2Map*  _instance;    
            }
            ;

            #endif //_CONFIG2MAP_H_

            config2map.cpp
            #include "config2map.h"

            CConfig2Map
            * CConfig2Map::_instance = NULL;
                    
            CConfig2Map::CConfig2Map(
            const char* pszinifilename) throw(const char*)
            {
                #ifdef _DEBUG
                CLog log;
                
            #endif
                FILE
            * pfile = fopen(pszinifilename, "r");
                
            char* pszerrmsg;
                
            if(!pfile)
                {
                    pszerrmsg 
            = new char[1024];
                    memset(pszerrmsg,
            0x00,1024);
                    SNPRINTF(pszerrmsg,
            1023,"failed to open file [%s]",pszinifilename);
                    
            throw(pszerrmsg);
                }

                #ifdef _DEBUG
                
            int ireadline = 0;
                
            #endif    
                
            char  szBuf[1024= {0};
                
            int buflen = 0;
                
            char* key;
                
            char* keybegin,*keyend;
                
            int bkeyendfound = 0;
                PNODELIST pnodelist 
            = NULL;
                
            while(fgets(szBuf, 1023, pfile))
                {
                    buflen 
            = strlen(szBuf);        
                    
            if(buflen<3)
                        
            continue;
                        
                    
            if(buflen>=1)
                    {
                        
            if(szBuf[buflen-1]==0x0a || szBuf[buflen-1]==0x0d)
                            szBuf[buflen
            -1= 0x00;
                    }
                    
            if(buflen>=2)
                    {
                        
            if(szBuf[buflen-2]==0x0a || szBuf[buflen-2]==0x0d)
                            szBuf[buflen
            -2= 0x00;            
                    }
                        
                    #ifdef _DEBUG
                    log.log(LOG_DBG,
            "%d=%s",buflen,szBuf);
                    
            #endif
                    
                    
            char* p = szBuf;
                    
            if(*p=='#' || *p=='\n' || *p=='\0'continue;
                    
            if(*== '[')
                    {
                        
                        #ifdef _DEBUG
                        printf(
            "found [\n");
                        
            #endif
                        keybegin 
            = p+1;
                        bkeyendfound 
            = 0;
                        
            while(*p)
                        {
                            
            if(*p==']')
                            {
                                
                                #ifdef _DEBUG
                                printf(
            "found ]\n");
                                
            #endif
                                bkeyendfound 
            = 1;
                                keyend 
            = p-1;
                                
            break;
                            }    
                            p
            ++;            
                        }
                        
            if(!bkeyendfound)
                        {
                            #ifdef _DEBUG
                            printf(
            "key [] not match\n");
                            
            #endif
                            
            throw("key [] not match\n");
                        }
                        
            if(keybegin>keyend)
                        {
                            #ifdef _DEBUG
                            printf(
            "invalid key[\"\"]\n");
                            
            #endif
                            
            throw("invalid key[""]");
                            
                        }
                        key 
            = new char[keyend-keybegin+1+1];
                        memset(key,
            0x00,keyend-keybegin+1+1);
                        strncpy(key,keybegin,keyend
            -keybegin+1);
                        
            //key[keyend-keybegin+1+1]='\0'; //2010-10-13:這個(gè)錯(cuò)誤,找了一天啊
                        key[keyend-keybegin+1]='\0';
                        #ifdef _DEBUG
                        printf(
            "new key=%s\n", key);
                        
            #endif
                        pnodelist 
            = new CNodeList();
                        (
            *this)[key] = pnodelist;
                         
                        
            continue;
                    }                
                    
            while(*&& *p++!='=');        
                    
            if(p==(szBuf+strlen(szBuf))) continue;

                    
            char  name[256= {0};
                    
            char  value[1024= {0};
                    strncpy(name, szBuf, p
            -szBuf-1);
                    strncpy(value, p, szBuf
            +strlen(szBuf)-p);
                    
            if(pnodelist==NULL)
                    {
                        #ifdef _DEBUG
                        printf(
            "key not found,%s=%s discard\n",name,value);
                        
            #endif
                    }
                    
            else
                    {
                        PNODE pnode
            = new NODE(name,value);
                        pnodelist
            ->push_back(pnode);
                    }

                    
            //proc(pvoid,key,value);        
                }

                fclose(pfile);
            }

            posted on 2010-12-14 15:48 changup 閱讀(3702) 評(píng)論(5)  編輯 收藏 引用 所屬分類: 設(shè)計(jì)模式實(shí)踐 、隨想

            Feedback

            # re: 單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map 2010-12-14 19:24 Skill

            有bug  回復(fù)  更多評(píng)論   

            # re: 單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map 2010-12-15 11:55 水星家紡

            我也看到有bug   回復(fù)  更多評(píng)論   

            # re: 單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map 2010-12-20 23:24 luckycat

            推薦使用 boost.property_tree,簡(jiǎn)潔,高效,支持多種格式: ini , xml ...
            并且使用統(tǒng)一的操作接口。  回復(fù)  更多評(píng)論   

            # re: 單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map 2010-12-23 21:47 changup

            @luckycat
            boost庫(kù)不想使用  回復(fù)  更多評(píng)論   

            # re: 單實(shí)例模式的一個(gè)C++讀取配置文件的類CConfig2Map 2011-12-26 15:31

            一點(diǎn)也沒(méi)看明白  回復(fù)  更多評(píng)論   

            久久精品国产亚洲AV高清热| 日本久久久精品中文字幕| 久久这里只精品国产99热| 一本色道久久88综合日韩精品 | AAA级久久久精品无码片| 蜜桃麻豆www久久国产精品| 99久久久国产精品免费无卡顿 | 香蕉久久夜色精品国产2020| 国产精品久久99| 色偷偷久久一区二区三区| 日韩久久无码免费毛片软件| 久久精品国产99国产精品澳门| 亚洲欧美日韩久久精品第一区| 亚洲Av无码国产情品久久| 久久成人永久免费播放| 亚洲国产精品久久久久久| 97久久精品国产精品青草| 久久夜色精品国产噜噜麻豆| 伊人久久成人成综合网222| 久久亚洲av无码精品浪潮| 久久99国产精品成人欧美| 91精品国产综合久久香蕉 | 91秦先生久久久久久久| 国产国产成人精品久久| 91精品国产9l久久久久| 东京热TOKYO综合久久精品| 久久国产高潮流白浆免费观看| 亚洲精品乱码久久久久久蜜桃图片 | 久久久久久久精品成人热色戒| 久久亚洲精品国产精品婷婷 | 精品久久国产一区二区三区香蕉| 久久久久久久尹人综合网亚洲| 国产精品岛国久久久久| 青青青青久久精品国产 | 久久777国产线看观看精品| 久久精品国产半推半就| 亚洲国产精品一区二区久久| 国产精品九九久久免费视频 | 久久精品亚洲日本波多野结衣| 久久人人爽爽爽人久久久| 国产精品久久久久久一区二区三区 |