Skip to content

Commit d4c3dc8

Browse files
authored
Add INIReader::ParseErrorMessage() to provide brief textual error messages (#205)
Provides a message describing an error when parsing fails. Also fixed the documentation for INIReader::ParseError() to acknowledge memory allocation errors. Added a simple test for the INIReader::ParseErrorMessage() method.
1 parent 216e21b commit d4c3dc8

5 files changed

Lines changed: 58 additions & 1 deletion

File tree

cpp/INIReader.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ int INIReader::ParseError() const
3232
return _error;
3333
}
3434

35+
string INIReader::ParseErrorMessage() const
36+
{
37+
// If _error is positive it means it is the line number on which a parse
38+
// error occurred. This could be an overlong line, that ValueHandler
39+
// indicated a user defined error, an unterminated section name, or a name
40+
// without a value.
41+
if (_error > 0) {
42+
return "parse error on line " + std::to_string(_error) + "; missing ']' or '='?";
43+
}
44+
45+
// If _error is negative it is a system type error, and 0 means success.
46+
switch (_error) {
47+
case -2:
48+
return "unable to allocate memory";
49+
50+
case -1:
51+
return "unable to open file";
52+
53+
case 0:
54+
return "";
55+
}
56+
57+
// This should never be reached. It probably means a new error code was
58+
// added to the C API without updating this method.
59+
return "unknown error " + std::to_string(_error);
60+
}
61+
3562
string INIReader::Get(const string& section, const string& name, const string& default_value) const
3663
{
3764
string key = MakeKey(section, name);

cpp/INIReader.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ class INIReader
5353
INI_API explicit INIReader(const char *buffer, size_t buffer_size);
5454

5555
// Return the result of ini_parse(), i.e., 0 on success, line number of
56-
// first error on parse error, or -1 on file open error.
56+
// first error on parse error, -1 on file open error, or -2 if there was a
57+
// memory allocation error.
5758
INI_API int ParseError() const;
5859

60+
// Return a message that describes the type of error that occurred.
61+
// It will return "" (empty string) if there was no error.
62+
INI_API std::string ParseErrorMessage() const;
63+
5964
// Get a string value from INI file, returning default_value if not found.
6065
INI_API std::string Get(const std::string& section, const std::string& name,
6166
const std::string& default_value) const;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Example that demonstrates the ParseErrorMessage() method.
2+
3+
#include <iostream>
4+
#include "../cpp/INIReader.h"
5+
6+
int main()
7+
{
8+
INIReader reader_file_not_found("/file_that_does_not_exist.ini");
9+
INIReader reader_parse_error("../tests/name_only_after_error.ini");
10+
INIReader reader_success("../tests/normal.ini");
11+
12+
std::cout
13+
<< "reader_file_not_found errmsg: \"" << reader_file_not_found.ParseErrorMessage() << "\"\n"
14+
<< "reader_parse_error errmsg: \"" << reader_parse_error.ParseErrorMessage() << "\"\n"
15+
<< "reader_success errmsg: \"" << reader_success.ParseErrorMessage() << "\"\n";
16+
17+
return 0;
18+
}

examples/cpptest.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
g++ -Wall INIReaderExample.cpp ../cpp/INIReader.cpp ../ini.c -o INIReaderExample
44
./INIReaderExample > cpptest.txt
55
rm INIReaderExample
6+
7+
g++ -Wall INIReaderExampleErrors.cpp ../cpp/INIReader.cpp ../ini.c -o INIReaderExampleErrors
8+
./INIReaderExampleErrors > cpptesterrors.txt
9+
rm INIReaderExampleErrors

examples/cpptesterrors.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
reader_file_not_found errmsg: "unable to open file"
2+
reader_parse_error errmsg: "parse error on line 5; missing ']' or '='?"
3+
reader_success errmsg: ""

0 commit comments

Comments
 (0)