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

            Javen-Studio 咖啡小屋

            http://javenstudio.org - C++ Java 分布式 搜索引擎
            Naven's Research Laboratory - Thinking of Life, Imagination of Future

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks
            //author Naven 2003 for both Win32 and Unix os

            class MyProperties {
            public:
                MyProperties();
                MyProperties(istream 
            &is); 
                MyProperties(
            const string &filename); 

                
            void load(const string &filename);
                unsigned 
            long size(); 
                string filename();
                string get(
            const string &name, const string &default_value); 
                
            void remove(const string &name); 
                
            void set(const string &name, const string &value); 
                
            void add(const string &name, const string &value); 
                string getName(
            const int &i);
                string getValue(
            const int &i);

                
            void save();
                
            void save(string &filename); 

            private:
                
            void init(istream &is); 

            private:
                string _file; 
                vector _vals, _elms; 
                unsigned 
            long _size; 

            }


            MyProperties::MyProperties()
            {
                _size 
            = 0;
            }


            MyProperties::MyProperties(istream 
            &is) 
            {
                _size 
            = 0;
                init(is); 
            }


            MyProperties::MyProperties(
            const string &filename) 
            {
                load(filename); 
            }


            void
            MyProperties::load(
            const string &filename) 
            {
                _elms.clear(); _vals.clear(); 
                _size 
            = 0
                
            if(filename.empty()) 
                    
            return
                ifstream is(filename.c_str()); 
                _file 
            = filename; 

                init(is); 
            }


            void 
            MyProperties::init(istream 
            &is) 
            {
                
            if(!is) return;
                
            char ln[LINE_SIZE+1], *pval; 
                _elms.clear(); _vals.clear(); 
                _size 
            = 0
                
            while(is.getline(ln, LINE_SIZE)) 
                    trimleft(ln); 
                    
            if(ln[0!= '#' && (pval = strchr(ln, '=')) != NULL) {
                        pval[
            0= 0; pval++
                        trimright(ln); trim(pval); 
                        
            if(ln[0!= 0 && pval[0!= 0
                            _elms.push_back(ln); 
                            _vals.push_back(pval);
                            _size 
            ++
                        }

                    }

                }

            }


            unsigned 
            long 
            MyProperties::size() 
            {
                
            return _size;
            }


            string
            MyProperties::filename()
            {
                
            return _file; 
            }


            string 
            MyProperties::getName(
            const int &i)
            {
                
            if(i >=0 && i<_elms.size()) 
                    
            return _elms[i];
                
            return "";
            }


            string 
            MyProperties::getValue(
            const int &i)
            {
                
            if(i >=0 && i<_vals.size()) 
                    
            return _vals[i];
                
            return "";
            }


            string
            MyProperties::get(
            const string &name, const string &default_value=""
            {
                
            if(name.empty()) 
                    
            return default_value; 
                vector::const_iterator elm_iter 
            = _elms.begin(), elm_end = _elms.end();
                vector::const_iterator val_iter 
            = _vals.begin(), val_end = _elms.end();
                
            while(elm_iter != elm_end && val_iter != val_end) {
                    
            if((*elm_iter) == name) 
                        
            return *val_iter; 
                    elm_iter 
            ++
                    val_iter 
            ++
                }

                
            return default_value; 
            }


            void
            MyProperties::remove(
            const string &name) 
            {
                
            if(name.empty()) return;
                vector::iterator elm_iter 
            = _elms.begin(), elm_end = _elms.end();
                vector::iterator val_iter 
            = _vals.begin(), val_end = _elms.end();
                
            while(elm_iter != elm_end && val_iter != val_end) {
                    
            if((*elm_iter) == name) {
                        _elms.erase(elm_iter); 
                        _vals.erase(val_iter); 
                        _size 
            --;
                    }
             
                    elm_iter 
            ++
                    val_iter 
            ++
                }

            }


            void
            MyProperties::set(
            const string &name, const string &value) 
            {
                
            if(name.empty()) return;
                vector::iterator elm_iter 
            = _elms.begin(), elm_end = _elms.end();
                vector::iterator val_iter 
            = _vals.begin(), val_end = _elms.end();
                
            while(elm_iter != elm_end && val_iter != val_end) {
                    
            if((*elm_iter) == name) 
                        
            *val_iter = value; 
                    elm_iter 
            ++
                    val_iter 
            ++
                }

            }


            void 
            MyProperties::add(
            const string &name, const string &value) 
            {
                
            if(name.empty()) return
                _elms.push_back(name); _vals.push_back(value); 
                _size 
            ++
            }


            void 
            MyProperties::save(string 
            &filename)
            {
                
            if(filename.empty()) 
                    
            return;
                ifstream is(filename.c_str()); 
                
            if(!is) return
                
                
            char ln[LINE_SIZE+1], *pval; 
                ostrstream sbuf; string str, str2; bool exist_flag;
                vector vec; 

                
            while(is.getline(ln, LINE_SIZE)) 
                    str 
            = ln; trimleft(ln); exist_flag = false;
                    
            if(ln[0!= '#' && (pval = strchr(ln, '=')) != NULL) {
                        pval[
            0= 0; trimright(ln); 
                        
            if(ln[0!= 0 && !(str2=get(ln)).empty()) {
                            sbuf 
            << ln << "=" << str2 << endl;
                            vec.push_back(ln); 
                            exist_flag 
            = true
                        }

                    }

                    
            if(exist_flag == false
                        sbuf 
            << str << endl; 
                }


                vector::iterator elm_iter 
            = _elms.begin(), elm_end = _elms.end();
                vector::iterator val_iter 
            = _vals.begin(), val_end = _elms.end();
                vector::iterator vec_iter, vec_end 
            = vec.end();
                
            while(elm_iter != elm_end && val_iter != val_end) {
                    vec_iter 
            = vec.begin(); exist_flag = false;
                    
            while(vec_iter != vec_end) {
                        
            if((*vec_iter) == (*elm_iter)) {
                            exist_flag 
            = truebreak
                        }
             
                        vec_iter 
            ++
                    }
             
                    
            if(exist_flag == false
                        sbuf 
            << *elm_iter << "=" << *val_iter << endl; 
                    elm_iter 
            ++; val_iter ++
                }
             
                sbuf 
            << ends;
                
                ofstream os(filename.c_str()); 
                
            if(os) os << sbuf.str();
            }


            void 
            MyProperties::save()
            {
                save(_file);
            }


            /* trim the space and tab at left */
            char*
            trimleft( 
            char *s )
            {
                register 
            int i=0int sl=0;
                
            char *sc =0;

                
            if( s == NULL || s[0== 0 ) return NULL;
                sl 
            = strlen( s );

                
            /* trim space and tab char at the left */
                
            for( i=0; i s )
                
            {
                    sl 
            = strlen(sc);
                    
            for( i=0; i=0; i-- )
                
            {
                    
            /* 32 is space, 9 is tab */
                    
            if( s[i] == 32 || s[i] == 9 )
                        s[i] 
            = 0;
                    
            else
                        
            break;
                }

                
            return s;
            }


            /* trim space and tab at left and right */
            char*
            trim( 
            char *s )
            {
                
            if( s == NULL || s[0== 0 ) return NULL;

                trimright( s );
                trimleft( s );

                
            return s;
            }


            posted on 2005-10-03 11:45 Javen-Studio 閱讀(374) 評論(0)  編輯 收藏 引用
            青青草原综合久久大伊人导航| 性做久久久久久久| 久久线看观看精品香蕉国产| 久久久久亚洲精品男人的天堂| 色偷偷久久一区二区三区| 久久精品无码一区二区日韩AV | 久久久久无码精品| 久久综合鬼色88久久精品综合自在自线噜噜| 亚洲av日韩精品久久久久久a| 2021精品国产综合久久| 久久久精品人妻一区二区三区四| 久久久高清免费视频| 国产午夜福利精品久久2021| 狠狠综合久久综合88亚洲| 人妻丰满?V无码久久不卡| 人妻无码αv中文字幕久久| 久久久久国产一区二区三区| avtt天堂网久久精品| 久久狠狠爱亚洲综合影院| 亚洲伊人久久综合中文成人网| 久久精品国产WWW456C0M| 久久婷婷五月综合色高清| 精品综合久久久久久88小说 | 久久九九久精品国产免费直播| 久久综合88熟人妻| 久久妇女高潮几次MBA| 久久婷婷人人澡人人| 国内精品欧美久久精品| 99久久精品毛片免费播放| 久久青青草原亚洲av无码app| 中文字幕无码久久久| 青青草国产97免久久费观看| 精品久久久久久无码人妻蜜桃| 亚洲国产精品一区二区久久| 国产69精品久久久久99尤物| 国产精品免费久久久久久久久| 久久99国内精品自在现线| 国产成人久久精品一区二区三区 | 久久精品aⅴ无码中文字字幕重口| 久久精品综合网| 亚洲AV无码成人网站久久精品大|