FIX: can not compile generated code

This commit is contained in:
Alexey Ismagilov 2024-02-23 02:14:49 +03:00
parent 23616153d4
commit 00def9c493
5 changed files with 63 additions and 16 deletions

View File

@ -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)
{

View File

@ -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");

View File

@ -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");

View File

@ -44,6 +44,7 @@
#include <android/asset_manager.h> // Required for: Android assets manager: AAsset, AAssetManager_open(), ...
#endif
#include <ctype.h> // Required for: isalpha(), isdigit(), toupper()
#include <stdlib.h> // Required for: exit()
#include <stdio.h> // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose()
#include <stdarg.h> // 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);

View File

@ -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