forked from mlukasek/M5_NightscoutMon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IniFile.h
191 lines (147 loc) · 4.79 KB
/
IniFile.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef _INIFILE_H
#define _INIFILE_H
#define INIFILE_VERSION "1.0.3"
// Maximum length for filename, excluding NULL char 26 chars allows an
// 8.3 filename instead and 8.3 directory with a leading slash
#define INI_FILE_MAX_FILENAME_LEN 26
#include "SD.h"
#include "IPAddress.h"
class IniFileState;
class IniFile {
public:
enum error_t {
errorNoError = 0,
errorFileNotFound,
errorFileNotOpen,
errorBufferTooSmall,
errorSeekError,
errorSectionNotFound,
errorKeyNotFound,
errorEndOfFile,
errorUnknownError,
};
static const uint8_t maxFilenameLen;
// Create an IniFile object. It isn't opened until open() is called on it.
IniFile(const char* filename);
~IniFile();
inline bool open(void); // Returns true if open succeeded
inline void close(void);
inline bool isOpen(void) const;
inline error_t getError(void) const;
inline void clearError(void) const;
// Get the file mode (FILE_READ/FILE_WRITE)
inline uint8_t getMode(void) const;
// Get the filename asscoiated with the ini file object
inline const char* getFilename(void) const;
bool validate(char* buffer, size_t len) const;
// Get value from the file, but split into many short tasks. Return
// value: false means continue, true means stop. Call getError() to
// find out if any error
bool getValue(const char* section, const char* key,
char* buffer, size_t len, IniFileState &state) const;
// Get value, as one big task. Return = true means value is present
// in buffer
bool getValue(const char* section, const char* key,
char* buffer, size_t len) const;
// Get the value as a string, storing the result in a new buffer
// (not the working buffer)
bool getValue(const char* section, const char* key,
char* buffer, size_t len, char *value, size_t vlen) const;
// Get a boolean value
bool getValue(const char* section, const char* key,
char* buffer, size_t len, bool& b) const;
// Get an integer value
bool getValue(const char* section, const char* key,
char* buffer, size_t len, int& val) const;
// Get a uint16_t value
bool getValue(const char* section, const char* key,
char* buffer, size_t len, uint16_t& val) const;
// Get a long value
bool getValue(const char* section, const char* key,
char* buffer, size_t len, long& val) const;
bool getValue(const char* section, const char* key,
char* buffer, size_t len, unsigned long& val) const;
// Get a float value
bool getValue(const char* section, const char* key,
char* buffer, size_t len, float& val) const;
bool getIPAddress(const char* section, const char* key,
char* buffer, size_t len, uint8_t* ip) const;
#if defined(ARDUINO) && ARDUINO >= 100
bool getIPAddress(const char* section, const char* key,
char* buffer, size_t len, IPAddress& ip) const;
#endif
bool getMACAddress(const char* section, const char* key,
char* buffer, size_t len, uint8_t mac[6]) const;
// Utility function to read a line from a file, make available to all
//static int8_t readLine(File &file, char *buffer, size_t len, uint32_t &pos);
static error_t readLine(File &file, char *buffer, size_t len, uint32_t &pos);
static bool isCommentChar(char c);
static char* skipWhiteSpace(char* str);
static void removeTrailingWhiteSpace(char* str);
bool getCaseSensitive(void) const;
void setCaseSensitive(bool cs);
protected:
// True means stop looking, false means not yet found
bool findSection(const char* section, char* buffer, size_t len,
IniFileState &state) const;
bool findKey(const char* section, const char* key, char* buffer,
size_t len, char** keyptr, IniFileState &state) const;
private:
char _filename[INI_FILE_MAX_FILENAME_LEN];
char _mode[4];
mutable error_t _error;
mutable File _file;
bool _caseSensitive;
};
bool IniFile::open(void)
{
if (_file)
_file.close();
_file = SD.open(_filename, _mode); //_mode
if (isOpen()) {
_error = errorNoError;
return true;
}
else {
_error = errorFileNotFound;
return false;
}
}
void IniFile::close(void)
{
if (_file)
_file.close();
}
bool IniFile::isOpen(void) const
{
return (_file == true);
}
IniFile::error_t IniFile::getError(void) const
{
return _error;
}
void IniFile::clearError(void) const
{
_error = errorNoError;
}
uint8_t IniFile::getMode(void) const
{
return _mode[0];
}
const char* IniFile::getFilename(void) const
{
return _filename;
}
class IniFileState {
public:
IniFileState();
private:
enum {funcUnset = 0,
funcFindSection,
funcFindKey,
};
uint32_t readLinePosition;
uint8_t getValueState;
friend class IniFile;
};
#endif