Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer optimization: long comments do not trigger errorBufferTooSmall #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions IniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <string.h>

//for testing in MS VC
#ifdef _MSC_VER
#define strcasecmp _stricmp
#endif

const uint8_t IniFile::maxFilenameLen = INI_FILE_MAX_FILENAME_LEN;

IniFile::IniFile(const char* filename, uint8_t mode,
Expand Down Expand Up @@ -310,6 +315,30 @@ bool IniFile::getMACAddress(const char* section, const char* key,
return true;
}

IniFile::error_t IniFile::readUntilNewLine(File &file, char *buffer, size_t len, uint32_t &pos){

char b;

//first check to see if a new line already exists in our buffer..
for(int i=0; i<len; i++){
if(buffer[i]=='\n') return errorNoError;
pos++;
}

memset(buffer, 0, len); //we wont be using this..

//no newline found in buffer..now we read ahead until we find it..
while(1){
size_t bytesRead = file.read(&b, 1);
if (!bytesRead) return errorEndOfFile;
if (b == '\n') return errorNoError; //new line - \r\n format still ok..
pos++;
if (!file.available()) return errorEndOfFile;

}

}

//int8_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_t &pos)
IniFile::error_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_t &pos)
{
Expand All @@ -329,6 +358,8 @@ IniFile::error_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_
return errorEndOfFile;
}

bool inBody = false;

for (size_t i = 0; i < bytesRead && i < len-1; ++i) {
// Test for '\n' with optional '\r' too
// if (endOfLineTest(buffer, len, i, '\n', '\r')
Expand All @@ -346,7 +377,13 @@ IniFile::error_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_
//return (i+1 == bytesRead && !file.available());
return errorNoError;
}

//dzzie: if its a comment line, just skip it to avoid errorBufferTooSmall
if(!inBody && isCommentChar(buffer[i])) return readUntilNewLine(file, buffer,len, pos);
if(!isspace(buffer[i])) inBody = true;

}

if (!file.available()) {
// end of file without a newline
buffer[bytesRead] = '\0';
Expand Down
1 change: 1 addition & 0 deletions IniFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class IniFile {


private:
static error_t readUntilNewLine(File &file, char *buffer, size_t len, uint32_t &pos);
char _filename[INI_FILE_MAX_FILENAME_LEN];
uint8_t _mode;
mutable error_t _error;
Expand Down
6 changes: 4 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ yourself. If multiple entries for the same key exist inside the
selected section (if using) then only the first value is returned. If
a section is defined more than once only the first is used. The ini
file can contain comments, which begin with a semicolon (;) or hash
(#). The user-supplied buffer must be large enough to accomodate the
longest line in the file.
(#).

The user-supplied buffer must be large enough to accomodate the
longest non-comment line in the file.

Example file format:

Expand Down
3 changes: 3 additions & 0 deletions test/test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mac = 01:23:45:67:89:AB
# extraneous spaces before and after key and value
ip = 192.168.1.2

# this is a super long comment to make sure it does not impact the minimum buffer length requirements. It is 131 characters long...
;this super long comment has leading white space before it will it be ignored as well..........................................

hosts allow = example.com

# A similarly-named section
Expand Down