Skip to content

Commit 16acc15

Browse files
committed
improved support for <space> and 0xE5 in filenames
1 parent 1296413 commit 16acc15

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/utility/SdFile.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,20 @@ uint8_t SdFile::dirEntry(dir_t* dir) {
203203
\param[out] name A 13 byte char array for the formatted name.
204204
*/
205205
void SdFile::dirName(const dir_t& dir, char* name) {
206-
uint8_t j = 0;
207-
for (uint8_t i = 0; i < 11; i++) {
208-
if (dir.name[i] == ' ') {
209-
continue;
210-
}
206+
uint8_t i = 0, j = 0;
207+
// unescape first character
208+
if ((uint8_t) dir.name[i] == DIR_NAME_0XE5) {
209+
name[j++] = 0xE5;
210+
i++;
211+
}
212+
for (; i < 11; i++) {
211213
if (i == 8) {
214+
while (j > 0 && name[j-1] == ' ') j--;
212215
name[j++] = '.';
213216
}
214217
name[j++] = dir.name[i];
215218
}
219+
while (name[j-1] == ' ') j--;
216220
name[j] = 0;
217221
}
218222
//------------------------------------------------------------------------------
@@ -288,10 +292,12 @@ uint8_t SdFile::make83Name(const char* str, uint8_t* name) {
288292
uint8_t n = 7; // max index for part before dot
289293
uint8_t i = 0;
290294
// blank fill name and extension
291-
while (i < 11) {
292-
name[i++] = ' ';
295+
memset(name, ' ', 11);
296+
// escape first character
297+
if ((uint8_t) *str == 0xE5) {
298+
name[i++] = DIR_NAME_0XE5;
299+
str++;
293300
}
294-
i = 0;
295301
while ((c = *str++) != '\0') {
296302
if (c == '.') {
297303
if (n == 10) {
@@ -301,16 +307,12 @@ uint8_t SdFile::make83Name(const char* str, uint8_t* name) {
301307
i = 8; // place for extension
302308
} else {
303309
// illegal FAT characters
304-
uint8_t b;
305310
#if defined(__AVR__)
306-
PGM_P p = PSTR("|<>:+=?/[];,*\"\\");
307-
while ((b = pgm_read_byte(p++))) if (b == c) {
311+
if (strchr_P(PSTR("|<>:+=?/[];,*\"\\"), c)) {
308312
return false;
309313
}
310-
#elif defined(__arm__)
311-
const uint8_t valid[] = "|<>:+=?/[];,*\"\\";
312-
const uint8_t *p = valid;
313-
while ((b = *p++)) if (b == c) {
314+
#else
315+
if (strchr("|<>:+=?/[];,*\"\\", c)) {
314316
return false;
315317
}
316318
#endif
@@ -323,7 +325,7 @@ uint8_t SdFile::make83Name(const char* str, uint8_t* name) {
323325
}
324326
}
325327
// must have a file name, extension is optional
326-
return name[0] != ' ';
328+
return i > 0;
327329
}
328330
//------------------------------------------------------------------------------
329331
/** Make a new directory.
@@ -690,11 +692,13 @@ uint8_t SdFile::openRoot(SdVolume* vol) {
690692
\param[in] width Blank fill name if length is less than \a width.
691693
*/
692694
void SdFile::printDirName(const dir_t& dir, uint8_t width) {
693-
uint8_t w = 0;
694-
for (uint8_t i = 0; i < 11; i++) {
695-
if (dir.name[i] == ' ') {
696-
continue;
697-
}
695+
uint8_t i = 0, w = 0;
696+
// unescape first character
697+
if ((uint8_t) dir.name[i] == DIR_NAME_0XE5) {
698+
Serial.write(0xE5);
699+
i++; w++;
700+
}
701+
for (; i < 11; i++) {
698702
if (i == 8) {
699703
Serial.print('.');
700704
w++;

0 commit comments

Comments
 (0)