properties.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_PROPERTIES_H
30 #define CXXTOOLS_PROPERTIES_H
31 
32 #include <string>
33 #include <map>
34 #include <cxxtools/char.h>
35 #include <cxxtools/textstream.h>
37 
38 namespace cxxtools
39 {
40  class Properties
41  {
42  typedef std::map<String, String> ValuesType;
43  ValuesType values;
44 
45  public:
46  explicit Properties(const std::string& filename);
47  explicit Properties(std::istream& in);
48 
49  void setValue(const String& key, const String& value)
50  {
51  values[key] = value;
52  }
53 
54  void removeValue(const String& key)
55  {
56  values.erase(key);
57  }
58 
59  bool hasValue(const String& key) const
60  {
61  return values.find(key) != values.end();
62  }
63 
64  String getValue(const String& key, const String& def = String()) const
65  {
66  ValuesType::const_iterator it = values.find(key);
67  return it == values.end() ? def : it->second;
68  }
69 
70  template <typename OutputIterator>
71  void getKeys(OutputIterator oi) const
72  {
73  for (ValuesType::const_iterator it = values.begin(); it != values.end(); ++it)
74  {
75  *oi = it->first;
76  ++oi;
77  }
78  }
79 
80  template <typename OutputIterator>
81  void getKeys(const String& praefix, OutputIterator oi) const
82  {
83  for (ValuesType::const_iterator it = values.begin(); it != values.end(); ++it)
84  {
85  String key = it->first;
86  if (key.size() > praefix.size()
87  && key.at(praefix.size()) == L'.'
88  && key.compare(0, praefix.size(), praefix) == 0
89  && key.find(L'.', praefix.size() + 2) == String::npos)
90  {
91  *oi = key.substr(praefix.size() + 1);
92  ++oi;
93  }
94  }
95  }
96 
97  template <typename OutputIterator>
98  void getKeysLong(const String& praefix, OutputIterator oi) const
99  {
100  for (ValuesType::const_iterator it = values.begin(); it != values.end(); ++it)
101  {
102  String key = it->first;
103  if (key.size() > praefix.size()
104  && key.at(praefix.size()) == '.'
105  && key.compare(0, praefix.size(), praefix) == 0
106  && key.find('.', praefix.size() + 2) == String::npos)
107  {
108  *oi = key;
109  ++oi;
110  }
111  }
112  }
113 
114  };
115 
117  {
118  public:
119  class Event
120  {
121  public:
122  virtual ~Event() { }
123  // return true, if parser should stop
124  virtual bool onKeyPart(const String& key) = 0;
125  virtual bool onKey(const String& key) = 0;
126  virtual bool onValue(const String& value) = 0;
127  };
128 
129  private:
130  Event& event;
131  String key;
132  String keypart;
133  String value;
134  Char::value_type unicode;
135  unsigned unicodeCount;
136  unsigned lineNo;
137 
138  enum {
139  state_0,
140  state_key,
141  state_key_esc,
142  state_key_unicode,
143  state_key_sp,
144  state_value,
145  state_value_esc,
146  state_unicode,
147  state_comment
148  } state;
149 
150  public:
152  : event(event_),
153  lineNo(1),
154  state(state_0)
155  { }
156 
157  void parse(TextIStream& in);
158  void parse(std::istream& in, TextCodec<Char, char>* codec = 0);
159  bool parse(Char ch);
160  void end();
161  };
162 
164  {
165  public:
166  explicit PropertiesParserError(const std::string& msg)
167  : SerializationError(msg)
168  { }
169  PropertiesParserError(const std::string& msg, unsigned lineNo);
170  };
171 }
172 
173 #endif // CXXTOOLS_PROPERTIES_H