clean up ledmap names when directly reading from file

This commit is contained in:
Frank
2023-06-09 17:03:04 +02:00
parent 7dcc8f1d1f
commit d4a37ecaea
3 changed files with 36 additions and 7 deletions

View File

@@ -1398,9 +1398,8 @@ void WS2812FX::enumerateLedmaps() {
size_t len = strlen(name);
if (len > 0 && len < 33) {
if ((len > 0) && (name[len-1] == '\r')) { name[len-1] = '\0'; len--;} // kill trailing `\r` (DOS format: \r\n)
if ((len > 0) && (name[len-1] == ',')) { name[len-1] = '\0'; len--;} // kill trailing `,`
if ((len > 0) && (name[len-1] == ' ')) { name[len-1] = '\0'; len--;} // kill trailing ` `
(void) cleanUpName(name);
len = strlen(name);
ledmapNames[i-1] = new char[len+1]; // +1 to include terminating \0
if (ledmapNames[i-1]) strlcpy(ledmapNames[i-1], name, 33);
}
@@ -1416,14 +1415,14 @@ void WS2812FX::enumerateLedmaps() {
char dim[34] = { '\0' };
f.find("\"width\":");
f.readBytesUntil('\n', dim, sizeof(dim)-1); //hack: use fileName as we have this allocated already
uint16_t maxWidth = atoi(dim);
uint16_t maxWidth = atoi(cleanUpName(dim));
f.find("\"height\":");
memset(dim, 0, sizeof(dim)); // clear buffer before reading
f.readBytesUntil('\n', dim, sizeof(dim)-1);
uint16_t maxHeight = atoi(dim);
uint16_t maxHeight = atoi(cleanUpName(dim));
ledmapMaxSize = MAX(ledmapMaxSize, maxWidth * maxHeight);
USER_PRINTF("enumerateLedmaps %s %s (%dx%d -> %d)\n", fileName, name, maxWidth, maxHeight, ledmapMaxSize);
USER_PRINTF("enumerateLedmaps %s \"%s\" (%dx%d -> %d)\n", fileName, name, maxWidth, maxHeight, ledmapMaxSize);
}
f.close();
USER_FLUSH();

View File

@@ -352,6 +352,7 @@ uint16_t __attribute__((pure)) crc16(const unsigned char* data_p, size_t length
um_data_t* simulateSound(uint8_t simulationId);
// WLEDMM enumerateLedmaps(); moved to FX.h
CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal); //WLEDMM netmindz ar palette
char *cleanUpName(char *in); // to clean up a name that was read from file
#ifdef WLED_ADD_EEPROM_SUPPORT
//wled_eeprom.cpp

View File

@@ -536,4 +536,33 @@ CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal) {
}
return value;
}
}
// WLEDMM extended "trim string" function to support enumerateLedmaps
// The function takes char* as input, and removes all leading and trailing "decorations" like spaces, tabs, line endings, quotes, colons
// The conversion is "in place" (destructive).
// example: cleanUpName("\t \"Ring241x 60/9 squeeze \" ,\r") returns "Ring241x 60/9 squeeze"
//
// Null pointer and zero size "C strings" are handled correctly.
// Will not work with flash strings. Unicode encoded multi-byte char strings may get corrupted.
//
static const char *unwantedChars = "\r\n\t\b ,;:\"\'`´\\"; // list of chars to delete
//
char *cleanUpName(char *in) {
if (nullptr == in) return(in);
size_t len = strlen(in);
if (len == 0) return(in);
// delete trailing garbage
while ((len > 0) && (strchr(unwantedChars, in[len-1]) != nullptr)) {
in[len-1] = '\0'; // deletes last char
len--;
}
// delete leading garbage
while ((len > 0) && (strchr(unwantedChars, in[0]) != nullptr)) {
(void) memmove(in, in+1, len); // shifts string left by one
len--;
}
return(in);
}