3djc/add f gets (#3998)

* Introduce f_gets()

* Comment out debug TRACE

* Windows editing protection

* Store strlen result + uncrustify

* Uncrustify was probably too harsh !

* Cosmetics
This commit is contained in:
3djc
2016-11-11 22:23:05 +01:00
committed by Bertrand Songis
parent 7b2d8b1f0a
commit dccfb03b67
2 changed files with 24 additions and 19 deletions
+10 -17
View File
@@ -229,26 +229,19 @@ class ModelsList
bool readNextLine(char * line, int maxlen) bool readNextLine(char * line, int maxlen)
{ {
char c; if (f_gets(line, maxlen, &file) != NULL) {
unsigned int read; int curlen = strlen(line) - 1;
int len=0; if (line[curlen] == '\n') { // remove unwanted chars if file was edited using windows
while (1) { if (line[curlen - 1] == '\r') {
FRESULT result = f_read(&file, (uint8_t *)&c, 1, &read); line[curlen - 1] = 0;
if (result != FR_OK || read != 1) { }
line[len] = '\0'; else {
return false; line[curlen] = 0;
}
if (c == '\n') {
if (len > 0) {
// we skip empty lines
line[len] = '\0';
return true;
} }
} }
else if (c != '\r' && len < maxlen) { return true;
line[len++] = c;
}
} }
return false;
} }
ModelsCategory * createCategory() ModelsCategory * createCategory()
+12
View File
@@ -694,6 +694,18 @@ FRESULT f_write (FIL* fil, const void* data, UINT size, UINT* written)
return FR_OK; return FR_OK;
} }
TCHAR * f_gets (TCHAR* buff, int len, FIL* fil)
{
if (fil && fil->obj.fs) {
buff = fgets(buff, len, (FILE*)fil->obj.fs);
if (buff != NULL) {
fil->fptr = *buff;
}
// TRACE("fgets(%p) %u, %s", fil->obj.fs, len, buff);
}
return buff;
}
FRESULT f_lseek (FIL* fil, DWORD offset) FRESULT f_lseek (FIL* fil, DWORD offset)
{ {
if (fil && fil->obj.fs) fseek((FILE*)fil->obj.fs, offset, SEEK_SET); if (fil && fil->obj.fs) fseek((FILE*)fil->obj.fs, offset, SEEK_SET);