Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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/data/ListMap.h>
26 #include <elm/data/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, Vector<string>& list);
49 
50  class Iterator: public map_t::PairIter {
51  public:
52  inline Iterator(Section *s): map_t::PairIter(s->values.pairs().begin()) { }
53  inline const string& key(void) const { return item().fst; }
54  inline const string& value(void) const { return item().snd; }
55  };
56 
57 private:
58  string _name;
59  map_t values;
60 };
61 
62 class File {
63  File(void);
65 
66 public:
67  static File *load(const sys::Path& path);
68  static File *load(io::InStream *in);
69  ~File(void);
70  inline Section *defaultSection(void) const { return def; }
71  inline Section *get(const string& name) const { return sects.get(name, 0); }
72  inline Section *operator[](const string& name) const { return get(name); }
73 
74  class Iterator: public map_t::Iter {
75  public:
76  inline Iterator(File *file): map_t::Iter(file->sects) { }
77  };
78 
79 private:
80  Section *def;
81  map_t sects;
82 };
83 
84 } } // elm::ini
85 
86 #endif /* ELM_INI_H_ */
elm::MessageException
Definition: MessageException.h:30
elm::ListMap< string, string >
elm::sys::Path
Definition: Path.h:33
elm::ini::Section::Iterator
Definition: ini.h:50
elm::ListMap< string, string >::PairIter
base_t::Iter PairIter
Definition: ListMap.h:33
elm::ini::File
Definition: ini.h:62
elm::ini::Exception::Exception
Exception(const string &msg)
Definition: ini.h:33
elm::ini::File::defaultSection
Section * defaultSection(void) const
Definition: ini.h:70
elm::ini::Section::getInt
int getInt(const string &key, int def)
Definition: ini.cpp:92
elm::ini::Section::Iterator::Iterator
Iterator(Section *s)
Definition: ini.h:52
elm::ini::File::Iterator
Definition: ini.h:74
elm::ini::Section::isDefined
bool isDefined(const string &key) const
Definition: ini.h:45
elm::ini::File::~File
~File(void)
Definition: ini.cpp:158
elm::ini::Section::get
string get(const string &key) const
Definition: ini.h:43
elm::t::in
typename type_info< T >::in_t in
Definition: type_info.h:283
elm
Definition: adapter.h:26
elm::ini::File::load
static File * load(const sys::Path &path)
Definition: ini.cpp:170
elm::ini::File::get
Section * get(const string &name) const
Definition: ini.h:71
elm::Vector< string >
elm::ini::Section::Iterator::key
const string & key(void) const
Definition: ini.h:53
elm::ini::Section::Iterator::value
const string & value(void) const
Definition: ini.h:54
elm::ini::File::operator[]
Section * operator[](const string &name) const
Definition: ini.h:72
elm::ListMap::get
Option< T > get(const K &k) const
Definition: ListMap.h:87
elm::ini::Section::getList
void getList(const string &key, Vector< string > &list)
Definition: ini.cpp:112
elm::ini::Section::name
const string & name(void) const
Definition: ini.h:42
elm::ini::Exception
Definition: ini.h:31
elm::ini::Section
Definition: ini.h:36
elm::ListMap::hasKey
bool hasKey(const K &k) const
Definition: ListMap.h:91
elm::ini::Section::operator[]
string operator[](const string &key) const
Definition: ini.h:44
elm::io::InStream
Definition: InStream.h:29
elm::ini::File::Iterator::Iterator
Iterator(File *file)
Definition: ini.h:76