bool IniFile::getValue(const char* section, const char* key, char* buffer, size_t len, IniFileState &state) const { bool done = false; if (!_file.isOpen()) { _error = errorFileNotOpen; return true; } switch (state.getValueState) { case IniFileState::funcUnset: state.getValueState = (section == NULL ? IniFileState::funcFindKey : IniFileState::funcFindSection); state.readLinePosition = 0; break; case IniFileState::funcFindSection: if (findSection(section, buffer, len, state)) { if (_error != errorNoError) return true; state.getValueState = IniFileState::funcFindKey; } break; case IniFileState::funcFindKey: char *cp; if (findKey(section, key, buffer, len, &cp, state)) { if (_error != errorNoError) return true; // Found key line in correct section cp = skipWhiteSpace(cp); removeTrailingWhiteSpace(cp); // Copy from cp to buffer, but the strings overlap so strcpy is out while (*cp != '\0') *buffer++ = *cp++; *buffer = '\0'; return true; } break; default: // How did this happen? _error = errorUnknownError; done = true; break; } return done; }
int8_t IniFile::getValue(const char* section, const char* key, char* buffer, int len, IniFileState &state) const { int8_t done = 0; if (!_file) return errorFileNotOpen; switch (state.getValueState) { case IniFileState::funcUnset: state.getValueState = (section == NULL ? IniFileState::funcFindKey : IniFileState::funcFindSection); state.readLinePosition = 0; break; case IniFileState::funcFindSection: if ((done = findSection(section, buffer, len, state)) == 1) { done = 0; state.getValueState = IniFileState::funcFindKey; } break; case IniFileState::funcFindKey: char *cp; if ((done = findKey(section, key, buffer, len, &cp, state)) == 1) { // Found key line in correct section cp = skipWhiteSpace(cp); removeTrailingWhiteSpace(cp); // Copy from cp to buffer, but the strings overlap so strcpy is out while (*cp != '\0') *buffer++ = *cp++; *buffer = '\0'; } break; default: // How did this happen? done = errorUnknownError; break; } return done; }
// From the current file location look for the matching key int8_t IniFile::findKey(const char* section, const char* key, char* buffer, int len, char** keyptr, IniFileState &state) const { if (key == NULL || *key == '\0') return errorKeyNotFound; int8_t done = IniFile::readLine(_file, buffer, len, state.readLinePosition); if (done < 0) return done; char *cp = skipWhiteSpace(buffer); if (isCommentChar(*cp)) return (done ? errorKeyNotFound : 0); if (section && *cp == '[') // Stat of a new section return errorKeyNotFound; // Find '=' char *ep = strchr(cp, '='); if (ep != NULL) { *ep = '\0'; // make = be the end of string removeTrailingWhiteSpace(cp); if (_caseSensitive) { if (strcmp(cp, key) == 0) { *keyptr = ep + 1; return 1; } } else { if (strcasecmp(cp, key) == 0) { *keyptr = ep + 1; return 1; } } } return (done ? errorKeyNotFound : 0); }
int8_t IniFile::findSection(const char* section, char* buffer, int len, IniFileState &state) const { if (section == NULL) return errorSectionNotFound; int8_t done = IniFile::readLine(_file, buffer, len, state.readLinePosition); if (done < 0) return done; char *cp = skipWhiteSpace(buffer); if (isCommentChar(*cp)) return (done ? errorSectionNotFound : 0); if (*cp == '[') { // Start of section ++cp; cp = skipWhiteSpace(cp); char *ep = strchr(cp, ']'); if (ep != NULL) { *ep = '\0'; // make ] be end of string removeTrailingWhiteSpace(cp); if (_caseSensitive) { if (strcmp(cp, section) == 0) return 1; } else { if (strcasecmp(cp, section) == 0) return 1; } } } // Not a valid section line return (done ? errorSectionNotFound : 0); }
// From the current file location look for the matching key. If // section is non-NULL don't look in the next section bool IniFile::findKey(const char* section, const char* key, char* buffer, size_t len, char** keyptr, IniFileState &state) const { if (key == NULL || *key == '\0') { _error = errorKeyNotFound; return true; } error_t err = IniFile::readLine(_file, buffer, len, state.readLinePosition); if (err != errorNoError && err != errorEndOfFile) { _error = err; return true; } char *cp = skipWhiteSpace(buffer); // if (isCommentChar(*cp)) // return (done ? errorKeyNotFound : 0); if (isCommentChar(*cp)) { if (err == errorEndOfFile) { _error = errorKeyNotFound; return true; } else return false; // Continue searching } if (section && *cp == '[') { // Start of a new section _error = errorKeyNotFound; return true; } // Find '=' char *ep = strchr(cp, '='); if (ep != NULL) { *ep = '\0'; // make = be the end of string removeTrailingWhiteSpace(cp); if (_caseSensitive) { if (strcmp(cp, key) == 0) { *keyptr = ep + 1; _error = errorNoError; return true; } } else { if (strcasecmp(cp, key) == 0) { *keyptr = ep + 1; _error = errorNoError; return true; } } } // Not the valid key line if (err == errorEndOfFile) { _error = errorKeyNotFound; return true; } return false; }
bool IniFile::findSection(const char* section, char* buffer, size_t len, IniFileState &state) const { if (section == NULL) { _error = errorSectionNotFound; return true; } error_t err = IniFile::readLine(_file, buffer, len, state.readLinePosition); if (err != errorNoError && err != errorEndOfFile) { // Signal to caller to stop looking and any error value _error = err; return true; } char *cp = skipWhiteSpace(buffer); //if (isCommentChar(*cp)) //return (done ? errorSectionNotFound : 0); if (isCommentChar(*cp)) { // return (err == errorEndOfFile ? errorSectionNotFound : errorNoError); if (err == errorEndOfFile) { _error = errorSectionNotFound; return true; } else return false; // Continue searching } if (*cp == '[') { // Start of section ++cp; cp = skipWhiteSpace(cp); char *ep = strchr(cp, ']'); if (ep != NULL) { *ep = '\0'; // make ] be end of string removeTrailingWhiteSpace(cp); if (_caseSensitive) { if (strcmp(cp, section) == 0) { _error = errorNoError; return true; } } else { if (strcasecmp(cp, section) == 0) { _error = errorNoError; return true; } } } } // Not a valid section line //return (done ? errorSectionNotFound : 0); if (err == errorEndOfFile) { _error = errorSectionNotFound; return true; } return false; }