• <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 閱讀(376) 評論(0)  編輯 收藏 引用
            久久久久亚洲av无码专区导航| 欧美日韩久久中文字幕| 久久最近最新中文字幕大全 | 久久综合九色综合久99| 无码人妻久久一区二区三区蜜桃| 亚洲国产一成人久久精品| 好久久免费视频高清| 国产免费久久精品99re丫y| 狠色狠色狠狠色综合久久| 久久97久久97精品免视看| 久久久久亚洲AV无码专区首JN| 国内精品久久久久伊人av| 久久国产AVJUST麻豆| 99久久精品费精品国产| 久久亚洲私人国产精品vA| 热综合一本伊人久久精品| 狠色狠色狠狠色综合久久| 午夜久久久久久禁播电影| 久久福利资源国产精品999| 国产伊人久久| 国产精品九九久久免费视频| 久久一日本道色综合久久| 久久人人爽人人人人片av| 人妻无码久久精品| 久久午夜福利电影| 久久精品国产99久久久香蕉| 99久久久精品| 久久se精品一区二区| 久久er国产精品免费观看2| 久久亚洲私人国产精品| 久久精品人妻中文系列| 亚洲精品第一综合99久久 | 亚洲AV日韩精品久久久久久久| 欧美性猛交xxxx免费看久久久 | 久久久久国产一区二区三区| 日本道色综合久久影院| 蜜桃麻豆www久久| 国产精品美女久久久久av爽| 久久久久久国产精品免费免费| 精品熟女少妇aⅴ免费久久| 午夜福利91久久福利|