-
Notifications
You must be signed in to change notification settings - Fork 3
/
ini_parser.hpp
97 lines (84 loc) · 2.73 KB
/
ini_parser.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include "fly/parser/parser.hpp"
#include "fly/types/json/json.hpp"
#include <istream>
#include <optional>
#include <string>
namespace fly::parser {
/**
* Implementation of the Parser interface for the .ini format.
*
* @author Timothy Flynn ([email protected])
* @version July 18, 2016
*/
class IniParser : public Parser
{
protected:
/**
* Parse a stream and retrieve the parsed values.
*
* @return If successful, the parsed values. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_internal() override;
private:
/**
* Enumeration to indicate the result of a call to IniParser::trim.
*/
enum class TrimResult
{
// The character to be trimmed was found at one end of the string but not the other.
Imbalanced,
// The string has been trimmed.
Trimmed,
// The character to be trimmed was not found at either end of the string.
Untrimmed,
};
/**
* Read symbols from the stream until a newline or end-of-file is reached.
*
* @param result The string to insert extracted symbols ito.
*
* @return True if any symbols were read.
*/
bool getline(std::string &result);
/**
* Parse a line containing a section name.
*
* @param section Line containing the section.
*
* @return If successful, the parsed section name. Otherwise, an uninitialized value.
*/
std::optional<std::string> on_section(std::string §ion);
/**
* Parse a line containing a name/value pair.
*
* @param section Section containing the pair.
* @param name_value Line containing the pair.
*
* @return If successful, the parsed name/value pair. Otherwise, an uninitialized value.
*/
std::optional<std::pair<std::string, std::string>>
on_name_value_pair(std::string const &name_value);
/**
* If the given string begins and ends with the given character, remove that character from each
* end of the string.
*
* @param str The string the trim.
* @param ch The character to look for.
*
* @return The result of the trim operation.
*/
TrimResult trim_value(std::string &str, char ch) const;
/**
* If the given string begins with the first given character and ends with the second given
* character, remove those characters from each end of the string.
*
* @param str The string the trim.
* @param start The character to look for at the beginning of the string.
* @param end The character to look for at the end of the string.
*
* @return The result of the trim operation.
*/
TrimResult trim_value(std::string &str, char start, char end) const;
};
} // namespace fly::parser