|
yast2-core
|
00001 00014 #ifndef __IniFile_h__ 00015 #define __IniFile_h__ 00016 00017 #include <string> 00018 #include <map> 00019 #include <list> 00020 #include <vector> 00021 00022 #include <YCP.h> 00023 00024 using std::map; 00025 using std::multimap; 00026 using std::list; 00027 using std::vector; 00028 using std::string; 00029 00036 class IniBase 00037 { 00038 protected: 00040 string name; 00042 string comment; 00044 int read_by; 00046 bool dirty; 00047 00049 IniBase (int rb) 00050 : name (), comment (), read_by (rb), dirty (false) {} 00052 IniBase (const string &n) 00053 : name (n), comment (), read_by (0), dirty (true) {} 00054 public: 00055 virtual ~IniBase () {} 00056 00057 const char* getName() const { return name.c_str(); } 00058 const char* getComment() const { return comment.c_str(); } 00059 int getReadBy() const { return read_by; } 00060 00062 virtual void clean() { dirty = false; } 00063 00065 void setName(const string&c) { dirty = true; name = c; } 00067 void setComment(const string&c) { dirty = true; comment = c; } 00069 void setReadBy(int r) { dirty = true; read_by = r; } 00071 void setDirty() { dirty = true; } 00072 00074 void initName(const string&c) { if (!dirty) name = c; } 00076 void initComment(const string&c) { if (!dirty) comment = c; } 00078 void initReadBy(const int r) { if (!dirty) read_by = r; } 00079 00081 void init(const string &n,const string&c, int rb) 00082 { 00083 if (!dirty) 00084 { 00085 name = n; 00086 comment = c; 00087 read_by = rb; 00088 } 00089 } 00090 00091 protected: 00095 virtual YCPMap getAllDoIt () { 00096 YCPMap m; 00097 00098 m->add (YCPString ("name"), YCPString (name)); 00099 m->add (YCPString ("type"), YCPInteger (read_by)); 00100 m->add (YCPString ("comment"), YCPString (comment)); 00101 return m; 00102 } 00103 00105 bool getMapString (const YCPMap &in, const string &k, string &s) { 00106 YCPValue v = in->value (YCPString (k)); 00107 if (v.isNull () || !v->isString ()) 00108 { 00109 y2error ("Missing in Write (.all): %s", k.c_str ()); 00110 return false; 00111 } 00112 s = v->asString ()->value (); 00113 return true; 00114 } 00115 00117 bool getMapInteger (const YCPMap &in, const string &k, int &i) { 00118 YCPValue v = in->value (YCPString (k)); 00119 if (v.isNull () || !v->isInteger ()) 00120 { 00121 y2error ("Missing in Write (.all): %s", k.c_str ()); 00122 return false; 00123 } 00124 i = v->asInteger ()->value (); 00125 return true; 00126 } 00127 00128 virtual int setAllDoIt (const YCPMap &in) { 00129 dirty = true; 00130 00131 bool ok = true; 00132 ok = ok && getMapString (in, "name", name); 00133 ok = ok && getMapInteger (in, "type", read_by); 00134 ok = ok && getMapString (in, "comment", comment); 00135 return ok? 0: -1; 00136 } 00137 }; 00138 00142 class IniEntry : public IniBase 00143 { 00144 private: 00146 string val; 00147 public: 00148 IniEntry () 00149 : IniBase (0), val () {} 00151 IniEntry (const char *u): IniBase (u) {} 00152 00153 const char* getValue() const { return val.c_str(); } 00154 00155 void setValue(const string&c) { dirty = true; val = c; } 00156 00158 void initValue(const string&c) { if (!dirty) val = c; } 00160 void initReadBy(const int r) { if (!dirty) read_by = r; } 00161 00163 void init(const string &n, const string &c, int rb, const string &v) 00164 { 00165 if (!dirty) 00166 { 00167 val = v; 00168 IniBase::init (n, c, rb); 00169 } 00170 } 00171 00172 YCPMap getAllDoIt () { 00173 YCPMap m = IniBase::getAllDoIt (); 00174 m->add (YCPString ("kind"), YCPString ("value")); 00175 m->add (YCPString ("value"), YCPString (val)); 00176 return m; 00177 } 00178 00179 int setAllDoIt (const YCPMap &in) { 00180 int ret = IniBase::setAllDoIt (in); 00181 if (ret == 0) 00182 { 00183 string kind; 00184 if (!getMapString (in, "kind", kind) || kind != "value") 00185 { 00186 y2error ("Kind should be 'value'"); 00187 return -1; 00188 } 00189 00190 if (!getMapString (in, "value", val)) 00191 { 00192 return -1; 00193 } 00194 } 00195 return ret; 00196 } 00197 }; 00198 00199 class IniSection; 00200 00201 00202 enum IniType { VALUE, SECTION,}; 00203 struct IniContainerElement; 00204 00205 typedef list<IniContainerElement> IniContainer; 00206 typedef IniContainer::iterator IniIterator; 00207 00209 typedef multimap<string, IniIterator> IniEntryIndex; 00210 typedef multimap<string, IniIterator> IniSectionIndex; 00219 typedef IniEntryIndex::iterator IniEntryIdxIterator; 00220 typedef IniSectionIndex::iterator IniSectionIdxIterator; 00221 00222 class IniParser; 00223 00227 class IniSection : public IniBase 00228 { 00229 private: 00230 // huh??? allow_values, allow_sections and allow_subsub 00231 // were never actuially used 00232 00236 const IniParser *ip; 00237 00242 string end_comment; 00243 00248 int rewrite_by; 00249 00255 IniContainer container; 00256 // these must be kept up to date! 00260 IniEntryIndex ivalues; 00264 IniSectionIndex isections; 00265 00267 void reindex (); 00268 00279 int getMyValue (const YCPPath &p, YCPValue &out, int what, int depth); 00290 int getValue (const YCPPath&p, YCPValue&out,int what, int depth = 0); 00301 int getSectionProp (const YCPPath&p, YCPValue&out,int what, int depth = 0); 00311 int getAll (const YCPPath&p, YCPValue&out, int depth); 00315 YCPMap getAllDoIt (); 00316 00323 int myDir (YCPList& l, IniType what); 00324 00334 int dirHelper (const YCPPath&p, YCPList&out,int sections,int depth = 0); 00345 int setMyValue (const YCPPath &p, const YCPValue&in, int what, int depth); 00354 int setValue (const YCPPath&p,const YCPValue&in,int what, int depth = 0); 00363 int setSectionProp (const YCPPath&p,const YCPValue&in, int what, int depth); 00372 int setAll (const YCPPath&p, const YCPValue& in, int depth); 00378 int setAllDoIt (const YCPMap &in); 00385 int delValue (const YCPPath&p, int depth); 00392 int delSection (const YCPPath&p, int depth); 00393 00398 void delMyValue (const string &k); 00402 void delValue1 (IniEntryIdxIterator exi); 00406 void delSection1 (IniSectionIdxIterator sxi); 00407 00414 int getValueFlat (const YCPPath&p, YCPValue&out); 00421 int setValueFlat (const YCPPath&p, const YCPValue& in); 00425 int delValueFlat (const YCPPath&p); 00429 int dirValueFlat (const YCPPath&p, YCPList&l); 00430 // IniSection (); 00431 public: 00433 IniSection (const char *u): IniBase (u) {} 00434 00435 IniSection (const IniParser *p) 00436 : IniBase (-1), 00437 ip (p), 00438 end_comment (), rewrite_by(-1), 00439 container (), ivalues (), isections () 00440 {} 00441 00446 IniSection (const IniSection &s) : 00447 IniBase (s), 00448 ip (s.ip), 00449 end_comment (s.end_comment), rewrite_by (s.rewrite_by), 00450 container (s.container) 00451 { reindex (); } 00452 00453 void operator = (const IniSection &s) 00454 { 00455 if (&s == this) 00456 { 00457 return; 00458 } 00459 IniBase::operator = (s); 00460 ip = s.ip; 00461 end_comment = s.end_comment; rewrite_by = s.rewrite_by; 00462 container = s.container; 00463 00464 reindex (); 00465 } 00466 00467 virtual ~IniSection () {} 00468 00474 IniSection (const IniParser *p, string n) 00475 : IniBase (n), 00476 ip (p), 00477 end_comment (), rewrite_by(0), 00478 container(), ivalues (), isections () 00479 {} 00488 void initValue (const string&key,const string&val,const string&comment,int rb); 00498 IniSection& initSection (const string&name,const string&comment,int rb, int wb=-2); 00503 void initReadBy () { read_by = -1; } 00504 00506 void setRewriteBy (int c) { dirty = true; rewrite_by = c; } 00507 int getRewriteBy() { return rewrite_by; } 00512 int getSubSectionRewriteBy (const char*name); 00513 00520 void setEndComment (const char*c); 00521 const char* getEndComment() const { return end_comment.c_str(); } 00522 00523 bool isDirty (); 00525 virtual void clean(); 00526 00536 IniSection& findSection(const vector<string>&path, int from = 0); 00551 int findEndFromUp(const vector<string>&path, int wanted, int found = -1, int from = 0); 00552 00556 void Dump (); 00557 00562 int Read (const YCPPath&p, YCPValue&out, bool rewrite); 00566 int Dir (const YCPPath&p, YCPList&out); 00571 int Write (const YCPPath&p, const YCPValue&v, bool rewrite); 00577 int Delete (const YCPPath&p); 00578 00579 // used by IniParser::write 00580 IniIterator getContainerBegin (); 00581 IniIterator getContainerEnd (); 00582 00588 //unused 00589 // IniEntry& getEntry (const char*name); 00596 IniSection& getSection (const char*name); 00597 }; 00598 00602 class IniContainerElement 00603 { 00604 IniType _t; 00605 IniEntry _e; 00606 IniSection _s; 00607 00608 public: 00610 IniType t () const { return _t; } 00611 const IniEntry& e () const { return _e; } 00612 IniEntry& e () { return _e; } 00613 const IniSection& s () const { return _s; } 00614 IniSection& s () { return _s; } 00615 00617 IniContainerElement (const IniEntry& e) : 00618 _t (VALUE), 00619 _e (e), 00620 // _s (IniSection ("uninitialized")) 00621 _s (IniSection ((const IniParser *)NULL)) 00622 {} 00623 00625 IniContainerElement (const IniSection& s) : 00626 _t (SECTION), 00627 // _e (IniEntry ("uninitialized")), 00628 _e (IniEntry ()), 00629 _s (s) 00630 {} 00631 00632 IniContainerElement (const IniContainerElement& c) : 00633 _t (c._t), 00634 _e (c._e), 00635 _s (c._s) 00636 {} 00637 }; 00638 00639 00640 #endif//__IniFile_h__
1.7.3