From 00de27798a9d8edec54b38c7ab72f669f3c19d55 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Mon, 12 Sep 2022 10:45:18 +0200 Subject: [PATCH] Replaced the rest of sprintf() to snprintf_P() functions. --- MicroDexed.ino | 36 ++++++++++++++++++------------------ dexed_sd.cpp | 44 ++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/MicroDexed.ino b/MicroDexed.ino index ef83321..00f153d 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -2630,25 +2630,25 @@ uint8_t check_sd_cards(void) #endif switch (card.type()) { case SD_CARD_TYPE_SD1: - sprintf(sd_string, "%-5s", "SD1"); + snprintf_P(sd_string, strlen(sd_string), PSTR("%-5s"), PSTR("SD1")); #ifdef DEBUG Serial.println(F("SD1")); #endif break; case SD_CARD_TYPE_SD2: - sprintf(sd_string, "%-5s", "SD2"); + snprintf_P(sd_string, strlen(sd_string), PSTR("%-5s"), PSTR("SD2")); #ifdef DEBUG Serial.println(F("SD2")); #endif break; case SD_CARD_TYPE_SDHC: - sprintf(sd_string, "%-5s", "SD2"); + snprintf_P(sd_string, strlen(sd_string), PSTR("%-5s"), PSTR("SD2")); #ifdef DEBUG Serial.println(F("SDHC")); #endif break; default: - sprintf(sd_string, "%-5s", "UKNW"); + snprintf_P(sd_string, strlen(sd_string), PSTR("%-5s"), PSTR("UKNW")); #ifdef DEBUG Serial.println(F("Unknown")); #endif @@ -2679,7 +2679,7 @@ uint8_t check_sd_cards(void) Serial.println(volumesize); #endif - sprintf(sd_string + 5, "FAT%2d %02dGB", volume.fatType(), int(volumesize)); + snprintf_P(sd_string + 5, strlen(sd_string), PSTR("FAT%2d %02dGB"), volume.fatType(), int(volumesize)); } #ifdef DEBUG @@ -2702,7 +2702,7 @@ void check_and_create_directories(void) // create directories for banks for (i = 0; i < MAX_BANKS; i++) { - sprintf(tmp, "/%d", i); + snprintf_P(tmp, strlen(tmp), PSTR("/%d"), i); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2713,7 +2713,7 @@ void check_and_create_directories(void) } } - sprintf(tmp, "/%s", PERFORMANCE_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), PERFORMANCE_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2725,7 +2725,7 @@ void check_and_create_directories(void) /* // create directories for configuration files - sprintf(tmp, "/%s", VOICE_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), VOICE_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2734,7 +2734,7 @@ void check_and_create_directories(void) #endif SD.mkdir(tmp); } - sprintf(tmp, "/%s", PERFORMANCE_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), PERFORMANCE_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2743,7 +2743,7 @@ void check_and_create_directories(void) #endif SD.mkdir(tmp); } - sprintf(tmp, "/%s", FX_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), FX_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2752,7 +2752,7 @@ void check_and_create_directories(void) #endif SD.mkdir(tmp); } - sprintf(tmp, "/%s", DRUM_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), DRUM_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2761,7 +2761,7 @@ void check_and_create_directories(void) #endif SD.mkdir(tmp); } - sprintf(tmp, "/%s", FAV_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), FAV_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2772,7 +2772,7 @@ void check_and_create_directories(void) } */ - sprintf(tmp, "/%s", PERFORMANCE_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s"), PERFORMANCE_CONFIG_PATH); if (!SD.exists(tmp)) { #ifdef DEBUG @@ -2783,14 +2783,14 @@ void check_and_create_directories(void) } //check if updated Fav-System is ready or if setup has to run once. - sprintf(tmp, "/%s/fav-v2", FAV_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s/fav-v2"), FAV_CONFIG_PATH); if (!SD.exists(tmp)) { // Clear now obsolte marker files from Favs. // Only needs to run once. for (uint8_t i = 0; i < MAX_BANKS; i++) { - sprintf(tmp, "/%s/%d/hasfav", FAV_CONFIG_PATH, i); + snprintf_P(tmp, strlen(tmp), PSTR("/%s/%d/hasfav"), FAV_CONFIG_PATH, i); #ifdef DEBUG Serial.print(F("Delete Marker File")); Serial.println(tmp); @@ -2802,7 +2802,7 @@ void check_and_create_directories(void) // Only needs to run once. for (uint8_t i = 0; i < MAX_BANKS; i++) { - sprintf(tmp, "/%s/%d", FAV_CONFIG_PATH, i); + snprintf_P(tmp, strlen(tmp), PSTR("/%s/%d"), FAV_CONFIG_PATH, i); #ifdef DEBUG Serial.print(F("Delete empty folder ")); Serial.println(tmp); @@ -2810,7 +2810,7 @@ void check_and_create_directories(void) if (SD.exists(tmp)) SD.rmdir(tmp); } - sprintf(tmp, "/%s/fav-v2", FAV_CONFIG_PATH); + snprintf_P(tmp, strlen(tmp), PSTR("/%s/fav-v2"), FAV_CONFIG_PATH); if (!SD.exists(tmp)) SD.mkdir(tmp); // Set Marker so that the Cleanup loops only run once. } @@ -3087,7 +3087,7 @@ void SerialPrintFormatInt3(uint8_t num) { char buf[4]; memset(buf, 0, 4); - sprintf(buf, "%3d", num); + snprintf_P(buf, strlen(buf), PSTR("%3d"), num); Serial.print(buf); } diff --git a/dexed_sd.cpp b/dexed_sd.cpp index 108c350..bad4255 100644 --- a/dexed_sd.cpp +++ b/dexed_sd.cpp @@ -83,7 +83,7 @@ bool load_sd_voice(uint8_t b, uint8_t v, uint8_t instance_id) uint8_t data[128]; get_bank_name(b, bank_name, sizeof(bank_name)); - sprintf(filename, "/%d/%s.syx", b, bank_name); + snprintf_P(filename, strlen(filename), PSTR("/%d/%s.syx"), b, bank_name); AudioNoInterrupts(); sysex = SD.open(filename); @@ -154,7 +154,7 @@ bool save_sd_voice(uint8_t b, uint8_t v, uint8_t instance_id) uint8_t data[128]; get_bank_name(b, bank_name, sizeof(bank_name)); - sprintf(filename, "/%d/%s.syx", b, bank_name); + snprintf_P(filename, strlen(filename), PSTR("/%d/%s.syx"), b, bank_name); AudioNoInterrupts(); sysex = SD.open(filename, FILE_WRITE); @@ -369,7 +369,7 @@ bool save_sd_bank(const char* bank_filename, uint8_t* data) // first remove old bank sscanf(bank_filename, "/%d/%s", &bank_number, tmp); - sprintf(tmp, "/%d", bank_number); + snprintf_P(tmp, strlen(tmp), PSTR("/%d"), bank_number); AudioNoInterrupts(); root = SD.open(tmp); while (42 == 42) @@ -385,7 +385,7 @@ bool save_sd_bank(const char* bank_filename, uint8_t* data) Serial.print(F("/")); Serial.println(entry.name()); #endif - sprintf(tmp2, "%s/%s", tmp, entry.name()); + snprintf_P(tmp2, strlen(tmp2), PSTR("%s/%s"), tmp, entry.name()); entry.close(); #ifndef DEBUG SD.remove(tmp2); @@ -444,7 +444,7 @@ bool load_sd_drumsettings_json(uint8_t number) StaticJsonDocument data_json; char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, DRUMS_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, DRUMS_CONFIG_NAME); // first check if file exists... AudioNoInterrupts(); @@ -518,7 +518,7 @@ bool save_sd_drumsettings_json(uint8_t number) if (check_performance_directory(number)) { - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, DRUMS_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, DRUMS_CONFIG_NAME); #ifdef DEBUG Serial.print(F("Saving drums config ")); @@ -598,7 +598,7 @@ bool load_sd_voiceconfig_json(uint8_t vc, uint8_t instance_id) File json; StaticJsonDocument data_json; - sprintf(filename, "/%s/%d/%s%d.json", PERFORMANCE_CONFIG_PATH, vc, VOICE_CONFIG_NAME, instance_id + 1); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s%d.json"), PERFORMANCE_CONFIG_PATH, vc, VOICE_CONFIG_NAME, instance_id + 1); // first check if file exists... AudioNoInterrupts(); @@ -691,7 +691,7 @@ bool save_sd_voiceconfig_json(uint8_t vc, uint8_t instance_id) { File json; StaticJsonDocument data_json; - sprintf(filename, "/%s/%d/%s%d.json", PERFORMANCE_CONFIG_PATH, vc, VOICE_CONFIG_NAME, instance_id + 1); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s%d.json"), PERFORMANCE_CONFIG_PATH, vc, VOICE_CONFIG_NAME, instance_id + 1); #ifdef DEBUG Serial.print(F("Saving voice config ")); @@ -782,7 +782,7 @@ bool load_sd_fx_json(uint8_t number) File json; StaticJsonDocument data_json; char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, FX_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, FX_CONFIG_NAME); // first check if file exists... AudioNoInterrupts(); @@ -882,7 +882,7 @@ bool save_sd_fx_json(uint8_t number) { File json; StaticJsonDocument data_json; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, FX_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, FX_CONFIG_NAME); #ifdef DEBUG Serial.print(F("Saving fx config ")); @@ -968,7 +968,7 @@ bool load_sd_epiano_json(uint8_t number) File json; StaticJsonDocument data_json; char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, EPIANO_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, EPIANO_CONFIG_NAME); // first check if file exists... AudioNoInterrupts(); @@ -1048,7 +1048,7 @@ bool save_sd_epiano_json(uint8_t number) { File json; StaticJsonDocument data_json; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, EPIANO_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, EPIANO_CONFIG_NAME); #ifdef DEBUG Serial.print(F("Saving epiano config ")); @@ -1117,7 +1117,7 @@ bool load_sd_sys_json(void) File json; StaticJsonDocument data_json; char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s.json", SYS_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s.json"), SYS_CONFIG_NAME); // first check if file exists... AudioNoInterrupts(); @@ -1182,7 +1182,7 @@ bool save_sd_sys_json(void) { File json; StaticJsonDocument data_json; - sprintf(filename, "/%s.json", SYS_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s.json"), SYS_CONFIG_NAME); #ifdef DEBUG Serial.print(F("Saving sys config to ")); @@ -1249,7 +1249,7 @@ bool save_sd_performance_json(uint8_t number) for (uint8_t i = 0; i < MAX_DEXED; i++) { - sprintf(filename, "/%s/%d/%s%d.json", PERFORMANCE_CONFIG_PATH, number, VOICE_CONFIG_NAME, i); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s%d.json"), PERFORMANCE_CONFIG_PATH, number, VOICE_CONFIG_NAME, i); #ifdef DEBUG Serial.print(F("Write Voice-Config")); Serial.println(filename); @@ -1261,7 +1261,7 @@ bool save_sd_performance_json(uint8_t number) { File json; StaticJsonDocument data_json; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); #ifdef DEBUG Serial.print(F("Saving performance config ")); Serial.print(number); @@ -1309,7 +1309,7 @@ bool check_performance_directory(uint8_t number) if (sd_card > 0) { - sprintf(dir, "/%s/%d", PERFORMANCE_CONFIG_PATH, number); + snprintf_P(dir, strlen(dir), PSTR("/%s/%d"), PERFORMANCE_CONFIG_PATH, number); AudioNoInterrupts(); SD.begin(); @@ -1353,7 +1353,7 @@ void get_sd_performance_name_json(uint8_t number) StaticJsonDocument data_json; char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); // first check if file exists... AudioNoInterrupts(); @@ -1439,7 +1439,7 @@ bool check_sd_performance_exists(uint8_t number) { char filename[CONFIG_FILENAME_LEN]; - sprintf(filename, "/%s/%d/%s.json", PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); + snprintf_P(filename, strlen(filename), PSTR("/%s/%d/%s.json"), PERFORMANCE_CONFIG_PATH, number, PERFORMANCE_CONFIG_NAME); // check if file exists... if (SD.exists(filename)) @@ -1647,7 +1647,7 @@ bool get_bank_name(uint8_t b, char* name, uint8_t len) memset(name, 0, len); - sprintf(bankdir, "/%d", b); + snprintf_P(bankdir, strlen(bankdir), PSTR("/%d"), b); // try to open directory sysex = SD.open(bankdir); @@ -1698,7 +1698,7 @@ bool get_voice_name(uint8_t b, uint8_t v, char* name, uint8_t len) v = constrain(v, 0, MAX_VOICES - 1); get_bank_name(b, bank_name, sizeof(bank_name)); - sprintf(filename, "/%d/%s.syx", b, bank_name); + snprintf_P(filename, strlen(filename), PSTR("/%d/%s.syx"), b, bank_name); #ifdef DEBUG Serial.print(F("Reading voice-name from [")); Serial.print(filename); @@ -1742,7 +1742,7 @@ bool get_voice_by_bank_name(uint8_t b, const char* bank_name, uint8_t v, char* v { char filename[FILENAME_LEN]; - sprintf(filename, "/%d/%s.syx", b, bank_name); + snprintf_P(filename, strlen(filename), PSTR("/%d/%s.syx"), b, bank_name); #ifdef DEBUG Serial.print(F("Reading voice-name from [")); Serial.print(filename);