OpenM++ runtime library (libopenm)
iniReader.h
Go to the documentation of this file.
1
5// Copyright (c) 2013-2015 OpenM++
6// This code is licensed under the MIT license (see LICENSE.txt for details)
7
8#ifndef OM_INI_READER_H
9#define OM_INI_READER_H
10
11#include "omFile.h"
12
13using namespace std;
14
15namespace openm
16{
18 struct IniEntry
19 {
21 string section;
22
24 string key;
25
27 string val;
28
36 IniEntry(const string & i_section, const string & i_key, const string & i_value);
37
39 bool bySectionKey(const char * i_sectionKey) const;
40
42 bool equalTo(const char * i_section, const char * i_key) const;
43
44 private:
45 // set key name: remove = if present, unquote and trim
46 void setKey(const string & i_key);
47
48 // set value: trim space and remove "quotes" or 'apostrophes'
49 void setValue(const string & i_value);
50 };
51
54 {
56 typedef vector<IniEntry> IniEntryVec;
57
58 public:
64 IniFileReader(const char * i_filePath, const char * i_codePageName = nullptr);
65
67 bool isLoaded(void) const noexcept { return is_loaded; }
68
70 bool isExist(const char * i_section, const char * i_key) const noexcept;
71
73 bool isExist(const char * i_sectionKey) const noexcept;
74
76 const string strValue(const char * i_section, const char * i_key, const string & i_default = "") const noexcept;
77
79 const string strValue(const char * i_sectionKey, const string & i_default = "") const noexcept;
80
82 const IniEntryVec & rowsCRef(void) const { return entryVec; }
83
85 const NoCaseSet sectionSet(void) const noexcept;
86
88 const NoCaseMap getSection(const char * i_section) const noexcept;
89
91 static void loadMessages(const char * i_iniMsgPath, const string & i_language = "", const char * i_codePageName = nullptr) noexcept;
92
97 static list<pair<string, unordered_map<string, string>>> loadAllMessages(const char * i_iniMsgPath, const char * i_codePageName = nullptr) noexcept;
98
99 private:
100 bool is_loaded; // if true then ini-file loaded OK else error
101 IniEntryVec entryVec; // ini-file entries: (section, key, value)
102
109 static const IniEntryVec load(const char * i_filePath, bool is_noCase, const char * i_codePageName);
110
111 // find index of section and key or -1 if not found
112 ptrdiff_t findIndex(const char * i_section, const char * i_key) const;
113
114 // find index of section.key or -1 if not found
115 ptrdiff_t findIndex(const char * i_sectionKey) const;
116
117 // insert new or update existing ini-file entry:
118 // unquote key and value if "quoted" or 'single quoted'
119 // return false on error: if section or key is empty
120 static bool addIniEntry(
121 bool is_noCase, const string & i_src, int i_nLine, const string & i_section, const string & i_key, const string & i_value, IniEntryVec & o_entryVec
122 );
123 };
124}
125
126#endif // OM_INI_READER_H
ini-file reader: load all entries in memory and provide search methods.
Definition: iniReader.h:54
const IniEntryVec & rowsCRef(void) const
return const reference to ini-file entries.
Definition: iniReader.h:82
bool isLoaded(void) const noexcept
return true if ini-file loaded correctly or false on error
Definition: iniReader.h:67
const NoCaseMap getSection(const char *i_section) const noexcept
return section by name as case-neutral map of (key,value).
Definition: iniReader.cpp:411
IniFileReader(const char *i_filePath, const char *i_codePageName=nullptr)
load all ini-file entries in memory and convert into UTF-8.
Definition: iniReader.cpp:57
static list< pair< string, unordered_map< string, string > > > loadAllMessages(const char *i_iniMsgPath, const char *i_codePageName=nullptr) noexcept
read language specific messages from path/to/theExe.message.ini for all languages
Definition: iniReader.cpp:516
bool isExist(const char *i_section, const char *i_key) const noexcept
return true if section and key found in ini-file using case-neutral search.
Definition: iniReader.cpp:344
const NoCaseSet sectionSet(void) const noexcept
return names of ini-file sections as case-neutral set of strings.
Definition: iniReader.cpp:396
const string strValue(const char *i_section, const char *i_key, const string &i_default="") const noexcept
return string value by section and key using case-neutral search, or default value if not found.
Definition: iniReader.cpp:366
static void loadMessages(const char *i_iniMsgPath, const string &i_language="", const char *i_codePageName=nullptr) noexcept
read language specific messages from path/to/theExe.message.ini and pass it to the log
Definition: iniReader.cpp:470
openM++ namespace
Definition: log.h:32
std::map< std::string, std::string, LessNoCase > NoCaseMap
map of key-value strings with case neutral key search
Definition: omHelper.h:224
std::set< std::string, LessNoCase > NoCaseSet
set of strings with case neutral key search
Definition: omHelper.h:227
OpenM++ common file utilities.
Ini-file entry: setcion, key and value.
Definition: iniReader.h:19
IniEntry(const string &i_section, const string &i_key, const string &i_value)
initialize ini-file entry.
Definition: iniReader.cpp:429
bool equalTo(const char *i_section, const char *i_key) const
ini-file entry section and key using case neutral equal comparison.
Definition: iniReader.cpp:464
bool bySectionKey(const char *i_sectionKey) const
ini-file entry section.key using case neutral equal comparison.
Definition: iniReader.cpp:458
string section
section: in lower case, without [], space trimed
Definition: iniReader.h:21
string key
key: in lower case, without =, space trimed
Definition: iniReader.h:24
string val
value: space trimed and unquoted ("quotes" or 'apostrophes' removed)
Definition: iniReader.h:27