1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 // -*- Mode: C++ -*- 3 // 4 // Copyright (C) 2013-2020 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. 66 class property_value 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 80 property_value(); 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 94 class string_property_value; 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. 100 class string_property_value : public property_value 101 { 102 struct priv; 103 std::unique_ptr<priv> priv_; 104 105 public: 106 string_property_value(); 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 120 string_property_value* 121 is_string_property_value(const property_value*); 122 123 string_property_value_sptr 124 is_string_property_value(const property_value_sptr); 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]. 141 class list_property_value : public property_value 142 { 143 struct priv; 144 std::unique_ptr<priv> priv_; 145 146 public: 147 list_property_value(); 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 160 list_property_value* 161 is_list_property_value(const property_value*); 162 163 list_property_value_sptr 164 is_list_property_value(const property_value_sptr&); 165 166 class tuple_property_value; 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. 176 class tuple_property_value : public property_value 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 198 tuple_property_value* 199 is_tuple_property_value(const property_value*); 200 201 tuple_property_value_sptr 202 is_tuple_property_value(const property_value_sptr); 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 223 const string_property_value_sptr& 224 get_value() const; 225 226 void 227 set_value(const string_property_value_sptr& value); 228 229 bool 230 has_empty_value() const; 231 232 virtual ~simple_property(); 233 }; // end class simple_property 234 235 simple_property* 236 is_simple_property(const property* p); 237 238 simple_property_sptr 239 is_simple_property(const property_sptr p); 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 265 const list_property_value_sptr& 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 274 list_property* 275 is_list_property(const property* p); 276 277 list_property_sptr 278 is_list_property(const property_sptr p); 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 298 set_value(const tuple_property_value_sptr value); 299 300 const tuple_property_value_sptr& 301 get_value() const; 302 303 virtual 304 ~tuple_property(); 305 }; // end class tuple_property 306 307 tuple_property* 308 is_tuple_property(const property* p); 309 310 tuple_property_sptr 311 is_tuple_property(const property_sptr p); 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. 358 class config::section 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 383 property_sptr 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 401 config_sptr 402 read_config(std::istream& input); 403 404 bool 405 read_config(const string& path, 406 config& conf); 407 408 config_sptr 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. 433 class function_call_expr 434 { 435 struct priv; 436 std::unique_ptr<priv> priv_; 437 438 function_call_expr(); 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, 456 function_call_expr_sptr& expr); 457 458 bool 459 read_function_call_expr(const string& input, 460 function_call_expr_sptr& expr); 461 462 function_call_expr_sptr 463 read_function_call_expr(const string& input); 464 }// end namespace ini 465 }// end namespace abigail 466 #endif // __ABG_INI_H__ 467