1 #pragma once
2 
3 #include <wayfire/config/config-manager.hpp>
4 
5 namespace wf
6 {
7 namespace config
8 {
9 /**
10  * Parse a multi-line string as a configuration file.
11  * The string consists of multiple sections of the following format:
12  *
13  * [section_name]
14  * option1 = value1
15  * option2 = value2
16  * ...
17  *
18  * Blank lines and whitespace characters around the '=' sign are ignored, as
19  * well as whitespaces at the beginning or at the end of each line.
20  *
21  * When a line contains a '#', the line from this point on is considered a
22  * comment except when it is immediately preceded by a '\'.
23  *
24  * When a line ends in '\', it automatically joined with the next line, except
25  * if it isn't escaped with another '\'.
26  *
27  * Each valid parsed option is used to set the value of the corresponding option
28  * in @manager. Each line which contains errors is reported on the log and then
29  * ignored.
30  *
31  * @param manager The config manager to update.
32  * @param source The multi-line string representing the source
33  * @param source_name The name to be used when reporting errors to the log
34  */
35 void load_configuration_options_from_string(config_manager_t& manager,
36     const std::string& source, const std::string& source_name = "");
37 
38 /**
39  * Create a string which conttains all the sections and the options in the given
40  * configuration manager. The format is the same one as the one described in
41  * load_configuration_options_from_string()
42  *
43  * @return The string representation of the config manager.
44  */
45 std::string save_configuration_options_to_string(
46     const config_manager_t& manager);
47 
48 /**
49  * Load the options from the given config file.
50  *
51  * This is roughly equivalent to reading the file to a string, and then calling
52  * load_configuration_options_from_string(), but this function also tries to get
53  * a shared lock on the config file, and does not do anything if another process
54  * already holds an exclusive lock.
55  *
56  * @param manager The config manager to update.
57  * @param file The config file to use.
58  *
59  * @return True if the config file was reloaded, false if file could not be
60  * opened or a lock could not be acquired.
61  */
62 bool load_configuration_options_from_file(config_manager_t& manager,
63     const std::string& file);
64 
65 /**
66  * Writes the options in the given configuration to the given file.
67  * It is roughly equivalent to calling serialize_configuration_manager() and
68  * then replacing the file contents with the resulting string, but this function
69  * waits until it can get an exclusive lock on the config file.
70  */
71 void save_configuration_to_file(const config_manager_t& manager,
72     const std::string& file);
73 
74 /**
75  * Build a configuration for the given program from the files on the filesystem.
76  *
77  * The following steps are performed:
78  * 1. Each of the XML files in each of @xmldirs are read, and options there
79  *   are used to build a configuration. Note that the XML nodes which are
80  *   allocated will not be freed.
81  * 2. The @sysconf file is used to overwrite default values from XML files.
82  * 3. The @userconf file is used to determine the actual values of options.
83  *
84  * If any of the steps results in an error, the error will be reported to the
85  * command line and the process will continue.
86  */
87 config_manager_t build_configuration(const std::vector<std::string>& xmldirs,
88     const std::string& sysconf, const std::string& userconf);
89 }
90 }
91