Skip to content

Commit 36ceecf

Browse files
committed
* Created new API BaseGenerator::getSaveMethodImplementation()
* Modified all generators to use the new function.
1 parent 5ab875b commit 36ceecf

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

src/ArrayGenerator.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,7 @@ namespace bin2cpp
129129
fprintf(cpp, " return (const char *)buffer;\n");
130130
fprintf(cpp, " }\n");
131131
fprintf(cpp, " virtual const char * getMd5() const { return \"%s\"; }\n", md5String.c_str() );
132-
fprintf(cpp, " virtual bool save(const char * iFilename) const\n");
133-
fprintf(cpp, " {\n");
134-
fprintf(cpp, " FILE * f = fopen(iFilename, \"wb\");\n");
135-
fprintf(cpp, " if (!f) return false;\n");
136-
fprintf(cpp, " size_t fileSize = getSize();\n");
137-
fprintf(cpp, " const char * buffer = getBuffer();\n");
138-
fprintf(cpp, " fwrite(buffer, 1, fileSize, f);\n");
139-
fprintf(cpp, " fclose(f);\n");
140-
fprintf(cpp, " return true;\n");
141-
fprintf(cpp, " }\n");
132+
fprintf(cpp, "%s", getSaveMethodImplementation().c_str());
142133
fprintf(cpp, " };\n");
143134
fprintf(cpp, " const File & %s() { static %s _instance; return _instance; }\n", getterFunctionName.c_str(), className.c_str());
144135
fprintf(cpp, "}; //bin2cpp\n");

src/BaseGenerator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ namespace bin2cpp
6161
return header;
6262
}
6363

64+
std::string BaseGenerator::getSaveMethodImplementation()
65+
{
66+
std::string output;
67+
output << " virtual bool save(const char * iFilename) const\n";
68+
output << " {\n";
69+
output << " FILE * f = fopen(iFilename, \"wb\");\n";
70+
output << " if (!f) return false;\n";
71+
output << " size_t fileSize = getSize();\n";
72+
output << " const char * buffer = getBuffer();\n";
73+
output << " fwrite(buffer, 1, fileSize, f);\n";
74+
output << " fclose(f);\n";
75+
output << " return true;\n";
76+
output << " }\n";
77+
return output;
78+
}
79+
6480
bin2cpp::ErrorCodes BaseGenerator::createHeaderEmbededFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier)
6581
{
6682
FILE * header = fopen(iHeaderFilePath, "w");

src/BaseGenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace bin2cpp
2222
virtual std::string getHeaderFilePath(const char * iCppFilePath);
2323
virtual std::string getCppFilePath(const char * iHeaderFilePath);
2424
virtual std::string getFileHeading(const char * iInputFilename);
25+
virtual std::string getSaveMethodImplementation();
2526
};
2627

2728
}; //bin2cpp

src/SegmentGenerator.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,7 @@ namespace bin2cpp
117117
//write cpp file footer
118118
fprintf(cpp, " }\n");
119119
fprintf(cpp, " virtual const char * getMd5() const { return \"%s\"; }\n", md5String.c_str() );
120-
fprintf(cpp, " virtual bool save(const char * iFilename) const\n");
121-
fprintf(cpp, " {\n");
122-
fprintf(cpp, " FILE * f = fopen(iFilename, \"wb\");\n");
123-
fprintf(cpp, " if (!f) return false;\n");
124-
fprintf(cpp, " size_t fileSize = getSize();\n");
125-
fprintf(cpp, " const char * buffer = getBuffer();\n");
126-
fprintf(cpp, " fwrite(buffer, 1, fileSize, f);\n");
127-
fprintf(cpp, " fclose(f);\n");
128-
fprintf(cpp, " return true;\n");
129-
fprintf(cpp, " }\n");
120+
fprintf(cpp, "%s", getSaveMethodImplementation().c_str());
130121
fprintf(cpp, " private:\n");
131122
fprintf(cpp, " std::string mBuffer;\n");
132123
fprintf(cpp, " };\n");

src/StringGenerator.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,7 @@ namespace bin2cpp
128128
fprintf(cpp, " return buffer;\n");
129129
fprintf(cpp, " }\n");
130130
fprintf(cpp, " virtual const char * getMd5() const { return \"%s\"; }\n", md5String.c_str() );
131-
fprintf(cpp, " virtual bool save(const char * iFilename) const\n");
132-
fprintf(cpp, " {\n");
133-
fprintf(cpp, " FILE * f = fopen(iFilename, \"wb\");\n");
134-
fprintf(cpp, " if (!f) return false;\n");
135-
fprintf(cpp, " size_t fileSize = getSize();\n");
136-
fprintf(cpp, " const char * buffer = getBuffer();\n");
137-
fprintf(cpp, " fwrite(buffer, 1, fileSize, f);\n");
138-
fprintf(cpp, " fclose(f);\n");
139-
fprintf(cpp, " return true;\n");
140-
fprintf(cpp, " }\n");
131+
fprintf(cpp, "%s", getSaveMethodImplementation().c_str());
141132
fprintf(cpp, " };\n");
142133
fprintf(cpp, " const File & %s() { static %s _instance; return _instance; }\n", getterFunctionName.c_str(), className.c_str());
143134
fprintf(cpp, "}; //bin2cpp\n");

0 commit comments

Comments
 (0)