advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
conf_file.h
Go to the documentation of this file.
1// Copyright (c) 2006-2024 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/advgetopt
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
29// self
30//
31#include <advgetopt/variables.h>
32
33
34// C++
35//
36#include <cstdint>
37#include <fstream>
38#include <functional>
39#include <map>
40#include <memory>
41#include <set>
42
43
44
45namespace advgetopt
46{
47
48
49
51{
52 created,
53 updated,
54 erased
55};
56
57
59{
60 line_continuation_single_line, // no continuation support
61 line_continuation_rfc_822, // like email/HTTP, whitespace at start of next line
62 line_continuation_msdos, // '&' at end of line
63 line_continuation_unix, // '\' at end of line
64 line_continuation_fortran, // '&' at start of next line
65 line_continuation_semicolon // ';' ends the _line_
66};
67
68
69typedef std::uint_fast16_t assignment_operator_t;
70
74constexpr assignment_operator_t ASSIGNMENT_OPERATOR_EXTENDED = 0x0008; // a += b AND a ?= b
75
77
78
79typedef std::uint_fast16_t comment_t;
80
81constexpr comment_t COMMENT_NONE = 0x0000; // no support for comments
82constexpr comment_t COMMENT_INI = 0x0001; // ; comment
83constexpr comment_t COMMENT_SHELL = 0x0002; // # comment
84constexpr comment_t COMMENT_CPP = 0x0004; // // comment
85
86constexpr comment_t COMMENT_SAVE = 0x8000; // save comments along parameters
87
88constexpr comment_t COMMENT_MASK = 0x0007;
89
90
91typedef std::uint_fast16_t section_operator_t;
92
93constexpr section_operator_t SECTION_OPERATOR_NONE = 0x0000; // no support
94constexpr section_operator_t SECTION_OPERATOR_C = 0x0001; // a.b
95constexpr section_operator_t SECTION_OPERATOR_CPP = 0x0002; // a::b
96constexpr section_operator_t SECTION_OPERATOR_BLOCK = 0x0004; // a { ... }
98
99constexpr section_operator_t SECTION_OPERATOR_ONE_SECTION = 0x8000; // accept at most 1 section
100
102
103
104typedef std::uint_fast16_t name_separator_t;
105
106constexpr name_separator_t NAME_SEPARATOR_UNDERSCORES = 0x0001; // output underscore ('_') instead of dashes
107constexpr name_separator_t NAME_SEPARATOR_DASHES = 0x0002; // output dashes ('-')
108
109
111{
112public:
113 typedef std::shared_ptr<conf_file_setup> pointer_t;
114
116 std::string const & filename
123 std::string const & filename
124 , conf_file_setup const & original);
125
126 bool is_valid() const;
127 std::string const & get_original_filename() const;
128 std::string const & get_filename() const;
131 comment_t get_comment() const;
133 std::string get_config_url() const;
135 void set_section_to_ignore(std::string const & section_name);
136 std::string const & get_section_to_ignore() const;
137
138private:
139 void initialize();
140
141 std::string f_original_filename = std::string();
142 std::string f_filename = std::string();
143 std::string f_section_to_ignore = std::string();
148 mutable std::string f_url = std::string();
150};
151
152
154{
155public:
157 parameter_value(parameter_value const & rhs);
158 parameter_value(std::string const & value);
159
161 parameter_value & operator = (std::string const & value);
162 operator std::string () const;
163
164 void set_value(std::string const & value);
165 void set_comment(std::string const & comment);
166 void set_line(int line);
168
169 std::string const & get_value() const;
170 std::string get_comment(bool ensure_newline = false) const;
171 int get_line() const;
173
174private:
175 std::string f_value = std::string();
176 std::string f_comment = std::string();
177 int f_line = 0;
179};
180
181
183 : public std::enable_shared_from_this<conf_file>
184{
185public:
186 typedef std::shared_ptr<conf_file> pointer_t;
188 typedef std::map<std::string, parameter_value> parameters_t;
189 typedef std::function<void(
191 , callback_action_t action
192 , std::string const & parameter_name
193 , std::string const & value)> callback_t;
194 typedef int callback_id_t;
195
196 static pointer_t get_conf_file(conf_file_setup const & setup);
197 static void reset_conf_files();
198
200 std::string backup_extension = std::string(".bak")
201 , bool replace_backup = false
202 , bool prepend_warning = true
203 , std::string output_filename = std::string());
204
205 conf_file_setup const & get_setup() const;
207 callback_t const & c
208 , std::string const & parameter_name = std::string());
210
211 bool exists() const;
212 int get_errno() const;
213
215 std::string const & section_name
219 sections_t get_sections() const;
221 bool has_parameter(std::string name) const;
222 std::string get_parameter(std::string name) const;
223 bool set_parameter(
224 std::string section
225 , std::string name
226 , std::string const & value
228 , std::string const & comment = std::string());
229 bool erase_parameter(std::string name);
231 bool was_modified() const;
232
233 assignment_t is_assignment_operator(char const * & s, bool skip) const;
234 bool is_comment(char const * s) const;
235
236private:
238 {
241 , callback_t const & c
242 , std::string const & name)
243 : f_id(id)
244 , f_callback(c)
245 , f_parameter_name(name)
246 {
247 }
248
251 std::string f_parameter_name = std::string();
252 };
253 typedef std::vector<callback_entry_t>
255
256 conf_file(conf_file_setup const & setup);
257
258 int getc(std::ifstream & stream);
259 void ungetc(int c);
260 bool get_line(std::ifstream & stream, std::string & line);
261 void read_configuration();
262 void value_changed(
263 callback_action_t action
264 , std::string const & parameter_name
265 , std::string const & value);
266
268
269 int f_unget_char = '\0';
270 int f_line = 0;
271 int f_errno = 0;
272 bool f_reading = false;
273 bool f_exists = false;
274
275 bool f_modified = false;
281};
282
283
284bool iswspace(int c);
285
286
287} // namespace advgetopt
288// vim: ts=4 sw=4 et
name_separator_t f_name_separator
Definition conf_file.h:149
std::shared_ptr< conf_file_setup > pointer_t
Definition conf_file.h:113
std::string const & get_section_to_ignore() const
Retrieve the name to be ignore.
comment_t get_comment() const
std::string f_section_to_ignore
Definition conf_file.h:143
line_continuation_t f_line_continuation
Definition conf_file.h:144
std::string const & get_original_filename() const
Get the original filename.
section_operator_t f_section_operator
Definition conf_file.h:147
std::string const & get_filename() const
Get the filename.
line_continuation_t get_line_continuation() const
Get the line continuation setting.
assignment_operator_t get_assignment_operator() const
Get the accepted assignment operators.
bool is_valid() const
Check whether the setup is considered valid.
std::string f_original_filename
Definition conf_file.h:141
name_separator_t get_name_separator() const
Retrieve the separator to use within names.
void set_section_to_ignore(std::string const &section_name)
Set a section name to ignore.
std::string get_config_url() const
Transform the setup in a URL.
section_operator_t get_section_operator() const
Get the accepted section operators.
assignment_operator_t f_assignment_operator
Definition conf_file.h:145
callback_id_t f_next_callback_id
Definition conf_file.h:280
int section_to_variables(std::string const &section_name, variables::pointer_t var)
Look for a section to convert in a list of variables.
conf_file_setup const & get_setup() const
Get the configuration file setup.
bool set_parameter(std::string section, std::string name, std::string const &value, assignment_t op=assignment_t::ASSIGNMENT_NONE, std::string const &comment=std::string())
Set a parameter.
callback_id_t add_callback(callback_t const &c, std::string const &parameter_name=std::string())
Add a callback to detect when changes happen.
void erase_all_parameters()
Clear the list of all existing parameters from this file.
static void reset_conf_files()
Forget all the cached configuration files.
variables::pointer_t get_variables() const
Retrieve the currently attached variables.
bool exists() const
Whether an input file was found.
std::map< std::string, parameter_value > parameters_t
Definition conf_file.h:188
conf_file_setup const f_setup
Definition conf_file.h:267
variables::pointer_t f_variables
Definition conf_file.h:277
bool save_configuration(std::string backup_extension=std::string(".bak"), bool replace_backup=false, bool prepend_warning=true, std::string output_filename=std::string())
Save the configuration file.
callback_vector_t f_callbacks
Definition conf_file.h:279
parameters_t f_parameters
Definition conf_file.h:278
sections_t f_sections
Definition conf_file.h:276
void read_configuration()
Read a configuration file.
void value_changed(callback_action_t action, std::string const &parameter_name, std::string const &value)
Call whenever the value changed so we can handle callbacks.
std::string get_parameter(std::string name) const
Get the named parameter.
assignment_t is_assignment_operator(char const *&s, bool skip) const
Check whether c is an assignment operator.
bool was_modified() const
Check whether this configuration file was modified.
bool has_parameter(std::string name) const
Check whether a parameter is defined.
void ungetc(int c)
Restore one character.
std::shared_ptr< conf_file > pointer_t
Definition conf_file.h:186
std::vector< callback_entry_t > callback_vector_t
Definition conf_file.h:254
bool is_comment(char const *s) const
Check whether the string starts with a comment introducer.
parameters_t get_parameters() const
Get a list of parameters.
sections_t get_sections() const
Get a list of sections.
void set_variables(variables::pointer_t variables)
Attach a variables object to the configuration file.
bool erase_parameter(std::string name)
Erase the named parameter from this configuration file.
void remove_callback(callback_id_t id)
Remove a callback.
int get_errno() const
Get the error number opening/reading the configuration file.
string_set_t sections_t
Definition conf_file.h:187
std::function< void(pointer_t conf_file, callback_action_t action, std::string const &parameter_name, std::string const &value)> callback_t
Definition conf_file.h:193
static pointer_t get_conf_file(conf_file_setup const &setup)
Create and read a conf_file.
bool get_line(std::ifstream &stream, std::string &line)
Get one line.
int getc(std::ifstream &stream)
Read one characte from the input stream.
std::string const & get_value() const
parameter_value & operator=(parameter_value const &rhs)
assignment_t get_assignment_operator() const
assignment_t f_assignment_operator
Definition conf_file.h:178
void set_assignment_operator(assignment_t a)
void set_value(std::string const &value)
void set_comment(std::string const &comment)
std::string get_comment(bool ensure_newline=false) const
std::shared_ptr< variables > pointer_t
Definition variables.h:61
The advgetopt environment to parse command line options.
constexpr comment_t COMMENT_SAVE
Definition conf_file.h:86
constexpr section_operator_t SECTION_OPERATOR_INI_FILE
Definition conf_file.h:97
constexpr comment_t COMMENT_CPP
Definition conf_file.h:84
constexpr comment_t COMMENT_MASK
Definition conf_file.h:88
std::uint_fast16_t assignment_operator_t
Definition conf_file.h:69
std::uint_fast16_t section_operator_t
Definition conf_file.h:91
constexpr section_operator_t SECTION_OPERATOR_CPP
Definition conf_file.h:95
constexpr comment_t COMMENT_NONE
Definition conf_file.h:81
constexpr section_operator_t SECTION_OPERATOR_ONE_SECTION
Definition conf_file.h:99
constexpr comment_t COMMENT_SHELL
Definition conf_file.h:83
line_continuation_t
Definition conf_file.h:59
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_EQUAL
Definition conf_file.h:71
constexpr section_operator_t SECTION_OPERATOR_C
Definition conf_file.h:94
constexpr name_separator_t NAME_SEPARATOR_DASHES
Definition conf_file.h:107
constexpr section_operator_t SECTION_OPERATOR_BLOCK
Definition conf_file.h:96
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_SPACE
Definition conf_file.h:73
constexpr name_separator_t NAME_SEPARATOR_UNDERSCORES
Definition conf_file.h:106
std::uint_fast16_t comment_t
Definition conf_file.h:79
std::set< std::string > string_set_t
Definition utils.h:42
constexpr comment_t COMMENT_INI
Definition conf_file.h:82
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_EXTENDED
Definition conf_file.h:74
bool iswspace(int c)
Returns true if c is considered to be a whitespace.
callback_action_t
Definition conf_file.h:51
constexpr section_operator_t SECTION_OPERATOR_MASK
Definition conf_file.h:101
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_MASK
Definition conf_file.h:76
std::uint_fast16_t name_separator_t
Definition conf_file.h:104
constexpr section_operator_t SECTION_OPERATOR_NONE
Definition conf_file.h:93
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_COLON
Definition conf_file.h:72
Definition conf_file.h:238
callback_entry_t(callback_id_t id, callback_t const &c, std::string const &name)
Definition conf_file.h:239
std::string f_parameter_name
Definition conf_file.h:251
callback_id_t f_id
Definition conf_file.h:249
callback_t f_callback
Definition conf_file.h:250
Declaration of the variable class.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.