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::setManagerEnabled (bool iManagerEnabled)
124+ {
125+ mManagerEnabled = iManagerEnabled;
126+ }
127+
128+ bool BaseGenerator::isManagerEnabled () const
129+ {
130+ return mManagerEnabled ;
131+ }
132+
111133 // -------------------------------
112134 // protected methods
113135 // -------------------------------
@@ -173,6 +195,29 @@ namespace bin2cpp
173195 return output;
174196 }
175197
198+ std::string BaseGenerator::getFileManagerTemplate ()
199+ {
200+ if (!this ->isManagerEnabled ())
201+ return std::string ();
202+
203+ // Build class name
204+ std::string className = getClassName ();
205+
206+ std::string output;
207+ output << " bool " << className << " Initializer() { bin2cpp::FileManager::getInstance().registerFile(&" << getGetterFunctionName () << " ); return true; }\n " ;
208+ output << " static bool k" << className << " Initialized = " << className << " Initializer();\n " ;
209+ return output;
210+ }
211+
212+ std::string BaseGenerator::getClassName ()
213+ {
214+ std::string functionIdentifier = ra::strings::CapitalizeFirstCharacter (mFunctionIdentifier );
215+ std::string className;
216+ className.append (functionIdentifier.c_str ());
217+ className.append (" File" );
218+ return className;
219+ }
220+
176221 bool BaseGenerator::createCppHeaderFile (const char * iHeaderFilePath)
177222 {
178223 FILE * header = fopen (iHeaderFilePath, " w" );
@@ -212,4 +257,126 @@ namespace bin2cpp
212257 return true ;
213258 }
214259
260+ bool BaseGenerator::createManagerHeaderFile (const char * iHeaderFilePath)
261+ {
262+ FILE * header = fopen (iHeaderFilePath, " w" );
263+ if (!header)
264+ return false ;
265+
266+ // define macro guard a macro matching the filename
267+ std::string macro_guard = getCppIncludeGuardMacroName (iHeaderFilePath);
268+
269+ std::string headercomments = getHeaderTemplate ();
270+ fprintf (header, " %s" , headercomments.c_str ());
271+ fprintf (header, " #ifndef %s\n " , macro_guard.c_str ());
272+ fprintf (header, " #define %s\n " , macro_guard.c_str ());
273+ fprintf (header, " \n " );
274+ fprintf (header, " #include <stddef.h>\n " );
275+ fprintf (header, " #include <vector>\n " );
276+ fprintf (header, " \n " );
277+ fprintf (header, " namespace %s\n " , mNamespace .c_str ());
278+ fprintf (header, " {\n " );
279+ fprintf (header, " #ifndef BIN2CPP_EMBEDDEDFILE_CLASS\n " );
280+ fprintf (header, " #define BIN2CPP_EMBEDDEDFILE_CLASS\n " );
281+ fprintf (header, " class %s\n " , mBaseClass .c_str ());
282+ fprintf (header, " {\n " );
283+ fprintf (header, " public:\n " );
284+ fprintf (header, " virtual size_t getSize() const = 0;\n " );
285+ fprintf (header, " virtual const char * getFilename() const = 0;\n " );
286+ fprintf (header, " virtual const char * getBuffer() const = 0;\n " );
287+ fprintf (header, " virtual bool save(const char * iFilename) const = 0;\n " );
288+ fprintf (header, " };\n " );
289+ fprintf (header, " #endif //BIN2CPP_EMBEDDEDFILE_CLASS\n " );
290+ fprintf (header, " \n " );
291+ fprintf (header, " #ifndef BIN2CPP_FILEMANAGER_CLASS\n " );
292+ fprintf (header, " #define BIN2CPP_FILEMANAGER_CLASS\n " );
293+ fprintf (header, " class FileManager\n " );
294+ fprintf (header, " {\n " );
295+ fprintf (header, " private:\n " );
296+ fprintf (header, " FileManager();\n " );
297+ fprintf (header, " ~FileManager();\n " );
298+ fprintf (header, " public:\n " );
299+ fprintf (header, " typedef const %s & (*t_func)();\n " , mBaseClass .c_str ());
300+ fprintf (header, " static FileManager & getInstance();\n " );
301+ fprintf (header, " void registerFile(t_func iFunctionPtr);\n " );
302+ fprintf (header, " size_t getFileCount() const;\n " );
303+ fprintf (header, " const %s * getFile(const size_t & index) const;\n " , mBaseClass .c_str ());
304+ fprintf (header, " bool saveFiles(const char * iDirectory) const;\n " );
305+ fprintf (header, " private:\n " );
306+ fprintf (header, " std::vector<t_func> functions_;\n " );
307+ fprintf (header, " };\n " );
308+ fprintf (header, " #endif //BIN2CPP_FILEMANAGER_CLASS\n " );
309+ fprintf (header, " }; //%s\n " , mNamespace .c_str ());
310+ fprintf (header, " \n " );
311+ fprintf (header, " #endif //%s\n " , macro_guard.c_str ());
312+
313+ fclose (header);
314+
315+ return true ;
316+ }
317+
318+ bool BaseGenerator::createManagerSourceFile (const char * iCppFilePath)
319+ {
320+ FILE * cpp = fopen (iCppFilePath, " w" );
321+ if (!cpp)
322+ return false ;
323+
324+ // Build header and cpp file path
325+ std::string headerPath = getHeaderFilePath (iCppFilePath);
326+ std::string cppPath = iCppFilePath;
327+ std::string headerFilename = ra::filesystem::GetFilename (headerPath.c_str ());
328+ std::string cppFilename = ra::filesystem::GetFilename (iCppFilePath);
329+
330+ std::string headercomments = getHeaderTemplate ();
331+ fprintf (cpp, " %s" , headercomments.c_str ());
332+ fprintf (cpp, " #include \" %s\"\n " , headerFilename.c_str ());
333+ fprintf (cpp, " #include <string>\n " , headerFilename.c_str ());
334+ fprintf (cpp, " \n " );
335+ fprintf (cpp, " namespace %s\n " , mNamespace .c_str ());
336+ fprintf (cpp, " {\n " );
337+ fprintf (cpp, " FileManager::FileManager() {}\n " );
338+ fprintf (cpp, " FileManager::~FileManager() {}\n " );
339+ fprintf (cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n " );
340+ fprintf (cpp, " void FileManager::registerFile(t_func iFunctionPtr) { functions_.push_back(iFunctionPtr); }\n " );
341+ fprintf (cpp, " size_t FileManager::getFileCount() const { return functions_.size(); }\n " );
342+ fprintf (cpp, " const File * FileManager::getFile(const size_t & index) const\n " );
343+ fprintf (cpp, " {\n " );
344+ fprintf (cpp, " if (index >= functions_.size())\n " );
345+ fprintf (cpp, " return NULL;\n " );
346+ fprintf (cpp, " t_func ressource_getter_function = functions_[index];\n " );
347+ fprintf (cpp, " const bin2cpp::File & resource = ressource_getter_function();\n " );
348+ fprintf (cpp, " return &resource;\n " );
349+ fprintf (cpp, " }\n " );
350+ fprintf (cpp, " bool FileManager::saveFiles(const char * iDirectory) const\n " );
351+ fprintf (cpp, " {\n " );
352+ fprintf (cpp, " if (iDirectory == NULL)\n " );
353+ fprintf (cpp, " return false;\n " );
354+ fprintf (cpp, " size_t count = getFileCount();\n " );
355+ fprintf (cpp, " for(size_t i=0; i<count; i++)\n " );
356+ fprintf (cpp, " {\n " );
357+ fprintf (cpp, " const File * f = getFile(i);\n " );
358+ fprintf (cpp, " if (!f)\n " );
359+ fprintf (cpp, " return false;\n " );
360+ fprintf (cpp, " std::string path;\n " );
361+ fprintf (cpp, " path.append(iDirectory);\n " );
362+ fprintf (cpp, " #ifdef _WIN32\n " );
363+ fprintf (cpp, " path.append(1, '\\\\ ');\n " );
364+ fprintf (cpp, " #else\n " );
365+ fprintf (cpp, " path.append(1, '/');\n " );
366+ fprintf (cpp, " #endif\n " );
367+ fprintf (cpp, " path.append(f->getFilename());\n " );
368+ fprintf (cpp, " bool saved = f->save(path.c_str());\n " );
369+ fprintf (cpp, " if (!saved)\n " );
370+ fprintf (cpp, " return false;\n " );
371+ fprintf (cpp, " }\n " );
372+ fprintf (cpp, " return true;\n " );
373+ fprintf (cpp, " }\n " );
374+ fprintf (cpp, " }; //%s\n " , mNamespace .c_str ());
375+ fprintf (cpp, " \n " );
376+
377+ fclose (cpp);
378+
379+ return true ;
380+ }
381+
215382}; // bin2cpp
0 commit comments