liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
inireader.cpp
Go to the documentation of this file.
1 /*
2  * Read an INI file into easy-to-access name/value pairs.
3  *
4  * Go to the project home page for more info:
5  * http://code.google.com/p/inih/
6  *
7  * inih and INIReader are released under the New BSD license:
8  *
9  * Copyright (c) 2009, Brush Technology
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * * Neither the name of Brush Technology nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <cctype>
36 #include <cstdlib>
37 #include "ini.h"
38 #include "inireader.h"
39 
40 using std::string;
41 
42 INIReader::INIReader(string filename)
43 {
44  _error = ini_parse(filename.c_str(), ValueHandler, this);
45 }
46 
48 {
49  return _error;
50 }
51 
52 string INIReader::Get(string section, string name, string default_value)
53 {
54  string key = MakeKey(section, name);
55  return _values.count(key) ? _values[key] : default_value;
56 }
57 
58 long INIReader::GetInteger(string section, string name, long default_value)
59 {
60  string valstr = Get(section, name, "");
61  const char* value = valstr.c_str();
62  char* end;
63  // This parses "1234" (decimal) and also "0x4D2" (hex)
64  long n = strtol(value, &end, 0);
65  return end > value ? n : default_value;
66 }
67 
68 string INIReader::MakeKey(string section, string name)
69 {
70  string key = section + "." + name;
71  // Convert to lower case to make lookups case-insensitive
72  for (unsigned int i = 0; i < key.length(); i++)
73  key[i] = tolower(key[i]);
74  return key;
75 }
76 
77 int INIReader::ValueHandler(void* user, const char* section, const char* name,
78  const char* value)
79 {
80  INIReader* reader = (INIReader*)user;
81  reader->_values[MakeKey(section, name)] = value;
82  return 1;
83 }
INIReader(std::string filename)
Definition: inireader.cpp:42
static int ValueHandler(void *user, const char *section, const char *name, const char *value)
Definition: inireader.cpp:77
std::map< std::string, std::string > _values
Definition: inireader.h:74
int ParseError() const
Definition: inireader.cpp:47
int ini_parse(const char *filename, int(*handler)(void *, const char *, const char *, const char *), void *user)
Definition: ini.cpp:94
int _error
Definition: inireader.h:73
long GetInteger(std::string section, std::string name, long default_value)
Definition: inireader.cpp:58
std::string Get(std::string section, std::string name, std::string default_value)
Definition: inireader.cpp:52
static std::string MakeKey(std::string section, std::string name)
Definition: inireader.cpp:68