Skip to content

Commit ca7742e

Browse files
committed
Implemented issue #16 - Implement a --namespace argument
Also created --baseclass argument to change the name of the default File class under bin2cpp namespace Created new test to support the new functionality: testNamespace() and testBaseClass()
1 parent 532bee0 commit ca7742e

15 files changed

+209
-33
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changes for 2.0:
66
* Fixed issue #19: Add NSIS as a prerequisite in README and INSTALL
77
* Fixed issue #17: Implement a --help and --h
88
* Fixed issue #5: Fixed issue #5 - Remove the concept of segment
9+
* Fixed issue #16: Implement a --namespace argument
910

1011
Changes for 1.4:
1112

msvc/bin2cpp_unittest.vcxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,15 @@
109109
<ClCompile Include="..\test\TestCLI.cpp" />
110110
<ClCompile Include="..\test\TestCommon.cpp" />
111111
<ClCompile Include="..\test\TestExtraction.cpp" />
112+
<ClCompile Include="generated_files\testBaseClass\_testBaseClass.cpp" />
113+
<ClCompile Include="generated_files\testGeneratorArray10000\_testGeneratorArray10000.cpp" />
114+
<ClCompile Include="generated_files\testGeneratorSegment10000\_testGeneratorSegment10000.cpp" />
115+
<ClCompile Include="generated_files\testGeneratorString10000\_testGeneratorString10000.cpp" />
112116
<ClCompile Include="generated_files\testHtml100000\_testHtml100000.cpp" />
113117
<ClCompile Include="generated_files\testIssue12Mini\_testIssue12Mini.cpp" />
114118
<ClCompile Include="generated_files\testIssue12\_testIssue12.cpp" />
115119
<ClCompile Include="generated_files\testIssue13\_testIssue13.cpp" />
120+
<ClCompile Include="generated_files\testNamespace\_testNamespace.cpp" />
116121
<ClCompile Include="generated_files\testRandom1\_testRandom1.cpp" />
117122
<ClCompile Include="generated_files\testRandom2\_testRandom2.cpp" />
118123
<ClCompile Include="generated_files\testRandom3\_testRandom3.cpp" />
@@ -147,10 +152,15 @@
147152
<ClInclude Include="..\test\TestCLI.h" />
148153
<ClInclude Include="..\test\TestCommon.h" />
149154
<ClInclude Include="..\test\TestExtraction.h" />
155+
<ClInclude Include="generated_files\testBaseClass\_testBaseClass.h" />
156+
<ClInclude Include="generated_files\testGeneratorArray10000\_testGeneratorArray10000.h" />
157+
<ClInclude Include="generated_files\testGeneratorSegment10000\_testGeneratorSegment10000.h" />
158+
<ClInclude Include="generated_files\testGeneratorString10000\_testGeneratorString10000.h" />
150159
<ClInclude Include="generated_files\testHtml100000\_testHtml100000.h" />
151160
<ClInclude Include="generated_files\testIssue12Mini\_testIssue12Mini.h" />
152161
<ClInclude Include="generated_files\testIssue12\_testIssue12.h" />
153162
<ClInclude Include="generated_files\testIssue13\_testIssue13.h" />
163+
<ClInclude Include="generated_files\testNamespace\_testNamespace.h" />
154164
<ClInclude Include="generated_files\testRandom1\_testRandom1.h" />
155165
<ClInclude Include="generated_files\testRandom2\_testRandom2.h" />
156166
<ClInclude Include="generated_files\testRandom3\_testRandom3.h" />

msvc/bin2cpp_unittest.vcxproj.filters

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@
9393
<ClCompile Include="..\test\TestCommon.cpp">
9494
<Filter>Tests</Filter>
9595
</ClCompile>
96+
<ClCompile Include="generated_files\testGeneratorArray10000\_testGeneratorArray10000.cpp">
97+
<Filter>Generated Files</Filter>
98+
</ClCompile>
99+
<ClCompile Include="generated_files\testGeneratorSegment10000\_testGeneratorSegment10000.cpp">
100+
<Filter>Generated Files</Filter>
101+
</ClCompile>
102+
<ClCompile Include="generated_files\testGeneratorString10000\_testGeneratorString10000.cpp">
103+
<Filter>Generated Files</Filter>
104+
</ClCompile>
105+
<ClCompile Include="generated_files\testBaseClass\_testBaseClass.cpp">
106+
<Filter>Generated Files</Filter>
107+
</ClCompile>
108+
<ClCompile Include="generated_files\testNamespace\_testNamespace.cpp">
109+
<Filter>Generated Files</Filter>
110+
</ClCompile>
96111
</ItemGroup>
97112
<ItemGroup>
98113
<None Include="bin2cppTest.x86.debug.xml">
@@ -170,6 +185,21 @@
170185
<ClInclude Include="..\test\TestCommon.h">
171186
<Filter>Tests</Filter>
172187
</ClInclude>
188+
<ClInclude Include="generated_files\testGeneratorArray10000\_testGeneratorArray10000.h">
189+
<Filter>Generated Files</Filter>
190+
</ClInclude>
191+
<ClInclude Include="generated_files\testGeneratorSegment10000\_testGeneratorSegment10000.h">
192+
<Filter>Generated Files</Filter>
193+
</ClInclude>
194+
<ClInclude Include="generated_files\testGeneratorString10000\_testGeneratorString10000.h">
195+
<Filter>Generated Files</Filter>
196+
</ClInclude>
197+
<ClInclude Include="generated_files\testBaseClass\_testBaseClass.h">
198+
<Filter>Generated Files</Filter>
199+
</ClInclude>
200+
<ClInclude Include="generated_files\testNamespace\_testNamespace.h">
201+
<Filter>Generated Files</Filter>
202+
</ClInclude>
173203
</ItemGroup>
174204
<ItemGroup>
175205
<CustomBuild Include="generate_test_files.bat" />

msvc/generate_test_files.bat

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ REM mkdir %OUTDIR% 1>NUL 2>NUL
9696
REM testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=31457280 --fill=sequential
9797
REM bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --override
9898

99+
set TEST_NAME=testGeneratorArray10000
100+
set OUTDIR=.\generated_files\%TEST_NAME%
101+
mkdir %OUTDIR% 1>NUL 2>NUL
102+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=10000 --fill=sequential
103+
bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --generator=array --override
104+
105+
set TEST_NAME=testGeneratorString10000
106+
set OUTDIR=.\generated_files\%TEST_NAME%
107+
mkdir %OUTDIR% 1>NUL 2>NUL
108+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=10000 --fill=sequential
109+
bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --generator=string --override
110+
111+
set TEST_NAME=testGeneratorSegment10000
112+
set OUTDIR=.\generated_files\%TEST_NAME%
113+
mkdir %OUTDIR% 1>NUL 2>NUL
114+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=10000 --fill=sequential
115+
bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --generator=segment --override
116+
117+
set TEST_NAME=testNamespace
118+
set OUTDIR=.\generated_files\%TEST_NAME%
119+
mkdir %OUTDIR% 1>NUL 2>NUL
120+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=10000 --fill=sequential
121+
bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --generator=segment --override --namespace=foobar
122+
123+
set TEST_NAME=testBaseClass
124+
set OUTDIR=.\generated_files\%TEST_NAME%
125+
mkdir %OUTDIR% 1>NUL 2>NUL
126+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=10000 --fill=sequential
127+
bin2cpp.exe --noheader --file=%OUTDIR%\%TEST_NAME%.bin --output=%OUTDIR% --headerfile=_%TEST_NAME%.h --identifier=%TEST_NAME% --chunksize=450 --generator=segment --override --baseclass=Resource
128+
99129
set TEST_NAME=testIssue12
100130
set OUTDIR=.\generated_files\%TEST_NAME%
101131
mkdir %OUTDIR% 1>NUL 2>NUL

src/ArrayGenerator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace bin2cpp
2222
return "array";
2323
}
2424

25-
bool ArrayGenerator::createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize)
25+
bool ArrayGenerator::createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass)
2626
{
2727
//check if input file exists
2828
FILE * input = fopen(iInputFilename, "rb");
@@ -64,9 +64,9 @@ namespace bin2cpp
6464
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
6565
fprintf(cpp, "#include <stdio.h> //for FILE\n");
6666
fprintf(cpp, "#include <string> //for memcpy\n");
67-
fprintf(cpp, "namespace bin2cpp\n");
67+
fprintf(cpp, "namespace %s\n", iNamespace);
6868
fprintf(cpp, "{\n");
69-
fprintf(cpp, " class %s : public virtual bin2cpp::File\n", className.c_str());
69+
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), iNamespace, iBaseClass);
7070
fprintf(cpp, " {\n");
7171
fprintf(cpp, " public:\n");
7272
fprintf(cpp, " %s() {}\n", className.c_str());
@@ -115,8 +115,8 @@ namespace bin2cpp
115115
fprintf(cpp, " }\n");
116116
fprintf(cpp, "%s", getSaveMethodImplementation().c_str());
117117
fprintf(cpp, " };\n");
118-
fprintf(cpp, " const File & %s() { static %s _instance; return _instance; }\n", getterFunctionName.c_str(), className.c_str());
119-
fprintf(cpp, "}; //bin2cpp\n");
118+
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", iBaseClass, getterFunctionName.c_str(), className.c_str());
119+
fprintf(cpp, "}; //%s\n", iNamespace);
120120

121121
fclose(input);
122122
fclose(cpp);

src/ArrayGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace bin2cpp
1515
ArrayGenerator();
1616
virtual ~ArrayGenerator();
1717
virtual const char * getName() const;
18-
virtual bool createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize);
18+
virtual bool createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass);
1919
};
2020

2121
}; //bin2cpp

src/BaseGenerator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace bin2cpp
7878
return output;
7979
}
8080

81-
bool BaseGenerator::createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier)
81+
bool BaseGenerator::createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass)
8282
{
8383
FILE * header = fopen(iHeaderFilePath, "w");
8484
if (!header)
@@ -88,11 +88,11 @@ namespace bin2cpp
8888
fprintf(header, "%s", headercomments.c_str());
8989
fprintf(header, "#pragma once\n");
9090
fprintf(header, "#include <stddef.h>\n");
91-
fprintf(header, "namespace bin2cpp\n");
91+
fprintf(header, "namespace %s\n", iNamespace);
9292
fprintf(header, "{\n");
9393
fprintf(header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n");
9494
fprintf(header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n");
95-
fprintf(header, " class File\n");
95+
fprintf(header, " class %s\n", iBaseClass);
9696
fprintf(header, " {\n");
9797
fprintf(header, " public:\n");
9898
fprintf(header, " virtual size_t getSize() const = 0;\n");
@@ -101,8 +101,8 @@ namespace bin2cpp
101101
fprintf(header, " virtual bool save(const char * iFilename) const = 0;\n");
102102
fprintf(header, " };\n");
103103
fprintf(header, " #endif\n");
104-
fprintf(header, " const File & %s();\n", getGetterFunctionName(iFunctionIdentifier).c_str());
105-
fprintf(header, "}; //bin2cpp\n");
104+
fprintf(header, " const %s & %s();\n", iBaseClass, getGetterFunctionName(iFunctionIdentifier).c_str());
105+
fprintf(header, "}; //%s\n", iNamespace);
106106

107107
fclose(header);
108108

src/BaseGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace bin2cpp
1515
virtual ~BaseGenerator();
1616

1717
//same header file for all generators
18-
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier);
18+
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass);
1919

2020
protected:
2121
virtual std::string getGetterFunctionName(const char * iFunctionIdentifier);

src/IGenerator.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ namespace bin2cpp
2121
///<param name="iInputFilename">The path of the input file (resource) to embeded as a C++ source code.</param>
2222
///<param name="iHeaderFilePath">The path of the header file (*.h) that is used for getting access to the functions.</param>
2323
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
24+
///<param name="iChunkSize">The size in bytes of each chunk of data.</param>
25+
///<param name="iNamespace">The namespace of the generated source code.</param>
26+
///<param name="iBaseClass">The name of the interface for embedded files.</param>
2427
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
25-
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier) = 0;
28+
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass) = 0;
2629

2730
///<summary>
2831
///Creates a cpp source file for embedding a given file into C++ source code.
@@ -31,8 +34,10 @@ namespace bin2cpp
3134
///<param name="iCppFilePath">The path of the source file (*.cpp) that is used for getting access to the functions.</param>
3235
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
3336
///<param name="iChunkSize">The size in bytes of each chunk of data.</param>
37+
///<param name="iNamespace">The namespace of the generated source code.</param>
38+
///<param name="iBaseClass">The name of the interface for embedded files.</param>
3439
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
35-
virtual bool createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize) = 0;
40+
virtual bool createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass) = 0;
3641
};
3742

3843
}; //bin2cpp

src/SegmentGenerator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace bin2cpp
2222
return "segment";
2323
}
2424

25-
bool SegmentGenerator::createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize)
25+
bool SegmentGenerator::createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass)
2626
{
2727
//check if input file exists
2828
FILE * input = fopen(iInputFilename, "rb");
@@ -66,9 +66,9 @@ namespace bin2cpp
6666
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
6767
fprintf(cpp, "#include <stdio.h> //for FILE\n");
6868
fprintf(cpp, "#include <string> //for memcpy\n");
69-
fprintf(cpp, "namespace bin2cpp\n");
69+
fprintf(cpp, "namespace %s\n", iNamespace);
7070
fprintf(cpp, "{\n");
71-
fprintf(cpp, " class %s : public virtual bin2cpp::File\n", className.c_str());
71+
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), iNamespace, iBaseClass);
7272
fprintf(cpp, " {\n");
7373
fprintf(cpp, " public:\n");
7474
fprintf(cpp, " %s() { build(); }\n", className.c_str());
@@ -105,8 +105,8 @@ namespace bin2cpp
105105
fprintf(cpp, " private:\n");
106106
fprintf(cpp, " std::string mBuffer;\n");
107107
fprintf(cpp, " };\n");
108-
fprintf(cpp, " const File & %s() { static %s _instance; return _instance; }\n", getterFunctionName.c_str(), className.c_str());
109-
fprintf(cpp, "}; //bin2cpp\n");
108+
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", iBaseClass, getterFunctionName.c_str(), className.c_str());
109+
fprintf(cpp, "}; //%s\n", iNamespace);
110110

111111
fclose(input);
112112
fclose(cpp);

0 commit comments

Comments
 (0)