Skip to content

Commit d2ad469

Browse files
committed
Changed the API of IGenerator again to use relevant file path (cpp path for generating the cpp file, header path for generating the header file) instead of the two arguments (output and header filename)
1 parent 52c56c0 commit d2ad469

File tree

10 files changed

+46
-52
lines changed

10 files changed

+46
-52
lines changed

src/ArrayGenerator.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace bin2cpp
2525
return "array";
2626
}
2727

28-
bin2cpp::ErrorCodes ArrayGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize)
28+
bin2cpp::ErrorCodes ArrayGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize)
2929
{
3030
//check if input file exists
3131
FILE * input = fopen(iInputFilename, "rb");
@@ -37,11 +37,13 @@ namespace bin2cpp
3737
functionIdentifier[0] = (char)toupper(functionIdentifier[0]);
3838

3939
//Build header and cpp file path
40-
std::string headerPath = getHeaderFilePath(iOutputFolder, iHeaderFilename);
41-
std::string outputCppFilename = getCppFilePath(iOutputFolder, iHeaderFilename);
40+
std::string headerPath = getHeaderFilePath(iCppFilePath);
41+
std::string cppPath = iCppFilePath;
42+
std::string headerFilename = getFilename(headerPath.c_str());
43+
std::string cppFilename = getFilename(iCppFilePath);
4244

4345
//create cpp file
44-
FILE * cpp = fopen(outputCppFilename.c_str(), "w");
46+
FILE * cpp = fopen(cppPath.c_str(), "w");
4547
if (!cpp)
4648
{
4749
fclose(input);
@@ -65,7 +67,7 @@ namespace bin2cpp
6567
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
6668
fprintf(cpp, " * Do not modify this file.\n");
6769
fprintf(cpp, " */\n");
68-
fprintf(cpp, "#include \"%s\"\n", iHeaderFilename );
70+
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
6971
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7072
fprintf(cpp, "#include <string> //for memcpy\n");
7173
fprintf(cpp, "namespace bin2cpp\n");

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 bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize);
18+
virtual bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize);
1919
};
2020

2121
}; //bin2cpp

src/BaseGenerator.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,25 @@ namespace bin2cpp
3131
return getter;
3232
}
3333

34-
std::string BaseGenerator::getHeaderFilePath(const char * iOutputFolder, const char * iHeaderFilename)
34+
std::string BaseGenerator::getHeaderFilePath(const char * iCppFilePath)
3535
{
3636
//Build header file path
37-
std::string headerPath = iOutputFolder;
38-
if (headerPath[headerPath.length()-1] != '\\')
39-
headerPath.append("\\");
40-
headerPath.append(iHeaderFilename);
41-
37+
std::string headerPath = iCppFilePath;
38+
strReplace(headerPath, ".cpp", ".h");
4239
return headerPath;
4340
}
4441

45-
std::string BaseGenerator::getCppFilePath(const char * iOutputFolder, const char * iHeaderFilename)
42+
std::string BaseGenerator::getCppFilePath(const char * iHeaderFilePath)
4643
{
47-
std::string headerPath = getHeaderFilePath(iOutputFolder, iHeaderFilename);
48-
49-
//Build cpp file file
50-
std::string cppPath = headerPath;
51-
strReplace(cppPath, ".h", ".cpp");
52-
44+
//Build header file path
45+
std::string cppPath = iHeaderFilePath;
46+
strReplace(cppPath, ".cpp", ".h");
5347
return cppPath;
5448
}
5549

56-
bin2cpp::ErrorCodes BaseGenerator::createHeaderEmbededFile(const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier)
50+
bin2cpp::ErrorCodes BaseGenerator::createHeaderEmbededFile(const char * iHeaderFilePath, const char * iFunctionIdentifier)
5751
{
58-
std::string outputHeaderPath = std::string(iOutputFolder) + "\\" + iHeaderFilename;
59-
60-
FILE * header = fopen(outputHeaderPath.c_str(), "w");
52+
FILE * header = fopen(iHeaderFilePath, "w");
6153
if (!header)
6254
return bin2cpp::ErrorCodes::UnableToCreateOutputFiles;
6355

src/BaseGenerator.h

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

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

2020
protected:
2121
virtual std::string getGetterFunctionName(const char * iFunctionIdentifier);
22-
virtual std::string getHeaderFilePath(const char * iOutputFolder, const char * iHeaderFilename);
23-
virtual std::string getCppFilePath(const char * iOutputFolder, const char * iHeaderFilename);
22+
virtual std::string getHeaderFilePath(const char * iCppFilePath);
23+
virtual std::string getCppFilePath(const char * iHeaderFilePath);
2424
};
2525

2626
}; //bin2cpp

src/IGenerator.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,20 @@ namespace bin2cpp
1919
///<summary>
2020
///Creates a header file for embedding a given file into C++ source code.
2121
///</summary>
22-
///<param name="iOutputFolder">The output folder where to save the generated files.</param>
23-
///<param name="iHeaderFilename">The filename of the header file (*.h) that is used for getting access to the functions.</param>
22+
///<param name="iFilePath">The path of the header file (*.h) that is used for getting access to the functions.</param>
2423
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
25-
///<param name="iOverrideExisting">Override existing output files.</param>
2624
///<returns>Returns an enum of type ErrorCodes which defines the results.</returns>
27-
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier) = 0;
25+
virtual bin2cpp::ErrorCodes createHeaderEmbededFile(const char * iHeaderFilePath, const char * iFunctionIdentifier) = 0;
2826

2927
///<summary>
3028
///Creates a cpp source file for embedding a given file into C++ source code.
3129
///</summary>
32-
///<param name="iInputFilename">The path of the file to embeded into C++ source code.</param>
33-
///<param name="iOutputFolder">The output folder where to save the generated files.</param>
34-
///<param name="iHeaderFilename">The filename of the header file (*.h) that is used for getting access to the functions.</param>
30+
///<param name="iInputFilename">The path of the input file (resource) to embeded as a C++ source code.</param>
31+
///<param name="iCppFilePath">The path of the source file (*.cpp) that is used for getting access to the functions.</param>
3532
///<param name="iFunctionIdentifier">The unique identifier name for each data accessor functions.</param>
3633
///<param name="iChunkSize">The size in bytes of each chunk of data.</param>
37-
///<param name="iOverrideExisting">Override existing output files.</param>
3834
///<returns>Returns an enum of type ErrorCodes which defines the results.</returns>
39-
virtual bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize) = 0;
35+
virtual bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize) = 0;
4036
};
4137

4238
}; //bin2cpp

src/SegmentGenerator.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace bin2cpp
2525
return "segment";
2626
}
2727

28-
bin2cpp::ErrorCodes SegmentGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize)
28+
bin2cpp::ErrorCodes SegmentGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize)
2929
{
3030
//check if input file exists
3131
FILE * input = fopen(iInputFilename, "rb");
@@ -37,11 +37,13 @@ namespace bin2cpp
3737
functionIdentifier[0] = (char)toupper(functionIdentifier[0]);
3838

3939
//Build header and cpp file path
40-
std::string headerPath = getHeaderFilePath(iOutputFolder, iHeaderFilename);
41-
std::string outputCppFilename = getCppFilePath(iOutputFolder, iHeaderFilename);
40+
std::string headerPath = getHeaderFilePath(iCppFilePath);
41+
std::string cppPath = iCppFilePath;
42+
std::string headerFilename = getFilename(headerPath.c_str());
43+
std::string cppFilename = getFilename(iCppFilePath);
4244

4345
//create cpp file
44-
FILE * cpp = fopen(outputCppFilename.c_str(), "w");
46+
FILE * cpp = fopen(cppPath.c_str(), "w");
4547
if (!cpp)
4648
{
4749
fclose(input);
@@ -67,7 +69,7 @@ namespace bin2cpp
6769
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
6870
fprintf(cpp, " * Do not modify this file.\n");
6971
fprintf(cpp, " */\n");
70-
fprintf(cpp, "#include \"%s\"\n", iHeaderFilename );
72+
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
7173
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7274
fprintf(cpp, "#include <string> //for memcpy\n");
7375
fprintf(cpp, "namespace bin2cpp\n");
@@ -96,7 +98,7 @@ namespace bin2cpp
9698
//read a chunk of the file
9799
size_t readSize = fread(buffer, 1, iChunkSize, input);
98100

99-
bool isLastChunk = !(readSize == iChunkSize);
101+
//bool isLastChunk = !(readSize == iChunkSize);
100102

101103
if (readSize == 0)
102104
continue; //nothing to output if nothing was read

src/SegmentGenerator.h

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

2121
}; //bin2cpp

src/StringGenerator.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace bin2cpp
2525
return "string";
2626
}
2727

28-
bin2cpp::ErrorCodes StringGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize)
28+
bin2cpp::ErrorCodes StringGenerator::createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize)
2929
{
3030
//check if input file exists
3131
FILE * input = fopen(iInputFilename, "rb");
@@ -37,11 +37,13 @@ namespace bin2cpp
3737
functionIdentifier[0] = (char)toupper(functionIdentifier[0]);
3838

3939
//Build header and cpp file path
40-
std::string headerPath = getHeaderFilePath(iOutputFolder, iHeaderFilename);
41-
std::string outputCppFilename = getCppFilePath(iOutputFolder, iHeaderFilename);
40+
std::string headerPath = getHeaderFilePath(iCppFilePath);
41+
std::string cppPath = iCppFilePath;
42+
std::string headerFilename = getFilename(headerPath.c_str());
43+
std::string cppFilename = getFilename(iCppFilePath);
4244

4345
//create cpp file
44-
FILE * cpp = fopen(outputCppFilename.c_str(), "w");
46+
FILE * cpp = fopen(cppPath.c_str(), "w");
4547
if (!cpp)
4648
{
4749
fclose(input);
@@ -65,7 +67,7 @@ namespace bin2cpp
6567
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
6668
fprintf(cpp, " * Do not modify this file.\n");
6769
fprintf(cpp, " */\n");
68-
fprintf(cpp, "#include \"%s\"\n", iHeaderFilename );
70+
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );
6971
fprintf(cpp, "#include <stdio.h> //for FILE\n");
7072
fprintf(cpp, "#include <string> //for memcpy\n");
7173
fprintf(cpp, "namespace bin2cpp\n");

src/StringGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace bin2cpp
1414
StringGenerator();
1515
virtual ~StringGenerator();
1616
virtual const char * getName() const;
17-
virtual bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iOutputFolder, const char * iHeaderFilename, const char * iFunctionIdentifier, size_t iChunkSize);
17+
virtual bin2cpp::ErrorCodes createCppEmbeddedFile(const char * iInputFilename, const char * iCppFilePath, const char * iFunctionIdentifier, size_t iChunkSize);
1818
};
1919

2020
}; //bin2cpp

src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ int main(int argc, char* argv[])
228228
//generate a temporary file and compare the two to see if they are different.
229229
std::string tmpFileName = bin2cpp::getTemporaryFileName();
230230
std::string tempFilePath = bin2cpp::getEnvironmentVariable("TEMP") + "\\" + tmpFileName;
231-
headerResult = generator->createHeaderEmbededFile(bin2cpp::getEnvironmentVariable("TEMP").c_str(), tmpFileName.c_str(), functionIdentifier.c_str());
231+
headerResult = generator->createHeaderEmbededFile(tempFilePath.c_str(), functionIdentifier.c_str());
232232

233233
//if still success
234234
if (headerResult == bin2cpp::ErrorCodes::Success)
@@ -253,7 +253,7 @@ int main(int argc, char* argv[])
253253
if (headerResult == bin2cpp::ErrorCodes::Success)
254254
{
255255
//generate file or override existing
256-
headerResult = generator->createHeaderEmbededFile(outputFolder.c_str(), headerFilename.c_str(), functionIdentifier.c_str());
256+
headerResult = generator->createHeaderEmbededFile(outputHeaderPath.c_str(), functionIdentifier.c_str());
257257
}
258258
if (headerResult == bin2cpp::ErrorCodes::Success)
259259
{
@@ -291,7 +291,7 @@ int main(int argc, char* argv[])
291291
//generate a temporary file and compare the two to see if they are different.
292292
std::string tmpFileName = bin2cpp::getTemporaryFileName();
293293
std::string tempFilePath = bin2cpp::getEnvironmentVariable("TEMP") + "\\" + cppFilename;
294-
cppResult = generator->createCppEmbeddedFile(inputFilename.c_str(), bin2cpp::getEnvironmentVariable("TEMP").c_str(), headerFilename.c_str(), functionIdentifier.c_str(), chunkSize);
294+
cppResult = generator->createCppEmbeddedFile(inputFilename.c_str(), tempFilePath.c_str(), functionIdentifier.c_str(), chunkSize);
295295

296296
//if still success
297297
if (cppResult == bin2cpp::ErrorCodes::Success)
@@ -316,7 +316,7 @@ int main(int argc, char* argv[])
316316
if (cppResult == bin2cpp::ErrorCodes::Success)
317317
{
318318
//generate file or override existing
319-
cppResult = generator->createCppEmbeddedFile(inputFilename.c_str(), outputFolder.c_str(), headerFilename.c_str(), functionIdentifier.c_str(), chunkSize);
319+
cppResult = generator->createCppEmbeddedFile(inputFilename.c_str(), outputCppPath.c_str(), functionIdentifier.c_str(), chunkSize);
320320
}
321321
if (cppResult == bin2cpp::ErrorCodes::Success)
322322
{

0 commit comments

Comments
 (0)