summaryrefslogtreecommitdiff
path: root/rld-config.h
blob: 4bcb964af41b23f9f8dd9f9c525265605579d395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2014, Chris Johns <chrisj@rtems.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/**
 * @file
 *
 * @ingroup rtems_rld
 *
 * @brief INI Configuration reader.
 *
 */

#if !defined (_RLD_CONFIG_H_)
#define _RLD_CONFIG_H_

#include <string>
#include <list>
#include <vector>

#include <rld.h>

namespace rld
{
  namespace config
  {
    /**
     * The configuration item. This is a data component of a record contained
     * in a section.
     */
    struct item
    {
      std::string text; /**< The text as read from the configuration. */

      /**
       * Construct an item.
       */
      item (const std::string& text);
      item (const char*        text);
    };

    /**
     * Configuration item container.
     */
    typedef std::vector < item > items;

    /**
     * Configuration record is a line in a section. There can be multiple
     * records with the same key in a section. Keys are specific to a section.
     */
    struct record
    {
      std::string name;   //< Name of the record.
      items       items_; //< The record's items.

      /**
       * Return true if there is only one item.
       */
      bool single () const {
        return items_.size () == 1;
      }
    };

    /**
     * Configuration record container.
     */
    typedef std::list < record > records;

    /**
     * Configuration section. A section contains a number of records and the
     * records contain [1..n] items.
     */
    struct section
    {
      std::string name;  //< Name of the section.
      records     recs;  //< The section's records.

      /**
       * Has the section got a record ?
       */
      bool has_record (const std::string& name) const;

      /**
       * Find a record and throw an error if not found.
       */
      const record& get_record (const std::string& name) const;

      /**
       * Return the single item in a record. If the record is duplicated an
       * error is thrown.
       */
      const std::string get_record_item (const std::string& name) const;

      /**
       * Return the list of items in a record in a strings container.
       */
      void get_record_items (const std::string& name, rld::strings& items_) const;
    };

    /**
     * Configuration section container.
     */
    typedef std::list < section > sections;

    /**
     * Container of configuration file paths loaded.
     */
    typedef std::vector < std::string > paths;

    /**
     * The configuration.
     */
    class config
    {
    public:
      /**
       * Construct an empty configuration.
       */
      config(const std::string& search_path = "");

      /**
       * Desctruct the configuration object.
       */
      virtual ~config();

      /**
       * Set the search path.
       */
      void set_search_path (const std::string& search_path);

      /**
       * Clear the current configuration.
       */
      void clear ();

      /**
       * Load a configuration.
       */
      void load (const std::string& name);

      /**
       * Process any include records in the section named. If the section has
       * any records named 'include' split the items and include the
       * configuration files.
       */
      void includes (const section& sec, bool must_exist = false);

      /**
       * Get the section and throw an error if not found.
       */
      const section& get_section (const std::string& name) const;

      /**
       * Get the paths of loaded configuration files.
       */
      const paths& get_paths () const;

    private:

      paths    search; //< The paths to search for config files in.
      paths    paths_; //< The path's of the loaded files.
      sections secs;   //< The sections loaded from configuration files
    };

    /**
     * Return the items from a record.
     */
    template < typename T >
    void parse_items (const rld::config::record& record,
                      T&                         items_,
                      bool                       clear = true,
                      bool                       split = true)
    {
      if (clear)
        items_.clear ();
      for (rld::config::items::const_iterator ii = record.items_.begin ();
           ii != record.items_.end ();
           ++ii)
      {
        if (split)
        {
          rld::strings ss;
          rld::split (ss, (*ii).text, ',');
          std::copy (ss.begin (), ss.end (), std::back_inserter (items_));
        }
        else
        {
          items_.push_back ((*ii).text);
        }
      }
    }

    /**
     * Return the items from a record in a section. Optionally raise an error
     * if the record is not found and it is to be present.
     */
    template < typename T >
    void parse_items (const rld::config::section& section,
                      const std::string&          name,
                      T&                          items_,
                      bool                        present = false,
                      bool                        clear = true,
                      bool                        split = true)
    {
      if (clear)
        items_.clear ();
      const rld::config::record* rec = 0;
      try
      {
        const rld::config::record& rr = section.get_record (name);
        rec = &rr;
      }
      catch (rld::error re)
      {
        /*
         * Ignore the error if it does not need to exist.
         */
        if (present)
          throw rld::error ("not found", "record: " + section.name + name);
      }

      if (rec)
        parse_items (*rec, items_, clear, split);
    }

    /**
     * Return the items from a record in a section in the
     * configuration. Optionally raise an error if the section is not found and
     * it is to be present.
     */
    template < typename T >
    void parse_items (const rld::config::config& config,
                      const std::string&         section,
                      const std::string&         record,
                      T&                         items_,
                      bool                       present = false)
    {
      items_.clear ();
      const rld::config::section* sec = 0;
      try
      {
        const rld::config::section& sr = config.get_section (section);
        sec = &sr;
      }
      catch (rld::error re)
      {
        /*
         * Ignore the error if it does not need to exist.
         */
        if (present)
          throw rld::error ("not found", "section: " + section);
      }

      if (sec)
        parse_items (*sec, record, items_);
    }
  }
}

#endif