Skip to content

Commit 59a4d99

Browse files
committed
Modified IGenerator to implement ITemplateVariableLookup.
Modified BaseGenerator to implement lookupTemplateVariable() and uses C/C++ header files template to generate output header files with the help of the TemplateProcessor.
1 parent 6b02ac3 commit 59a4d99

File tree

3 files changed

+178
-82
lines changed

3 files changed

+178
-82
lines changed

src/bin2cpp/BaseGenerator.cpp

Lines changed: 168 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*********************************************************************************/
2424

2525
#include "BaseGenerator.h"
26+
#include "TemplateProcessor.h"
2627
#include <stdio.h>
2728
#include <stdlib.h>
2829
#include <string>
@@ -55,6 +56,50 @@ namespace bin2cpp
5556
return mContext;
5657
}
5758

59+
std::string BaseGenerator::lookupTemplateVariable(const std::string& name)
60+
{
61+
if ( name == "bin2cpp_output_file_macro_guard" ) return getCppIncludeGuardMacroName(mContext.headerFilename);
62+
if ( name == "bin2cpp_embedded_file_class_macro_guard" ) return getClassMacroGuardPrefix();
63+
if ( name == "bin2cpp_output_file_header" ) return getHeaderTemplate();
64+
if ( name == "bin2cpp_baseclass" ) return mContext.baseClass;
65+
if ( name == "bin2cpp_classname" ) return getClassName();
66+
if ( name == "bin2cpp_function_identifier" ) return mContext.functionIdentifier;
67+
if ( name == "bin2cpp_function_identifier_lowercase" ) return ra::strings::Lowercase(mContext.functionIdentifier);
68+
if ( name == "bin2cpp_classname" ) return getClassName();
69+
if ( name == "bin2cpp_namespace" ) return mContext.codeNamespace;
70+
if ( name == "bin2cpp_cpp_getter_function_name" ) return getGetterFunctionName();
71+
if ( name == "bin2cpp_insert_input_file_as_code" ) return getInputFileDataAsCode();
72+
if ( name == "bin2cpp_cpp_header_include_path" ) return getCppHeaderIncludePath();
73+
if ( name == "bin2cpp_cpp_get_file_name_impl" ) return getImplOfGetFileName();
74+
if ( name == "bin2cpp_cpp_get_file_path_impl" ) return getImplOfGetFilePath();
75+
if ( name == "bin2cpp_get_file_obj_file_name" ) return getFileClassFileName();
76+
if ( name == "bin2cpp_get_file_obj_file_path" ) return getFileClassFilePath();
77+
if ( name == "bin2cpp_cpp_get_save_method_impl" ) return getSaveMethodTemplate();
78+
if ( name == "bin2cpp_cpp_get_file_manager_registration_impl" && mContext.registerFiles ) return getCppFileManagerRegistrationImplementationTemplate();
79+
if ( name == "bin2cpp_c_file_manager_registration_predeclaration" && mContext.registerFiles ) return getCFileManagerRegistrationPredeclarationTemplate();
80+
if ( name == "bin2cpp_c_file_manager_registration_implementation" && mContext.registerFiles ) return getCFileManagerRegistrationImplementationTemplate();
81+
82+
//if ( name == "bin2cpp_c_registration_post_init_impl" && mContext.registerFiles )
83+
//{
84+
// std::string output;
85+
// output += " \n";
86+
// output += " // register when loaded if static initialisation does not work\n";
87+
// output += " ${bin2cpp_classname}_filemanager_register_file(file);\n";
88+
// return output;
89+
//}
90+
91+
if ( name == "bin2cpp_input_file_size" )
92+
{
93+
//determine file properties
94+
uint32_t file_size = ra::filesystem::GetFileSize(mContext.inputFilePath.c_str());
95+
std::string file_size_str = ra::strings::ToString(file_size);
96+
return file_size_str;
97+
}
98+
99+
// Unknown name
100+
return "";
101+
}
102+
58103
//-------------------------------
59104
//protected methods
60105
//-------------------------------
@@ -179,6 +224,7 @@ namespace bin2cpp
179224
output << " typedef const " << mContext.baseClass << " & (*t_func)();\n";
180225
output << " extern bool RegisterFile(t_func iFunctionPointer);\n";
181226
output << " static bool k" << className << "Registered = " << mContext.codeNamespace << "::RegisterFile(&" << getGetterFunctionName() << ");\n";
227+
output << " \n";
182228
return output;
183229
}
184230

@@ -344,47 +390,95 @@ namespace bin2cpp
344390
return output;
345391
}
346392

347-
bool BaseGenerator::createCppHeaderFile(const char * header_file_path)
393+
std::string BaseGenerator::getCppHeaderIncludePath()
348394
{
349-
FILE * header = fopen(header_file_path, "w");
350-
if (!header)
351-
return false;
395+
return mContext.headerFilename;
396+
}
397+
398+
std::string BaseGenerator::getInputFileDataAsCode()
399+
{
400+
std::string output;
352401

353-
//define macro guard matching the filename
354-
std::string macroGuard = getCppIncludeGuardMacroName(header_file_path);
355-
356-
std::string classMacroGuardPrefix = getClassMacroGuardPrefix();
357-
std::string fileHeader = getHeaderTemplate();
358-
359-
fprintf(header, "%s", fileHeader.c_str());
360-
fprintf(header, "#ifndef %s\n", macroGuard.c_str());
361-
fprintf(header, "#define %s\n", macroGuard.c_str());
362-
fprintf(header, "\n");
363-
fprintf(header, "#include <stddef.h>\n");
364-
fprintf(header, "\n");
365-
fprintf(header, "namespace %s\n", mContext.codeNamespace.c_str());
366-
fprintf(header, "{\n");
367-
fprintf(header, " #ifndef %s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
368-
fprintf(header, " #define %s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
369-
fprintf(header, " class %s\n", mContext.baseClass.c_str());
370-
fprintf(header, " {\n");
371-
fprintf(header, " public:\n");
372-
fprintf(header, " virtual size_t getSize() const = 0;\n");
373-
fprintf(header, " /* DEPRECATED */ virtual inline const char * getFilename() const { return getFileName(); }\n");
374-
fprintf(header, " virtual const char * getFileName() const = 0;\n");
375-
fprintf(header, " virtual const char * getFilePath() const = 0;\n");
376-
fprintf(header, " virtual const char * getBuffer() const = 0;\n");
377-
fprintf(header, " virtual bool save(const char * filename) const = 0;\n");
378-
fprintf(header, " };\n");
379-
fprintf(header, " #endif //%s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
380-
fprintf(header, " const %s & %s();\n", mContext.baseClass.c_str(), getGetterFunctionName().c_str());
381-
fprintf(header, "}; //%s\n", mContext.codeNamespace.c_str());
382-
fprintf(header, "\n");
383-
fprintf(header, "#endif //%s\n", macroGuard.c_str());
384-
385-
fclose(header);
402+
//check if input file exists
403+
FILE* fin = fopen(mContext.inputFilePath.c_str(), "rb");
404+
if ( !fin )
405+
return "";
386406

387-
return true;
407+
uint64_t fileSize = ra::filesystem::GetFileSize64(mContext.inputFilePath.c_str());
408+
size_t chunkCount = fileSize / mContext.chunkSize;
409+
if ( fileSize % mContext.chunkSize > 0 )
410+
chunkCount++;
411+
412+
//create buffer for each chunks from input buffer
413+
int numLinePrinted = 0;
414+
size_t chunkIndex = 0;
415+
unsigned char* buffer = new unsigned char[mContext.chunkSize];
416+
while ( !feof(fin) )
417+
{
418+
//read a chunk of the file
419+
size_t readSize = fread(buffer, 1, mContext.chunkSize, fin);
420+
421+
bool isLastChunk = (chunkIndex == (chunkCount - 1));
422+
423+
if ( readSize > 0 )
424+
{
425+
//output
426+
std::string encoded_chunk = getInputFileChunkAsCode(buffer, readSize, chunkIndex, chunkCount, isLastChunk);
427+
output += encoded_chunk;
428+
429+
numLinePrinted++;
430+
chunkIndex++;
431+
}
432+
433+
}
434+
delete[] buffer;
435+
buffer = NULL;
436+
437+
fclose(fin);
438+
439+
return output;
440+
}
441+
442+
std::string BaseGenerator::getInputFileChunkAsCode(const unsigned char* buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk)
443+
{
444+
return "";
445+
}
446+
447+
bool BaseGenerator::createCppHeaderFile(const char * header_file_path)
448+
{
449+
const std::string text = ""
450+
"${bin2cpp_output_file_header}"
451+
"#ifndef ${bin2cpp_output_file_macro_guard}\n"
452+
"#define ${bin2cpp_output_file_macro_guard}\n"
453+
"\n"
454+
"#include <stddef.h>\n"
455+
"\n"
456+
"namespace ${bin2cpp_namespace}\n"
457+
"{\n"
458+
" #ifndef ${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_CLASS\n"
459+
" #define ${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_CLASS\n"
460+
" class ${bin2cpp_baseclass}\n"
461+
" {\n"
462+
" public:\n"
463+
" virtual size_t getSize() const = 0;\n"
464+
" /* DEPRECATED */ virtual inline const char * getFilename() const { return getFileName(); }\n"
465+
" virtual const char * getFileName() const = 0;\n"
466+
" virtual const char * getFilePath() const = 0;\n"
467+
" virtual const char * getBuffer() const = 0;\n"
468+
" virtual bool save(const char * filename) const = 0;\n"
469+
" };\n"
470+
" #endif //${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_CLASS\n"
471+
" const ${bin2cpp_baseclass} & ${bin2cpp_cpp_getter_function_name}();\n"
472+
"}; //${bin2cpp_namespace}\n"
473+
"\n"
474+
"#endif //${bin2cpp_output_file_macro_guard}\n"
475+
;
476+
477+
TemplateProcessor processor(&text);
478+
processor.setTemplateVariableLookup(this);
479+
bool write_success = processor.writeFile(header_file_path);
480+
481+
return write_success;
388482
}
389483

390484
bool BaseGenerator::printFileContent()
@@ -441,48 +535,42 @@ namespace bin2cpp
441535

442536
bool BaseGenerator::createCHeaderFile(const char* file_path)
443537
{
444-
FILE* header = fopen(file_path, "w");
445-
if ( !header )
446-
return false;
447-
448-
//define macro guard matching the filename
449-
std::string macroGuard = getCppIncludeGuardMacroName(file_path);
450-
451-
std::string classMacroGuardPrefix = getClassMacroGuardPrefix();
452-
std::string fileHeader = getHeaderTemplate();
453-
454-
fprintf(header, "%s", fileHeader.c_str());
455-
fprintf(header, "#ifndef %s\n", macroGuard.c_str());
456-
fprintf(header, "#define %s\n", macroGuard.c_str());
457-
fprintf(header, "\n");
458-
fprintf(header, "#include <stddef.h>\n");
459-
fprintf(header, "#include <stdbool.h>\n");
460-
fprintf(header, "\n");
461-
fprintf(header, "#ifndef %s_EMBEDDEDFILE_STRUCT\n", classMacroGuardPrefix.c_str());
462-
fprintf(header, "#define %s_EMBEDDEDFILE_STRUCT\n", classMacroGuardPrefix.c_str());
463-
fprintf(header, "typedef struct %s %s;\n", mContext.baseClass.c_str(), mContext.baseClass.c_str());
464-
fprintf(header, "typedef bool(*%s_load_func)();\n", mContext.codeNamespace.c_str());
465-
fprintf(header, "typedef void(*%s_free_func)();\n", mContext.codeNamespace.c_str());
466-
fprintf(header, "typedef bool(*%s_save_func)(const char*);\n", mContext.codeNamespace.c_str());
467-
fprintf(header, "typedef struct %s\n", mContext.baseClass.c_str());
468-
fprintf(header, "{\n");
469-
fprintf(header, " size_t size;\n");
470-
fprintf(header, " const char* file_name;\n");
471-
fprintf(header, " const char* file_path;\n");
472-
fprintf(header, " const unsigned char* buffer;\n");
473-
fprintf(header, " %s_load_func load;\n", mContext.codeNamespace.c_str());
474-
fprintf(header, " %s_free_func unload;\n", mContext.codeNamespace.c_str());
475-
fprintf(header, " %s_save_func save;\n", mContext.codeNamespace.c_str());
476-
fprintf(header, "} %s;\n", mContext.baseClass.c_str());
477-
fprintf(header, "typedef %s* %sPtr;\n", mContext.baseClass.c_str(), mContext.baseClass.c_str());
478-
fprintf(header, "#endif //%s_EMBEDDEDFILE_STRUCT\n", classMacroGuardPrefix.c_str());
479-
fprintf(header, "%s* %s(void);\n", mContext.baseClass.c_str(), getGetterFunctionName().c_str());
480-
fprintf(header, "\n");
481-
fprintf(header, "#endif //%s\n", macroGuard.c_str());
482-
483-
fclose(header);
484-
485-
return true;
538+
const std::string text = ""
539+
"${bin2cpp_output_file_header}"
540+
"#ifndef ${bin2cpp_output_file_macro_guard}\n"
541+
"#define ${bin2cpp_output_file_macro_guard}\n"
542+
"\n"
543+
"#include <stddef.h>\n"
544+
"#include <stdbool.h>\n"
545+
"\n"
546+
"#ifndef ${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_STRUCT\n"
547+
"#define ${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_STRUCT\n"
548+
"typedef struct ${bin2cpp_baseclass} ${bin2cpp_baseclass};\n"
549+
"typedef bool(*${bin2cpp_namespace}_load_func)();\n"
550+
"typedef void(*${bin2cpp_namespace}_free_func)();\n"
551+
"typedef bool(*${bin2cpp_namespace}_save_func)(const char*);\n"
552+
"typedef struct ${bin2cpp_baseclass}\n"
553+
"{\n"
554+
" size_t size;\n"
555+
" const char* file_name;\n"
556+
" const char* file_path;\n"
557+
" const unsigned char* buffer;\n"
558+
" ${bin2cpp_namespace}_load_func load;\n"
559+
" ${bin2cpp_namespace}_free_func unload;\n"
560+
" ${bin2cpp_namespace}_save_func save;\n"
561+
"} ${bin2cpp_baseclass};\n"
562+
"typedef ${bin2cpp_baseclass}* ${bin2cpp_baseclass}Ptr;\n"
563+
"#endif //${bin2cpp_embedded_file_class_macro_guard}_EMBEDDEDFILE_STRUCT\n"
564+
"${bin2cpp_baseclass}* ${bin2cpp_cpp_getter_function_name}(void);\n"
565+
"\n"
566+
"#endif //${bin2cpp_output_file_macro_guard}\n"
567+
;
568+
569+
TemplateProcessor processor(&text);
570+
processor.setTemplateVariableLookup(this);
571+
bool write_success = processor.writeFile(file_path);
572+
573+
return write_success;
486574
}
487575

488576
bool BaseGenerator::createCSourceFile(const char* file_path)

src/bin2cpp/BaseGenerator.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define BASEGENERATOR_H
2727

2828
#include "IGenerator.h"
29+
#include <map>
2930

3031
namespace bin2cpp
3132
{
@@ -43,6 +44,9 @@ namespace bin2cpp
4344
virtual void setContext(const Context& c);
4445
virtual const Context & getContext() const;
4546

47+
//ITemplateVariableLookup methods
48+
virtual std::string lookupTemplateVariable(const std::string& name);
49+
4650
//same header file for all generators
4751
virtual bool createCppHeaderFile(const char * header_file_path);
4852
virtual bool printFileContent();
@@ -66,6 +70,9 @@ namespace bin2cpp
6670
virtual std::string getImplOfGetFilePath();
6771
virtual std::string getFileClassFileName();
6872
virtual std::string getFileClassFilePath();
73+
virtual std::string getCppHeaderIncludePath();
74+
virtual std::string getInputFileDataAsCode();
75+
virtual std::string getInputFileChunkAsCode(const unsigned char * buffer, size_t buffer_size, size_t index, size_t count, bool is_last_chunk);
6976

7077
//attributes
7178
Context mContext;

src/bin2cpp/IGenerator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727

2828
#include <string>
2929
#include "Context.h"
30-
30+
#include "ITemplateVariableLookup.h"
31+
3132
namespace bin2cpp
3233
{
3334

34-
class IGenerator
35+
class IGenerator : public virtual ITemplateVariableLookup
3536
{
3637
public:
3738

0 commit comments

Comments
 (0)