TeensyNanoExoCode
Loading...
Searching...
No Matches
IniFile.h
Go to the documentation of this file.
1#ifndef _INIFILE_H
2#define _INIFILE_H
3
4#include <stdint.h>
5
6#if defined(PREFER_SDFAT_LIBRARY)
7#include "SdFat.h"
8extern SdFat SD;
9#else
10#include "SD.h"
11#endif
12#include "IPAddress.h"
13#include <stdint.h>
14
15#define INIFILE_VERSION "1.3.0"
16
17// Maximum length for filename, excluding NULL char 26 chars allows an
18// 8.3 filename instead and 8.3 directory with a leading slash
19#define INI_FILE_MAX_FILENAME_LEN 26
20
21class IniFileState;
22
23class IniFile {
24public:
25#if defined(PREFER_SDFAT_LIBRARY)
26 typedef oflag_t mode_t;
27#elif defined(ARDUINO_ARCH_ESP32)
28 typedef const char* mode_t;
29#else
30 typedef uint8_t mode_t;
31#endif
32
33 enum error_t {
43 };
44
45 static const uint8_t maxFilenameLen;
46
47 // Create an IniFile object. It isn't opened until open() is called on it.
48 IniFile(const char* filename, mode_t mode = FILE_READ,
49 bool caseSensitive = false);
50 ~IniFile();
51
52 inline bool open(void); // Returns true if open succeeded
53 inline void close(void);
54
55 inline bool isOpen(void) const;
56
57 inline error_t getError(void) const;
58 inline void clearError(void) const;
59 // Get the file mode (FILE_READ/FILE_WRITE)
60 inline mode_t getMode(void) const;
61
62 // Get the filename asscoiated with the ini file object
63 inline const char* getFilename(void) const;
64
65 bool validate(char* buffer, size_t len) const;
66
67 // Get value from the file, but split into many short tasks. Return
68 // value: false means continue, true means stop. Call getError() to
69 // find out if any error
70 bool getValue(const char* section, const char* key,
71 char* buffer, size_t len, IniFileState &state) const;
72
73 // Get value, as one big task. Return = true means value is present
74 // in buffer
75 bool getValue(const char* section, const char* key,
76 char* buffer, size_t len) const;
77
78 // Get the value as a string, storing the result in a new buffer
79 // (not the working buffer)
80 bool getValue(const char* section, const char* key,
81 char* buffer, size_t len, char *value, size_t vlen) const;
82
83 // Get a boolean value
84 bool getValue(const char* section, const char* key,
85 char* buffer, size_t len, bool& b) const;
86
87 // Get an integer value
88 bool getValue(const char* section, const char* key,
89 char* buffer, size_t len, int& val) const;
90
91 // Get a double value
92 bool getValue(const char* section, const char* key,
93 char* buffer, size_t len, double& val) const;
94
95 // Get a uint8_t value
96 bool getValue(const char* section, const char* key,
97 char* buffer, size_t len, uint8_t& val) const;
98
99 // Get a uint16_t value
100 bool getValue(const char* section, const char* key,
101 char* buffer, size_t len, uint16_t& val) const;
102
103 // Get a long value
104 bool getValue(const char* section, const char* key,
105 char* buffer, size_t len, long& val) const;
106
107 bool getValue(const char* section, const char* key,
108 char* buffer, size_t len, unsigned long& val) const;
109
110 // Get a float value
111 bool getValue(const char* section, const char* key,
112 char* buffer, size_t len, float& val) const;
113
114 bool getIPAddress(const char* section, const char* key,
115 char* buffer, size_t len, uint8_t* ip) const;
116
117#if defined(ARDUINO) && ARDUINO >= 100
118 bool getIPAddress(const char* section, const char* key,
119 char* buffer, size_t len, IPAddress& ip) const;
120#endif
121
122 bool getMACAddress(const char* section, const char* key,
123 char* buffer, size_t len, uint8_t mac[6]) const;
124
125 // From the file location saved in 'state' look for the next section and read its name.
126 // The name will be in the buffer. Returns false if no section found.
127 bool browseSections(char* buffer, size_t len, IniFileState &state) const;
128
129 // Utility function to read a line from a file, make available to all
130 //static int8_t readLine(File &file, char *buffer, size_t len, uint32_t &pos);
131 static error_t readLine(File &file, char *buffer, size_t len, uint32_t &pos);
132 static bool isCommentChar(char c);
133 static char* skipWhiteSpace(char* str);
134 static void removeTrailingWhiteSpace(char* str);
135
136 bool getCaseSensitive(void) const;
137 void setCaseSensitive(bool cs);
138
139protected:
140 // True means stop looking, false means not yet found
141 bool findSection(const char* section, char* buffer, size_t len,
142 IniFileState &state) const;
143 bool findKey(const char* section, const char* key, char* buffer,
144 size_t len, char** keyptr, IniFileState &state) const;
145
146
147private:
148 char _filename[INI_FILE_MAX_FILENAME_LEN];
149 mode_t _mode;
150 mutable error_t _error;
151 mutable File _file;
152 bool _caseSensitive;
153};
154
156{
157 if (_file)
158 _file.close();
159 _file = SD.open(_filename, _mode);
160 if (isOpen()) {
161 _error = errorNoError;
162 return true;
163 }
164 else {
165 _error = errorFileNotFound;
166 return false;
167 }
168}
169
171{
172 if (_file)
173 _file.close();
174}
175
176bool IniFile::isOpen(void) const
177{
178 return (_file == true);
179}
180
182{
183 return _error;
184}
185
186void IniFile::clearError(void) const
187{
188 _error = errorNoError;
189}
190
192{
193 return _mode;
194}
195
196const char* IniFile::getFilename(void) const
197{
198 return _filename;
199}
200
201
202
204public:
205 IniFileState();
206
207private:
208 enum {funcUnset = 0,
209 funcFindSection,
210 funcFindKey,
211 };
212
213 uint32_t readLinePosition;
214 uint8_t getValueState;
215
216 friend class IniFile;
217};
218
219
220#endif
#define INI_FILE_MAX_FILENAME_LEN
Definition IniFile.h:19
Definition IniFile.h:23
void clearError(void) const
Definition IniFile.h:186
bool open(void)
Definition IniFile.h:155
bool getMACAddress(const char *section, const char *key, char *buffer, size_t len, uint8_t mac[6]) const
Definition IniFile.cpp:294
static error_t readLine(File &file, char *buffer, size_t len, uint32_t &pos)
Definition IniFile.cpp:375
bool getIPAddress(const char *section, const char *key, char *buffer, size_t len, uint8_t *ip) const
Definition IniFile.cpp:227
void setCaseSensitive(bool cs)
Definition IniFile.cpp:580
bool validate(char *buffer, size_t len) const
Definition IniFile.cpp:25
bool findKey(const char *section, const char *key, char *buffer, size_t len, char **keyptr, IniFileState &state) const
Definition IniFile.cpp:513
~IniFile()
Definition IniFile.cpp:18
bool isOpen(void) const
Definition IniFile.h:176
error_t
Definition IniFile.h:33
@ errorSectionNotFound
Definition IniFile.h:39
@ errorFileNotFound
Definition IniFile.h:35
@ errorUnknownError
Definition IniFile.h:42
@ errorNoError
Definition IniFile.h:34
@ errorFileNotOpen
Definition IniFile.h:36
@ errorBufferTooSmall
Definition IniFile.h:37
@ errorSeekError
Definition IniFile.h:38
@ errorEndOfFile
Definition IniFile.h:41
@ errorKeyNotFound
Definition IniFile.h:40
static bool isCommentChar(char c)
Definition IniFile.cpp:426
bool getCaseSensitive(void) const
Definition IniFile.cpp:575
bool findSection(const char *section, char *buffer, size_t len, IniFileState &state) const
Definition IniFile.cpp:449
mode_t getMode(void) const
Definition IniFile.h:191
bool getValue(const char *section, const char *key, char *buffer, size_t len, IniFileState &state) const
Definition IniFile.cpp:41
static void removeTrailingWhiteSpace(char *str)
Definition IniFile.cpp:440
const char * getFilename(void) const
Definition IniFile.h:196
static char * skipWhiteSpace(char *str)
Definition IniFile.cpp:431
bool browseSections(char *buffer, size_t len, IniFileState &state) const
Definition IniFile.cpp:335
void close(void)
Definition IniFile.h:170
error_t getError(void) const
Definition IniFile.h:181
static const uint8_t maxFilenameLen
Definition IniFile.h:45
uint8_t mode_t
Definition IniFile.h:30
Definition IniFile.h:203
IniFileState()
Definition IniFile.cpp:585