A simple .ini file interface.
Dependents: Smart-WiFly-WebServer SignalGenerator WattEye X10Svr
Revision 15:3fc2b87a234d, committed 2016-12-11
- Comitter:
- WiredHome
- Date:
- Sun Dec 11 14:05:17 2016 +0000
- Parent:
- 14:af370f01dfef
- Child:
- 18:282ed56d983b
- Commit message:
- Add methods for iterating over sections and keys
Changed in this revision
| IniManager.cpp | Show annotated file Show diff for this revision Revisions of this file |
| IniManager.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/IniManager.cpp Sun Nov 13 02:04:16 2016 +0000
+++ b/IniManager.cpp Sun Dec 11 14:05:17 2016 +0000
@@ -45,6 +45,105 @@
}
+bool INI::GetNextSection(const char * after, char * buffer, size_t bufferSize) {
+ bool returnNext = false;
+ bool found = false;
+
+ if (!iniFile)
+ return found;
+ CleanUp();
+ INFO("GetNextSection after [%s]", after);
+ FILE * fp = fopen(iniFile,"rt");
+ if (fp) {
+ char buf[INTERNAL_BUF_SIZE];
+
+ if (after == NULL || *after == '\0')
+ returnNext = true;
+ while(fgets(buf, sizeof(buf), fp)) {
+ int x = strlen(buf) - 1; // remove trailing \r\n combinations
+ while (x >= 0 && buf[x] < ' ')
+ buf[x--] = '\0';
+ INFO(" reading \"%s\"", buf);
+ if (buf[0] == '[') {
+ char * pStart = buf + 1;
+ char * pRBrkt = strchr(buf, ']');
+ if (pRBrkt) {
+ *pRBrkt = '\0';
+ if (returnNext) {
+ if (strlen(pStart) < bufferSize) {
+ strcpy(buffer, pStart);
+ found = true;
+ break;
+ }
+ } else if (strcmp(after, pStart) == 0) {
+ returnNext = true;
+ }
+ }
+ }
+ }
+ fclose(fp);
+ }
+ return found;
+}
+
+
+bool INI::GetNextKey(const char * Section, const char * after, char * buffer, size_t bufferSize) {
+ bool returnNext = false;
+ bool inSection = false;
+ bool found = false;
+
+ if (!iniFile)
+ return found;
+ CleanUp();
+ INFO("GetNextLey after [%s]", after);
+ FILE * fp = fopen(iniFile,"rt");
+ if (fp) {
+ char buf[INTERNAL_BUF_SIZE];
+
+ if (after == NULL || *after == '\0')
+ returnNext = true;
+ while(fgets(buf, sizeof(buf), fp)) {
+ int x = strlen(buf) - 1; // remove trailing \r\n combinations
+ while (x >= 0 && buf[x] < ' ')
+ buf[x--] = '\0';
+ INFO(" reading \"%s\"", buf);
+ if (!(buf[0] == '[' || (buf[0] >= 'A' && buf[0] <= 'Z') || (buf[0] >= 'a' && buf[0] <= 'z')))
+ continue;
+ if (buf[0] == '[') {
+ char * pStart = buf + 1;
+ char * pRBrkt = strchr(buf, ']');
+ if (pRBrkt) {
+ *pRBrkt = '\0';
+ if (inSection == true) { // section after wanted, so done.
+ break;
+ } else if (strcmp(pStart, Section) == 0) {
+ inSection = true;
+ continue;
+ }
+ }
+ } else if (inSection) {
+ char * pStart = buf;
+ char * pEqual = strchr(pStart, '=');
+ if (pEqual) {
+ *pEqual = '\0';
+ if (returnNext) {
+ if (strlen(pStart) < bufferSize) {
+ strcpy(buffer, pStart);
+ found = true;
+ break;
+ }
+ } else if (strcmp(after, pStart) == 0) {
+ returnNext = true;
+ }
+ }
+ }
+ }
+ fclose(fp);
+ }
+ return found;
+}
+
+
bool INI::Exists(const char * file)
{
if (file == NULL)
@@ -99,6 +198,9 @@
while (x >= 0 && buf[x] < ' ')
buf[x--] = '\0';
INFO(" reading \"%s\"", buf);
+ if (!(buf[0] == '[' || (buf[0] >= 'A' && buf[0] <= 'Z') || (buf[0] >= 'a' && buf[0] <= 'z')))
+ continue;
+
if (inSection && buf[0] != '[') {
char * eq = strchr(buf, '=');
if (eq) {
--- a/IniManager.h Sun Nov 13 02:04:16 2016 +0000
+++ b/IniManager.h Sun Dec 11 14:05:17 2016 +0000
@@ -115,6 +115,34 @@
*/
bool WriteString(const char * section, const char * key, const char * buffer);
+
+ /** Get Section, or Next Section name
+ *
+ * This can be used to walk the list of section names in a file.
+ *
+ * @param[in] After is the name of the section to search after. When NULL, it
+ * is a "find-first" method.
+ * @param[out] buffer is the caller provided buffer for this method to put the string into.
+ * @param[in] bufferSize is the caller provided declaration of the available space.
+ * @returns true if a new section was found, false otherwise.
+ */
+ bool GetNextSection(const char * after, char * buffer, size_t bufferSize);
+
+
+ /** Get the first Key, or the next Key, within a section
+ *
+ * This can be used to walk the list of keys in a file.
+ *
+ * @param[in] Section is the name of the section to search.
+ * @param[in] After is the name of the key to search after. When NULL, it
+ * is a "find-first" method.
+ * @param[out] buffer is the caller provided buffer for this method to put the string into.
+ * @param[in] bufferSize is the caller provided declaration of the available space.
+ * @returns true if a new key was found, false otherwise.
+ */
+ bool GetNextKey(const char * Section, const char * after, char * buffer, size_t bufferSize);
+
+
private:
char * iniFile;