summaryrefslogtreecommitdiff
path: root/lib/sbuild/keyfile-reader.h
blob: aabc990ac68d1de1115829c26a3bb8db5976c1b4 (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
/* Copyright © 2005-2013  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#ifndef SBUILD_KEYFILE_READER_H
#define SBUILD_KEYFILE_READER_H

#include <istream>

#include <sbuild/keyfile.h>

namespace sbuild
{

  /**
   * Keyfile reader.
   */
  class keyfile_reader
  {
  public:
    /// Exception type.
    typedef keyfile::error error;

    /**
     * The constructor.
     *
     * @param store the keyfile to operate with.
     */
    keyfile_reader(keyfile& store);

    /**
     * The constructor.
     *
     * @param store the keyfile to operate with.
     * @param file the file to load the configuration from.
     */
    keyfile_reader (keyfile&           store,
                    const std::string& file);

    /**
     * The constructor.
     *
     * @param store the keyfile to operate with.
     * @param stream the stream to load the configuration from.
     */
    keyfile_reader (keyfile&       store,
                    std::istream&  stream);

    /// The destructor.
    virtual ~keyfile_reader();

    /**
     * Parse keyfile from a stream.  The keyfile specified during
     * construction will be used to store the parsed data.
     *
     * @param stream the stream to read from.
     */
    virtual void
    read_stream(std::istream& stream);

  protected:
    /**
     * Start processing input.
     * Any setup may be done here.
     */
    virtual void
    begin();

    /**
     * Parse a line of input.  This function will be called for every
     * line of input in the source file.  The input line, line, is
     * parsed appropriately.  Any of the group, key, value, and
     * comment members are set as required.  If any of these members
     * are ready for insertion into the keyfile, then the
     * corresponding _set member must be set to true to signal the
     * fact to the caller.
     * @param line the line to parse.
     */
    virtual void
    parse_line (const std::string& line);

    /**
     * Stop processing input.  Any cleanup may be done here.  For
     * example, any cached group or item may be set here.
     */
    virtual void
    end();

    /// The keyfile to operate with.
    keyfile& store;

    /// Group name.
    keyfile::group_name_type group;

    /// Group name is set.
    bool group_set;

    /// Key name.
    keyfile::key_type key;

    /// Key name is set.
    bool key_set;

    /// Value.
    keyfile::value_type value;

    /// Value is set.
    bool value_set;

    /// Comment.
    keyfile::comment_type comment;

    /// Comment is set.
    bool         comment_set;

    /// Line number.
    keyfile::size_type line_number;

    /**
     * keyfile initialisation from an istream.
     *
     * @param stream the stream to input from.
     * @param kf the keyfile to set.
     * @returns the stream.
     */
    friend
    std::istream&
    operator >> (std::istream&   stream,
                 keyfile_reader& kp)
    {
      kp.read_stream(stream);
      return stream;
    }
  };


}

#endif /* SBUILD_KEYFILE_READER_H */

/*
 * Local Variables:
 * mode:C++
 * End:
 */