From 00def9c493c00196c613f554189a6846dfc221d5 Mon Sep 17 00:00:00 2001 From: Alexey Ismagilov Date: Fri, 23 Feb 2024 02:14:49 +0300 Subject: [PATCH] FIX: can not compile generated code --- src/raudio.c | 34 ++++++++++++++++++++++++++++++---- src/rmodels.c | 5 +---- src/rtextures.c | 5 +---- src/utils.c | 33 +++++++++++++++++++++++++++++---- src/utils.h | 2 ++ 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index 4d0de45a5..e18ee5c4e 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -418,6 +418,7 @@ static bool IsFileExtension(const char *fileName, const char *ext); // Check fil static const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes the dot: .png) static const char *GetFileName(const char *filePath); // Get pointer to filename for a path string static const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string) +const char *GetFileIdentifier(const char *fileName) // Get identifier for file which is used in Export...AsCode functions (uses static string) static unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read) static bool SaveFileData(const char *fileName, void *data, int dataSize); // Save data to file from byte array (write) @@ -1097,10 +1098,7 @@ bool ExportWaveAsCode(Wave wave, const char *fileName) byteCount += sprintf(txtData + byteCount, "// //\n"); byteCount += sprintf(txtData + byteCount, "//////////////////////////////////////////////////////////////////////////////////\n\n"); - // Get file name from path and convert variable name to uppercase - char varFileName[256] = { 0 }; - strcpy(varFileName, GetFileNameWithoutExt(fileName)); - for (int i = 0; varFileName[i] != '\0'; i++) if (varFileName[i] >= 'a' && varFileName[i] <= 'z') { varFileName[i] = varFileName[i] - 32; } + const char *varFileName = GetFileIdentifier(fileName); // Add wave information byteCount += sprintf(txtData + byteCount, "// Wave data information\n"); @@ -2689,6 +2687,34 @@ static const char *GetFileNameWithoutExt(const char *filePath) return fileName; } +// Get identifier for file which is used in Export...AsCode functions (uses static string) +const char *GetFileIdentifier(const char *fileName) +{ + // We get GetFileNameWithoutExt() output, replace some characters, delete some characters and maybe add '_' + // at begin. In the worst case, we will not delete any characters and add one character at the beginning. + #define MAX_IDENTIFIER_LENGTH (MAX_FILENAME_LENGTH + 1) + + static char identifier[MAX_IDENTIFIER_LENGTH]; + memset(identifier, 0, MAX_IDENTIFIER_LENGTH); + + const char *tmpFileName = GetFileNameWithoutExt(fileName); + int pos = 0; + + if (isdigit(*tmpFileName)) identifier[pos++] = '_'; + + while (*tmpFileName != '\0') + { + char c = *(tmpFileName++); + bool isAlphanumeric = isalpha(c) || isdigit(c); + if (!isAlphanumeric && pos > 0 && identifier[pos - 1] == '_') continue; + identifier[pos++] = isAlphanumeric ? toupper(c) : '_'; + } + + // We do not need add '\0' manually since we call memset() at the beginning of the function. + + return identifier; +} + // Load data from file into a buffer static unsigned char *LoadFileData(const char *fileName, int *dataSize) { diff --git a/src/rmodels.c b/src/rmodels.c index 166f51a6a..1f7af1786 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -1874,10 +1874,7 @@ bool ExportMeshAsCode(Mesh mesh, const char *fileName) byteCount += sprintf(txtData + byteCount, "// //\n"); byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n"); - // Get file name from path and convert variable name to uppercase - char varFileName[256] = { 0 }; - strcpy(varFileName, GetFileNameWithoutExt(fileName)); - for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } + const char *varFileName = GetFileIdentifier(fileName); // Add image information byteCount += sprintf(txtData + byteCount, "// Mesh basic information\n"); diff --git a/src/rtextures.c b/src/rtextures.c index 97d9e3858..0bcee6c14 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -852,10 +852,7 @@ bool ExportImageAsCode(Image image, const char *fileName) byteCount += sprintf(txtData + byteCount, "// //\n"); byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n"); - // Get file name from path and convert variable name to uppercase - char varFileName[256] = { 0 }; - strcpy(varFileName, GetFileNameWithoutExt(fileName)); - for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } + const char *varFileName = GetFileIdentifier(fileName); // Add image information byteCount += sprintf(txtData + byteCount, "// Image data information\n"); diff --git a/src/utils.c b/src/utils.c index 987021c73..d4ff83bc2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -44,6 +44,7 @@ #include // Required for: Android assets manager: AAsset, AAssetManager_open(), ... #endif +#include // Required for: isalpha(), isdigit(), toupper() #include // Required for: exit() #include // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose() #include // Required for: va_list, va_start(), va_end() @@ -288,6 +289,33 @@ bool SaveFileData(const char *fileName, void *data, int dataSize) return success; } +// Get identifier for file which is used in Export...AsCode functions (uses static string) +// NOTE: this function is not a part of the public API +const char *GetFileIdentifier(const char *fileName) +{ + #define MAX_IDENTIFIER_LENGTH 256 + + static char identifier[MAX_IDENTIFIER_LENGTH]; + memset(identifier, 0, MAX_IDENTIFIER_LENGTH); + + const char *tmpFileName = GetFileNameWithoutExt(fileName); + int pos = 0; + + if (isdigit(*tmpFileName)) identifier[pos++] = '_'; + + while (*tmpFileName != '\0') + { + char c = *(tmpFileName++); + bool isAlphanumeric = isalpha(c) || isdigit(c); + if (!isAlphanumeric && pos > 0 && identifier[pos - 1] == '_') continue; + identifier[pos++] = isAlphanumeric ? toupper(c) : '_'; + } + + // We do not need add '\0' manually since we call memset() at the beginning of the function. + + return identifier; +} + // Export data to code (.h), returns true on success bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileName) { @@ -313,10 +341,7 @@ bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileN byteCount += sprintf(txtData + byteCount, "// //\n"); byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n"); - // Get file name from path and convert variable name to uppercase - char varFileName[256] = { 0 }; - strcpy(varFileName, GetFileNameWithoutExt(fileName)); - for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } + const char *varFileName = GetFileIdentifier(fileName); byteCount += sprintf(txtData + byteCount, "#define %s_DATA_SIZE %i\n\n", varFileName, dataSize); diff --git a/src/utils.h b/src/utils.h index 23eca8ee9..a016e65fb 100644 --- a/src/utils.h +++ b/src/utils.h @@ -74,6 +74,8 @@ void InitAssetManager(AAssetManager *manager, const char *dataPath); // Initia FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only! #endif +const char *GetFileIdentifier(const char *fileName); // Get identifier for file which is used in Export...AsCode functions (uses static string) + #if defined(__cplusplus) } #endif