yast2-core
IniParser.h
Go to the documentation of this file.
1 
13 #ifndef __IniParser_h__
14 #define __IniParser_h__
15 
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <regex.h>
20 #include <locale.h>
21 
22 #include <y2util/RepDef.h>
23 #include <YCP.h>
24 
25 #include <iosfwd>
26 #include <fstream>
27 #include <string>
28 #include <vector>
29 #include <set>
30 
31 #include "scr/SCRAgent.h"
32 
33 #include "IniFile.h"
34 
35 using std::string;
36 using std::vector;
37 using std::ifstream;
38 using std::ofstream;
39 using std::set;
40 
42 
43 #pragma GCC visibility push(hidden)
44 
46 // It is restored when we go out of scope.
48 {
49 public:
50  TemporaryLocale (int category, const char * locale);
52 private:
54  char *my_setlocale(int category, const char *locale);
55 
56  int _category;
57  char * _oldlocale;
58 };
59 #pragma GCC visibility pop
60 
65 class Regex_t : virtual public Rep
66 {
67  REP_BODY (Regex_t);
68 private:
69  friend class Regex;
70 
71  regex_t regex;
72  bool live;
73 
74 public:
75  Regex_t ():
76  live (false) {}
77  ~Regex_t () {
78  if (live)
79  {
80  regfree (&regex);
81  }
82  }
90  int compile (const string& pattern, bool ignore_case) {
91  int ret = -1;
92  if (live)
93  {
94  y2error ("Regex_t @%p already compiled", this);
95  }
96  else
97  {
98  // #177560: [A-Za-z] excludes some ASCII letters in Estonian
99  TemporaryLocale tl (LC_ALL, "C");
100 
101  ret = regcomp (&regex, pattern.c_str (),
102  REG_EXTENDED | (ignore_case ? REG_ICASE : 0));
103  if (ret)
104  {
105  char error[256];
106  regerror (ret, &regex, error, 256);
107  y2error ("Regex_t %s error: %s", pattern.c_str (), error);
108  }
109  else
110  {
111  live = true;
112  }
113  }
114  return ret;
115  }
116 };
117 
121 class Regex
122 {
123  Regex_tPtr rxtp;
124 public:
125  Regex (): rxtp (0) {}
132  int compile (const string& pattern, bool ignore_case) {
133  if (rxtp)
134  {
135  y2error ("Regex_t @%p already compiled", this);
136  return -1;
137  }
138  else
139  {
140  rxtp = new Regex_t;
141  return rxtp->compile (pattern, ignore_case);
142  }
143  }
144  const regex_t * regex () const { return & rxtp->regex; }
145 };
146 
151 {
152 public:
154  vector<string> matches;
156  string rest;
157 
159  const string& operator[] (size_t i) { return matches[i]; }
161  operator bool () { return matches.size () > 0; }
162 
168  RegexMatch (const Regex& rx, const string& s, size_t nmatch = 20) {
169  // allocate at least for the whole match
170  if (nmatch == 0)
171  {
172  nmatch = 1;
173  }
174  regmatch_t rm_matches[nmatch];
175  if (0 == regexec (rx.regex (), s.c_str (), nmatch, rm_matches, 0))
176  {
177  // match
178  matches.reserve (nmatch);
179  rest = s.substr (0, rm_matches[0].rm_so) +
180  s.substr (rm_matches[0].rm_eo);
181  }
182  else
183  {
184  // no match
185  rm_matches[0].rm_so = -1;
186  rest = s;
187  }
188 
189  size_t i;
190  for (i = 0; i < nmatch && rm_matches[i].rm_so != -1; ++i)
191  {
192  matches.push_back (s.substr (rm_matches[i].rm_so,
193  rm_matches[i].rm_eo - rm_matches[i].rm_so));
194  }
195  }
196 
197 };
198 
202 struct IoPattern
203 {
205  string out;
206 };
207 
211 struct section
212 {
215  bool end_valid;
216 };
217 
221 struct param
222 {
231 };
232 
234 struct FileDescr
235 {
239  string fn;
243  string sn;
247  time_t timestamp;
248  FileDescr (const char*fn);
249  bool changed ();
250  FileDescr () {}
251 };
252 
257 {
258 private:
262  time_t timestamp;
267  map<string,FileDescr> multi_files;
271  string file;
275  time_t getTimeStamp();
302  bool read_only;
304  bool flat;
307 
309  string subindent;
313  vector<Regex> linecomments;
317  vector<Regex> comments;
321  vector<section> sections;
325  vector<param> params;
329  vector<IoPattern> rewrites;
330 
334  ifstream scanner;
338  string scanner_file;
343 
348  bool started;
349 
357  vector<string> files;
358 
362  const SCRAgent &agent;
363 
367  int scanner_start(const char*fn);
371  void scanner_stop();
375  int scanner_get(string&s);
376 
380  int parse_helper(IniSection&ini);
384  int write_file(const string & filename, IniSection & section);
388  int write_helper(IniSection&ini, ofstream&of,int depth);
389 
390 public:
401  set<string> deleted_sections;
406  // apparently the uninitialized members are filled in
407  // by the grammar definition
408  IniParser (const SCRAgent &agent_) :
409  timestamp (0),
410  linecomments (), comments (),
411  sections (), params (), rewrites (),
412  started (false), multiple_files (false),
413  agent(agent_), inifile (this)
414  {}
415  ~IniParser ();
420  void initFiles (const char*fn);
425  void initFiles (const YCPList&f);
431  int initMachine (const YCPMap&scr);
432  bool isStarted() { return started; }
433 
438  int parse();
443  void UpdateIfModif ();
444 
448  int write ();
449 
455  bool sectionNeedsEnd (int i) { return sections[i].end_valid; }
456 
464  string getFileName (const string&sec, int rb) const;
468  bool HaveRewrites () const { return rewrites.size () > 0; }
469 
471  bool repeatNames () const { return repeat_names; }
473  bool isFlat () const { return flat; }
474 
480  string changeCase (const string&str) const;
481 };
482 
483 #endif//__IniParser_h__
vector< section > sections
Definition: IniParser.h:321
int write()
Definition: IniParser.cc:937
Regex begin
Definition: IniParser.h:226
Definition: IniParser.h:202
Set and later restore a locale category.
Definition: IniParser.h:47
Regex_tPtr rxtp
Definition: IniParser.h:123
int scanner_start(const char *fn)
Definition: IniParser.cc:414
#define str
Definition: scanner.cc:997
string sn
Definition: IniParser.h:243
TemporaryLocale(int category, const char *locale)
Definition: IniParser.cc:35
void initFiles(const char *fn)
Definition: IniParser.cc:166
regex_t regex
Definition: IniParser.h:71
Definition: IniFile.h:227
int parse_helper(IniSection &ini)
Definition: IniParser.cc:568
bool sectionNeedsEnd(int i)
Definition: IniParser.h:455
string out
Definition: IniParser.h:205
string subindent
Definition: IniParser.h:309
map< const string, inisection > inifile
Definition: miniini.h:21
bool end_valid
Definition: IniParser.h:215
bool ignore_case_regexps
Definition: IniParser.h:279
Definition: IniParser.h:150
IoPattern end
Definition: IniParser.h:214
Definition: IniParser.h:211
ifstream scanner
Definition: IniParser.h:334
bool flat
Definition: IniParser.h:304
vector< Regex > linecomments
Definition: IniParser.h:313
int compile(const string &pattern, bool ignore_case)
Definition: IniParser.h:132
vector< string > files
Definition: IniParser.h:357
bool repeatNames() const
accessor method
Definition: IniParser.h:471
set< string > deleted_sections
Definition: IniParser.h:401
bool prefer_uppercase
Definition: IniParser.h:283
Definition: IniParser.h:121
Regex end
Definition: IniParser.h:228
bool isFlat() const
accessor method
Definition: IniParser.h:473
bool first_upper
Definition: IniParser.h:288
IniSection inifile
Definition: IniParser.h:405
int write_file(const string &filename, IniSection &section)
Definition: IniParser.cc:1004
~IniParser()
Definition: IniParser.cc:58
const SCRAgent & agent
Definition: IniParser.h:362
map< string, FileDescr > multi_files
Definition: IniParser.h:267
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
int scanner_line
Definition: IniParser.h:342
bool started
Definition: IniParser.h:348
time_t getTimeStamp()
Definition: IniParser.cc:922
string fn
Definition: IniParser.h:239
bool comments_last
Definition: IniParser.h:296
bool global_values
Definition: IniParser.h:292
bool repeat_names
Definition: IniParser.h:294
SuSE Configuration Repository Agent.
Definition: SCRAgent.h:37
REP_BODY(Regex_t)
bool join_multiline
Definition: IniParser.h:298
string getFileName(const string &sec, int rb) const
Definition: IniParser.cc:1109
Definition: IniParser.h:65
~Regex_t()
Definition: IniParser.h:77
Regex rx
Definition: IniParser.h:204
bool line_can_continue
Definition: IniParser.h:277
bool shell_quoted_value
Definition: IniParser.h:306
int initMachine(const YCPMap &scr)
Definition: IniParser.cc:171
int parse()
Definition: IniParser.cc:488
vector< IoPattern > rewrites
Definition: IniParser.h:329
bool no_finalcomment_kill
Definition: IniParser.h:300
const string & operator[](size_t i)
Definition: IniParser.h:159
IoPattern line
Definition: IniParser.h:224
vector< param > params
Definition: IniParser.h:325
string rest
Definition: IniParser.h:156
Definition: IniParser.h:221
#define y2error(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:112
string file
Definition: IniParser.h:271
FileDescr()
Definition: IniParser.h:250
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 UpdateIfModif()
Definition: IniParser.cc:897
bool isStarted()
Definition: IniParser.h:432
bool ignore_case
Definition: IniParser.h:281
bool multiple_files
Definition: IniParser.h:353
RegexMatch(const Regex &rx, const string &s, size_t nmatch=20)
Definition: IniParser.h:168
~TemporaryLocale()
Definition: IniParser.cc:43
bool read_only
Definition: IniParser.h:302
string changeCase(const string &str) const
Definition: IniParser.cc:1129
File description (name, section name, mtime); ini-agent.
Definition: IniParser.h:234
IoPattern begin
Definition: IniParser.h:213
bool HaveRewrites() const
Definition: IniParser.h:468
Regex_t()
has regex been regcomp&#39;d and should it be regfree&#39;d?
Definition: IniParser.h:75
char * my_setlocale(int category, const char *locale)
call setlocale but log errors
Definition: IniParser.cc:50
time_t timestamp
Definition: IniParser.h:247
bool no_nested_sections
Definition: IniParser.h:290
const regex_t * regex() const
Definition: IniParser.h:144
int write_helper(IniSection &ini, ofstream &of, int depth)
Definition: IniParser.cc:1049
bool live
glibc regex buffer
Definition: IniParser.h:72
IniParser(const SCRAgent &agent_)
Definition: IniParser.h:408
bool multiline_valid
Definition: IniParser.h:230
void scanner_stop()
Definition: IniParser.cc:423
Base class for reference counted objects.
Definition: Rep.h:46
int scanner_get(string &s)
Definition: IniParser.cc:428
int compile(const string &pattern, bool ignore_case)
Definition: IniParser.h:90
time_t timestamp
Definition: IniParser.h:262
bool changed()
Definition: IniParser.cc:459
string scanner_file
Definition: IniParser.h:338
vector< Regex > comments
Definition: IniParser.h:317
int _category
Definition: IniParser.h:56
vector< string > matches
Definition: IniParser.h:154
#define DEFINE_BASE_POINTER(NAME)
Definition: RepDef.h:53
char * _oldlocale
Definition: IniParser.h:57
Regex()
Definition: IniParser.h:125
Definition: IniParser.h:256

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