VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gmapfile.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gmapfile.h
19 ///
20 
21 #ifndef G_MAP_FILE_H
22 #define G_MAP_FILE_H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gstrings.h"
27 #include "goptionvalue.h"
28 #include "goptionmap.h"
29 #include <map>
30 #include <string>
31 #include <iostream>
32 
33 namespace G
34 {
35  class MapFile ;
36 }
37 
38 /// \class G::MapFile
39 /// A class for reading and editing key=value files, supporting the creation of
40 /// backup files, variable expansion, character-set encoding and logging.
41 /// \see G::OptionValue
42 ///
44 {
45 public:
46  MapFile() ;
47  ///< Constructor for an empty map.
48 
49  explicit MapFile( const G::StringMap & map ) ;
50  ///< Constructor that reads from a string map.
51 
52  explicit MapFile( const OptionMap & map , const std::string & yes = std::string() ) ;
53  ///< Constructor that reads from an option value map, typically parsed out
54  ///< from a command-line. Unvalued 'true' options in the option value map
55  ///< are loaded into this mapfile object with a value given by the 'yes'
56  ///< parameter, whereas unvalued 'false' options are not loaded. Multi-valued
57  ///< options are loaded as a comma-separated list.
58 
59  explicit MapFile( const G::Path & , bool utf8 = false ) ;
60  ///< Constructor that reads from a file.
61 
62  explicit MapFile( std::istream & , bool utf8 = false ) ;
63  ///< Constructor that reads from a stream.
64 
65  const G::StringArray & keys() const ;
66  ///< Returns a reference to the ordered list of keys.
67 
68  static void check( const G::Path & , bool utf8 = false ) ;
69  ///< Throws if the file is invalid. This is equivalent to constructing
70  ///< a temporary MapFile object, but it specifically does not do
71  ///< any logging.
72 
73  void add( const std::string & key , const std::string & value ) ;
74  ///< Adds or updates a single item in the map.
75 
76  void writeItem( std::ostream & , const std::string & key , bool utf8 = false ) const ;
77  ///< Writes a single item from this map to the stream.
78 
79  static void writeItem( std::ostream & , const std::string & key , const std::string & value , bool utf8 = false ) ;
80  ///< Writes an arbitrary item to the stream.
81 
82  void editInto( const G::Path & path , bool make_backup = true ,
83  bool allow_read_error = false , bool allow_write_error = false , bool utf8 = false ) const ;
84  ///< Edits an existing file so that its contents reflect this map.
85 
86  bool contains( const std::string & key ) const ;
87  ///< Returns true if the map contains the given key.
88 
89  G::Path pathValue( const std::string & key ) const ;
90  ///< Returns a mandatory path value from the map. Throws if it does not exist.
91 
92  G::Path pathValue( const std::string & key , const G::Path & default_ ) const ;
93  ///< Returns a path value from the map.
94 
95  G::Path expandedPathValue( const std::string & key ) const ;
96  ///< Returns a mandatory path value from the map with expand(). Throws if it does not exist.
97 
98  G::Path expandedPathValue( const std::string & key , const G::Path & default_ ) const ;
99  ///< Returns a path value from the map with expand().
100 
101  unsigned int numericValue( const std::string & key , unsigned int default_ ) const ;
102  ///< Returns a numeric value from the map.
103 
104  std::string value( const std::string & key , const std::string & default_ = std::string() ) const ;
105  ///< Returns a string value from the map.
106 
107  std::string value( const std::string & key , const char * default_ ) const ;
108  ///< Returns a string value from the map.
109 
110  bool booleanValue( const std::string & key , bool default_ ) const ;
111  ///< Returns a boolean value from the map.
112 
113  void remove( const std::string & key ) ;
114  ///< Removes a value (if it exists).
115 
116  const G::StringMap & map() const ;
117  ///< Returns a reference to the internal map.
118 
119  void log() const ;
120  ///< Logs the contents.
121 
122  std::string expand( const std::string & value ) const ;
123  ///< Does one-pass variable substitution for the given string.
124  ///< Sub-strings like "%xyz%" are replaced by 'value("xyz")'
125  ///< and "%%" is replaced by "%". If there are no appropriate
126  ///< value in the map then the sub-string is left alone
127  ///< (so "%xyz%" remains as "%xyz%" if there is no "xyz"
128  ///< map item).
129 
130 private:
131  typedef std::list<std::string> List ;
132  void readFrom( const G::Path & , bool ) ;
133  void readFrom( std::istream & ss , bool ) ;
134  static std::string quote( const std::string & ) ;
135  List read( const G::Path & , bool ) const ;
136  void commentOut( List & ) const ;
137  void replace( List & , bool ) const ;
138  bool expand_( std::string & ) const ;
139  std::string expandAll( const std::string & ) const ;
140  static void backup( const G::Path & ) ;
141  static void save( const G::Path & , List & , bool ) ;
142  void log( const std::string & , const std::string & ) const ;
143  static void log( bool , const std::string & , const std::string & ) ;
144  static std::string fromUtf( const std::string & value , bool utf8 ) ;
145  static std::string toUtf( const std::string & value , bool utf8 ) ;
146  std::string mandatoryValue( const std::string & ) const ;
147 
148 private:
149  bool m_logging ;
150  G::StringMap m_map ;
151  G::StringArray m_keys ; // kept in input order
152 } ;
153 
154 #endif
void editInto(const G::Path &path, bool make_backup=true, bool allow_read_error=false, bool allow_write_error=false, bool utf8=false) const
Edits an existing file so that its contents reflect this map.
Definition: gmapfile.cpp:175
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns a string value from the map.
Definition: gmapfile.cpp:259
const G::StringArray & keys() const
Returns a reference to the ordered list of keys.
Definition: gmapfile.cpp:405
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
A map-like container for command-line options and their values.
Definition: goptionmap.h:38
G::Path pathValue(const std::string &key) const
Returns a mandatory path value from the map. Throws if it does not exist.
Definition: gmapfile.cpp:278
bool contains(const std::string &key) const
Returns true if the map contains the given key.
Definition: gmapfile.cpp:395
const G::StringMap & map() const
Returns a reference to the internal map.
Definition: gmapfile.cpp:400
void writeItem(std::ostream &, const std::string &key, bool utf8=false) const
Writes a single item from this map to the stream.
Definition: gmapfile.cpp:157
G::Path expandedPathValue(const std::string &key) const
Returns a mandatory path value from the map with expand(). Throws if it does not exist.
Definition: gmapfile.cpp:283
A class for reading and editing key=value files, supporting the creation of backup files...
Definition: gmapfile.h:43
unsigned int numericValue(const std::string &key, unsigned int default_) const
Returns a numeric value from the map.
Definition: gmapfile.cpp:298
void add(const std::string &key, const std::string &value)
Adds or updates a single item in the map.
Definition: gmapfile.cpp:388
std::string expand(const std::string &value) const
Does one-pass variable substitution for the given string.
Definition: gmapfile.cpp:321
MapFile()
Constructor for an empty map.
Definition: gmapfile.cpp:37
bool booleanValue(const std::string &key, bool default_) const
Returns a boolean value from the map.
Definition: gmapfile.cpp:304
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:34
A Path object represents a file system path.
Definition: gpath.h:72
static void check(const G::Path &, bool utf8=false)
Throws if the file is invalid.
Definition: gmapfile.cpp:129
void log() const
Logs the contents.
Definition: gmapfile.cpp:136