Skip to content

Commit 4cb3618

Browse files
committed
Merge remote-tracking branch 'remotes/origin/issue28'
For issue #28
2 parents 62dd95d + 0a85571 commit 4cb3618

16 files changed

+601
-39
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ os:
66
branches:
77
only:
88
- master
9+
- issue28
910

1011
install:
1112
- cd $TRAVIS_BUILD_DIR/ci/travis; ./install_googletest.sh;

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+
| --namespace=\<value> | The namespace of the generated source code [default: bin2cpp]. |
100+
| --managerfile=\<name> | File name of the generated C++ Header file for the FileManager class. ie: FileManager.h. |
101+
| --registerfile | 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

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ version: "{branch} (#{build})"
1717
branches:
1818
only:
1919
- master
20+
- issue28
2021

2122
# Do not build on tags (GitHub and BitBucket)
2223
skip_tags: true

src/bin2cpp/ArrayGenerator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ namespace bin2cpp
7676
std::string filename = ra::filesystem::GetFilename(mInputFile.c_str());
7777

7878
//Build class name
79-
std::string className;
80-
className.append(functionIdentifier.c_str());
81-
className.append("File");
79+
std::string className = getClassName();
8280

8381
//Build function
8482
std::string getterFunctionName = getGetterFunctionName();

src/bin2cpp/BaseGenerator.cpp

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
namespace bin2cpp
3737
{
3838
BaseGenerator::BaseGenerator() :
39-
mCppEncoder(IGenerator::CPP_ENCODER_OCT)
39+
mCppEncoder(IGenerator::CPP_ENCODER_OCT),
40+
mManagerEnabled(false)
4041
{
4142
}
4243

@@ -108,6 +109,27 @@ namespace bin2cpp
108109
return mCppEncoder;
109110
}
110111

112+
void BaseGenerator::setManagerHeaderFile(const char * iManagerFile)
113+
{
114+
if (iManagerFile)
115+
mManagerFile = iManagerFile;
116+
}
117+
118+
const char * BaseGenerator::getManagerHeaderFile() const
119+
{
120+
return mManagerFile.c_str();
121+
}
122+
123+
void BaseGenerator::setRegisterFileEnabled(bool iRegisterFileEnabled)
124+
{
125+
mManagerEnabled = iRegisterFileEnabled;
126+
}
127+
128+
bool BaseGenerator::isRegisterFileEnabled() const
129+
{
130+
return mManagerEnabled;
131+
}
132+
111133
//-------------------------------
112134
//protected methods
113135
//-------------------------------
@@ -173,6 +195,30 @@ namespace bin2cpp
173195
return output;
174196
}
175197

198+
std::string BaseGenerator::getFileManagerRegistrationTemplate()
199+
{
200+
if (!this->isRegisterFileEnabled())
201+
return std::string();
202+
203+
//Build class name
204+
std::string className = getClassName();
205+
206+
std::string output;
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";
210+
return output;
211+
}
212+
213+
std::string BaseGenerator::getClassName()
214+
{
215+
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mFunctionIdentifier);
216+
std::string className;
217+
className.append(functionIdentifier.c_str());
218+
className.append("File");
219+
return className;
220+
}
221+
176222
bool BaseGenerator::createCppHeaderFile(const char * iHeaderFilePath)
177223
{
178224
FILE * header = fopen(iHeaderFilePath, "w");
@@ -212,4 +258,132 @@ namespace bin2cpp
212258
return true;
213259
}
214260

261+
bool BaseGenerator::createManagerHeaderFile(const char * iHeaderFilePath)
262+
{
263+
FILE * header = fopen(iHeaderFilePath, "w");
264+
if (!header)
265+
return false;
266+
267+
//define macro guard a macro matching the filename
268+
std::string macro_guard = getCppIncludeGuardMacroName(iHeaderFilePath);
269+
270+
std::string headercomments = getHeaderTemplate();
271+
fprintf(header, "%s", headercomments.c_str());
272+
fprintf(header, "#ifndef %s\n", macro_guard.c_str());
273+
fprintf(header, "#define %s\n", macro_guard.c_str());
274+
fprintf(header, "\n");
275+
fprintf(header, "#include <stddef.h>\n");
276+
fprintf(header, "#include <vector>\n");
277+
fprintf(header, "\n");
278+
fprintf(header, "namespace %s\n", mNamespace.c_str());
279+
fprintf(header, "{\n");
280+
fprintf(header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n");
281+
fprintf(header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n");
282+
fprintf(header, " class %s\n", mBaseClass.c_str());
283+
fprintf(header, " {\n");
284+
fprintf(header, " public:\n");
285+
fprintf(header, " virtual size_t getSize() const = 0;\n");
286+
fprintf(header, " virtual const char * getFilename() const = 0;\n");
287+
fprintf(header, " virtual const char * getBuffer() const = 0;\n");
288+
fprintf(header, " virtual bool save(const char * iFilename) const = 0;\n");
289+
fprintf(header, " };\n");
290+
fprintf(header, " #endif //BIN2CPP_EMBEDDEDFILE_CLASS\n");
291+
fprintf(header, "\n");
292+
fprintf(header, " #ifndef BIN2CPP_FILEMANAGER_CLASS\n");
293+
fprintf(header, " #define BIN2CPP_FILEMANAGER_CLASS\n");
294+
fprintf(header, " class FileManager\n");
295+
fprintf(header, " {\n");
296+
fprintf(header, " private:\n");
297+
fprintf(header, " FileManager();\n");
298+
fprintf(header, " ~FileManager();\n");
299+
fprintf(header, " public:\n");
300+
fprintf(header, " typedef const %s & (*t_func)();\n", mBaseClass.c_str());
301+
fprintf(header, " static FileManager & getInstance();\n");
302+
fprintf(header, " void registerFile(t_func iFunctionPtr);\n");
303+
fprintf(header, " size_t getFileCount() const;\n");
304+
fprintf(header, " const %s * getFile(const size_t & index) const;\n", mBaseClass.c_str());
305+
fprintf(header, " bool saveFiles(const char * iDirectory) const;\n");
306+
fprintf(header, " private:\n");
307+
fprintf(header, " std::vector<t_func> functions_;\n");
308+
fprintf(header, " };\n");
309+
fprintf(header, " #endif //BIN2CPP_FILEMANAGER_CLASS\n");
310+
fprintf(header, "}; //%s\n", mNamespace.c_str());
311+
fprintf(header, "\n");
312+
fprintf(header, "#endif //%s\n", macro_guard.c_str());
313+
314+
fclose(header);
315+
316+
return true;
317+
}
318+
319+
bool BaseGenerator::createManagerSourceFile(const char * iCppFilePath)
320+
{
321+
FILE * cpp = fopen(iCppFilePath, "w");
322+
if (!cpp)
323+
return false;
324+
325+
//Build header and cpp file path
326+
std::string headerPath = getHeaderFilePath(iCppFilePath);
327+
std::string cppPath = iCppFilePath;
328+
std::string headerFilename = ra::filesystem::GetFilename(headerPath.c_str());
329+
std::string cppFilename = ra::filesystem::GetFilename(iCppFilePath);
330+
331+
std::string headercomments = getHeaderTemplate();
332+
fprintf(cpp, "%s", headercomments.c_str());
333+
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str());
334+
fprintf(cpp, "#include <string>\n");
335+
fprintf(cpp, "\n");
336+
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
337+
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");
345+
fprintf(cpp, " FileManager::FileManager() {}\n");
346+
fprintf(cpp, " FileManager::~FileManager() {}\n");
347+
fprintf(cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n");
348+
fprintf(cpp, " void FileManager::registerFile(t_func iFunctionPtr) { functions_.push_back(iFunctionPtr); }\n");
349+
fprintf(cpp, " size_t FileManager::getFileCount() const { return functions_.size(); }\n");
350+
fprintf(cpp, " const File * FileManager::getFile(const size_t & index) const\n");
351+
fprintf(cpp, " {\n");
352+
fprintf(cpp, " if (index >= functions_.size())\n");
353+
fprintf(cpp, " return NULL;\n");
354+
fprintf(cpp, " t_func ressource_getter_function = functions_[index];\n");
355+
fprintf(cpp, " const bin2cpp::File & resource = ressource_getter_function();\n");
356+
fprintf(cpp, " return &resource;\n");
357+
fprintf(cpp, " }\n");
358+
fprintf(cpp, " bool FileManager::saveFiles(const char * iDirectory) const\n");
359+
fprintf(cpp, " {\n");
360+
fprintf(cpp, " if (iDirectory == NULL)\n");
361+
fprintf(cpp, " return false;\n");
362+
fprintf(cpp, " size_t count = getFileCount();\n");
363+
fprintf(cpp, " for(size_t i=0; i<count; i++)\n");
364+
fprintf(cpp, " {\n");
365+
fprintf(cpp, " const File * f = getFile(i);\n");
366+
fprintf(cpp, " if (!f)\n");
367+
fprintf(cpp, " return false;\n");
368+
fprintf(cpp, " std::string path;\n");
369+
fprintf(cpp, " path.append(iDirectory);\n");
370+
fprintf(cpp, " #ifdef _WIN32\n");
371+
fprintf(cpp, " path.append(1, '\\\\');\n");
372+
fprintf(cpp, " #else\n");
373+
fprintf(cpp, " path.append(1, '/');\n");
374+
fprintf(cpp, " #endif\n");
375+
fprintf(cpp, " path.append(f->getFilename());\n");
376+
fprintf(cpp, " bool saved = f->save(path.c_str());\n");
377+
fprintf(cpp, " if (!saved)\n");
378+
fprintf(cpp, " return false;\n");
379+
fprintf(cpp, " }\n");
380+
fprintf(cpp, " return true;\n");
381+
fprintf(cpp, " }\n");
382+
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
383+
384+
fclose(cpp);
385+
386+
return true;
387+
}
388+
215389
}; //bin2cpp

src/bin2cpp/BaseGenerator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,34 @@ namespace bin2cpp
5252
virtual const char * getBaseClass() const;
5353
virtual void setCppEncoder(const CppEncoderEnum & iCppEncoder);
5454
virtual CppEncoderEnum getCppEncoder() const;
55+
virtual void setManagerHeaderFile(const char * iManagerFile);
56+
virtual const char * getManagerHeaderFile() const;
57+
virtual void setRegisterFileEnabled(bool iRegisterFileEnabled);
58+
virtual bool isRegisterFileEnabled() const;
5559

5660
//same header file for all generators
5761
virtual bool createCppHeaderFile(const char * iHeaderFilePath);
62+
virtual bool createManagerHeaderFile(const char * iHeaderFilePath);
63+
virtual bool createManagerSourceFile(const char * iCppFilePath);
5864

5965
protected:
6066
virtual std::string getGetterFunctionName();
6167
virtual std::string getHeaderFilePath(const char * iCppFilePath);
6268
virtual std::string getCppFilePath(const char * iHeaderFilePath);
6369
virtual std::string getHeaderTemplate();
6470
virtual std::string getSaveMethodTemplate();
71+
virtual std::string getFileManagerRegistrationTemplate();
72+
virtual std::string getClassName();
6573

6674
//attributes
6775
std::string mInputFile;
6876
std::string mFunctionIdentifier;
6977
size_t mChunkSize;
7078
std::string mNamespace;
7179
std::string mBaseClass;
80+
std::string mManagerFile;
7281
IGenerator::CppEncoderEnum mCppEncoder;
82+
bool mManagerEnabled;
7383
};
7484

7585
}; //bin2cpp

src/bin2cpp/IGenerator.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ namespace bin2cpp
100100
///<returns>Returns the name of the base class of the generated code. Returns an empty string if not defined.</returns>
101101
virtual const char * getBaseClass() const = 0;
102102

103+
///<summary>
104+
///Defines the path of the FileManager output file.
105+
///</summary>
106+
///<param name="iPath">The path of the FileManager output file.</param>
107+
virtual void setManagerHeaderFile(const char * iPath) = 0;
108+
109+
///<summary>
110+
///Provides the path of the FileManager output file.
111+
///</summary>
112+
///<returns>Returns the path of the FileManager output file. Returns an empty string if not defined.</returns>
113+
virtual const char * getManagerHeaderFile() const = 0;
114+
115+
///<summary>
116+
///Enable or disable the registration of the generated file to the FileManager.
117+
///</summary>
118+
///<param name="iFileManagerEnabled">The new value of the flag.</param>
119+
virtual void setRegisterFileEnabled(bool iRegisterFileEnabled) = 0;
120+
121+
///<summary>
122+
///Returns true if the generated file should be registated FileManager should be used in generated code.
123+
///</summary>
124+
///<returns>Returns true if the FileManager should be used in generated code. Returns false otherwise.</returns>
125+
virtual bool isRegisterFileEnabled() const = 0;
126+
103127
///<summary>
104128
///Defines the different type of cpp encoding.
105129
///See setCppEncoder() and getCppEncoder() functions.
@@ -136,6 +160,20 @@ namespace bin2cpp
136160
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
137161
virtual bool createCppSourceFile(const char * iCppFilePath) = 0;
138162

163+
///<summary>
164+
///Creates a FileManager header file.
165+
///</summary>
166+
///<param name="iHeaderFilePath">The path of the header file (*.h).</param>
167+
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
168+
virtual bool createManagerHeaderFile(const char * iHeaderFilePath) = 0;
169+
170+
///<summary>
171+
///Creates a FileManager cpp file.
172+
///</summary>
173+
///<param name="iHeaderFilePath">The path of the cpp file (*.h).</param>
174+
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
175+
virtual bool createManagerSourceFile(const char * iCppFilePath) = 0;
176+
139177
};
140178

141179
}; //bin2cpp

src/bin2cpp/SegmentGenerator.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ namespace bin2cpp
7878
//size_t numSegments = fileSize/iChunkSize + (lastSegmentSize == 0 ? 0 : 1);
7979

8080
//Build class name
81-
std::string className;
82-
className.append(functionIdentifier.c_str());
83-
className.append("File");
81+
std::string className = getClassName();
8482

8583
//Build function
8684
std::string getterFunctionName = getGetterFunctionName();
8785

86+
//Build FileManager class template
87+
std::string manager = getManagerHeaderFile();
88+
8889
//write cpp file heading
8990
fprintf(cpp, "%s", getHeaderTemplate().c_str());
9091
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
@@ -143,6 +144,11 @@ namespace bin2cpp
143144
fprintf(cpp, " std::string mBuffer;\n");
144145
fprintf(cpp, " };\n");
145146
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
147+
if (isRegisterFileEnabled())
148+
{
149+
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
150+
fprintf(cpp, "%s", fileManagerTemplate.c_str());
151+
}
146152
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
147153

148154
fclose(input);

src/bin2cpp/StringGenerator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ namespace bin2cpp
7676
std::string filename = ra::filesystem::GetFilename(mInputFile.c_str());
7777

7878
//Build class name
79-
std::string className;
80-
className.append(functionIdentifier.c_str());
81-
className.append("File");
79+
std::string className = getClassName();
8280

8381
//Build function
8482
std::string getterFunctionName = getGetterFunctionName();

0 commit comments

Comments
 (0)