diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index b370122b..780376d0 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1397,9 +1397,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); } @@ -1415,14 +1414,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(); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 519ea5a6..6f092c05 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -352,6 +352,7 @@ uint16_t 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 diff --git a/wled00/util.cpp b/wled00/util.cpp index 9d453e59..1ca91043 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -537,4 +537,33 @@ CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal) { } return value; -} \ No newline at end of file +} + +// 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); +}