• <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不| 久久国产欧美日韩精品| 久久精品中文字幕有码| 亚洲国产精品成人AV无码久久综合影院| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久久久久久人妻无码中文字幕爆 | 四虎影视久久久免费观看| 久久精品国产亚洲AV久| 狠狠色丁香久久婷婷综| 亚洲综合久久久| 国产91久久综合| 久久亚洲美女精品国产精品| 久久婷婷人人澡人人| 久久久久亚洲精品无码蜜桃| 亚洲嫩草影院久久精品| 性欧美大战久久久久久久久| 久久精品国产亚洲5555| 国产欧美久久久精品| 国产69精品久久久久观看软件| 色综合久久88色综合天天 | 久久综合九色综合久99| 亚洲精品乱码久久久久久| 久久丝袜精品中文字幕| 成人精品一区二区久久| 精品乱码久久久久久久| 国产精品成人久久久| 欧美激情精品久久久久久久九九九 | 久久夜色精品国产噜噜亚洲AV| 日本加勒比久久精品| 久久国产成人午夜AV影院| 久久精品国产精品青草app| 午夜欧美精品久久久久久久| 久久久久久亚洲精品影院| 亚洲&#228;v永久无码精品天堂久久 | 久久66热人妻偷产精品9| 人妻精品久久久久中文字幕69| 99久久精品免费看国产一区二区三区| 久久久久久青草大香综合精品| 久久99国产精品成人欧美| 国内精品久久久久久久亚洲| 狠狠色综合网站久久久久久久|