Skip to content

Commit dec8e73

Browse files
committed
* Fixed issue #22: Modify IGenerator interface to use setters/getters.
* Renamed BaseGenerator::getFileHeading() to getHeaderTemplate() * Renamed BaseGenerator::getSaveMethodImplementation() to getSaveMethodTemplate()
1 parent 8e53178 commit dec8e73

File tree

11 files changed

+217
-81
lines changed

11 files changed

+217
-81
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Changes for 2.0:
1111
* Fixed issue #19: Add NSIS as a prerequisite in README and INSTALL
1212
* Fixed issue #20: Modify how 'override' argument is implemented
1313
* Fixed issue #21: Add a reference to source code location in generated files.
14+
* Fixed issue #22: Modify IGenerator interface to use setters/getters.
1415

1516
Changes for 1.4:
1617

src/ArrayGenerator.cpp

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

25-
bool ArrayGenerator::createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass)
25+
bool ArrayGenerator::createCppSourceFile(const char * iCppFilePath)
2626
{
2727
//check if input file exists
28-
FILE * input = fopen(iInputFilename, "rb");
28+
FILE * input = fopen(mInputFile.c_str(), "rb");
2929
if (!input)
3030
return false;
3131

3232
//Uppercase function identifier
33-
std::string functionIdentifier = iFunctionIdentifier;
33+
std::string functionIdentifier = mFunctionIdentifier;
3434
functionIdentifier[0] = (char)toupper(functionIdentifier[0]);
3535

3636
//Build header and cpp file path
@@ -49,43 +49,43 @@ namespace bin2cpp
4949

5050
//determine file properties
5151
long fileSize = getFileSize(input);
52-
std::string filename = getFilename(iInputFilename);
52+
std::string filename = getFilename(mInputFile.c_str());
5353

5454
//Build class name
5555
std::string className;
5656
className.append(functionIdentifier.c_str());
5757
className.append("File");
5858

5959
//Build function
60-
std::string getterFunctionName = getGetterFunctionName(functionIdentifier.c_str());
60+
std::string getterFunctionName = getGetterFunctionName();
6161

6262
//write cpp file heading
63-
fprintf(cpp, "%s", getFileHeading(iInputFilename).c_str());
63+
fprintf(cpp, "%s", getHeaderTemplate().c_str());
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 %s\n", iNamespace);
67+
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
6868
fprintf(cpp, "{\n");
69-
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), iNamespace, iBaseClass);
69+
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), mNamespace.c_str(), mBaseClass.c_str());
7070
fprintf(cpp, " {\n");
7171
fprintf(cpp, " public:\n");
7272
fprintf(cpp, " %s() {}\n", className.c_str());
7373
fprintf(cpp, " ~%s() {}\n", className.c_str());
7474
fprintf(cpp, " virtual size_t getSize() const { return %d; }\n", fileSize);
75-
fprintf(cpp, " virtual const char * getFilename() const { return \"%s\"; }\n", getFilename(iInputFilename).c_str());
75+
fprintf(cpp, " virtual const char * getFilename() const { return \"%s\"; }\n", getFilename(mInputFile.c_str()).c_str());
7676
fprintf(cpp, " virtual const char * getBuffer() const\n");
7777
fprintf(cpp, " {\n");
7878
fprintf(cpp, " static const unsigned char buffer[] = {\n");
7979

8080
//create buffer for each chunks from input buffer
8181
int numLinePrinted = 0;
82-
unsigned char * buffer = new unsigned char[iChunkSize];
82+
unsigned char * buffer = new unsigned char[mChunkSize];
8383
while(!feof(input))
8484
{
8585
//read a chunk of the file
86-
size_t readSize = fread(buffer, 1, iChunkSize, input);
86+
size_t readSize = fread(buffer, 1, mChunkSize, input);
8787

88-
bool isLastChunk = !(readSize == iChunkSize);
88+
bool isLastChunk = !(readSize == mChunkSize);
8989

9090
if (readSize > 0)
9191
{
@@ -113,10 +113,10 @@ namespace bin2cpp
113113
//write cpp file footer
114114
fprintf(cpp, " return (const char *)buffer;\n");
115115
fprintf(cpp, " }\n");
116-
fprintf(cpp, "%s", getSaveMethodImplementation().c_str());
116+
fprintf(cpp, "%s", getSaveMethodTemplate().c_str());
117117
fprintf(cpp, " };\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);
118+
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
119+
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
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, const char * iNamespace, const char * iBaseClass);
18+
virtual bool createCppSourceFile(const char * iCppFilePath);
1919
};
2020

2121
}; //bin2cpp

src/BaseGenerator.cpp

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,68 @@ namespace bin2cpp
1616
{
1717
}
1818

19-
std::string BaseGenerator::getGetterFunctionName(const char * iFunctionIdentifier)
19+
void BaseGenerator::setInputFile(const char * iPath)
20+
{
21+
if (iPath)
22+
mInputFile = iPath;
23+
}
24+
25+
const char * BaseGenerator::getInputFile() const
26+
{
27+
return mInputFile.c_str();
28+
}
29+
30+
void BaseGenerator::setFunctionIdentifier(const char * iFunctionIdentifier)
31+
{
32+
if (iFunctionIdentifier)
33+
mFunctionIdentifier = iFunctionIdentifier;
34+
}
35+
36+
const char * BaseGenerator::getFunctionIdentifier() const
37+
{
38+
return mFunctionIdentifier.c_str();
39+
}
40+
41+
void BaseGenerator::setChunkSize(size_t iChunkSize)
42+
{
43+
mChunkSize = iChunkSize;
44+
}
45+
46+
size_t BaseGenerator::getChunkSize() const
47+
{
48+
return mChunkSize;
49+
}
50+
51+
void BaseGenerator::setNamespace(const char * iName)
52+
{
53+
if (iName)
54+
mNamespace = iName;
55+
}
56+
57+
const char * BaseGenerator::getNamespace() const
58+
{
59+
return mNamespace.c_str();
60+
}
61+
62+
void BaseGenerator::setBaseClass(const char * iName)
63+
{
64+
if (iName)
65+
mBaseClass = iName;
66+
}
67+
68+
const char * BaseGenerator::getBaseClass() const
69+
{
70+
return mBaseClass.c_str();
71+
}
72+
73+
//-------------------------------
74+
//protected methods
75+
//-------------------------------
76+
77+
std::string BaseGenerator::getGetterFunctionName()
2078
{
2179
//Uppercase function identifier
22-
std::string functionIdentifier = iFunctionIdentifier;
80+
std::string functionIdentifier = mFunctionIdentifier;
2381
functionIdentifier[0] = (char)toupper(functionIdentifier[0]);
2482

2583
std::string getter;
@@ -45,10 +103,10 @@ namespace bin2cpp
45103
return cppPath;
46104
}
47105

48-
std::string BaseGenerator::getFileHeading(const char * iInputFilename)
106+
std::string BaseGenerator::getHeaderTemplate()
49107
{
50-
std::string filename = getFilename(iInputFilename);
51-
uint64_t lastModifiedDate = getFileModifiedDate(iInputFilename);
108+
std::string filename = getFilename(mInputFile.c_str());
109+
uint64_t lastModifiedDate = getFileModifiedDate(mInputFile);
52110

53111
std::string header;
54112
header <<
@@ -62,7 +120,7 @@ namespace bin2cpp
62120
return header;
63121
}
64122

65-
std::string BaseGenerator::getSaveMethodImplementation()
123+
std::string BaseGenerator::getSaveMethodTemplate()
66124
{
67125
std::string output;
68126
output << " virtual bool save(const char * iFilename) const\n";
@@ -78,21 +136,21 @@ namespace bin2cpp
78136
return output;
79137
}
80138

81-
bool BaseGenerator::createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass)
139+
bool BaseGenerator::createCppHeaderFile(const char * iHeaderFilePath)
82140
{
83141
FILE * header = fopen(iHeaderFilePath, "w");
84142
if (!header)
85143
return false;
86144

87-
std::string headercomments = getFileHeading(iInputFilename);
145+
std::string headercomments = getHeaderTemplate();
88146
fprintf(header, "%s", headercomments.c_str());
89147
fprintf(header, "#pragma once\n");
90148
fprintf(header, "#include <stddef.h>\n");
91-
fprintf(header, "namespace %s\n", iNamespace);
149+
fprintf(header, "namespace %s\n", mNamespace.c_str());
92150
fprintf(header, "{\n");
93151
fprintf(header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n");
94152
fprintf(header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n");
95-
fprintf(header, " class %s\n", iBaseClass);
153+
fprintf(header, " class %s\n", mBaseClass.c_str());
96154
fprintf(header, " {\n");
97155
fprintf(header, " public:\n");
98156
fprintf(header, " virtual size_t getSize() const = 0;\n");
@@ -101,8 +159,8 @@ namespace bin2cpp
101159
fprintf(header, " virtual bool save(const char * iFilename) const = 0;\n");
102160
fprintf(header, " };\n");
103161
fprintf(header, " #endif\n");
104-
fprintf(header, " const %s & %s();\n", iBaseClass, getGetterFunctionName(iFunctionIdentifier).c_str());
105-
fprintf(header, "}; //%s\n", iNamespace);
162+
fprintf(header, " const %s & %s();\n", mBaseClass.c_str(), getGetterFunctionName().c_str());
163+
fprintf(header, "}; //%s\n", mNamespace.c_str());
106164

107165
fclose(header);
108166

src/BaseGenerator.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,34 @@ namespace bin2cpp
1414
BaseGenerator();
1515
virtual ~BaseGenerator();
1616

17+
//IGenerator methods
18+
virtual void setInputFile(const char * iPath);
19+
virtual const char * getInputFile() const;
20+
virtual void setFunctionIdentifier(const char * iFunctionIdentifier);
21+
virtual const char * getFunctionIdentifier() const;
22+
virtual void setChunkSize(size_t iChunkSize);
23+
virtual size_t getChunkSize() const;
24+
virtual void setNamespace(const char * iName);
25+
virtual const char * getNamespace() const;
26+
virtual void setBaseClass(const char * iName);
27+
virtual const char * getBaseClass() const;
28+
1729
//same header file for all generators
18-
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass);
30+
virtual bool createCppHeaderFile(const char * iHeaderFilePath);
1931

2032
protected:
21-
virtual std::string getGetterFunctionName(const char * iFunctionIdentifier);
33+
virtual std::string getGetterFunctionName();
2234
virtual std::string getHeaderFilePath(const char * iCppFilePath);
2335
virtual std::string getCppFilePath(const char * iHeaderFilePath);
24-
virtual std::string getFileHeading(const char * iInputFilename);
25-
virtual std::string getSaveMethodImplementation();
36+
virtual std::string getHeaderTemplate();
37+
virtual std::string getSaveMethodTemplate();
38+
39+
//attributes
40+
std::string mInputFile;
41+
std::string mFunctionIdentifier;
42+
size_t mChunkSize;
43+
std::string mNamespace;
44+
std::string mBaseClass;
2645
};
2746

2847
}; //bin2cpp

src/IGenerator.h

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,80 @@ namespace bin2cpp
1515
///<returns>Returns the name of the generator</returns>
1616
virtual const char * getName() const = 0;
1717

18+
///<summary>
19+
///Defines the path of the binary input file.
20+
///</summary>
21+
///<param name="iPath">The path of the input file (resource) to embeded as a C++ source code.</param>
22+
virtual void setInputFile(const char * iPath) = 0;
23+
24+
///<summary>
25+
///Provides the path of the binary input file.
26+
///</summary>
27+
///<returns>Returns the path of the binary input file. Returns an empty string if not defined.</returns>
28+
virtual const char * getInputFile() const = 0;
29+
30+
///<summary>
31+
///Defines the unique identifier name for the File class getter function.
32+
///</summary>
33+
///<param name="iFunctionIdentifier">The unique identifier name for the File class getter function.</param>
34+
virtual void setFunctionIdentifier(const char * iFunctionIdentifier) = 0;
35+
36+
///<summary>
37+
///Provides the unique identifier name for the File class getter function.
38+
///</summary>
39+
///<returns>Returns the unique identifier name for the File class getter function. Returns an empty string if not defined.</returns>
40+
virtual const char * getFunctionIdentifier() const = 0;
41+
42+
///<summary>
43+
///Defines the size in bytes of each chunk of data.
44+
///</summary>
45+
///<param name="iChunkSize">The size in bytes of each chunk of data.</param>
46+
virtual void setChunkSize(size_t iChunkSize) = 0;
47+
48+
///<summary>
49+
///Provides the size in bytes of each chunk of data.
50+
///</summary>
51+
///<returns>Returns the unique identifier name for the File class getter function. Returns an empty string if not defined.</returns>
52+
virtual size_t getChunkSize() const = 0;
53+
54+
///<summary>
55+
///Defines the namespace of the generated code.
56+
///</summary>
57+
///<param name="iName">The name of the namespace of the generated code.</param>
58+
virtual void setNamespace(const char * iName) = 0;
59+
60+
///<summary>
61+
///Provides the namespace of the generated code.
62+
///</summary>
63+
///<returns>Returns the name of the namespace of the generated code. Returns an empty string if not defined.</returns>
64+
virtual const char * getNamespace() const = 0;
65+
66+
///<summary>
67+
///Defines the base class of the generated code.
68+
///</summary>
69+
///<param name="iName">The name of the base class of the generated code.</param>
70+
virtual void setBaseClass(const char * iName) = 0;
71+
72+
///<summary>
73+
///Provides the base class of the generated code.
74+
///</summary>
75+
///<returns>Returns the name of the base class of the generated code. Returns an empty string if not defined.</returns>
76+
virtual const char * getBaseClass() const = 0;
77+
1878
///<summary>
1979
///Creates a header file for embedding a given file into C++ source code.
2080
///</summary>
21-
///<param name="iInputFilename">The path of the input file (resource) to embeded as a C++ source code.</param>
2281
///<param name="iHeaderFilePath">The path of the header file (*.h) that is used for getting access to the functions.</param>
23-
///<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>
2782
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
28-
virtual bool createCppHeaderFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass) = 0;
83+
virtual bool createCppHeaderFile(const char * iHeaderFilePath) = 0;
2984

3085
///<summary>
3186
///Creates a cpp source file for embedding a given file into C++ source code.
3287
///</summary>
33-
///<param name="iInputFilename">The path of the input file (resource) to embeded as a C++ source code.</param>
3488
///<param name="iCppFilePath">The path of the source file (*.cpp) that is used for getting access to the functions.</param>
35-
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
36-
///<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>
3989
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
40-
virtual bool createCppSourceFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize, const char * iNamespace, const char * iBaseClass) = 0;
90+
virtual bool createCppSourceFile(const char * iCppFilePath) = 0;
91+
4192
};
4293

4394
}; //bin2cpp

0 commit comments

Comments
 (0)