A simple .ini file interface.
Dependents: Smart-WiFly-WebServer SignalGenerator WattEye X10Svr
Diff: IniManager.cpp
- Revision:
- 15:3fc2b87a234d
- Parent:
- 13:d5957065d066
- Child:
- 18:282ed56d983b
diff -r af370f01dfef -r 3fc2b87a234d IniManager.cpp --- 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) {