Skip to content

Commit b2c1416

Browse files
committed
Modified IGenerator interface to add support for generating C source and header files with functions createCHeaderFile() and createCSourceFile().
Modified main.cpp to properly parse CodeGenerationEnum and generate *.h/*.c files when `--code=c` is specified.
1 parent 22dbb60 commit b2c1416

File tree

7 files changed

+111
-23
lines changed

7 files changed

+111
-23
lines changed

src/bin2cpp/BaseGenerator.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,29 @@ namespace bin2cpp
6161

6262
std::string BaseGenerator::getGetterFunctionName()
6363
{
64-
//Uppercase function identifier
65-
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mContext.functionIdentifier);
64+
//Uppercase function identifier
65+
std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter(mContext.functionIdentifier);
6666

6767
std::string getter;
68-
getter.append("get");
69-
getter.append(functionIdentifier);
70-
getter.append("File");
68+
getter.append("get");
69+
getter.append(functionIdentifier);
70+
getter.append("File");
7171
return getter;
7272
}
7373

7474
std::string BaseGenerator::getHeaderFilePath(const char * cpp_file_path)
7575
{
7676
//Build header file path
7777
std::string headerPath = cpp_file_path;
78-
ra::strings::Replace(headerPath, ".cpp", ".h");
78+
ra::strings::Replace(headerPath, ".cpp", ".h");
7979
return headerPath;
8080
}
8181

8282
std::string BaseGenerator::getCppFilePath(const char * header_file_path)
8383
{
8484
//Build header file path
8585
std::string cppPath = header_file_path;
86-
ra::strings::Replace(cppPath, ".cpp", ".h");
86+
ra::strings::Replace(cppPath, ".cpp", ".h");
8787
return cppPath;
8888
}
8989

@@ -320,4 +320,16 @@ namespace bin2cpp
320320
return true;
321321
}
322322

323+
bool BaseGenerator::createCHeaderFile(const char* file_path)
324+
{
325+
// not supported yet
326+
return false;
327+
}
328+
329+
bool BaseGenerator::createCSourceFile(const char* file_path)
330+
{
331+
// not supported yet
332+
return false;
333+
}
334+
323335
}; //bin2cpp

src/bin2cpp/BaseGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace bin2cpp
4646
//same header file for all generators
4747
virtual bool createCppHeaderFile(const char * header_file_path);
4848
virtual bool printFileContent();
49+
virtual bool createCHeaderFile(const char* file_path);
50+
virtual bool createCSourceFile(const char* file_path);
4951

5052
protected:
5153

src/bin2cpp/IGenerator.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ namespace bin2cpp
6161
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
6262
virtual bool createCppSourceFile(const char * cpp_file_path) = 0;
6363

64+
///<summary>
65+
///Creates a header file for embedding a given file into C source code.
66+
///</summary>
67+
///<param name="file_path">The path of the header file (*.h) to generate.</param>
68+
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
69+
virtual bool createCHeaderFile(const char* file_path) = 0;
70+
71+
///<summary>
72+
///Creates a C source file for embedding a given file into C source code.
73+
///</summary>
74+
///<param name="file_path">The path of the source file (*.c) to generate.</param>
75+
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
76+
virtual bool createCSourceFile(const char* file_path) = 0;
77+
6478
///<summary>
6579
///Print the encoded file content to stdout
6680
///</summary>

src/bin2cpp/bin2cpp.samples.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
--file=..\..\test\bin2cpp_unittest\generated_files\testHtml100000\testHtml100000.bin --output=..\..\test\bin2cpp_unittest\generated_files\testHtml100000 --headerfile=_testHtml100000_C.h --override --code=c
12
--file=.\generated_files\testText1000\testText1000.bin --output=.\generated_files\testText1000 --headerfile=_testText1000.h --identifier=testText1000 --chunksize=450 --override
23
--file=.\generated_files\testSequential1000\testSequential1000.bin --output=.\generated_files\testSequential1000 --headerfile=_testSequential1000.h --identifier=testSequential1000 --chunksize=180 --override
3-
--file=C:\Temp\foobar.bin --output=C:\Temp --headerfile=_testFoo.h --identifier=testFoo --override
4-
--file=C:\Temp\foobar.bin --output=C:\Temp --headerfile=_testFoo.h --identifier=testFoo --override --noheader
5-
--dir=D:\Temp\foobar --output=C:\Temp\foobar --override �noheader
64
--file=..\..\test\bin2cpp_unittest\generated_files\testIssue28\testIssue28.bin --output=..\..\test\bin2cpp_unittest\generated_files\testIssue28 --headerfile=_testIssue28.h --identifier=testIssue28 --managerfile=FileManager.h --override
75
--file=..\..\test\bin2cpp_unittest\generated_files\testIssue28\testIssue28.bin --output=..\..\test\bin2cpp_unittest\generated_files\testIssue28 --headerfile=_testIssue28.h --identifier=testIssue28 --managerfile=FileManager.h --override
86
--file=..\..\test\bin2cpp_unittest\generated_files\testSequential1000\testSequential1000.bin --headerfile="generated/foo-data.h" --output=. --identifier=TestLongPath

src/bin2cpp/common.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ namespace bin2cpp
9494
return false;
9595
}
9696

97+
bool isCHeaderFile(const std::string & path)
98+
{
99+
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
100+
if (extension == "H")
101+
return true;
102+
return false;
103+
}
104+
105+
bool isCSourceFile(const std::string & path)
106+
{
107+
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
108+
if (extension == "C")
109+
return true;
110+
return false;
111+
}
112+
97113
std::string getCppIncludeGuardMacroName(const std::string & path)
98114
{
99115
static const std::string EMPTY_STRING;

src/bin2cpp/common.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ namespace bin2cpp
6464
///<return>Returns true if path is a c++ source file. Returns false otherwise.<return>
6565
bool isCppSourceFile(const std::string & path);
6666

67+
///<summary>
68+
///Determine if a given path is a C header file.
69+
///</summary>
70+
///<param name="path">An valid file path.</param>
71+
///<return>Returns true if path is a C header file. Returns false otherwise.<return>
72+
bool isCHeaderFile(const std::string & path);
73+
74+
///<summary>
75+
///Determine if a given path is a C source file.
76+
///</summary>
77+
///<param name="path">An valid file path.</param>
78+
///<return>Returns true if path is a C source file. Returns false otherwise.<return>
79+
bool isCSourceFile(const std::string & path);
80+
6781
///<summary>
6882
///Determine the appropriate macro name for the include guard of the given c++ header file.
6983
///See also https://en.wikipedia.org/wiki/Include_guard

src/bin2cpp/main.cpp

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ enum FILE_UPDATE_MODE
7373

7474
//default values
7575
static const size_t DEFAULT_CHUNK_SIZE = 200;
76-
static const char * DEFAULT_NAMESPACE = "bin2cpp";
77-
static const char * DEFAULT_BASECLASSNAME = "File";
76+
static const char * DEFAULT_NAMESPACE_CPP = "bin2cpp";
77+
static const char * DEFAULT_NAMESPACE_C = "bin2c";
78+
static const char * DEFAULT_BASECLASS_NAME_CPP = "File";
79+
static const char * DEFAULT_BASECLASS_NAME_C = "Bin2cFile";
7880
static const CppEncoderEnum DEFAULT_ENCODING = CPP_ENCODER_OCT;
7981
static const CodeGenerationEnum DEFAULT_CODE_GENERATION = CODE_GENERATION_CPP;
8082
static Dictionary identifiers_dictionary; // unique values for identifiers
@@ -391,12 +393,31 @@ int main(int argc, char* argv[])
391393

392394
if (!ra::cli::ParseArgument("namespace", c.codeNamespace, argc, argv))
393395
{
394-
c.codeNamespace = DEFAULT_NAMESPACE;
396+
switch ( c.code )
397+
{
398+
default:
399+
case CODE_GENERATION_CPP:
400+
c.codeNamespace = DEFAULT_NAMESPACE_CPP;
401+
break;
402+
case CODE_GENERATION_C:
403+
c.codeNamespace = DEFAULT_NAMESPACE_C;
404+
break;
405+
};
406+
395407
}
396408

397409
if (!ra::cli::ParseArgument("baseclass", c.baseClass, argc, argv))
398410
{
399-
c.baseClass = DEFAULT_BASECLASSNAME;
411+
switch ( c.code )
412+
{
413+
default:
414+
case CODE_GENERATION_CPP:
415+
c.baseClass = DEFAULT_BASECLASS_NAME_CPP;
416+
break;
417+
case CODE_GENERATION_C:
418+
c.baseClass = DEFAULT_BASECLASS_NAME_C;
419+
break;
420+
};
400421
}
401422

402423
c.registerFiles = ra::cli::ParseArgument("registerfile", dummy, argc, argv);
@@ -567,8 +588,9 @@ APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * genera
567588
return APP_ERROR_INPUTFILENOTFOUND;
568589

569590
//prepare output files path
570-
std::string headerExtention = ra::filesystem::GetFileExtention(c.headerFilename);
571-
std::string cppFilename = c.headerFilename.substr(0, c.headerFilename.size() - headerExtention.size()) + "cpp"; // strip out header file's extension and add 'cpp'.
591+
const std::string headerExtention = ra::filesystem::GetFileExtention(c.headerFilename);
592+
const std::string & sourceExtension = getDefaultCodeSourceFileExtension(c.code);
593+
std::string sourceFilename = c.headerFilename.substr(0, c.headerFilename.size() - headerExtention.size()) + sourceExtension; // strip out header file's extension and add 'cpp'.
572594

573595
//create a copy of the context.
574596
//we may have already generated files from a previous call to processInputFile().
@@ -577,11 +599,11 @@ APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * genera
577599

578600
//build unique output relative file paths
579601
cCopy.headerFilename = bin2cpp::getUniqueFilePath(cCopy.headerFilename, output_files_dictionary);
580-
cppFilename = bin2cpp::getUniqueFilePath(cppFilename, output_files_dictionary);
602+
sourceFilename = bin2cpp::getUniqueFilePath(sourceFilename, output_files_dictionary);
581603

582604
//build full absolute paths
583605
std::string outputHeaderPath = cCopy.outputDirPath + ra::filesystem::GetPathSeparatorStr() + cCopy.headerFilename;
584-
std::string outputCppPath = cCopy.outputDirPath + ra::filesystem::GetPathSeparatorStr() + cppFilename;
606+
std::string outputSourcePath = cCopy.outputDirPath + ra::filesystem::GetPathSeparatorStr() + sourceFilename;
585607

586608
//configure the generator
587609
generator->setContext(cCopy);
@@ -607,7 +629,7 @@ APP_ERROR_CODES processInputFile(const Context & c, bin2cpp::IGenerator * genera
607629
if (!headerResult)
608630
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
609631

610-
bool cppResult = generateOutputFile(c, outputCppPath, generator);
632+
bool cppResult = generateOutputFile(c, outputSourcePath, generator);
611633
if (!cppResult)
612634
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
613635

@@ -760,16 +782,26 @@ bool generateOutputFile(const Context & c, const std::string & output_file_path,
760782

761783
//generate file
762784
bool result = false;
763-
if (isCppHeaderFile(output_file_path))
785+
if (c.code == CODE_GENERATION_CPP && isCppHeaderFile(output_file_path))
764786
{
765-
//generate header
787+
//generate C++ header
766788
result = generator->createCppHeaderFile(output_file_path.c_str());
767789
}
768-
else
790+
else if ( c.code == CODE_GENERATION_CPP)
769791
{
770-
//generate cpp
792+
//generate C++ source
771793
result = generator->createCppSourceFile(output_file_path.c_str());
772794
}
795+
else if ( c.code == CODE_GENERATION_C && isCHeaderFile(output_file_path) )
796+
{
797+
//generate C header
798+
result = generator->createCHeaderFile(output_file_path.c_str());
799+
}
800+
else if ( c.code == CODE_GENERATION_C )
801+
{
802+
//generate C source
803+
result = generator->createCSourceFile(output_file_path.c_str());
804+
}
773805
if (!result)
774806
{
775807
//there was an error generating file

0 commit comments

Comments
 (0)