Skip to content

Commit 863d84d

Browse files
committed
Completed implementation of issue #28.
1 parent aaa1ec6 commit 863d84d

File tree

8 files changed

+44
-39
lines changed

8 files changed

+44
-39
lines changed

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,24 @@ bin2cpp --help
8484
bin2cpp --version
8585
```
8686

87-
| Argument | Description |
88-
|----------------------|------------------------------------------------------------------------------------------------------------|
89-
| --help | Display this help message. |
90-
| --version | Display this application version. |
91-
| --file=\<path> | Path of the input file used for embedding as a C++ source code. |
92-
| --output=\<path> | Output folder where to create generated code. ie: .\generated_files |
93-
| --headerfile=\<name> | File name of the generated C++ Header file. ie: SplashScreen.h |
94-
| --generator=\<name> | Name of the generator to use. Possible values are 'segment', 'string' and 'array'. [default: segment]. |
95-
| --encoding=\<name> | Name of the binary to string literal encoding to use. Possible values are 'oct' and 'hex'. [default: oct]. |
96-
| --identifier=\<name> | Identifier of the function name that is used to get an instance of the file. ie: SplashScreen |
97-
| --chunksize=\<value> | Size in bytes of each string segments (bytes per row). [default: 200]. |
98-
| --baseclass=\<value> | The name of the interface for embedded files. [default: File]. |
99-
| --namespace=\<value> | The namespace of the generated source code [default: bin2cpp]. |
100-
| --override | Tells bin2cpp to overwrite the destination files. |
101-
| --noheader | Do not print program header to standard output. |
102-
| --quiet | Do not log any message to standard output. |
87+
| Argument | Description |
88+
|-----------------------|------------------------------------------------------------------------------------------------------------|
89+
| --help | Display this help message. |
90+
| --version | Display this application version. |
91+
| --file=\<path> | Path of the input file used for embedding as a C++ source code. |
92+
| --output=\<path> | Output folder where to create generated code. ie: .\generated_files |
93+
| --headerfile=\<name> | File name of the generated C++ Header file. ie: SplashScreen.h |
94+
| --generator=\<name> | Name of the generator to use. Possible values are 'segment', 'string' and 'array'. [default: segment]. |
95+
| --encoding=\<name> | Name of the binary to string literal encoding to use. Possible values are 'oct' and 'hex'. [default: oct]. |
96+
| --identifier=\<name> | Identifier of the function name that is used to get an instance of the file. ie: SplashScreen |
97+
| --chunksize=\<value> | Size in bytes of each string segments (bytes per row). [default: 200]. |
98+
| --baseclass=\<value> | The name of the interface for embedded files. [default: File]. |
99+
| --managerfile=\<name> | File name of the generated C++ Header file for the FileManager class. ie: FileManager.h. |
100+
| --namespace=\<value> | The namespace of the generated source code [default: bin2cpp]. |
101+
| --usefilemanager | Register the generated file to the FileManager class. [default: false]. |
102+
| --override | Tells bin2cpp to overwrite the destination files. |
103+
| --noheader | Do not print program header to standard output. |
104+
| --quiet | Do not log any message to standard output. |
103105

104106
## Example ##
105107

src/bin2cpp/BaseGenerator.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ namespace bin2cpp
195195
return output;
196196
}
197197

198-
std::string BaseGenerator::getFileManagerTemplate()
198+
std::string BaseGenerator::getFileManagerRegistrationTemplate()
199199
{
200200
if (!this->isManagerEnabled())
201201
return std::string();
@@ -204,8 +204,9 @@ namespace bin2cpp
204204
std::string className = getClassName();
205205

206206
std::string output;
207-
output << " bool " << className << "Initializer() { bin2cpp::FileManager::getInstance().registerFile(&" << getGetterFunctionName() << "); return true; }\n";
208-
output << " static bool k" << className << "Initialized = " << className << "Initializer();\n";
207+
output << " typedef const " << mBaseClass << " & (*t_func)();\n";
208+
output << " extern bool RegisterFile(t_func iFunctionPointer);\n";
209+
output << " static bool k" << className << "Registered = " << mNamespace << "::RegisterFile(&" << getGetterFunctionName() << ");\n";
209210
return output;
210211
}
211212

@@ -334,6 +335,13 @@ namespace bin2cpp
334335
fprintf(cpp, "\n");
335336
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
336337
fprintf(cpp, "{\n");
338+
fprintf(cpp, " bool RegisterFile(FileManager::t_func iFunctionPointer)\n");
339+
fprintf(cpp, " {\n");
340+
fprintf(cpp, " if (iFunctionPointer == NULL)\n");
341+
fprintf(cpp, " return false;\n");
342+
fprintf(cpp, " FileManager::getInstance().registerFile(iFunctionPointer);\n");
343+
fprintf(cpp, " return true;\n");
344+
fprintf(cpp, " }\n");
337345
fprintf(cpp, " FileManager::FileManager() {}\n");
338346
fprintf(cpp, " FileManager::~FileManager() {}\n");
339347
fprintf(cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n");
@@ -372,7 +380,6 @@ namespace bin2cpp
372380
fprintf(cpp, " return true;\n");
373381
fprintf(cpp, " }\n");
374382
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
375-
fprintf(cpp, "\n");
376383

377384
fclose(cpp);
378385

src/bin2cpp/BaseGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace bin2cpp
6868
virtual std::string getCppFilePath(const char * iHeaderFilePath);
6969
virtual std::string getHeaderTemplate();
7070
virtual std::string getSaveMethodTemplate();
71-
virtual std::string getFileManagerTemplate();
71+
virtual std::string getFileManagerRegistrationTemplate();
7272
virtual std::string getClassName();
7373

7474
//attributes

src/bin2cpp/SegmentGenerator.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ namespace bin2cpp
8989
//write cpp file heading
9090
fprintf(cpp, "%s", getHeaderTemplate().c_str());
9191
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
92-
if (isManagerEnabled())
93-
{
94-
fprintf(cpp, "#include \"%s\"\n", manager.c_str() );
95-
}
9692
fprintf(cpp, "#include <stdio.h> //for FILE\n");
9793
fprintf(cpp, "#include <string> //for memcpy\n");
9894
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
@@ -150,7 +146,7 @@ namespace bin2cpp
150146
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
151147
if (isManagerEnabled())
152148
{
153-
std::string fileManagerTemplate = getFileManagerTemplate();
149+
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
154150
fprintf(cpp, "%s", fileManagerTemplate.c_str());
155151
}
156152
fprintf(cpp, "}; //%s\n", mNamespace.c_str());

src/bin2cpp/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,10 @@ int main(int argc, char* argv[])
360360

361361
args.usefilemanager = ra::cli::ParseArgument("usefilemanager", dummy, argc, argv);
362362

363-
if (args.usefilemanager && args.managerHeaderFilename.empty())
363+
//force usefilemanager if managerfile is specified
364+
if (!args.managerHeaderFilename.empty())
364365
{
365-
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
366-
ra::logging::Log(ra::logging::LOG_ERROR, "%s (managerfile)", getErrorCodeDescription(error));
367-
printUsage();
368-
return error;
366+
args.usefilemanager = true;
369367
}
370368

371369
std::string encodingStr;

test/bin2cpp_unittest/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ set(GENERATED_TEST_FILES
8181
${GENERATED_TEST_FILES_DIR}/testText1000/_testText1000.cpp
8282
${GENERATED_TEST_FILES_DIR}/testText100000/_testText100000.h
8383
${GENERATED_TEST_FILES_DIR}/testText100000/_testText100000.cpp
84-
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.h
85-
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.cpp
86-
${GENERATED_TEST_FILES_DIR}/testFileManager/_FileManager.h
87-
${GENERATED_TEST_FILES_DIR}/testFileManager/_FileManager.cpp
84+
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.1.h
85+
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.1.cpp
86+
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.2.h
87+
${GENERATED_TEST_FILES_DIR}/testFileManager/_testFileManager.2.cpp
88+
${GENERATED_TEST_FILES_DIR}/testFileManager/FileManager.h
89+
${GENERATED_TEST_FILES_DIR}/testFileManager/FileManager.cpp
8890
${GENERATED_TEST_FILES_DIR}/testDir01/sources/_img0001.h
8991
${GENERATED_TEST_FILES_DIR}/testDir01/sources/_img0002.h
9092
${GENERATED_TEST_FILES_DIR}/testDir01/sources/_img0003.h

test/bin2cpp_unittest/generate_test_files.bat.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ set OUTDIR=.\generated_files\%TEST_NAME%
195195
mkdir %OUTDIR% 1>NUL 2>NUL
196196
@TESTFILEGENERATOR_TARGET_FILE@ --file=%OUTDIR%\%TEST_NAME%.1.bin --size=1000 --fill=random --seed=1
197197
@TESTFILEGENERATOR_TARGET_FILE@ --file=%OUTDIR%\%TEST_NAME%.2.bin --size=1000 --fill=random --seed=2
198-
@BIN2CPP_TARGET_FILE@ --noheader --file=%OUTDIR%\%TEST_NAME%.1.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --managerfile=_FileManager.h --override
199-
@BIN2CPP_TARGET_FILE@ --noheader --file=%OUTDIR%\%TEST_NAME%.2.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --managerfile=_FileManager.h --override
198+
@BIN2CPP_TARGET_FILE@ --noheader --file=%OUTDIR%\%TEST_NAME%.1.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.1.h --identifier=%TEST_NAME%1 --managerfile=FileManager.h --override
199+
@BIN2CPP_TARGET_FILE@ --noheader --file=%OUTDIR%\%TEST_NAME%.2.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.2.h --identifier=%TEST_NAME%2 --usefilemanager --override
200200
if %errorlevel% neq 0 exit /b %errorlevel%
201201

202202
set TEST_NAME=testDir01

test/bin2cpp_unittest/generate_test_files.sh.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export OUTDIR=./generated_files/$TEST_NAME
169169
mkdir -p ${OUTDIR}
170170
@TESTFILEGENERATOR_TARGET_FILE@ --file=$OUTDIR/$TEST_NAME.1.bin --size=1000 --fill=random --seed=1
171171
@TESTFILEGENERATOR_TARGET_FILE@ --file=$OUTDIR/$TEST_NAME.2.bin --size=1000 --fill=random --seed=2
172-
@BIN2CPP_TARGET_FILE@ --noheader --file=$OUTDIR/$TEST_NAME.1.bin --output=$OUTDIR --headerfile=_$TEST_NAME.h --identifier=$TEST_NAME --managerfile=_FileManager.h --override
173-
@BIN2CPP_TARGET_FILE@ --noheader --file=$OUTDIR/$TEST_NAME.2.bin --output=$OUTDIR --headerfile=_$TEST_NAME.h --identifier=$TEST_NAME --managerfile=_FileManager.h --usefilemanager --override
172+
@BIN2CPP_TARGET_FILE@ --noheader --file=$OUTDIR/$TEST_NAME.1.bin --output=$OUTDIR --headerfile=_$TEST_NAME.1.h --identifier=testFileManager1 --managerfile=FileManager.h --override
173+
@BIN2CPP_TARGET_FILE@ --noheader --file=$OUTDIR/$TEST_NAME.2.bin --output=$OUTDIR --headerfile=_$TEST_NAME.2.h --identifier=testFileManager2 --usefilemanager --override
174174

175175
export TEST_NAME=testDir01
176176
export OUTDIR=./generated_files/$TEST_NAME

0 commit comments

Comments
 (0)