3636namespace bin2cpp
3737{
3838 BaseGenerator::BaseGenerator () :
39- mCppEncoder (IGenerator::CPP_ENCODER_OCT)
39+ mCppEncoder (IGenerator::CPP_ENCODER_OCT),
40+ mManagerEnabled (false )
4041 {
4142 }
4243
@@ -108,6 +109,27 @@ namespace bin2cpp
108109 return mCppEncoder ;
109110 }
110111
112+ void BaseGenerator::setManagerHeaderFile (const char * iManagerFile)
113+ {
114+ if (iManagerFile)
115+ mManagerFile = iManagerFile;
116+ }
117+
118+ const char * BaseGenerator::getManagerHeaderFile () const
119+ {
120+ return mManagerFile .c_str ();
121+ }
122+
123+ void BaseGenerator::setRegisterFileEnabled (bool iRegisterFileEnabled)
124+ {
125+ mManagerEnabled = iRegisterFileEnabled;
126+ }
127+
128+ bool BaseGenerator::isRegisterFileEnabled () const
129+ {
130+ return mManagerEnabled ;
131+ }
132+
111133 // -------------------------------
112134 // protected methods
113135 // -------------------------------
@@ -173,6 +195,30 @@ namespace bin2cpp
173195 return output;
174196 }
175197
198+ std::string BaseGenerator::getFileManagerRegistrationTemplate ()
199+ {
200+ if (!this ->isRegisterFileEnabled ())
201+ return std::string ();
202+
203+ // Build class name
204+ std::string className = getClassName ();
205+
206+ std::string output;
207+ output << " typedef const " << mBaseClass << " & (*t_func)();\n " ;
208+ output << " extern bool RegisterFile(t_func iFunctionPointer);\n " ;
209+ output << " static bool k" << className << " Registered = " << mNamespace << " ::RegisterFile(&" << getGetterFunctionName () << " );\n " ;
210+ return output;
211+ }
212+
213+ std::string BaseGenerator::getClassName ()
214+ {
215+ std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter (mFunctionIdentifier );
216+ std::string className;
217+ className.append (functionIdentifier.c_str ());
218+ className.append (" File" );
219+ return className;
220+ }
221+
176222 bool BaseGenerator::createCppHeaderFile (const char * iHeaderFilePath)
177223 {
178224 FILE * header = fopen (iHeaderFilePath, " w" );
@@ -212,4 +258,132 @@ namespace bin2cpp
212258 return true ;
213259 }
214260
261+ bool BaseGenerator::createManagerHeaderFile (const char * iHeaderFilePath)
262+ {
263+ FILE * header = fopen (iHeaderFilePath, " w" );
264+ if (!header)
265+ return false ;
266+
267+ // define macro guard a macro matching the filename
268+ std::string macro_guard = getCppIncludeGuardMacroName (iHeaderFilePath);
269+
270+ std::string headercomments = getHeaderTemplate ();
271+ fprintf (header, " %s" , headercomments.c_str ());
272+ fprintf (header, " #ifndef %s\n " , macro_guard.c_str ());
273+ fprintf (header, " #define %s\n " , macro_guard.c_str ());
274+ fprintf (header, " \n " );
275+ fprintf (header, " #include <stddef.h>\n " );
276+ fprintf (header, " #include <vector>\n " );
277+ fprintf (header, " \n " );
278+ fprintf (header, " namespace %s\n " , mNamespace .c_str ());
279+ fprintf (header, " {\n " );
280+ fprintf (header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n " );
281+ fprintf (header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n " );
282+ fprintf (header, " class %s\n " , mBaseClass .c_str ());
283+ fprintf (header, " {\n " );
284+ fprintf (header, " public:\n " );
285+ fprintf (header, " virtual size_t getSize() const = 0;\n " );
286+ fprintf (header, " virtual const char * getFilename() const = 0;\n " );
287+ fprintf (header, " virtual const char * getBuffer() const = 0;\n " );
288+ fprintf (header, " virtual bool save(const char * iFilename) const = 0;\n " );
289+ fprintf (header, " };\n " );
290+ fprintf (header, " #endif //BIN2CPP_EMBEDDEDFILE_CLASS\n " );
291+ fprintf (header, " \n " );
292+ fprintf (header, " #ifndef BIN2CPP_FILEMANAGER_CLASS\n " );
293+ fprintf (header, " #define BIN2CPP_FILEMANAGER_CLASS\n " );
294+ fprintf (header, " class FileManager\n " );
295+ fprintf (header, " {\n " );
296+ fprintf (header, " private:\n " );
297+ fprintf (header, " FileManager();\n " );
298+ fprintf (header, " ~FileManager();\n " );
299+ fprintf (header, " public:\n " );
300+ fprintf (header, " typedef const %s & (*t_func)();\n " , mBaseClass .c_str ());
301+ fprintf (header, " static FileManager & getInstance();\n " );
302+ fprintf (header, " void registerFile(t_func iFunctionPtr);\n " );
303+ fprintf (header, " size_t getFileCount() const;\n " );
304+ fprintf (header, " const %s * getFile(const size_t & index) const;\n " , mBaseClass .c_str ());
305+ fprintf (header, " bool saveFiles(const char * iDirectory) const;\n " );
306+ fprintf (header, " private:\n " );
307+ fprintf (header, " std::vector<t_func> functions_;\n " );
308+ fprintf (header, " };\n " );
309+ fprintf (header, " #endif //BIN2CPP_FILEMANAGER_CLASS\n " );
310+ fprintf (header, " }; //%s\n " , mNamespace .c_str ());
311+ fprintf (header, " \n " );
312+ fprintf (header, " #endif //%s\n " , macro_guard.c_str ());
313+
314+ fclose (header);
315+
316+ return true ;
317+ }
318+
319+ bool BaseGenerator::createManagerSourceFile (const char * iCppFilePath)
320+ {
321+ FILE * cpp = fopen (iCppFilePath, " w" );
322+ if (!cpp)
323+ return false ;
324+
325+ // Build header and cpp file path
326+ std::string headerPath = getHeaderFilePath (iCppFilePath);
327+ std::string cppPath = iCppFilePath;
328+ std::string headerFilename = ra::filesystem::GetFilename (headerPath.c_str ());
329+ std::string cppFilename = ra::filesystem::GetFilename (iCppFilePath);
330+
331+ std::string headercomments = getHeaderTemplate ();
332+ fprintf (cpp, " %s" , headercomments.c_str ());
333+ fprintf (cpp, " #include \" %s\"\n " , headerFilename.c_str ());
334+ fprintf (cpp, " #include <string>\n " );
335+ fprintf (cpp, " \n " );
336+ fprintf (cpp, " namespace %s\n " , mNamespace .c_str ());
337+ fprintf (cpp, " {\n " );
338+ fprintf (cpp, " bool RegisterFile(FileManager::t_func iFunctionPointer)\n " );
339+ fprintf (cpp, " {\n " );
340+ fprintf (cpp, " if (iFunctionPointer == NULL)\n " );
341+ fprintf (cpp, " return false;\n " );
342+ fprintf (cpp, " FileManager::getInstance().registerFile(iFunctionPointer);\n " );
343+ fprintf (cpp, " return true;\n " );
344+ fprintf (cpp, " }\n " );
345+ fprintf (cpp, " FileManager::FileManager() {}\n " );
346+ fprintf (cpp, " FileManager::~FileManager() {}\n " );
347+ fprintf (cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n " );
348+ fprintf (cpp, " void FileManager::registerFile(t_func iFunctionPtr) { functions_.push_back(iFunctionPtr); }\n " );
349+ fprintf (cpp, " size_t FileManager::getFileCount() const { return functions_.size(); }\n " );
350+ fprintf (cpp, " const File * FileManager::getFile(const size_t & index) const\n " );
351+ fprintf (cpp, " {\n " );
352+ fprintf (cpp, " if (index >= functions_.size())\n " );
353+ fprintf (cpp, " return NULL;\n " );
354+ fprintf (cpp, " t_func ressource_getter_function = functions_[index];\n " );
355+ fprintf (cpp, " const bin2cpp::File & resource = ressource_getter_function();\n " );
356+ fprintf (cpp, " return &resource;\n " );
357+ fprintf (cpp, " }\n " );
358+ fprintf (cpp, " bool FileManager::saveFiles(const char * iDirectory) const\n " );
359+ fprintf (cpp, " {\n " );
360+ fprintf (cpp, " if (iDirectory == NULL)\n " );
361+ fprintf (cpp, " return false;\n " );
362+ fprintf (cpp, " size_t count = getFileCount();\n " );
363+ fprintf (cpp, " for(size_t i=0; i<count; i++)\n " );
364+ fprintf (cpp, " {\n " );
365+ fprintf (cpp, " const File * f = getFile(i);\n " );
366+ fprintf (cpp, " if (!f)\n " );
367+ fprintf (cpp, " return false;\n " );
368+ fprintf (cpp, " std::string path;\n " );
369+ fprintf (cpp, " path.append(iDirectory);\n " );
370+ fprintf (cpp, " #ifdef _WIN32\n " );
371+ fprintf (cpp, " path.append(1, '\\\\ ');\n " );
372+ fprintf (cpp, " #else\n " );
373+ fprintf (cpp, " path.append(1, '/');\n " );
374+ fprintf (cpp, " #endif\n " );
375+ fprintf (cpp, " path.append(f->getFilename());\n " );
376+ fprintf (cpp, " bool saved = f->save(path.c_str());\n " );
377+ fprintf (cpp, " if (!saved)\n " );
378+ fprintf (cpp, " return false;\n " );
379+ fprintf (cpp, " }\n " );
380+ fprintf (cpp, " return true;\n " );
381+ fprintf (cpp, " }\n " );
382+ fprintf (cpp, " }; //%s\n " , mNamespace .c_str ());
383+
384+ fclose (cpp);
385+
386+ return true ;
387+ }
388+
215389}; // bin2cpp
0 commit comments