liblcf
inireader.h
Go to the documentation of this file.
1 // Read an INI file into easy-to-access name/value pairs.
2 
3 // inih and INIReader are released under the New BSD license:
4 //
5 // Copyright (c) 2009, Ben Hoyt
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 // * Neither the name of Ben Hoyt nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY BEN HOYT ''AS IS'' AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 // DISCLAIMED. IN NO EVENT SHALL BEN HOYT BE LIABLE FOR ANY
23 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Go to the project home page for more info: https://github.com/benhoyt/inih
31 
32 #ifndef LCF_INIREADER_H
33 #define LCF_INIREADER_H
34 
35 #include <map>
36 #include <string>
37 
38 // Read an INI file into easy-to-access name/value pairs. (Note that I've gone
39 // for simplicity here rather than speed, but it should be pretty decent.)
40 class INIReader
41 {
42 public:
43  // Construct INIReader and parse given filename. See ini.h for more info
44  // about the parsing.
45  explicit INIReader(const std::string& filename);
46 
47  // Construct INIReader and parse given stream. See ini.h for more info
48  // about the parsing.
49  // Custom function for liblcf.
50  INIReader(std::istream& filestream);
51 
52  // Return the result of ini_parse(), i.e., 0 on success, line number of
53  // first error on parse error, or -1 on file open error.
54  int ParseError() const;
55 
56  // Get a string value from INI file, returning default_value if not found.
57  std::string Get(const std::string& section, const std::string& name,
58  const std::string& default_value) const;
59 
60  // Get a string value from INI file, returning default_value if not found,
61  // empty, or contains only whitespace.
62  std::string GetString(const std::string& section, const std::string& name,
63  const std::string& default_value) const;
64 
65  // Get an integer (long) value from INI file, returning default_value if
66  // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
67  long GetInteger(const std::string& section, const std::string& name, long default_value) const;
68 
69  // Get a real (floating point double) value from INI file, returning
70  // default_value if not found or not a valid floating point value
71  // according to strtod().
72  double GetReal(const std::string& section, const std::string& name, double default_value) const;
73 
74  // Get a boolean value from INI file, returning default_value if not found or if
75  // not a valid true/false value. Valid true values are "true", "yes", "on", "1",
76  // and valid false values are "false", "no", "off", "0" (not case sensitive).
77  bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;
78 
79  // Return true if a value exists with the given section and field names.
80  bool HasValue(const std::string& section, const std::string& name) const;
81 
82 private:
83  int _error;
84  std::map<std::string, std::string> _values;
85  static std::string MakeKey(const std::string& section, const std::string& name);
86  static int ValueHandler(void* user, const char* section, const char* name, const char* value);
87 };
88 
89 #endif
int ParseError() const
Definition: inireader.cpp:98
double GetReal(const std::string &section, const std::string &name, double default_value) const
Definition: inireader.cpp:126
static int ValueHandler(void *user, const char *section, const char *name, const char *value)
Definition: inireader.cpp:162
std::string Get(const std::string &section, const std::string &name, const std::string &default_value) const
Definition: inireader.cpp:103
std::map< std::string, std::string > _values
Definition: inireader.h:84
long GetInteger(const std::string &section, const std::string &name, long default_value) const
Definition: inireader.cpp:116
std::string GetString(const std::string &section, const std::string &name, const std::string &default_value) const
Definition: inireader.cpp:110
bool GetBoolean(const std::string &section, const std::string &name, bool default_value) const
Definition: inireader.cpp:135
int _error
Definition: inireader.h:83
INIReader(const std::string &filename)
Definition: inireader.cpp:42
static std::string MakeKey(const std::string &section, const std::string &name)
Definition: inireader.cpp:154
bool HasValue(const std::string &section, const std::string &name) const
Definition: inireader.cpp:148