forked from bneedhamia/sdconfigfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDConfigFile.cpp
271 lines (236 loc) · 5.97 KB
/
SDConfigFile.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* SD card configuration file reading library
*
* Copyright (c) 2014 Bradford Needham
* (@bneedhamia, https://www.needhamia.com )
* Licensed under LGPL version 2.1
* a version of which should have been supplied with this file.
*/
#include <SDConfigFile.h>
/*
* Opens the given file on the SD card.
* Returns true if successful, false if not.
*
* configFileName = the name of the configuration file on the SD card.
*
* NOTE: SD.begin() must be called before calling our begin().
*/
boolean SDConfigFile::begin(const char *configFileName, uint8_t maxLineLength) {
_lineLength = 0;
_lineSize = 0;
_valueIdx = -1;
_atEnd = true;
/*
* Allocate a buffer for the current line.
*/
_lineSize = maxLineLength + 1;
_line = (char *) malloc(_lineSize);
if (_line == 0) {
#ifdef SDCONFIGFILE_DEBUG
Serial.println("out of memory");
#endif
_atEnd = true;
return false;
}
/*
* To avoid stale references to configFileName
* we don't save it. To minimize memory use, we don't copy it.
*/
_file = SD.open(configFileName, FILE_READ);
if (!_file) {
#ifdef SDCONFIGFILE_DEBUG
Serial.print("Could not open SD file: ");
Serial.println(configFileName);
#endif
_atEnd = true;
return false;
}
// Initialize our reader
_atEnd = false;
return true;
}
/*
* Cleans up our SDCOnfigFile object.
*/
void SDConfigFile::end() {
if (_file) {
_file.close();
}
if (_line != 0) {
free(_line);
_line = 0;
}
_atEnd = true;
}
/*
* Reads the next name=value setting from the file.
* Returns true if the setting was successfully read,
* false if an error occurred or end-of-file occurred.
*/
boolean SDConfigFile::readNextSetting() {
int bint;
if (_atEnd) {
return false; // already at end of file (or error).
}
_lineLength = 0;
_valueIdx = -1;
/*
* Assume beginning of line.
* Skip blank and comment lines
* until we read the first character of the key
* or get to the end of file.
*/
while (true) {
bint = _file.read();
if (bint < 0) {
_atEnd = true;
return false;
}
if ((char) bint == '#') {
// Comment line. Read until end of line or end of file.
while (true) {
bint = _file.read();
if (bint < 0) {
_atEnd = true;
return false;
}
if ((char) bint == '\r' || (char) bint == '\n') {
break;
}
}
continue; // look for the next line.
}
// Ignore line ends and blank text
if ((char) bint == '\r' || (char) bint == '\n'
|| (char) bint == ' ' || (char) bint == '\t') {
continue;
}
break; // bint contains the first character of the name
}
// Copy from this first character to the end of the line.
while (bint >= 0 && (char) bint != '\r' && (char) bint != '\n') {
if (_lineLength >= _lineSize - 1) { // -1 for a terminating null.
_line[_lineLength] = '\0';
#ifdef SDCONFIGFILE_DEBUG
Serial.print("Line too long: ");
Serial.println(_line);
#endif
_atEnd = true;
return false;
}
if ((char) bint == '=') {
// End of Name; the next character starts the value.
_line[_lineLength++] = '\0';
_valueIdx = _lineLength;
} else {
_line[_lineLength++] = (char) bint;
}
bint = _file.read();
}
if (bint < 0) {
_atEnd = true;
// Don't exit. This is a normal situation:
// the last line doesn't end in newline.
}
_line[_lineLength] = '\0';
/*
* Sanity checks of the line:
* No =
* No name
* It's OK to have a null value (nothing after the '=')
*/
if (_valueIdx < 0) {
#ifdef SDCONFIGFILE_DEBUG
Serial.print("Missing '=' in line: ");
Serial.println(_line);
#endif
_atEnd = true;
return false;
}
if (_valueIdx == 1) {
#ifdef SDCONFIGFILE_DEBUG
Serial.print("Missing Name in line: =");
Serial.println(_line[_valueIdx]);
#endif
_atEnd = true;
return false;
}
// Name starts at _line[0]; Value starts at _line[_valueIdx].
return true;
}
/*
* Returns true if the most-recently-read setting name
* matches the given name, false otherwise.
*/
boolean SDConfigFile::nameIs(const char *name) {
if (strcmp(name, _line) == 0) {
return true;
}
return false;
}
/*
* Returns the name part of the most-recently-read setting.
* or null if an error occurred.
* WARNING: calling this when an error has occurred can crash your sketch.
*/
const char *SDConfigFile::getName() {
if (_lineLength <= 0 || _valueIdx <= 1) {
return 0;
}
return &_line[0];
}
/*
* Returns the value part of the most-recently-read setting,
* or null if there was an error.
* WARNING: calling this when an error has occurred can crash your sketch.
*/
const char *SDConfigFile::getValue() {
if (_lineLength <= 0 || _valueIdx <= 1) {
return 0;
}
return &_line[_valueIdx];
}
/*
* Returns a persistent, dynamically-allocated copy of the value part
* of the most-recently-read setting, or null if a failure occurred.
*
* Unlike getValue(), the return value of this function
* persists after readNextSetting() is called or end() is called.
*/
char *SDConfigFile::copyValue() {
char *result = 0;
int length;
if (_lineLength <= 0 || _valueIdx <= 1) {
return 0; // begin() wasn't called, or failed.
}
length = strlen(&_line[_valueIdx]);
result = (char *) malloc(length + 1);
if (result == 0) {
return 0; // out of memory
}
strcpy(result, &_line[_valueIdx]);
return result;
}
/*
* Returns the value part of the most-recently-read setting
* as an integer, or 0 if an error occurred.
*/
int SDConfigFile::getIntValue() {
const char *str = getValue();
if (!str) {
return 0;
}
return atoi(str);
}
/*
* Returns the value part of the most-recently-read setting
* as a boolean.
* The value "true" corresponds to true;
* all other values correspond to false.
*/
boolean SDConfigFile::getBooleanValue() {
if (strcmp("true", getValue()) == 0) {
return true;
}
return false;
}