sbuild  1.6.10
sbuild-keyfile.h
1 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_KEYFILE_H
20 #define SBUILD_KEYFILE_H
21 
22 #include <sbuild/sbuild-basic-keyfile.h>
23 
24 namespace sbuild
25 {
26 
32  {
34  typedef std::string group_name_type;
35 
37  typedef std::string key_type;
38 
40  typedef std::string value_type;
41 
43  typedef std::string comment_type;
44 
46  typedef unsigned int size_type;
47  };
48 
52  template <typename K>
54  {
55  public:
56  // Workaround for GCC bug.
57  typedef keyfile_base::error error;
58  // This is the correct form, but is not currently supported by
59  // GCC. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14258
60  // using typename basic_keyfile_parser<K>::error;
61 
71 
74  {}
75 
76  virtual ~keyfile_parser()
77  {}
78 
79  virtual void
80  parse_line (std::string const& line)
81  {
82  if (comment_set == true)
83  {
84  comment.clear();
85  comment_set = false;
86  }
87  if (group_set == true)
88  {
89  // The group isn't cleared
90  group_set = false;
91  }
92  if (key_set == true)
93  {
94  key.clear();
95  key_set = false;
96  }
97  if (value_set == true)
98  {
99  value.clear();
100  value_set = false;
101  }
102 
103  if (line.length() == 0)
104  {
105  // Empty line; do nothing.
106  }
107  else if (line[0] == '#') // Comment line
108  {
109  if (!comment.empty())
110  comment += '\n';
111  comment += line.substr(1);
112  }
113  else if (line[0] == '[') // Group
114  {
115  std::string::size_type fpos = line.find_first_of(']');
116  std::string::size_type lpos = line.find_last_of(']');
117  if (fpos == std::string::npos || lpos == std::string::npos ||
118  fpos != lpos)
119  throw error(line_number, keyfile_base::INVALID_GROUP, line);
120  group = line.substr(1, fpos - 1);
121 
122  if (group.length() == 0)
123  throw error(line_number, keyfile_base::INVALID_GROUP, line);
124 
125  comment_set = true;
126  group_set = true;
127  }
128  else // Item
129  {
130  std::string::size_type pos = line.find_first_of('=');
131  if (pos == std::string::npos)
132  throw error(line_number, keyfile_base::INVALID_LINE, line);
133  if (pos == 0)
134  throw error(line_number, keyfile_base::NO_KEY, line);
135  key = line.substr(0, pos);
136  if (pos == line.length() - 1)
137  value = "";
138  else
139  value = line.substr(pos + 1);
140 
141  // No group specified
142  if (group.empty())
143  throw error(line_number, keyfile_base::NO_GROUP, line);
144 
145  comment_set = true;
146  key_set = true;
147  value_set = true;
148  }
149 
151  }
152  };
153 
160 
161 }
162 
163 #endif /* SBUILD_KEYFILE_H */
164 
165 /*
166  * Local Variables:
167  * mode:C++
168  * End:
169  */
unsigned int size_type
Line number.
Definition: sbuild-keyfile.h:46
The group is invalid.
Definition: sbuild-keyfile-base.h:66
Debian source builder components.
Definition: sbuild-auth-null.h:24
The line is invalid.
Definition: sbuild-keyfile-base.h:68
Basic keyfile parser template.
Definition: sbuild-basic-keyfile.h:44
std::string comment_type
Comment.
Definition: sbuild-keyfile.h:43
Configuration file parser.
Definition: sbuild-basic-keyfile.h:138
virtual void parse_line(std::string const &line)
Parse a line of input.
Definition: sbuild-basic-keyfile.h:117
No key was specified.
Definition: sbuild-keyfile-base.h:72
Traits class for an INI-style configuration file.
Definition: sbuild-keyfile.h:31
std::string key_type
Key name.
Definition: sbuild-keyfile.h:37
std::string value_type
Value.
Definition: sbuild-keyfile.h:40
std::string group_name_type
Group name.
Definition: sbuild-keyfile.h:34
Parse error.
Definition: sbuild-parse-error.h:35
No group was specified.
Definition: sbuild-keyfile-base.h:71
System group database entry.
Definition: sbuild-util.h:795
basic_keyfile< keyfile_traits, keyfile_parser< keyfile_traits > > keyfile
Configuration file parser.
Definition: sbuild-keyfile.h:159
virtual void parse_line(std::string const &line)
Parse a line of input.
Definition: sbuild-keyfile.h:80
Keyfile parser template.
Definition: sbuild-keyfile.h:53