This repository has been archived on 2021-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
raylib-test/include/fileToCharArray.h

72 lines
2.2 KiB
C
Raw Permalink Normal View History

2021-11-24 14:36:30 +01:00
#include "raylib.h"
#include <string.h>
2021-12-06 08:41:02 +01:00
static char const DECLARATION_BEGIN[] = "static const unsigned char ";
static char const DECLARATION_END[] = "[] = {\n";
static char const FILE_END[] = "};";
static char const LINE[] = " 0x00\n";
static int const DECLARATION_BEGIN_SIZE = sizeof(DECLARATION_BEGIN) - 1;
static int const DECLARATION_END_SIZE = sizeof(DECLARATION_END) - 1;
static int const FILE_END_SIZE = sizeof(FILE_END) - 1;
static int const LINE_SIZE = sizeof(LINE) - 1;
static int const ADDITIONAL_SIZE = DECLARATION_BEGIN_SIZE + DECLARATION_END_SIZE + FILE_END_SIZE;
void fileToBytes(char const *const inputFileName, char const *const outputFileName, char const *const arrayName)
2021-11-24 14:36:30 +01:00
{
unsigned int fileSize = 0;
unsigned char *const fileData = LoadFileData(inputFileName, &fileSize);
char *const content = (char *const)MemAlloc(sizeof(char) * fileSize * 10);
2021-11-24 14:36:30 +01:00
int cursor = 0;
for (unsigned int i = 0; i < fileSize; i++)
{
TextAppend(content, TextFormat(" 0x%02x,\n", fileData[i]), &cursor);
}
cursor = 0;
2021-12-06 08:41:02 +01:00
char *const final = (char *const)MemAlloc((ADDITIONAL_SIZE + strlen(arrayName) + (LINE_SIZE * fileSize)) * sizeof(char));
TextAppend(final, DECLARATION_BEGIN, &cursor);
TextAppend(final, arrayName, &cursor);
TextAppend(final, DECLARATION_END, &cursor);
TextAppend(final, content, &cursor);
TextAppend(final, FILE_END, &cursor);
MemFree(content);
SaveFileText(outputFileName, final);
MemFree(final);
}
void bytesToFile(unsigned char const *const input, unsigned int const inputSize, char const *const outputFileName, char const *const arrayName)
{
char *const content = (char *const)MemAlloc(sizeof(char) * inputSize * sizeof(" 0x00,\n"));
int cursor = 0;
for (unsigned int i = 0; i < inputSize; i++)
{
TextAppend(content, TextFormat(" 0x%02x,\n", input[i]), &cursor);
}
cursor = 0;
char *const final = (char *const)MemAlloc((ADDITIONAL_SIZE + strlen(arrayName) + (LINE_SIZE * inputSize)) * sizeof(char));
2021-11-24 14:36:30 +01:00
2021-12-06 08:41:02 +01:00
TextAppend(final, DECLARATION_BEGIN, &cursor);
2021-11-24 14:36:30 +01:00
TextAppend(final, arrayName, &cursor);
2021-12-06 08:41:02 +01:00
TextAppend(final, DECLARATION_END, &cursor);
2021-11-24 14:36:30 +01:00
TextAppend(final, content, &cursor);
2021-12-06 08:41:02 +01:00
TextAppend(final, FILE_END, &cursor);
2021-11-24 14:36:30 +01:00
MemFree(content);
SaveFileText(outputFileName, final);
MemFree(final);
}