Skip to content

Commit 8ff426c

Browse files
committed
Moved generation of output files c++ header (comments) to the same function: BaseGenerator::getHeaderComments()
1 parent f034f11 commit 8ff426c

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

src/ArrayGenerator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ namespace bin2cpp
5353
//determine file properties
5454
long fileSize = getFileSize(input);
5555
std::string filename = getFilename(iInputFilename);
56-
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5756

5857
//Build class name
5958
std::string className;
@@ -64,12 +63,7 @@ namespace bin2cpp
6463
std::string getterFunctionName = getGetterFunctionName(functionIdentifier.c_str());
6564

6665
//write cpp file heading
67-
fprintf(cpp, "/**\n");
68-
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
69-
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
70-
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
71-
fprintf(cpp, " * Do not modify this file.\n");
72-
fprintf(cpp, " */\n");
66+
fprintf(cpp, "%s", getHeaderComments(iInputFilename).c_str());
7367
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
7468
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7569
fprintf(cpp, "#include <string> //for memcpy\n");

src/BaseGenerator.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
99

1010
namespace bin2cpp
1111
{
12+
std::string& operator<<(std::string& str, const std::string & value)
13+
{
14+
str.append(value);
15+
return str;
16+
}
17+
18+
std::string& operator<<(std::string& str, int value)
19+
{
20+
char buffer[1024];
21+
sprintf(buffer, "%d", value);
22+
str.append(buffer);
23+
return str;
24+
}
25+
26+
std::string& operator<<(std::string& str, uint64_t value)
27+
{
28+
std::stringstream out;
29+
out << value;
30+
str.append( out.str() );
31+
return str;
32+
}
1233

1334
BaseGenerator::BaseGenerator()
1435
{
@@ -47,17 +68,30 @@ namespace bin2cpp
4768
return cppPath;
4869
}
4970

50-
bin2cpp::ErrorCodes BaseGenerator::createHeaderEmbededFile(const char * iHeaderFilePath, const char * iFunctionIdentifier)
71+
std::string BaseGenerator::getHeaderComments(const char * iInputFilename)
72+
{
73+
std::string filename = getFilename(iInputFilename);
74+
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
75+
76+
std::string header;
77+
header <<
78+
header << "/**\n";
79+
header << " * This file was generated by bin2cpp v" << getVersionString() << "\n";
80+
header << " * Copyright (C) 2013-" << bin2cpp::getCopyrightYear() << " end2endzone.com. All rights reserved.\n";
81+
header << " * Source code for file '" << filename << "', last modified " << fileDateModified << ".\n";
82+
header << " * Do not modify this file.\n";
83+
header << " */\n";
84+
return header;
85+
}
86+
87+
bin2cpp::ErrorCodes BaseGenerator::createHeaderEmbededFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier)
5188
{
5289
FILE * header = fopen(iHeaderFilePath, "w");
5390
if (!header)
5491
return bin2cpp::ErrorCodes::UnableToCreateOutputFiles;
5592

56-
fprintf(header, "/**\n");
57-
fprintf(header, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
58-
fprintf(header, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
59-
fprintf(header, " * Do not modify this file.\n");
60-
fprintf(header, " */\n");
93+
std::string headercomments = getHeaderComments(iInputFilename);
94+
fprintf(header, "%s", headercomments.c_str());
6195
fprintf(header, "#pragma once\n");
6296
fprintf(header, "#include <stddef.h>\n");
6397
fprintf(header, "namespace bin2cpp\n");

src/BaseGenerator.h

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

1717
//same header file for all generators
18-
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iHeaderFilePath, const char * iFunctionIdentifier);
18+
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier);
1919

2020
protected:
2121
virtual std::string getGetterFunctionName(const char * iFunctionIdentifier);
2222
virtual std::string getHeaderFilePath(const char * iCppFilePath);
2323
virtual std::string getCppFilePath(const char * iHeaderFilePath);
24+
virtual std::string getHeaderComments(const char * iInputFilename);
2425
};
2526

2627
}; //bin2cpp

src/IGenerator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ namespace bin2cpp
1919
///<summary>
2020
///Creates a header file for embedding a given file into C++ source code.
2121
///</summary>
22-
///<param name="iFilePath">The path of the header file (*.h) that is used for getting access to the functions.</param>
22+
///<param name="iInputFilename">The path of the input file (resource) to embeded as a C++ source code.</param>
23+
///<param name="iHeaderFilePath">The path of the header file (*.h) that is used for getting access to the functions.</param>
2324
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
2425
///<returns>Returns an enum of type ErrorCodes which defines the results.</returns>
25-
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iHeaderFilePath, const char * iFunctionIdentifier) = 0;
26+
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iInputFilename, const char * iHeaderFilePath, const char * iFunctionIdentifier) = 0;
2627

2728
///<summary>
2829
///Creates a cpp source file for embedding a given file into C++ source code.

src/SegmentGenerator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ namespace bin2cpp
5353
//determine file properties
5454
long fileSize = getFileSize(input);
5555
std::string filename = getFilename(iInputFilename);
56-
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5756
//long lastSegmentSize = fileSize%iChunkSize;
5857
//size_t numSegments = fileSize/iChunkSize + (lastSegmentSize == 0 ? 0 : 1);
5958

@@ -66,12 +65,7 @@ namespace bin2cpp
6665
std::string getterFunctionName = getGetterFunctionName(functionIdentifier.c_str());
6766

6867
//write cpp file heading
69-
fprintf(cpp, "/**\n");
70-
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
71-
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
72-
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
73-
fprintf(cpp, " * Do not modify this file.\n");
74-
fprintf(cpp, " */\n");
68+
fprintf(cpp, "%s", getHeaderComments(iInputFilename).c_str());
7569
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
7670
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7771
fprintf(cpp, "#include <string> //for memcpy\n");

src/StringGenerator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ namespace bin2cpp
5353
//determine file properties
5454
long fileSize = getFileSize(input);
5555
std::string filename = getFilename(iInputFilename);
56-
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5756

5857
//Build class name
5958
std::string className;
@@ -64,12 +63,7 @@ namespace bin2cpp
6463
std::string getterFunctionName = getGetterFunctionName(functionIdentifier.c_str());
6564

6665
//write cpp file heading
67-
fprintf(cpp, "/**\n");
68-
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
69-
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
70-
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
71-
fprintf(cpp, " * Do not modify this file.\n");
72-
fprintf(cpp, " */\n");
66+
fprintf(cpp, "%s", getHeaderComments(iInputFilename).c_str());
7367
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
7468
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7569
fprintf(cpp, "#include <string> //for memcpy\n");

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ int main(int argc, char* argv[])
227227
if (headerResult == bin2cpp::ErrorCodes::Success)
228228
{
229229
//generate file or override existing
230-
headerResult = generator->createHeaderEmbededFile(outputHeaderPath.c_str(), functionIdentifier.c_str());
230+
headerResult = generator->createHeaderEmbededFile(inputFile.c_str(), outputHeaderPath.c_str(), functionIdentifier.c_str());
231231
}
232232
if (headerResult == bin2cpp::ErrorCodes::Success)
233233
{

0 commit comments

Comments
 (0)