65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
#include <cstdio>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
#define NB_VAL_PER_LINE 256
|
|
|
|
bool addFile(const string &inFile, FILE *out)
|
|
{
|
|
static unsigned int n = 0;
|
|
FILE *in = fopen(inFile.c_str(), "r");
|
|
if(in == NULL)
|
|
{
|
|
fprintf(stderr, "can't open \"%s\" for readingn", inFile.c_str());
|
|
return false;
|
|
}
|
|
fprintf(out, "\n\tunsigned char data%d[] = {\n", n);
|
|
unsigned char ptr[NB_VAL_PER_LINE];
|
|
size_t nbRead;
|
|
do
|
|
{
|
|
nbRead = fread(ptr, sizeof(unsigned char), NB_VAL_PER_LINE, in);
|
|
for(unsigned int i=0; i<nbRead; ++i)
|
|
fprintf(out, i ? ",0x%x" : "\t\t0x%x", ptr[i]);
|
|
fprintf(out, "\n\t");
|
|
}
|
|
while(nbRead == NB_VAL_PER_LINE);
|
|
fprintf(out, "};\n\tresourceFilesData[std::string(\"%s\")] = data%d;\n", inFile.c_str(), n);
|
|
++n;
|
|
fclose(in);
|
|
return true;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if(argc < 3)
|
|
{
|
|
printf("usage : %s RESOURCE_FILE.cpp [LIST_OF_FILES_TO_BAKE]\n", argv[0]);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
FILE *out = fopen(argv[1], "w");
|
|
if(out == NULL)
|
|
{
|
|
fprintf(stderr, "can't open \"%s\" for writing\n", argv[1]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
fprintf(out, "#include <string>\n#include <unordered_map>\n\nnamespace Resource {\nstd::unordered_map<std::string, unsigned char*> *resourceFilesData;\n\nvoid initResourceData()\n{\n\tresourceFilesData = new std::unordered_map<std::string, unsigned char*>();\n");
|
|
for(int i=2; i<argc; ++i)
|
|
{
|
|
if(!addFile(argv[i], out))
|
|
{
|
|
fclose(out);
|
|
return EXIT_FAILURE;
|
|
}
|
|
else
|
|
printf("successfully added \"%s\" to \"%s\".\n", argv[i], argv[1]);
|
|
}
|
|
fprintf(out, "\n}\n\nunsigned char* get(std::string fileName)\n{\n\tif(resourceFilesData->count(fileName) > 0)\n\t\treturn (*resourceFilesData)[fileName];\n\telse\n\t\treturn NULL;\n}\n}\n");
|
|
fclose(out);
|
|
printf("successfully created resource file \"%s\".\n", argv[1]);
|
|
return EXIT_SUCCESS;
|
|
}
|