Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ini.h
1 /*
2  * ini::Loader class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_INI_H_
22 #define ELM_INI_H_
23 
24 #include <elm/string.h>
25 #include <elm/genstruct/AssocList.h>
26 #include <elm/genstruct/Vector.h>
27 #include <elm/sys/Path.h>
28 
29 namespace elm { namespace ini {
30 
31 class Exception: public MessageException {
32 public:
33  inline Exception(const string& msg): MessageException(msg) { }
34 };
35 
36 class Section {
37  friend class File;
39  inline Section(const string& name): _name(name) { }
40 
41 public:
42  inline const string& name(void) const { return _name; }
43  inline string get(const string& key) const { return values.get(key, ""); }
44  inline string operator[](const string& key) const { return get(key); }
45  inline bool isDefined(const string& key) const { return values.hasKey(key); }
46  string get(const string& key, const string& def) const;
47  int getInt(const string& key, int def);
48  void getList(const string& key, genstruct::Vector<string>& list);
49 
50  class Iterator: public map_t::PairIterator {
51  public:
52  inline Iterator(Section *s): map_t::PairIterator(s->values) { }
53  inline Iterator(const Iterator& i): map_t::PairIterator(i) { }
54  inline Iterator& operator=(const Iterator& i) { map_t::PairIterator::operator=(i); return *this; }
55  inline const string& key(void) const { return item().fst; }
56  inline const string& value(void) const { return item().snd; }
57  };
58 
59 private:
60  string _name;
61  map_t values;
62 };
63 
64 class File {
65  File(void);
67 
68 public:
69  static File *load(const sys::Path& path) throw(Exception);
70  static File *load(io::InStream *in) throw (Exception);
71  ~File(void);
72  inline Section *defaultSection(void) const { return def; }
73  inline Section *get(const string& name) const { return sects.get(name, 0); }
74  inline Section *operator[](const string& name) const { return get(name); }
75 
76  class Iterator: public map_t::Iterator {
77  public:
78  inline Iterator(File *file): map_t::Iterator(file->sects) { }
79  inline Iterator(const Iterator& i): map_t::Iterator(i) { }
80  inline Iterator operator=(const Iterator& i) { map_t::Iterator::operator=(i); return *this; }
81  };
82 
83 private:
84  Section *def;
85  map_t sects;
86 };
87 
88 } } // elm::ini
89 
90 #endif /* ELM_INI_H_ */
Iterator(const Iterator &i)
Definition: ini.h:53
Definition: ini.h:31
const string & value(void) const
Definition: ini.h:56
Iterator(const Iterator &i)
Definition: ini.h:79
Definition: ini.h:50
void getList(const string &key, genstruct::Vector< string > &list)
Definition: ini.cpp:112
Definition: Path.h:31
Iterator & operator=(const Iterator &i)
Definition: ini.h:54
Iterator(File *file)
Definition: ini.h:78
bool isDefined(const string &key) const
Definition: ini.h:45
Definition: ini.h:64
Definition: ini.h:76
string operator[](const string &key) const
Definition: ini.h:44
Option< T > get(const K &key) const
Definition: AssocList.h:89
bool hasKey(const K &key) const
Definition: AssocList.h:95
Iterator(Section *s)
Definition: ini.h:52
sys::SystemInStream & in
Definition: system_SystemIO.cpp:95
Section * operator[](const string &name) const
Definition: ini.h:74
~File(void)
Definition: ini.cpp:158
Exception(const string &msg)
Definition: ini.h:33
Section * defaultSection(void) const
Definition: ini.h:72
const string & key(void) const
Definition: ini.h:55
static File * load(const sys::Path &path)
Definition: ini.cpp:170
Definition: Vector.h:35
int getInt(const string &key, int def)
Definition: ini.cpp:92
Definition: ini.h:36
Iterator operator=(const Iterator &i)
Definition: ini.h:80
Definition: MessageException.h:30
Definition: InStream.h:30
const string & name(void) const
Definition: ini.h:42