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 "IniFile.h"
32 
33 using std::string;
34 using std::vector;
35 using std::ifstream;
36 using std::ofstream;
37 using std::set;
38 
40 
41 #pragma GCC visibility push(hidden)
42 
44 // It is restored when we go out of scope.
46 {
47 public:
48  TemporaryLocale (int category, const char * locale);
50 private:
52  char *my_setlocale(int category, const char *locale);
53 
54  int _category;
55  char * _oldlocale;
56 };
57 #pragma GCC visibility pop
58 
63 class Regex_t : virtual public Rep
64 {
65  REP_BODY (Regex_t);
66 private:
67  friend class Regex;
68 
69  regex_t regex;
70  bool live;
71 
72 public:
73  Regex_t ():
74  live (false) {}
75  ~Regex_t () {
76  if (live)
77  {
78  regfree (&regex);
79  }
80  }
88  int compile (const string& pattern, bool ignore_case) {
89  int ret = -1;
90  if (live)
91  {
92  y2error ("Regex_t @%p already compiled", this);
93  }
94  else
95  {
96  // #177560: [A-Za-z] excludes some ASCII letters in Estonian
97  TemporaryLocale tl (LC_ALL, "C");
98 
99  ret = regcomp (&regex, pattern.c_str (),
100  REG_EXTENDED | (ignore_case ? REG_ICASE : 0));
101  if (ret)
102  {
103  char error[256];
104  regerror (ret, &regex, error, 256);
105  y2error ("Regex_t %s error: %s", pattern.c_str (), error);
106  }
107  else
108  {
109  live = true;
110  }
111  }
112  return ret;
113  }
114 };
115 
119 class Regex
120 {
121  Regex_tPtr rxtp;
122 public:
123  Regex (): rxtp (0) {}
130  int compile (const string& pattern, bool ignore_case) {
131  if (rxtp)
132  {
133  y2error ("Regex_t @%p already compiled", this);
134  return -1;
135  }
136  else
137  {
138  rxtp = new Regex_t;
139  return rxtp->compile (pattern, ignore_case);
140  }
141  }
142  const regex_t * regex () const { return & rxtp->regex; }
143 };
144 
149 {
150 public:
152  vector<string> matches;
154  string rest;
155 
157  const string& operator[] (size_t i) { return matches[i]; }
159  operator bool () { return matches.size () > 0; }
160 
166  RegexMatch (const Regex& rx, const string& s, size_t nmatch = 20) {
167  // allocate at least for the whole match
168  if (nmatch == 0)
169  {
170  nmatch = 1;
171  }
172  regmatch_t rm_matches[nmatch];
173  if (0 == regexec (rx.regex (), s.c_str (), nmatch, rm_matches, 0))
174  {
175  // match
176  matches.reserve (nmatch);
177  rest = s.substr (0, rm_matches[0].rm_so) +
178  s.substr (rm_matches[0].rm_eo);
179  }
180  else
181  {
182  // no match
183  rm_matches[0].rm_so = -1;
184  rest = s;
185  }
186 
187  size_t i;
188  for (i = 0; i < nmatch && rm_matches[i].rm_so != -1; ++i)
189  {
190  matches.push_back (s.substr (rm_matches[i].rm_so,
191  rm_matches[i].rm_eo - rm_matches[i].rm_so));
192  }
193  }
194 
195 };
196 
200 struct IoPattern
201 {
203  string out;
204 };
205 
209 struct section
210 {
213  bool end_valid;
214 };
215 
219 struct param
220 {
229 };
230 
232 struct FileDescr
233 {
237  string fn;
241  string sn;
245  time_t timestamp;
246  FileDescr (char*fn);
247  bool changed ();
248  FileDescr () {}
249 };
250 
255 {
256 private:
260  time_t timestamp;
265  map<string,FileDescr> multi_files;
269  string file;
273  time_t getTimeStamp();
300  bool read_only;
302  bool flat;
305 
307  string subindent;
311  vector<Regex> linecomments;
315  vector<Regex> comments;
319  vector<section> sections;
323  vector<param> params;
327  vector<IoPattern> rewrites;
328 
332  ifstream scanner;
336  string scanner_file;
341 
346  bool started;
347 
355  vector<string> files;
356 
360  int scanner_start(const char*fn);
364  void scanner_stop();
368  int scanner_get(string&s);
369 
373  int parse_helper(IniSection&ini);
377  int write_file(const string & filename, IniSection & section);
381  int write_helper(IniSection&ini, ofstream&of,int depth);
382 
383 public:
394  set<string> deleted_sections;
399  // apparently the uninitialized members are filled in
400  // by the grammar definition
402  timestamp (0),
403  linecomments (), comments (),
404  sections (), params (), rewrites (),
405  started (false), multiple_files (false),
406 // inifile ("toplevel")
407  inifile (this)
408  {}
409  ~IniParser ();
414  void initFiles (const char*fn);
419  void initFiles (const YCPList&f);
425  int initMachine (const YCPMap&scr);
426  bool isStarted() { return started; }
427 
432  int parse();
437  void UpdateIfModif ();
438 
442  int write ();
443 
449  bool sectionNeedsEnd (int i) { return sections[i].end_valid; }
450 
458  string getFileName (const string&sec, int rb) const;
462  bool HaveRewrites () const { return rewrites.size () > 0; }
463 
465  bool repeatNames () const { return repeat_names; }
467  bool isFlat () const { return flat; }
468 
474  string changeCase (const string&str) const;
475 };
476 
477 #endif//__IniParser_h__
vector< section > sections
Definition: IniParser.h:319
int write()
Definition: IniParser.cc:937
Regex begin
Definition: IniParser.h:224
Definition: IniParser.h:200
Set and later restore a locale category.
Definition: IniParser.h:45
Regex_tPtr rxtp
Definition: IniParser.h:121
int scanner_start(const char *fn)
Definition: IniParser.cc:414
#define str
Definition: scanner.cc:997
string sn
Definition: IniParser.h:241
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:69
Definition: IniFile.h:227
int parse_helper(IniSection &ini)
Definition: IniParser.cc:568
bool sectionNeedsEnd(int i)
Definition: IniParser.h:449
string out
Definition: IniParser.h:203
string subindent
Definition: IniParser.h:307
map< const string, inisection > inifile
Definition: miniini.h:21
bool end_valid
Definition: IniParser.h:213
bool ignore_case_regexps
Definition: IniParser.h:277
Definition: IniParser.h:148
IoPattern end
Definition: IniParser.h:212
Definition: IniParser.h:209
ifstream scanner
Definition: IniParser.h:332
bool flat
Definition: IniParser.h:302
vector< Regex > linecomments
Definition: IniParser.h:311
int compile(const string &pattern, bool ignore_case)
Definition: IniParser.h:130
vector< string > files
Definition: IniParser.h:355
IniParser()
Definition: IniParser.h:401
bool repeatNames() const
accessor method
Definition: IniParser.h:465
set< string > deleted_sections
Definition: IniParser.h:394
bool prefer_uppercase
Definition: IniParser.h:281
Definition: IniParser.h:119
Regex end
Definition: IniParser.h:226
bool isFlat() const
accessor method
Definition: IniParser.h:467
bool first_upper
Definition: IniParser.h:286
IniSection inifile
Definition: IniParser.h:398
int write_file(const string &filename, IniSection &section)
Definition: IniParser.cc:1004
~IniParser()
Definition: IniParser.cc:58
map< string, FileDescr > multi_files
Definition: IniParser.h:265
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:340
bool started
Definition: IniParser.h:346
time_t getTimeStamp()
Definition: IniParser.cc:922
string fn
Definition: IniParser.h:237
bool comments_last
Definition: IniParser.h:294
bool global_values
Definition: IniParser.h:290
bool repeat_names
Definition: IniParser.h:292
REP_BODY(Regex_t)
bool join_multiline
Definition: IniParser.h:296
string getFileName(const string &sec, int rb) const
Definition: IniParser.cc:1109
Definition: IniParser.h:63
~Regex_t()
Definition: IniParser.h:75
Regex rx
Definition: IniParser.h:202
bool line_can_continue
Definition: IniParser.h:275
bool shell_quoted_value
Definition: IniParser.h:304
int initMachine(const YCPMap &scr)
Definition: IniParser.cc:171
int parse()
Definition: IniParser.cc:488
vector< IoPattern > rewrites
Definition: IniParser.h:327
bool no_finalcomment_kill
Definition: IniParser.h:298
const string & operator[](size_t i)
Definition: IniParser.h:157
IoPattern line
Definition: IniParser.h:222
vector< param > params
Definition: IniParser.h:323
string rest
Definition: IniParser.h:154
Definition: IniParser.h:219
#define y2error(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:112
string file
Definition: IniParser.h:269
FileDescr()
Definition: IniParser.h:248
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:426
bool ignore_case
Definition: IniParser.h:279
bool multiple_files
Definition: IniParser.h:351
RegexMatch(const Regex &rx, const string &s, size_t nmatch=20)
Definition: IniParser.h:166
~TemporaryLocale()
Definition: IniParser.cc:43
bool read_only
Definition: IniParser.h:300
string changeCase(const string &str) const
Definition: IniParser.cc:1129
File description (name, section name, mtime); ini-agent.
Definition: IniParser.h:232
IoPattern begin
Definition: IniParser.h:211
bool HaveRewrites() const
Definition: IniParser.h:462
Regex_t()
has regex been regcomp&#39;d and should it be regfree&#39;d?
Definition: IniParser.h:73
char * my_setlocale(int category, const char *locale)
call setlocale but log errors
Definition: IniParser.cc:50
time_t timestamp
Definition: IniParser.h:245
bool no_nested_sections
Definition: IniParser.h:288
const regex_t * regex() const
Definition: IniParser.h:142
int write_helper(IniSection &ini, ofstream &of, int depth)
Definition: IniParser.cc:1049
bool live
glibc regex buffer
Definition: IniParser.h:70
bool multiline_valid
Definition: IniParser.h:228
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:88
time_t timestamp
Definition: IniParser.h:260
bool changed()
Definition: IniParser.cc:459
string scanner_file
Definition: IniParser.h:336
vector< Regex > comments
Definition: IniParser.h:315
int _category
Definition: IniParser.h:54
vector< string > matches
Definition: IniParser.h:152
#define DEFINE_BASE_POINTER(NAME)
Definition: RepDef.h:53
char * _oldlocale
Definition: IniParser.h:55
Regex()
Definition: IniParser.h:123
Definition: IniParser.h:254

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