added SparrowResource

This commit is contained in:
Anselme 2016-03-31 19:24:12 +02:00
parent f7d69b4a20
commit 8f6ebade36
8 changed files with 67 additions and 19 deletions

8
resource/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
project(SparrowResource)
cmake_minimum_required(VERSION 2.8)
# choose source file
set(EXEC_SRC_LIST main.cpp)
include(../template.cmake)

View File

@ -5,27 +5,50 @@ using namespace std;
#define NB_VAL_PER_LINE 256
#define FILE_BEGIN "#include <string>\n\
#include <unordered_map>\n\
\n\
namespace Resource {\n\
std::unordered_map<std::string, const char*> *resourceFilesData;\n"
#define FILE_MID "\nvoid initResourceData()\n\
{\n\
resourceFilesData = new std::unordered_map<std::string, const char*>();\n"
#define FILE_END "\n\
}\n\
\n\
const char* get(const std::string &fileName)\n\
{\n\
if(resourceFilesData->count(fileName) > 0)\n\
return (*resourceFilesData)[fileName];\n\
else\n\
return NULL;\n\
}\n\
}\n"
unsigned int n = 0;
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];
fprintf(out, "\nconst char data%d[] = {\n", n);
char ptr[NB_VAL_PER_LINE];
size_t nbRead;
do
{
nbRead = fread(ptr, sizeof(unsigned char), NB_VAL_PER_LINE, in);
nbRead = fread(ptr, sizeof(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");
fprintf(out, i ? ",0x%x" : "\t0x%x", ptr[i]);
fprintf(out, "\n");
}
while(nbRead == NB_VAL_PER_LINE);
fprintf(out, "};\n\tresourceFilesData[std::string(\"%s\")] = data%d;\n", inFile.c_str(), n);
fprintf(out, "};\n");
++n;
fclose(in);
return true;
@ -46,7 +69,7 @@ int main(int argc, char *argv[])
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");
fprintf(out, FILE_BEGIN);
for(int i=2; i<argc; ++i)
{
if(!addFile(argv[i], out))
@ -57,7 +80,10 @@ int main(int argc, char *argv[])
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");
fprintf(out, FILE_MID);
for(int i=0; i<n; ++i)
fprintf(out, "\t(*resourceFilesData)[\"%s\"] = data%d;\n", argv[2+i], i);
fprintf(out, FILE_END);
fclose(out);
printf("successfully created resource file \"%s\".\n", argv[1]);
return EXIT_SUCCESS;

View File

@ -6,7 +6,7 @@
namespace Resource {
extern void initResourceData();
extern unsigned char *get();
extern const char *get(const std::string&);
}

7
resource/test/README Normal file
View File

@ -0,0 +1,7 @@
g++ -std=c++11 ../main.cpp -o SparrowResource.exe
SparrowResource res.cpp plop.txt
g++ -std=c++11 test.cpp res.cpp -o test.exe
test

1
resource/test/plop.txt Normal file
View File

@ -0,0 +1 @@
Hello World !

15
resource/test/test.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
#include "resource.h"
using namespace std;
int main()
{
cout << "Resource test" << endl;
Resource::initResourceData();
const char *str = Resource::get("plop.txt");
if(str != NULL)
cout << str << endl;
else
cout << "nope" << endl;
}

View File

@ -1,9 +0,0 @@
include(test.cmake)
cmake_minimum_required(VERSION 2.8)
include(../template.cmake)

View File