Skip to content

Commit e481422

Browse files
committed
INIReader: Migrate API to std::string_view
Due to a different conversion API used (std::from_chars) conversion from hex strings is not supported anymore. We do not use this feature so should be fine to remove it
1 parent ff08c2c commit e481422

4 files changed

Lines changed: 37 additions & 38 deletions

File tree

src/inireader.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
#include <cstdlib>
3535
#include <cstring>
3636
#include <istream>
37+
#include <charconv>
3738
#include <ini.h>
3839
#include "lcf/inireader.h"
3940

4041
namespace lcf {
4142

42-
INIReader::INIReader(const std::string& filename)
43+
INIReader::INIReader(std::string filename)
4344
{
4445
_error = ini_parse(filename.c_str(), ValueHandler, this);
4546
}
@@ -100,41 +101,38 @@ int INIReader::ParseError() const
100101
return _error;
101102
}
102103

103-
std::string INIReader::Get(const std::string& section, const std::string& name, const std::string& default_value) const
104+
std::string_view INIReader::Get(std::string_view section, std::string_view name, std::string_view default_value) const
104105
{
105106
std::string key = MakeKey(section, name);
106107
// Use _values.find() here instead of _values.at() to support pre C++11 compilers
107108
return _values.count(key) ? _values.find(key)->second : default_value;
108109
}
109110

110-
std::string INIReader::GetString(const std::string& section, const std::string& name, const std::string& default_value) const
111+
std::string_view INIReader::GetString(std::string_view section, std::string_view name, std::string_view default_value) const
111112
{
112-
const std::string str = Get(section, name, "");
113+
auto str = Get(section, name, "");
113114
return str.empty() ? default_value : str;
114115
}
115116

116-
long INIReader::GetInteger(const std::string& section, const std::string& name, long default_value) const
117+
long INIReader::GetInteger(std::string_view section, std::string_view name, long default_value) const
117118
{
118-
std::string valstr = Get(section, name, "");
119-
const char* value = valstr.c_str();
120-
char* end;
121-
// This parses "1234" (decimal) and also "0x4D2" (hex)
122-
long n = strtol(value, &end, 0);
123-
return end > value ? n : default_value;
119+
std::string_view valstr = Get(section, name, "");
120+
long n;
121+
auto ec = std::from_chars(valstr.data(), valstr.data() + valstr.size(), n).ec;
122+
return ec == std::errc() ? n : default_value;
124123
}
125124

126-
double INIReader::GetReal(const std::string& section, const std::string& name, double default_value) const
125+
double INIReader::GetReal(std::string_view section, std::string_view name, double default_value) const
127126
{
128-
std::string valstr = Get(section, name, "");
129-
const char* value = valstr.c_str();
130-
char* end;
131-
double n = strtod(value, &end);
132-
return end > value ? n : default_value;
127+
std::string_view valstr = Get(section, name, "");
128+
double n;
129+
auto ec = std::from_chars(valstr.data(), valstr.data() + valstr.size(), n).ec;
130+
return ec == std::errc() ? n : default_value;
133131
}
134132

135-
bool INIReader::GetBoolean(const std::string& section, const std::string& name, bool default_value) const
133+
bool INIReader::GetBoolean(std::string_view section, std::string_view name, bool default_value) const
136134
{
137-
std::string valstr = Get(section, name, "");
135+
auto valstr = std::string(Get(section, name, ""));
138136
// Convert to lower case to make string comparisons case-insensitive
139137
std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower);
140138
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
@@ -145,15 +143,15 @@ bool INIReader::GetBoolean(const std::string& section, const std::string& name,
145143
return default_value;
146144
}
147145

148-
bool INIReader::HasValue(const std::string& section, const std::string& name) const
146+
bool INIReader::HasValue(std::string_view section, std::string_view name) const
149147
{
150148
std::string key = MakeKey(section, name);
151149
return _values.count(key);
152150
}
153151

154-
std::string INIReader::MakeKey(const std::string& section, const std::string& name)
152+
std::string INIReader::MakeKey(std::string_view section, std::string_view name)
155153
{
156-
std::string key = section + "=" + name;
154+
std::string key = std::string(section) + "=" + std::string(name);
157155
// Convert to lower case to make section/name lookups case-insensitive
158156
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
159157
return key;

src/lcf/encoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Encoder {
4545

4646
bool IsOk() const;
4747

48-
const std::string& GetEncoding() const;
48+
std::string_view GetEncoding() const;
4949
private:
5050
#if LCF_SUPPORT_ICU
5151
void Init();
@@ -67,7 +67,7 @@ class Encoder {
6767
};
6868

6969

70-
inline const std::string& Encoder::GetEncoding() const {
70+
inline std::string_view Encoder::GetEncoding() const {
7171
return _encoding;
7272
}
7373

src/lcf/inireader.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <map>
3636
#include <string>
37+
#include <string_view>
3738

3839
namespace lcf {
3940

@@ -44,7 +45,7 @@ class INIReader
4445
public:
4546
// Construct INIReader and parse given filename. See ini.h for more info
4647
// about the parsing.
47-
explicit INIReader(const std::string& filename);
48+
explicit INIReader(std::string filename);
4849

4950
// Construct INIReader and parse given stream. See ini.h for more info
5051
// about the parsing.
@@ -56,35 +57,35 @@ class INIReader
5657
int ParseError() const;
5758

5859
// Get a string value from INI file, returning default_value if not found.
59-
std::string Get(const std::string& section, const std::string& name,
60-
const std::string& default_value) const;
60+
std::string_view Get(std::string_view section, std::string_view name,
61+
std::string_view default_value) const;
6162

6263
// Get a string value from INI file, returning default_value if not found,
6364
// empty, or contains only whitespace.
64-
std::string GetString(const std::string& section, const std::string& name,
65-
const std::string& default_value) const;
65+
std::string_view GetString(std::string_view section, std::string_view name,
66+
std::string_view default_value) const;
6667

6768
// Get an integer (long) value from INI file, returning default_value if
6869
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
69-
long GetInteger(const std::string& section, const std::string& name, long default_value) const;
70+
long GetInteger(std::string_view section, std::string_view name, long default_value) const;
7071

7172
// Get a real (floating point double) value from INI file, returning
7273
// default_value if not found or not a valid floating point value
7374
// according to strtod().
74-
double GetReal(const std::string& section, const std::string& name, double default_value) const;
75+
double GetReal(std::string_view section, std::string_view name, double default_value) const;
7576

7677
// Get a boolean value from INI file, returning default_value if not found or if
7778
// not a valid true/false value. Valid true values are "true", "yes", "on", "1",
7879
// and valid false values are "false", "no", "off", "0" (not case sensitive).
79-
bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;
80+
bool GetBoolean(std::string_view section, std::string_view name, bool default_value) const;
8081

8182
// Return true if a value exists with the given section and field names.
82-
bool HasValue(const std::string& section, const std::string& name) const;
83+
bool HasValue(std::string_view section, std::string_view name) const;
8384

8485
private:
8586
int _error;
8687
std::map<std::string, std::string> _values;
87-
static std::string MakeKey(const std::string& section, const std::string& name);
88+
static std::string MakeKey(std::string_view section, std::string_view name);
8889
static int ValueHandler(void* user, const char* section, const char* name, const char* value);
8990
};
9091

src/reader_util.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ std::string ReaderUtil::GetEncoding(std::string_view ini_file) {
210210
#if LCF_SUPPORT_INI
211211
INIReader ini(ToString(ini_file));
212212
if (ini.ParseError() != -1) {
213-
std::string encoding = ini.Get("EasyRPG", "Encoding", std::string());
213+
auto encoding = ini.Get("EasyRPG", "Encoding", "");
214214
if (!encoding.empty()) {
215-
return ReaderUtil::CodepageToEncoding(atoi(encoding.c_str()));
215+
return ReaderUtil::CodepageToEncoding(atoi(std::string(encoding).c_str()));
216216
}
217217
}
218218
#else
@@ -225,9 +225,9 @@ std::string ReaderUtil::GetEncoding(std::istream& filestream) {
225225
#if LCF_SUPPORT_INI
226226
INIReader ini(filestream);
227227
if (ini.ParseError() != -1) {
228-
std::string encoding = ini.Get("EasyRPG", "Encoding", std::string());
228+
auto encoding = ini.Get("EasyRPG", "Encoding", "");
229229
if (!encoding.empty()) {
230-
return ReaderUtil::CodepageToEncoding(atoi(encoding.c_str()));
230+
return ReaderUtil::CodepageToEncoding(atoi(std::string(encoding).c_str()));
231231
}
232232
}
233233
#else

0 commit comments

Comments
 (0)