Skip to content

Commit 2e1afb5

Browse files
committed
Created Context class for holding generation parameters. Moved from a inherited methods in the BaseGenerator design (inheritance) to a composition design where the Context is given to the generator.
1 parent 3372492 commit 2e1afb5

File tree

10 files changed

+293
-723
lines changed

10 files changed

+293
-723
lines changed

src/bin2cpp/ArrayGenerator.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ namespace bin2cpp
5050
bool ArrayGenerator::createCppSourceFile(const char * cpp_file_path)
5151
{
5252
//check if input file exists
53-
FILE * input = fopen(getInputFilePath(), "rb");
53+
FILE * input = fopen(mContext.inputFilePath.c_str(), "rb");
5454
if (!input)
5555
return false;
5656

5757
//Uppercase function identifier
58-
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mFunctionIdentifier);
58+
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(getContext().functionIdentifier);
5959

6060
//Build header and cpp file path
6161
std::string headerPath = getHeaderFilePath(cpp_file_path);
@@ -71,7 +71,7 @@ namespace bin2cpp
7171

7272
//determine file properties
7373
uint32_t fileSize = ra::filesystem::GetFileSize(input);
74-
std::string filename = ra::filesystem::GetFilename(getInputFilePath());
74+
std::string filename = ra::filesystem::GetFilename(mContext.inputFilePath.c_str());
7575

7676
//Build class name
7777
std::string className = getClassName();
@@ -81,12 +81,12 @@ namespace bin2cpp
8181

8282
//write cpp file heading
8383
fprintf(cpp, "%s", getHeaderTemplate().c_str());
84-
fprintf(cpp, "#include \"%s\"\n", getHeaderFilename() );
84+
fprintf(cpp, "#include \"%s\"\n", mContext.headerFilename.c_str() );
8585
fprintf(cpp, "#include <iostream>\n");
8686
fprintf(cpp, "#include <fstream> //for ofstream\n");
87-
fprintf(cpp, "namespace %s\n", mNamespace.c_str());
87+
fprintf(cpp, "namespace %s\n", getContext().codeNamespace.c_str());
8888
fprintf(cpp, "{\n");
89-
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), mNamespace.c_str(), mBaseClass.c_str());
89+
fprintf(cpp, " class %s : public virtual %s::%s\n", className.c_str(), getContext().codeNamespace.c_str(), getContext().baseClass.c_str());
9090
fprintf(cpp, " {\n");
9191
fprintf(cpp, " public:\n");
9292
fprintf(cpp, " %s() {}\n", className.c_str());
@@ -100,13 +100,13 @@ namespace bin2cpp
100100

101101
//create buffer for each chunks from input buffer
102102
int numLinePrinted = 0;
103-
unsigned char * buffer = new unsigned char[mChunkSize];
103+
unsigned char * buffer = new unsigned char[getContext().chunkSize];
104104
while(!feof(input))
105105
{
106106
//read a chunk of the file
107-
size_t readSize = fread(buffer, 1, mChunkSize, input);
107+
size_t readSize = fread(buffer, 1, getContext().chunkSize, input);
108108

109-
bool isLastChunk = !(readSize == mChunkSize);
109+
bool isLastChunk = !(readSize == getContext().chunkSize);
110110

111111
if (readSize > 0)
112112
{
@@ -136,13 +136,13 @@ namespace bin2cpp
136136
fprintf(cpp, " }\n");
137137
fprintf(cpp, "%s", getSaveMethodTemplate().c_str());
138138
fprintf(cpp, " };\n");
139-
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", mBaseClass.c_str(), getterFunctionName.c_str(), className.c_str());
140-
if (isRegisterFileEnabled())
139+
fprintf(cpp, " const %s & %s() { static %s _instance; return _instance; }\n", getContext().baseClass.c_str(), getterFunctionName.c_str(), className.c_str());
140+
if (mContext.registerFiles)
141141
{
142142
std::string fileManagerTemplate = getFileManagerRegistrationTemplate();
143143
fprintf(cpp, "%s", fileManagerTemplate.c_str());
144144
}
145-
fprintf(cpp, "}; //%s\n", mNamespace.c_str());
145+
fprintf(cpp, "}; //%s\n", getContext().codeNamespace.c_str());
146146

147147
fclose(input);
148148
fclose(cpp);
@@ -153,7 +153,7 @@ namespace bin2cpp
153153
bool ArrayGenerator::printFileContent()
154154
{
155155
//check if input file exists
156-
FILE * input = fopen(getInputFilePath(), "rb");
156+
FILE * input = fopen(mContext.inputFilePath.c_str(), "rb");
157157
if (!input)
158158
return false;
159159

@@ -162,13 +162,13 @@ namespace bin2cpp
162162

163163
//create buffer for each chunks from input buffer
164164
int numLinePrinted = 0;
165-
unsigned char * buffer = new unsigned char[mChunkSize];
165+
unsigned char * buffer = new unsigned char[getContext().chunkSize];
166166
while(!feof(input))
167167
{
168168
//read a chunk of the file
169-
size_t readSize = fread(buffer, 1, mChunkSize, input);
169+
size_t readSize = fread(buffer, 1, getContext().chunkSize, input);
170170

171-
bool isLastChunk = !(readSize == mChunkSize);
171+
bool isLastChunk = !(readSize == getContext().chunkSize);
172172

173173
if (readSize > 0)
174174
{

0 commit comments

Comments
 (0)