libabigail
abg-ini.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// This file contains the declarations for the ini file reader used in
11 /// the libabigail library.
12 
13 #ifndef __ABG_INI_H__
14 #define __ABG_INI_H__
15 
16 #include <istream>
17 #include <memory>
18 #include <ostream>
19 #include <string>
20 #include <vector>
21 
22 namespace abigail
23 {
24 /// Namespace for handling ini-style files
25 namespace ini
26 {
27 // Inject some standard types in this namespace.
28 using std::shared_ptr;
29 using std::dynamic_pointer_cast;
30 using std::string;
31 using std::vector;
32 using std:: pair;
33 
34 class property;
35 /// Convenience typefef for shared_ptr to @ref property.
36 typedef shared_ptr<property> property_sptr;
37 
38 /// The base class of the different kinds of properties of an INI
39 /// file.
40 class property
41 {
42  struct priv;
43  std::unique_ptr<priv> priv_;
44 
45 public:
46 
47  property();
48 
49  property(const string& name);
50 
51  const string&
52  get_name() const;
53 
54  void
55  set_name(const string& name);
56 
57  virtual ~property();
58 }; // end class property
59 
60 class property_value;
61 
62 /// Convenience typedef for a shared_ptr to @ref property_value.
63 typedef shared_ptr<property_value> property_value_sptr;
64 
65 /// Base class of propertie values.
67 {
68  struct priv;
69  std::unique_ptr<priv> priv_;
70 
71 public:
72  enum value_kind
73  {
74  ABSTRACT_PROPERTY_VALUE = 0,
75  STRING_PROPERTY_VALUE = 1,
76  LIST_PROPERTY_VALUE = 2,
77  TUPLE_PROPERTY_VALUE = 3,
78  };
79 
81  property_value(value_kind);
82 
83  value_kind
84  get_kind() const;
85 
86  virtual const string&
87  as_string() const = 0;
88 
89  operator const string& () const;
90 
91  virtual ~property_value();
92 }; // end class property_value.
93 
95 
96 /// A convenience typedef for a shared_ptr to @ref string_property_value.
97 typedef shared_ptr<string_property_value> string_property_value_sptr;
98 
99 /// A property value which is a string.
101 {
102  struct priv;
103  std::unique_ptr<priv> priv_;
104 
105 public:
107  string_property_value(const string& value);
108 
109  void
110  set_content(const string&);
111 
112  virtual const string&
113  as_string() const;
114 
115  operator string() const;
116 
117  virtual ~string_property_value();
118 }; // end class string_property_value
119 
122 
125 
126 class list_property_value;
127 
128 /// A convenience typedef for a shared_ptr to @ref
129 /// list_property_value.
130 typedef shared_ptr<list_property_value> list_property_value_sptr;
131 
132 /// Abstracts the value of a property representing a list of strings.
133 ///
134 /// It's the right hand side of the construct which syntax looks like:
135 ///
136 /// name = val1, val2, val3
137 ///
138 /// where val1, val2 and val3 are strings.
139 ///
140 /// So this class abstracts the set [val1, val2, val3].
142 {
143  struct priv;
144  std::unique_ptr<priv> priv_;
145 
146 public:
148  list_property_value(const vector<string>& values);
149 
150  const vector<string>&
151  get_content() const;
152 
153  void
154  set_content(const vector<string>&);
155 
156  virtual const string&
157  as_string() const;
158 }; // end class list_property_value
159 
162 
165 
167 
168 /// Convenience typedef for a shared_ptr to a @ref
169 /// tuple_property_value.
170 typedef shared_ptr<tuple_property_value> tuple_property_value_sptr;
171 
172 /// A property value that is a tuple.
173 ///
174 /// Each element of the tuple is itself a property value that can
175 /// either be a string, or another tuple, for instance.
177 {
178  struct priv;
179  std::unique_ptr<priv> priv_;
180 
181 public:
182  tuple_property_value(const vector<property_value_sptr>&);
183 
184  const vector<property_value_sptr>&
185  get_value_items() const;
186 
187  vector<property_value_sptr>&
188  get_value_items();
189 
190  virtual const string&
191  as_string() const;
192 
193  operator string() const;
194 
195  virtual ~tuple_property_value();
196 }; // end class tuple_property_value
197 
200 
203 
204 class simple_property;
205 /// Convenience typedef for a shared_ptr to an @ref simple_property.
206 typedef shared_ptr<simple_property> simple_property_sptr;
207 
208 /// A simple property. That is, one which value is a
209 /// @ref string_property_value.
210 class simple_property : public property
211 {
212  struct priv;
213  std::unique_ptr<priv> priv_;
214 
215 public:
216  simple_property();
217 
218  simple_property(const string& name,
219  const string_property_value_sptr& value);
220 
221  simple_property(const string& name);
222 
224  get_value() const;
225 
226  void
228 
229  bool
230  has_empty_value() const;
231 
232  virtual ~simple_property();
233 }; // end class simple_property
234 
236 is_simple_property(const property* p);
237 
240 
241 class list_property;
242 
243 /// A convenience typedef for a shared_ptr to a @ref list_property.
244 typedef shared_ptr<list_property> list_property_sptr;
245 
246 /// A class representing a list property.
247 ///
248 /// It abstracts a construct which syntax looks like:
249 ///
250 /// name = val1, val2, val3
251 ///
252 /// The value of a list property is a @ref list_property_value, i.e, a
253 /// list of strings.
254 class list_property : public property
255 {
256  struct priv;
257  std::unique_ptr<priv> priv_;
258 
259 public:
260  list_property();
261 
262  list_property(const string& name,
263  const list_property_value_sptr& value);
264 
266  get_value() const;
267 
268  void
269  set_value(const list_property_value_sptr& value);
270 
271  virtual ~list_property();
272 }; // end class list_property
273 
275 is_list_property(const property* p);
276 
279 
280 class tuple_property;
281 /// Convenience typedef for a shared_ptr of @ref tuple_property.
282 typedef shared_ptr<tuple_property> tuple_property_sptr;
283 
284 /// Abstraction of a tuple property. A tuple property is a property
285 /// which value is a @ref tuple_property_value.
286 class tuple_property : public property
287 {
288  struct priv;
289  std::unique_ptr<priv> priv_;
290 
291 public:
292  tuple_property();
293 
294  tuple_property(const string& name,
295  const tuple_property_value_sptr v);
296 
297  void
299 
301  get_value() const;
302 
303  virtual
304  ~tuple_property();
305 }; // end class tuple_property
306 
308 is_tuple_property(const property* p);
309 
312 
313 class config;
314 
315 /// A convenience typedef for a shared pointer to @ref config
316 typedef shared_ptr<config> config_sptr;
317 
318 /// The abstraction of the structured content of an .ini file. This
319 /// roughly follows what is explained at
320 /// http://en.wikipedia.org/wiki/INI_file.
321 class config
322 {
323  class priv;
324  std::unique_ptr<priv> priv_;
325 
326 public:
327  class section;
328  /// A convenience typedef for a shared pointer to a config::section.
329  typedef shared_ptr<section> section_sptr;
330 
331  /// A convenience typedef for a vector of config::section_sptr.
332  typedef vector<section_sptr> sections_type;
333 
334  /// A convenience typedef for a vector of @ref property_sptr
335  typedef vector<property_sptr> properties_type;
336 
337  config();
338 
339  config(const string& path,
340  sections_type& sections);
341 
342  virtual ~config();
343 
344  const string&
345  get_path() const;
346 
347  void
348  set_path(const string& path);
349 
350  const sections_type&
351  get_sections() const;
352 
353  void
354  set_sections(const sections_type& sections);
355 }; // end class config
356 
357 /// The abstraction of one section of the .ini config.
359 {
360  class priv;
361  std::unique_ptr<priv> priv_;
362 
363  // Forbid this
364  section();
365 
366 public:
367  section(const string& name);
368 
369  section(const string& name, const properties_type& properties);
370 
371  const string&
372  get_name() const;
373 
374  const properties_type&
375  get_properties() const;
376 
377  void
378  set_properties(const properties_type& properties);
379 
380  void
381  add_property(const property_sptr prop);
382 
384  find_property(const string& prop_name) const;
385 
386  virtual ~section();
387 }; //end class config::section
388 
389 bool
390 read_sections(std::istream& input,
391  config::sections_type& sections);
392 
393 bool
394 read_sections(const string& path,
395  config::sections_type& sections);
396 
397 bool
398 read_config(std::istream& input,
399  config& conf);
400 
402 read_config(std::istream& input);
403 
404 bool
405 read_config(const string& path,
406  config& conf);
407 
409 read_config(const string& path);
410 
411 bool
412 write_sections(const config::sections_type& sections,
413  std::ostream& output);
414 
415 bool
416 write_sections(const config::sections_type& sections,
417  const string& path);
418 
419 bool
420 write_config(const config& conf,
421  std::ostream& output);
422 
423 bool
424 write_config(const config& conf,
425  const string& path);
426 
427 class function_call_expr;
428 
429 /// Convenience typedef for a shared pointer to function_call_expr
430 typedef shared_ptr<function_call_expr> function_call_expr_sptr;
431 
432 /// The abstraction of a function call expression.
434 {
435  struct priv;
436  std::unique_ptr<priv> priv_;
437 
439 
440 public:
441  function_call_expr(const string& name,
442  const vector<string>& args);
443 
444  const string&
445  get_name() const;
446 
447  const vector<string>&
448  get_arguments() const;
449 
450  vector<string>&
451  get_arguments();
452 }; //end function_call_expr
453 
454 bool
455 read_function_call_expr(std::istream& input,
457 
458 bool
459 read_function_call_expr(const string& input,
461 
463 read_function_call_expr(const string& input);
464 }// end namespace ini
465 }// end namespace abigail
466 #endif // __ABG_INI_H__
The abstraction of one section of the .ini config.
Definition: abg-ini.h:359
const string & get_name() const
Get the name of the section.
Definition: abg-ini.cc:812
void set_properties(const properties_type &properties)
Set the properties of the section.
Definition: abg-ini.cc:826
property_sptr find_property(const string &prop_name) const
Find a property that has a given name.
Definition: abg-ini.cc:845
const properties_type & get_properties() const
Get the properties of the section.
Definition: abg-ini.cc:819
virtual ~section()
Destructor of config::section.
Definition: abg-ini.cc:856
void add_property(const property_sptr prop)
Add one property to this section.
Definition: abg-ini.cc:833
The abstraction of the structured content of an .ini file. This roughly follows what is explained at ...
Definition: abg-ini.h:322
vector< property_sptr > properties_type
A convenience typedef for a vector of property_sptr.
Definition: abg-ini.h:335
shared_ptr< section > section_sptr
A convenience typedef for a shared pointer to a config::section.
Definition: abg-ini.h:327
void set_path(const string &path)
Set the path to the config file.
Definition: abg-ini.cc:1672
void set_sections(const sections_type &sections)
Set new sections to the ini config.
Definition: abg-ini.cc:1684
vector< section_sptr > sections_type
A convenience typedef for a vector of config::section_sptr.
Definition: abg-ini.h:332
const sections_type & get_sections() const
Definition: abg-ini.cc:1677
const string & get_path() const
Definition: abg-ini.cc:1665
The abstraction of a function call expression.
Definition: abg-ini.h:434
const vector< string > & get_arguments() const
Getter for the arguments of the function call expression.
Definition: abg-ini.cc:1994
const string & get_name() const
Getter of the name of the function being called.
Definition: abg-ini.cc:1985
Abstracts the value of a property representing a list of strings.
Definition: abg-ini.h:142
list_property_value()
Default constructor of the list_property_value type.
Definition: abg-ini.cc:374
void set_content(const vector< string > &)
Setter of the content of the list_property_value.
Definition: abg-ini.cc:403
virtual const string & as_string() const
Return a string representation of the @list_property_value.
Definition: abg-ini.cc:413
const vector< string > & get_content() const
Getter of the content of the list_property_value.
Definition: abg-ini.cc:396
A class representing a list property.
Definition: abg-ini.h:255
list_property()
Default constructor for list_property.
Definition: abg-ini.cc:647
virtual ~list_property()
Destructor of the list_property type.
Definition: abg-ini.cc:675
const list_property_value_sptr & get_value() const
Getter for the value of the list_property_value.
Definition: abg-ini.cc:664
void set_value(const list_property_value_sptr &value)
Setter for the value of the list_property.
Definition: abg-ini.cc:671
Base class of propertie values.
Definition: abg-ini.h:67
property_value()
Default constructor for the property_value type.
Definition: abg-ini.cc:256
value_kind get_kind() const
Getter for the kind of the property_value type.
Definition: abg-ini.cc:271
virtual ~property_value()
Destructor for the proprerty_value type.
Definition: abg-ini.cc:281
The base class of the different kinds of properties of an INI file.
Definition: abg-ini.h:41
property()
Constructor of property.
Definition: abg-ini.cc:211
virtual ~property()
Destructor of the property.
Definition: abg-ini.cc:237
const string & get_name() const
Getter of the name of the property.
Definition: abg-ini.cc:226
void set_name(const string &name)
Setter of the name of the property.
Definition: abg-ini.cc:233
A simple property. That is, one which value is a string_property_value.
Definition: abg-ini.h:211
void set_value(const string_property_value_sptr &value)
Setter for the string value of the property.
Definition: abg-ini.cc:593
bool has_empty_value() const
Test if the property has an empty value.
Definition: abg-ini.cc:602
const string_property_value_sptr & get_value() const
Getter for the string value of the property.
Definition: abg-ini.cc:586
virtual ~simple_property()
Destructor of the simple_property type.
Definition: abg-ini.cc:610
simple_property()
Default constructor of the simple_property type.
Definition: abg-ini.cc:556
A property value which is a string.
Definition: abg-ini.h:101
virtual ~string_property_value()
Destructor for the string_property_value.
Definition: abg-ini.cc:354
void set_content(const string &)
Setter of the content of the string property value.
Definition: abg-ini.cc:318
virtual const string & as_string() const
Convert the string property value into a string.
Definition: abg-ini.cc:325
string_property_value()
Constructor of the string_property_value type.
Definition: abg-ini.cc:301
A property value that is a tuple.
Definition: abg-ini.h:177
virtual ~tuple_property_value()
Destructor of the tuple_property_value type.
Definition: abg-ini.cc:492
tuple_property_value(const vector< property_value_sptr > &)
Constructor for the tuple_property_value type.
Definition: abg-ini.cc:472
const vector< property_value_sptr > & get_value_items() const
Getter for the content of the tuple_property_value instance.
Definition: abg-ini.cc:481
virtual const string & as_string() const
Convert to the instance of tuple_property_value to a string.
Definition: abg-ini.cc:499
Abstraction of a tuple property. A tuple property is a property which value is a tuple_property_value...
Definition: abg-ini.h:287
tuple_property()
Default constructor of the tuple_property type.
Definition: abg-ini.cc:715
void set_value(const tuple_property_value_sptr value)
Setter for the tuple value of the property.
Definition: abg-ini.cc:735
virtual ~tuple_property()
Destructor for the tuple_property type.
Definition: abg-ini.cc:746
const tuple_property_value_sptr & get_value() const
Getter for the tuple value of the property.
Definition: abg-ini.cc:742
shared_ptr< property_value > property_value_sptr
Convenience typedef for a shared_ptr to property_value.
Definition: abg-ini.h:60
shared_ptr< list_property_value > list_property_value_sptr
A convenience typedef for a shared_ptr to list_property_value.
Definition: abg-ini.h:126
bool read_config(istream &input, config &conf)
Parse an ini config file from an input stream.
Definition: abg-ini.cc:1747
bool read_function_call_expr(std::istream &input, function_call_expr_sptr &expr)
Read a function call expression and build its representation.
Definition: abg-ini.cc:2017
list_property * is_list_property(const property *p)
Test if an instance of a property is actually an instance of list_property.
Definition: abg-ini.cc:686
bool write_config(const config &conf, std::ostream &output)
Serialize an instance of config to an output stream.
Definition: abg-ini.cc:1927
shared_ptr< property > property_sptr
Convenience typefef for shared_ptr to property.
Definition: abg-ini.h:34
shared_ptr< list_property > list_property_sptr
A convenience typedef for a shared_ptr to a list_property.
Definition: abg-ini.h:241
shared_ptr< config > config_sptr
A convenience typedef for a shared pointer to config.
Definition: abg-ini.h:313
list_property_value * is_list_property_value(const property_value *v)
Test if an instance of @property_value is a list_property_value.
Definition: abg-ini.cc:437
bool read_sections(std::istream &input, config::sections_type &sections)
Parse the sections of an *.ini file.
Definition: abg-ini.cc:1701
shared_ptr< tuple_property > tuple_property_sptr
Convenience typedef for a shared_ptr of tuple_property.
Definition: abg-ini.h:280
shared_ptr< simple_property > simple_property_sptr
Convenience typedef for a shared_ptr to an simple_property.
Definition: abg-ini.h:204
shared_ptr< function_call_expr > function_call_expr_sptr
Convenience typedef for a shared pointer to function_call_expr.
Definition: abg-ini.h:427
shared_ptr< string_property_value > string_property_value_sptr
A convenience typedef for a shared_ptr to string_property_value.
Definition: abg-ini.h:94
string_property_value * is_string_property_value(const property_value *v)
Test if a given property value is a string property value.
Definition: abg-ini.cc:341
tuple_property * is_tuple_property(const property *p)
Test if an instance of property is an instance of tuple_property.
Definition: abg-ini.cc:757
shared_ptr< tuple_property_value > tuple_property_value_sptr
Convenience typedef for a shared_ptr to a tuple_property_value.
Definition: abg-ini.h:166
simple_property * is_simple_property(const property *p)
Tests if a property is a simple property.
Definition: abg-ini.cc:619
tuple_property_value * is_tuple_property_value(const property_value *v)
Test if a given instance of property_value is an instance of tuple_property_value too.
Definition: abg-ini.cc:525
bool write_sections(const config::sections_type &sections, std::ostream &out)
Serialize a vector of sections that make up an ini config file to an output stream.
Definition: abg-ini.cc:1882
Toplevel namespace for libabigail.