]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/subsystems/ini_manager.hpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / subsystems / ini_manager.hpp
diff --git a/src/stlplus/subsystems/ini_manager.hpp b/src/stlplus/subsystems/ini_manager.hpp
new file mode 100644 (file)
index 0000000..f1d7a66
--- /dev/null
@@ -0,0 +1,201 @@
+#ifndef STLPLUS_INI_MANAGER\r
+#define STLPLUS_INI_MANAGER\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+//   A subsystem for managing INI (i.e. .ini) files\r
+//   An INI file has the following format\r
+\r
+//     file           ::= header { section }*\r
+//     header         ::= { comment | blank }*\r
+//     section        ::= section_header { declaration | comment | blank }*\r
+//     section_header ::= '[' title ']' '\n'\r
+//     declaration    ::= variable '=' value '\n'\r
+//     comment        ::= ';' text '\n'\r
+//     blank          ::= '\n'\r
+//     title          ::= [~']']*\r
+//     variable       ::= [~'=']*\r
+//     value          ::= .*\r
+//     text           ::= .*\r
+\r
+//   Whitespace is trimmed from the leading and trailing ends of title, variable and value\r
+//   Note: a header is represented internally as a Clint section (i.e. a section with no name)\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "subsystems_fixes.hpp"\r
+#include <vector>\r
+#include <string>\r
+#include <iostream>\r
+\r
+namespace stlplus\r
+{\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // Internals\r
+\r
+  class ini_manager_body;\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // Ini-file manager class\r
+\r
+  class ini_manager\r
+  {\r
+  public:\r
+\r
+    ini_manager(void);\r
+\r
+    explicit ini_manager(const std::vector<std::string>& filenames);\r
+\r
+    ini_manager(const ini_manager&);\r
+    ini_manager& operator= (const ini_manager&);\r
+\r
+    ~ini_manager(void);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // file management\r
+\r
+    // add files starting with the most local file (e.g. the current project) which has depth 0\r
+    // and working back to the most global (e.g. the installation settings) which has a depth of size()-1\r
+    // This does nothing if the file has already been loaded - it is not permitted to manage the same file twice.\r
+    // Returns true if the file loaded okay or was already loaded (it is counted as successful if the file did\r
+    // not exist, only read errors cause a failure)\r
+    bool add_file(const std::string& filename);\r
+\r
+    // as above, returns false if *none* of the files were added\r
+    // filenames[0] is the local file, and so on\r
+    bool add_files(const std::vector<std::string>& filenames);\r
+\r
+    // saves modified ini files - returns true if all modified files were written successfully\r
+    bool save(void);\r
+\r
+    // get the number of files being managed\r
+    unsigned size(void) const;\r
+\r
+    // get the ini filename associated with a depth\r
+    std::string filename(unsigned depth = 0) const;\r
+\r
+    // test whether a file in the ini manager is writable\r
+    bool writable(unsigned depth = 0) const;\r
+\r
+    // test whether a file is empty\r
+    // An ini file is considered empty if it has no named sections and the header is empty or missing\r
+    bool empty(unsigned depth = 0) const;\r
+\r
+    // erase the ini file from the ini manager and from the disk\r
+    bool erase(unsigned depth = 0);\r
+\r
+    // remove the file from the ini manager but do not erase it from the disk\r
+    bool remove(unsigned depth = 0);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // section management\r
+\r
+    // returns the union of all section names in all of the ini files\r
+    std::vector<std::string> section_names(void) const;\r
+\r
+    // returns the section names in one of the ini files\r
+    std::vector<std::string> section_names(unsigned depth) const;\r
+\r
+    // tests whether a section is found in any of the ini files\r
+    bool section_exists(const std::string& title) const;\r
+\r
+    // tests whether the section is found in the specific ini file\r
+    bool section_exists(const std::string& title, unsigned depth) const;\r
+\r
+    // adds a section to the specified ini file - does nothing if it is already present\r
+    bool add_section(const std::string& section, unsigned depth = 0);\r
+\r
+    // test whether a section is empty\r
+    bool empty_section(const std::string& section, unsigned depth = 0);\r
+\r
+    // removes a section from the specified ini file if it exists there but cannot remove it from any other file\r
+    bool erase_section(const std::string& section, unsigned depth = 0);\r
+\r
+    // removes all the contents of a section from the specified ini file but keeps the empty section\r
+    bool clear_section(const std::string& section, unsigned depth = 0);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // variable management\r
+\r
+    // test whether a variable exists in any of the ini files\r
+    bool variable_exists(const std::string& section, const std::string variable) const;\r
+\r
+    // test whether a variable exists in specified ini file\r
+    bool variable_exists(const std::string& section, const std::string variable, unsigned depth) const;\r
+\r
+    // get the union of all variables declared in all ini files\r
+    std::vector<std::string> variable_names(const std::string& section) const;\r
+\r
+    // get the set of all varaibale names from one file\r
+    std::vector<std::string> variable_names(const std::string& section, unsigned depth) const;\r
+\r
+    // get the depth of the first ini file to define a variable\r
+    // returns 0 if defined in the local ini file, etc. Returns (unsigned)-1 if the variable doesn't exist\r
+    unsigned variable_depth(const std::string& section, const std::string variable) const;\r
+\r
+    // get the filename that first defines the variable\r
+    std::string variable_filename(const std::string& section, const std::string variable) const;\r
+    // ditto for its linenumber within that file\r
+    unsigned variable_linenumber(const std::string& section, const std::string variable) const;\r
+\r
+    // get the value of a variable as a single unprocessed string\r
+    // if the variable does not exist the string will be empty, but beware that\r
+    // you also get an empty string if a variable exists but has no value\r
+    // you can differentiate between the two cases by using variable_exists_all above\r
+    std::string variable_value(const std::string& section, const std::string variable) const;\r
+\r
+    // get the value from the specified file\r
+    std::string variable_value(const std::string& section, const std::string variable, unsigned depth) const;\r
+\r
+    // get the value of a variable as a processed string\r
+    // processing splits the value at commas and furthermore supports quoted strings (so that values can contain commas for example)\r
+    // quoted strings are dequoted before they are added to the result\r
+    // the result is a vector of dequoted strings, one per value in the comma-separated list\r
+    std::vector<std::string> variable_values(const std::string& section, const std::string variable) const;\r
+\r
+    // get the processed variable from the specified file\r
+    std::vector<std::string> variable_values(const std::string& section, const std::string variable, unsigned depth) const;\r
+\r
+    // add a variable to the specified file\r
+    bool add_variable(const std::string& section, const std::string& variable, const std::string& value, unsigned depth = 0);\r
+\r
+    // add a variable as a processed string\r
+    // processing means that the values in the string vector are converted into a comma-separated list\r
+    // values containing reserved characters are automatically quoted - so you should not even try to quote them yourself\r
+    bool add_variable(const std::string& section, const std::string& variable, const std::vector<std::string>& values, unsigned depth = 0);\r
+\r
+    // erase a variable from the specified file\r
+    // this does not remove the variable from other ini files, so the variable may still exist\r
+    // to mask a global variable, set the variable to an empty string instead\r
+    bool erase_variable(const std::string& section, const std::string& variable, unsigned depth = 0);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // sundry line-entry management\r
+\r
+    // add a comment to the specified ini file\r
+    bool add_comment(const std::string& section, const std::string& comment, unsigned depth = 0);\r
+\r
+    // add a blank line to the specified ini file\r
+    bool add_blank(const std::string& section, unsigned depth = 0);\r
+\r
+    bool print(std::ostream&) const;\r
+\r
+  private:\r
+    friend class ini_manager_body;\r
+    ini_manager_body* m_body;\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // diagnostic print routine\r
+\r
+  std::ostream& operator << (std::ostream&, const ini_manager&);\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
+\r
+#endif\r
This page took 0.02323 seconds and 4 git commands to generate.