yast2-core
IniFile.h
Go to the documentation of this file.
1 
14 #ifndef __IniFile_h__
15 #define __IniFile_h__
16 
17 #include <string>
18 #include <map>
19 #include <list>
20 #include <vector>
21 
22 #include <YCP.h>
23 
24 using std::map;
25 using std::multimap;
26 using std::list;
27 using std::vector;
28 using std::string;
29 
36 class IniBase
37 {
38 protected:
40  string name;
42  string comment;
44  int read_by;
46  bool dirty;
47 
49  IniBase (int rb)
50  : name (), comment (), read_by (rb), dirty (false) {}
52  IniBase (const string &n)
53  : name (n), comment (), read_by (0), dirty (true) {}
54 public:
55  virtual ~IniBase () {}
56 
57  const char* getName() const { return name.c_str(); }
58  const char* getComment() const { return comment.c_str(); }
59  int getReadBy() const { return read_by; }
60 
62  virtual void clean() { dirty = false; }
63 
65  void setName(const string&c) { dirty = true; name = c; }
67  void setComment(const string&c) { dirty = true; comment = c; }
69  void setReadBy(int r) { dirty = true; read_by = r; }
71  void setDirty() { dirty = true; }
72 
74  void initName(const string&c) { if (!dirty) name = c; }
76  void initComment(const string&c) { if (!dirty) comment = c; }
78  void initReadBy(const int r) { if (!dirty) read_by = r; }
79 
81  void init(const string &n,const string&c, int rb)
82  {
83  if (!dirty)
84  {
85  name = n;
86  comment = c;
87  read_by = rb;
88  }
89  }
90 
91 protected:
95  virtual YCPMap getAllDoIt () {
96  YCPMap m;
97 
98  m->add (YCPString ("name"), YCPString (name));
99  m->add (YCPString ("type"), YCPInteger (read_by));
100  m->add (YCPString ("comment"), YCPString (comment));
101  return m;
102  }
103 
105  bool getMapString (const YCPMap &in, const string &k, string &s) {
106  YCPValue v = in->value (YCPString (k));
107  if (v.isNull () || !v->isString ())
108  {
109  y2error ("Missing in Write (.all): %s", k.c_str ());
110  return false;
111  }
112  s = v->asString ()->value ();
113  return true;
114  }
115 
117  bool getMapInteger (const YCPMap &in, const string &k, int &i) {
118  YCPValue v = in->value (YCPString (k));
119  if (v.isNull () || !v->isInteger ())
120  {
121  y2error ("Missing in Write (.all): %s", k.c_str ());
122  return false;
123  }
124  i = v->asInteger ()->value ();
125  return true;
126  }
127 
128  virtual int setAllDoIt (const YCPMap &in) {
129  dirty = true;
130 
131  bool ok = true;
132  ok = ok && getMapString (in, "name", name);
133  ok = ok && getMapInteger (in, "type", read_by);
134  ok = ok && getMapString (in, "comment", comment);
135  return ok? 0: -1;
136  }
137 };
138 
142 class IniEntry : public IniBase
143 {
144 private:
146  string val;
147 public:
149  : IniBase (0), val () {}
151  IniEntry (const char *u): IniBase (u) {}
152 
153  const char* getValue() const { return val.c_str(); }
154 
155  void setValue(const string&c) { dirty = true; val = c; }
156 
158  void initValue(const string&c) { if (!dirty) val = c; }
160  void initReadBy(const int r) { if (!dirty) read_by = r; }
161 
163  void init(const string &n, const string &c, int rb, const string &v)
164  {
165  if (!dirty)
166  {
167  val = v;
168  IniBase::init (n, c, rb);
169  }
170  }
171 
174  m->add (YCPString ("kind"), YCPString ("value"));
175  m->add (YCPString ("value"), YCPString (val));
176  return m;
177  }
178 
179  int setAllDoIt (const YCPMap &in) {
180  int ret = IniBase::setAllDoIt (in);
181  if (ret == 0)
182  {
183  string kind;
184  if (!getMapString (in, "kind", kind) || kind != "value")
185  {
186  y2error ("Kind should be 'value'");
187  return -1;
188  }
189 
190  if (!getMapString (in, "value", val))
191  {
192  return -1;
193  }
194  }
195  return ret;
196  }
197 };
198 
199 class IniSection;
200 
201 
204 
205 typedef list<IniContainerElement> IniContainer;
206 typedef IniContainer::iterator IniIterator;
207 
209 typedef multimap<string, IniIterator> IniEntryIndex;
210 typedef multimap<string, IniIterator> IniSectionIndex;
219 typedef IniEntryIndex::iterator IniEntryIdxIterator;
220 typedef IniSectionIndex::iterator IniSectionIdxIterator;
221 
222 class IniParser;
223 
227 class IniSection : public IniBase
228 {
229 private:
230  // huh??? allow_values, allow_sections and allow_subsub
231  // were never actuially used
232 
236  const IniParser *ip;
237 
242  string end_comment;
243 
250 
256 
263  // these must be kept up to date!
272 
274  void reindex ();
275 
286  int getMyValue (const YCPPath &p, YCPValue &out, int what, int depth);
297  int getValue (const YCPPath&p, YCPValue&out,int what, int depth = 0);
308  int getSectionProp (const YCPPath&p, YCPValue&out,int what, int depth = 0);
318  int getAll (const YCPPath&p, YCPValue&out, int depth);
322  YCPMap getAllDoIt ();
323 
330  int myDir (YCPList& l, IniType what);
331 
341  int dirHelper (const YCPPath&p, YCPList&out,int sections,int depth = 0);
352  int setMyValue (const YCPPath &p, const YCPValue&in, int what, int depth);
361  int setValue (const YCPPath&p,const YCPValue&in,int what, int depth = 0);
370  int setSectionProp (const YCPPath&p,const YCPValue&in, int what, int depth);
379  int setAll (const YCPPath&p, const YCPValue& in, int depth);
385  int setAllDoIt (const YCPMap &in);
392  int delValue (const YCPPath&p, int depth);
399  int delSection (const YCPPath&p, int depth);
400 
405  void delMyValue (const string &k);
409  void delValue1 (IniEntryIdxIterator exi);
414 
421  int getValueFlat (const YCPPath&p, YCPValue&out);
428  int setValueFlat (const YCPPath&p, const YCPValue& in);
432  int delValueFlat (const YCPPath&p);
436  int dirValueFlat (const YCPPath&p, YCPList&l);
437 // IniSection ();
438 public:
440  IniSection (const char *u): IniBase (u) {}
441 
443  : IniBase (-1),
444  ip (p),
445  end_comment (), is_private(false), rewrite_by(-1),
446  container (), ivalues (), isections ()
447  {}
448 
453  IniSection (const IniSection &s) :
454  IniBase (s),
455  ip (s.ip),
456  end_comment (s.end_comment), is_private(s.is_private), rewrite_by (s.rewrite_by),
457  container (s.container)
458  { reindex (); }
459 
460  void operator = (const IniSection &s)
461  {
462  if (&s == this)
463  {
464  return;
465  }
466  IniBase::operator = (s);
467  ip = s.ip;
468  end_comment = s.end_comment;
469  is_private = s.is_private;
470  rewrite_by = s.rewrite_by;
471  container = s.container;
472 
473  reindex ();
474  }
475 
476  virtual ~IniSection () {}
477 
483  IniSection (const IniParser *p, string n)
484  : IniBase (n),
485  ip (p),
486  end_comment (), is_private(false), rewrite_by(0),
487  container(), ivalues (), isections ()
488  {}
497  void initValue (const string&key,const string&val,const string&comment,int rb);
507  IniSection& initSection (const string&name,const string&comment,int rb, int wb=-2);
512  void initReadBy () { read_by = -1; }
513 
515  void setRewriteBy (int c) { dirty = true; rewrite_by = c; }
516  int getRewriteBy() { return rewrite_by; }
521  int getSubSectionRewriteBy (const char*name);
522 
523  void setPrivate(bool p) { is_private = p; }
524  bool isPrivate() const { return is_private; }
525 
532  void setEndComment (const char*c);
533  const char* getEndComment() const { return end_comment.c_str(); }
534 
535  bool isDirty ();
537  virtual void clean();
538 
548  IniSection& findSection(const vector<string>&path, int from = 0);
563  int findEndFromUp(const vector<string>&path, int wanted, int found = -1, int from = 0);
564 
568  void Dump ();
569 
574  int Read (const YCPPath&p, YCPValue&out, bool rewrite);
578  int Dir (const YCPPath&p, YCPList&out);
583  int Write (const YCPPath&p, const YCPValue&v, bool rewrite);
589  int Delete (const YCPPath&p);
590 
591  // used by IniParser::write
594 
600 //unused
601 // IniEntry& getEntry (const char*name);
608  IniSection& getSection (const char*name);
609 };
610 
615 {
619 
620 public:
622  IniType t () const { return _t; }
623  const IniEntry& e () const { return _e; }
624  IniEntry& e () { return _e; }
625  const IniSection& s () const { return _s; }
626  IniSection& s () { return _s; }
627 
630  _t (VALUE),
631  _e (e),
632 // _s (IniSection ("uninitialized"))
633  _s (IniSection ((const IniParser *)NULL))
634  {}
635 
638  _t (SECTION),
639 // _e (IniEntry ("uninitialized")),
640  _e (IniEntry ()),
641  _s (s)
642  {}
643 
645  _t (c._t),
646  _e (c._e),
647  _s (c._s)
648  {}
649 };
650 
651 
652 #endif//__IniFile_h__
IniSection & s()
Definition: IniFile.h:626
const char * getEndComment() const
Definition: IniFile.h:533
bool getMapInteger(const YCPMap &in, const string &k, int &i)
helper for setAllDoIt
Definition: IniFile.h:117
IniSection(const char *u)
Definition: IniFile.h:440
IniEntryIndex ivalues
Definition: IniFile.h:267
list< IniContainerElement > IniContainer
Definition: IniFile.h:203
IniSection(const IniSection &s)
Definition: IniFile.h:453
void delMyValue(const string &k)
Definition: IniFile.cc:956
const char * getValue() const
Definition: IniFile.h:153
bool dirty
Definition: IniFile.h:46
Definition: IniFile.h:202
int delValueFlat(const YCPPath &p)
Definition: IniFile.cc:1045
void setComment(const string &c)
Definition: IniFile.h:67
multimap< string, IniIterator > IniEntryIndex
Definition: IniFile.h:209
const IniParser * ip
Definition: IniFile.h:236
void add(const YCPValue &key, const YCPValue &value)
Definition: YCPMap.h:199
int rewrite_by
Definition: IniFile.h:255
IniType
Definition: IniFile.h:202
bool is_private
Definition: IniFile.h:249
string val
Definition: IniFile.h:146
const char * getComment() const
Definition: IniFile.h:58
void setValue(const string &c)
Definition: IniFile.h:155
int getSubSectionRewriteBy(const char *name)
Definition: IniFile.cc:1153
YCPValue value(const YCPValue &key) const
Definition: YCPMap.h:205
IniContainer container
Definition: IniFile.h:262
Definition: IniFile.h:227
Wrapper for YCPIntegerRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPIntegerRep with the arrow operator. See YCPIntegerRep.
Definition: YCPInteger.h:92
int setAllDoIt(const YCPMap &in)
Definition: IniFile.h:179
int setSectionProp(const YCPPath &p, const YCPValue &in, int what, int depth)
Definition: IniFile.cc:508
YCPMap getAllDoIt()
Definition: IniFile.cc:426
const char * getName() const
Definition: IniFile.h:57
IniIterator getContainerBegin()
Definition: IniFile.cc:1218
IniSection(const IniParser *p)
Definition: IniFile.h:442
void setDirty()
Definition: IniFile.h:71
int myDir(YCPList &l, IniType what)
Definition: IniFile.cc:1004
IniEntry & e()
Definition: IniFile.h:624
void delValue1(IniEntryIdxIterator exi)
Definition: IniFile.cc:948
int Dir(const YCPPath &p, YCPList &out)
Definition: IniFile.cc:1095
void setEndComment(const char *c)
Definition: IniFile.cc:1180
bool isDirty()
Definition: IniFile.cc:1187
virtual ~IniSection()
Definition: IniFile.h:476
const IniSection & s() const
Definition: IniFile.h:625
void init(const string &n, const string &c, int rb)
Definition: IniFile.h:81
IniContainer::iterator IniIterator
Definition: IniFile.h:206
int Delete(const YCPPath &p)
Definition: IniFile.cc:459
void operator=(const IniSection &s)
Definition: IniFile.h:460
void initComment(const string &c)
Definition: IniFile.h:76
multimap< string, IniIterator > IniSectionIndex
Definition: IniFile.h:210
virtual ~IniBase()
Definition: IniFile.h:55
int getSectionProp(const YCPPath &p, YCPValue &out, int what, int depth=0)
Definition: IniFile.cc:345
IniSection & findSection(const vector< string > &path, int from=0)
Definition: IniFile.cc:161
IniSectionIndex::iterator IniSectionIdxIterator
Definition: IniFile.h:220
int read_by
Definition: IniFile.h:44
virtual void clean()
Definition: IniFile.cc:1201
int dirValueFlat(const YCPPath &p, YCPList &l)
Definition: IniFile.cc:1020
IniSectionIndex isections
Definition: IniFile.h:271
void initReadBy()
Definition: IniFile.h:512
int getValueFlat(const YCPPath &p, YCPValue &out)
Definition: IniFile.cc:1035
int delSection(const YCPPath &p, int depth)
Definition: IniFile.cc:643
IniContainerElement(const IniEntry &e)
construct from a value
Definition: IniFile.h:629
void initReadBy(const int r)
Definition: IniFile.h:78
Wrapper for YCPMapRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPMapRep with the arrow operator. See YCPMapRep.
Definition: YCPMap.h:184
virtual void clean()
Definition: IniFile.h:62
virtual YCPMap getAllDoIt()
Definition: IniFile.h:95
bool getMapString(const YCPMap &in, const string &k, string &s)
helper for setAllDoIt
Definition: IniFile.h:105
IniSection & getSection(const char *name)
Definition: IniFile.cc:1166
int getAll(const YCPPath &p, YCPValue &out, int depth)
Definition: IniFile.cc:402
void initValue(const string &c)
Definition: IniFile.h:158
bool isNull() const
Definition: YCPElement.h:347
int delValue(const YCPPath &p, int depth)
Definition: IniFile.cc:972
string comment
Definition: IniFile.h:42
IniContainerElement(const IniContainerElement &c)
Definition: IniFile.h:644
int findEndFromUp(const vector< string > &path, int wanted, int found=-1, int from=0)
Definition: IniFile.cc:174
IniContainerElement(const IniSection &s)
construct from a section
Definition: IniFile.h:637
YCPMap getAllDoIt()
Definition: IniFile.h:172
IniType t() const
accessors
Definition: IniFile.h:622
string end_comment
Definition: IniFile.h:242
Wrapper for YCPStringRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPStringRep with the arrow operator. See YCPStringRep.
Definition: YCPString.h:133
void Dump()
Definition: IniFile.cc:207
IniEntryIndex::iterator IniEntryIdxIterator
Definition: IniFile.h:219
int getMyValue(const YCPPath &p, YCPValue &out, int what, int depth)
Definition: IniFile.cc:283
IniBase(const string &n)
Definition: IniFile.h:52
IniType _t
Definition: IniFile.h:616
int dirHelper(const YCPPath &p, YCPList &out, int sections, int depth=0)
Definition: IniFile.cc:1115
Definition: IniFile.h:142
int Read(const YCPPath &p, YCPValue &out, bool rewrite)
Definition: IniFile.cc:1065
#define y2error(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:112
void setName(const string &c)
Definition: IniFile.h:65
Definition: IniFile.h:202
void initValue(const string &key, const string &val, const string &comment, int rb)
Definition: IniFile.cc:91
IniSection _s
Definition: IniFile.h:618
Wrapper for YCPListRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPListRep with the arrow operator. See YCPListRep.
Definition: YCPList.h:236
void init(const string &n, const string &c, int rb, const string &v)
Definition: IniFile.h:163
IniSection(const IniParser *p, string n)
Definition: IniFile.h:483
int getRewriteBy()
Definition: IniFile.h:516
int setMyValue(const YCPPath &p, const YCPValue &in, int what, int depth)
Definition: IniFile.cc:806
int setValueFlat(const YCPPath &p, const YCPValue &in)
Definition: IniFile.cc:1055
int setAll(const YCPPath &p, const YCPValue &in, int depth)
Definition: IniFile.cc:683
void initReadBy(const int r)
Definition: IniFile.h:160
int getValue(const YCPPath &p, YCPValue &out, int what, int depth=0)
Definition: IniFile.cc:321
IniEntry(const char *u)
Definition: IniFile.h:151
const IniEntry & e() const
Definition: IniFile.h:623
int setValue(const YCPPath &p, const YCPValue &in, int what, int depth=0)
Definition: IniFile.cc:907
void setPrivate(bool p)
Definition: IniFile.h:523
Definition: IniFile.h:36
Wrapper for YCPValueRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPValueRep with the arrow operator. See YCPValueRep.
Definition: YCPValue.h:275
virtual int setAllDoIt(const YCPMap &in)
Definition: IniFile.h:128
void delSection1(IniSectionIdxIterator sxi)
Definition: IniFile.cc:635
IniSection & initSection(const string &name, const string &comment, int rb, int wb=-2)
Definition: IniFile.cc:121
Wrapper for YCPPathRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPPathRep with the arrow operator. See YCPPathRep.
Definition: YCPPath.h:175
IniEntry()
Definition: IniFile.h:148
int Write(const YCPPath &p, const YCPValue &v, bool rewrite)
Definition: IniFile.cc:476
IniIterator getContainerEnd()
Definition: IniFile.cc:1223
void initName(const string &c)
Definition: IniFile.h:74
string name
Definition: IniFile.h:40
void setRewriteBy(int c)
Definition: IniFile.h:515
IniEntry _e
Definition: IniFile.h:617
bool isPrivate() const
Definition: IniFile.h:524
IniBase(int rb)
Definition: IniFile.h:49
void reindex()
Definition: IniFile.cc:258
Definition: IniFile.h:614
void setReadBy(int r)
Definition: IniFile.h:69
int setAllDoIt(const YCPMap &in)
Definition: IniFile.cc:706
int getReadBy() const
Definition: IniFile.h:59
Definition: IniParser.h:256

Generated on a sunny day for yast2-core by doxygen 1.8.8