Skip to content

Commit aaa1ec6

Browse files
committed
First draft for issue #28
1 parent 62dd95d commit aaa1ec6

14 files changed

+357
-15
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;

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: 168 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::setManagerEnabled(bool iManagerEnabled)
124+
{
125+
mManagerEnabled = iManagerEnabled;
126+
}
127+
128+
bool BaseGenerator::isManagerEnabled() const
129+
{
130+
return mManagerEnabled;
131+
}
132+
111133
//-------------------------------
112134
//protected methods
113135
//-------------------------------
@@ -173,6 +195,29 @@ namespace bin2cpp
173195
return output;
174196
}
175197

198+
std::string BaseGenerator::getFileManagerTemplate()
199+
{
200+
if (!this->isManagerEnabled())
201+
return std::string();
202+
203+
//Build class name
204+
std::string className = getClassName();
205+
206+
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";
209+
return output;
210+
}
211+
212+
std::string BaseGenerator::getClassName()
213+
{
214+
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mFunctionIdentifier);
215+
std::string className;
216+
className.append(functionIdentifier.c_str());
217+
className.append("File");
218+
return className;
219+
}
220+
176221
bool BaseGenerator::createCppHeaderFile(const char * iHeaderFilePath)
177222
{
178223
FILE * header = fopen(iHeaderFilePath, "w");
@@ -212,4 +257,126 @@ namespace bin2cpp
212257
return true;
213258
}
214259

260+
bool BaseGenerator::createManagerHeaderFile(const char * iHeaderFilePath)
261+
{
262+
FILE * header = fopen(iHeaderFilePath, "w");
263+
if (!header)
264+
return false;
265+
266+
//define macro guard a macro matching the filename
267+
std::string macro_guard = getCppIncludeGuardMacroName(iHeaderFilePath);
268+
269+
std::string headercomments = getHeaderTemplate();
270+
fprintf(header, "%s", headercomments.c_str());
271+
fprintf(header, "#ifndef %s\n", macro_guard.c_str());
272+
fprintf(header, "#define %s\n", macro_guard.c_str());
273+
fprintf(header, "\n");
274+
fprintf(header, "#include <stddef.h>\n");
275+
fprintf(header, "#include <vector>\n");
276+
fprintf(header, "\n");
277+
fprintf(header, "namespace %s\n", mNamespace.c_str());
278+
fprintf(header, "{\n");
279+
fprintf(header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n");
280+
fprintf(header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n");
281+
fprintf(header, " class %s\n", mBaseClass.c_str());
282+
fprintf(header, " {\n");
283+
fprintf(header, " public:\n");
284+
fprintf(header, " virtual size_t getSize() const = 0;\n");
285+
fprintf(header, " virtual const char * getFilename() const = 0;\n");
286+
fprintf(header, " virtual const char * getBuffer() const = 0;\n");
287+
fprintf(header, " virtual bool save(const char * iFilename) const = 0;\n");
288+
fprintf(header, " };\n");
289+
fprintf(header, " #endif //BIN2CPP_EMBEDDEDFILE_CLASS\n");
290+
fprintf(header, "\n");
291+
fprintf(header, " #ifndef BIN2CPP_FILEMANAGER_CLASS\n");
292+
fprintf(header, " #define BIN2CPP_FILEMANAGER_CLASS\n");
293+
fprintf(header, " class FileManager\n");
294+
fprintf(header, " {\n");
295+
fprintf(header, " private:\n");
296+
fprintf(header, " FileManager();\n");
297+
fprintf(header, " ~FileManager();\n");
298+
fprintf(header, " public:\n");
299+
fprintf(header, " typedef const %s & (*t_func)();\n", mBaseClass.c_str());
300+
fprintf(header, " static FileManager & getInstance();\n");
301+
fprintf(header, " void registerFile(t_func iFunctionPtr);\n");
302+
fprintf(header, " size_t getFileCount() const;\n");
303+
fprintf(header, " const %s * getFile(const size_t & index) const;\n", mBaseClass.c_str());
304+
fprintf(header, " bool saveFiles(const char * iDirectory) const;\n");
305+
fprintf(header, " private:\n");
306+
fprintf(header, " std::vector<t_func> functions_;\n");
307+
fprintf(header, " };\n");
308+
fprintf(header, " #endif //BIN2CPP_FILEMANAGER_CLASS\n");
309+
fprintf(header, "}; //%s\n", mNamespace.c_str());
310+
fprintf(header, "\n");
311+
fprintf(header, "#endif //%s\n", macro_guard.c_str());
312+
313+
fclose(header);
314+
315+
return true;
316+
}
317+
318+
bool BaseGenerator::createManagerSourceFile(const char * iCppFilePath)
319+
{
320+
FILE * cpp = fopen(iCppFilePath, "w");
321+
if (!cpp)
322+
return false;
323+
324+
//Build header and cpp file path
325+
std::string headerPath = getHeaderFilePath(iCppFilePath);
326+
std::string cppPath = iCppFilePath;
327+
std::string headerFilename = ra::filesystem::GetFilename(headerPath.c_str());
328+
std::string cppFilename = ra::filesystem::GetFilename(iCppFilePath);
329+
330+
std::string headercomments = getHeaderTemplate();
331+
fprintf(cpp, "%s", headercomments.c_str());
332+
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str());
333+
fprintf(cpp, "#include <string>\n", headerFilename.c_str());
334+
fprintf(cpp, "\n");
335+
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
336+
fprintf(cpp, "{\n");
337+
fprintf(cpp, " FileManager::FileManager() {}\n");
338+
fprintf(cpp, " FileManager::~FileManager() {}\n");
339+
fprintf(cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n");
340+
fprintf(cpp, " void FileManager::registerFile(t_func iFunctionPtr) { functions_.push_back(iFunctionPtr); }\n");
341+
fprintf(cpp, " size_t FileManager::getFileCount() const { return functions_.size(); }\n");
342+
fprintf(cpp, " const File * FileManager::getFile(const size_t & index) const\n");
343+
fprintf(cpp, " {\n");
344+
fprintf(cpp, " if (index >= functions_.size())\n");
345+
fprintf(cpp, " return NULL;\n");
346+
fprintf(cpp, " t_func ressource_getter_function = functions_[index];\n");
347+
fprintf(cpp, " const bin2cpp::File & resource = ressource_getter_function();\n");
348+
fprintf(cpp, " return &resource;\n");
349+
fprintf(cpp, " }\n");
350+
fprintf(cpp, " bool FileManager::saveFiles(const char * iDirectory) const\n");
351+
fprintf(cpp, " {\n");
352+
fprintf(cpp, " if (iDirectory == NULL)\n");
353+
fprintf(cpp, " return false;\n");
354+
fprintf(cpp, " size_t count = getFileCount();\n");
355+
fprintf(cpp, " for(size_t i=0; i<count; i++)\n");
356+
fprintf(cpp, " {\n");
357+
fprintf(cpp, " const File * f = getFile(i);\n");
358+
fprintf(cpp, " if (!f)\n");
359+
fprintf(cpp, " return false;\n");
360+
fprintf(cpp, " std::string path;\n");
361+
fprintf(cpp, " path.append(iDirectory);\n");
362+
fprintf(cpp, " #ifdef _WIN32\n");
363+
fprintf(cpp, " path.append(1, '\\\\');\n");
364+
fprintf(cpp, " #else\n");
365+
fprintf(cpp, " path.append(1, '/');\n");
366+
fprintf(cpp, " #endif\n");
367+
fprintf(cpp, " path.append(f->getFilename());\n");
368+
fprintf(cpp, " bool saved = f->save(path.c_str());\n");
369+
fprintf(cpp, " if (!saved)\n");
370+
fprintf(cpp, " return false;\n");
371+
fprintf(cpp, " }\n");
372+
fprintf(cpp, " return true;\n");
373+
fprintf(cpp, " }\n");
374+
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
375+
fprintf(cpp, "\n");
376+
377+
fclose(cpp);
378+
379+
return true;
380+
}
381+
215382
}; //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 setManagerEnabled(bool iManagerEnabled);
58+
virtual bool isManagerEnabled() 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 getFileManagerTemplate();
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 usage of the FileManager in generated code.
117+
///</summary>
118+
///<param name="iFileManagerEnabled">The new value of the flag.</param>
119+
virtual void setManagerEnabled(bool iManagerEnabled) = 0;
120+
121+
///<summary>
122+
///Returns true if the 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 isManagerEnabled() 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,21 @@ 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() );
92+
if (isManagerEnabled())
93+
{
94+
fprintf(cpp, "#include \"%s\"\n", manager.c_str() );
95+
}
9196
fprintf(cpp, "#include <stdio.h> //for FILE\n");
9297
fprintf(cpp, "#include <string> //for memcpy\n");
9398
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
@@ -143,6 +148,11 @@ namespace bin2cpp
143148
fprintf(cpp, " std::string mBuffer;\n");
144149
fprintf(cpp, " };\n");
145150
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
151+
if (isManagerEnabled())
152+
{
153+
std::string fileManagerTemplate = getFileManagerTemplate();
154+
fprintf(cpp, "%s", fileManagerTemplate.c_str());
155+
}
146156
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
147157

148158
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();

src/bin2cpp/Win32ResourceGenerator.cpp

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

8686
//Build class name
87-
std::string className;
88-
className.append(functionIdentifier.c_str());
89-
className.append("File");
87+
std::string className = getClassName();
9088

9189
//Build function
9290
std::string getterFunctionName = getGetterFunctionName();

src/bin2cpp/bin2cpp.samples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
--file=C:\Temp\foobar.bin --output=C:\Temp --headerfile=_testFoo.h --identifier=testFoo --override
44
--file=C:\Temp\foobar.bin --output=C:\Temp --headerfile=_testFoo.h --identifier=testFoo --override --noheader
55
--dir=D:\Temp\foobar --output=C:\Temp\foobar --override �noheader
6+
--file=..\..\test\bin2cpp_unittest\generated_files\testIssue28\testIssue28.bin --output=..\..\test\bin2cpp_unittest\generated_files\testIssue28 --headerfile=_testIssue28.h --identifier=testIssue28 --managerfile=FileManager.h --override
7+
--file=..\..\test\bin2cpp_unittest\generated_files\testIssue28\testIssue28.bin --output=..\..\test\bin2cpp_unittest\generated_files\testIssue28 --headerfile=_testIssue28.h --identifier=testIssue28 --managerfile=FileManager.h --override

0 commit comments

Comments
 (0)