Skip to content

Commit 538b79b

Browse files
committed
Move FileManager class generating code from main.cpp to a dedicated ManagerGenerator class which is now an IGenerator implementation.
1 parent a0d0cd9 commit 538b79b

File tree

7 files changed

+310
-217
lines changed

7 files changed

+310
-217
lines changed

src/bin2cpp/BaseGenerator.cpp

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -268,200 +268,6 @@ namespace bin2cpp
268268
return true;
269269
}
270270

271-
bool BaseGenerator::createManagerHeaderFile(const char * header_file_path)
272-
{
273-
FILE * header = fopen(header_file_path, "w");
274-
if (!header)
275-
return false;
276-
277-
//define macro guard a macro matching the filename
278-
std::string macroGuard;
279-
macroGuard += getCppIncludeGuardMacroName(mContext.codeNamespace.c_str()); //prefix the custom namespace for the file manager
280-
if (!macroGuard.empty())
281-
macroGuard += "_";
282-
macroGuard += getCppIncludeGuardMacroName(header_file_path);
283-
284-
std::string classMacroGuardPrefix = getClassMacroGuardPrefix();
285-
std::string fileHeader = getHeaderTemplate(false);
286-
287-
fprintf(header, "%s", fileHeader.c_str());
288-
fprintf(header, "#ifndef %s\n", macroGuard.c_str());
289-
fprintf(header, "#define %s\n", macroGuard.c_str());
290-
fprintf(header, "\n");
291-
fprintf(header, "#include <stddef.h>\n");
292-
fprintf(header, "#include <vector>\n");
293-
fprintf(header, "\n");
294-
fprintf(header, "namespace %s\n", mContext.codeNamespace.c_str());
295-
fprintf(header, "{\n");
296-
fprintf(header, " #ifndef %s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
297-
fprintf(header, " #define %s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
298-
fprintf(header, " class %s\n", mContext.baseClass.c_str());
299-
fprintf(header, " {\n");
300-
fprintf(header, " public:\n");
301-
fprintf(header, " virtual size_t getSize() const = 0;\n");
302-
fprintf(header, " /* DEPRECATED */ virtual inline const char * getFilename() const { return getFileName(); }\n");
303-
fprintf(header, " virtual const char * getFileName() const = 0;\n");
304-
fprintf(header, " virtual const char * getFilePath() const = 0;\n");
305-
fprintf(header, " virtual const char * getBuffer() const = 0;\n");
306-
fprintf(header, " virtual bool save(const char * filename) const = 0;\n");
307-
fprintf(header, " };\n");
308-
fprintf(header, " #endif //%s_EMBEDDEDFILE_CLASS\n", classMacroGuardPrefix.c_str());
309-
fprintf(header, "\n");
310-
fprintf(header, " #ifndef %s_FILEMANAGER_CLASS\n", classMacroGuardPrefix.c_str());
311-
fprintf(header, " #define %s_FILEMANAGER_CLASS\n", classMacroGuardPrefix.c_str());
312-
fprintf(header, " class FileManager\n");
313-
fprintf(header, " {\n");
314-
fprintf(header, " private:\n");
315-
fprintf(header, " FileManager();\n");
316-
fprintf(header, " ~FileManager();\n");
317-
fprintf(header, " public:\n");
318-
fprintf(header, " typedef const %s & (*t_func)();\n", mContext.baseClass.c_str());
319-
fprintf(header, " static FileManager & getInstance();\n");
320-
fprintf(header, " void registerFile(t_func func);\n");
321-
fprintf(header, " size_t getFileCount() const;\n");
322-
fprintf(header, " const %s * getFile(const size_t & index) const;\n", mContext.baseClass.c_str());
323-
fprintf(header, " bool saveFiles(const char * directory) const;\n");
324-
fprintf(header, " bool createParentDirectories(const char * file_path) const;\n");
325-
fprintf(header, " bool createDirectories(const char * path) const;\n");
326-
fprintf(header, " private:\n");
327-
fprintf(header, " std::vector<t_func> functions_;\n");
328-
fprintf(header, " };\n");
329-
fprintf(header, " #endif //%s_FILEMANAGER_CLASS\n", classMacroGuardPrefix.c_str());
330-
fprintf(header, "}; //%s\n", mContext.codeNamespace.c_str());
331-
fprintf(header, "\n");
332-
fprintf(header, "#endif //%s\n", macroGuard.c_str());
333-
334-
fclose(header);
335-
336-
return true;
337-
}
338-
339-
bool BaseGenerator::createManagerSourceFile(const char * cpp_file_path)
340-
{
341-
FILE * cpp = fopen(cpp_file_path, "w");
342-
if (!cpp)
343-
return false;
344-
345-
//Build header and cpp file path
346-
std::string headerPath = getHeaderFilePath(cpp_file_path);
347-
std::string cppPath = cpp_file_path;
348-
349-
std::string fileHeader = getHeaderTemplate(false);
350-
351-
fprintf(cpp, "%s", fileHeader.c_str());
352-
fprintf(cpp, "#include \"%s\"\n", mContext.managerHeaderFilename.c_str());
353-
fprintf(cpp, "#include <string>\n");
354-
fprintf(cpp, "#include <string.h> // strlen\n");
355-
fprintf(cpp, "#include <sys/stat.h> // stat\n");
356-
fprintf(cpp, "#include <errno.h> // errno, EEXIST\n");
357-
fprintf(cpp, "#if defined(_WIN32)\n");
358-
fprintf(cpp, "#include <direct.h> // _mkdir\n");
359-
fprintf(cpp, "#endif\n");
360-
fprintf(cpp, "\n");
361-
fprintf(cpp, "#if defined(_WIN32)\n");
362-
fprintf(cpp, "#define portable_stat _stat\n");
363-
fprintf(cpp, "#define portable_mkdir(path) _mkdir(path)\n");
364-
fprintf(cpp, "#define PATH_SEPARATOR_CHAR '\\\\'\n");
365-
fprintf(cpp, "#define PATH_SEPARATOR_STR \"\\\\\"\n");
366-
fprintf(cpp, "#else\n");
367-
fprintf(cpp, "#define portable_stat stat\n");
368-
fprintf(cpp, "#define portable_mkdir(path) mkdir(path, 0755)\n");
369-
fprintf(cpp, "#define PATH_SEPARATOR_CHAR '/'\n");
370-
fprintf(cpp, "#define PATH_SEPARATOR_STR \"/\"\n");
371-
fprintf(cpp, "#endif\n");
372-
fprintf(cpp, "\n");
373-
fprintf(cpp, "namespace %s\n", mContext.codeNamespace.c_str());
374-
fprintf(cpp, "{\n");
375-
fprintf(cpp, " bool RegisterFile(FileManager::t_func functionPointer)\n");
376-
fprintf(cpp, " {\n");
377-
fprintf(cpp, " if (functionPointer == NULL)\n");
378-
fprintf(cpp, " return false;\n");
379-
fprintf(cpp, " FileManager::getInstance().registerFile(functionPointer);\n");
380-
fprintf(cpp, " return true;\n");
381-
fprintf(cpp, " }\n");
382-
fprintf(cpp, " FileManager::FileManager() {}\n");
383-
fprintf(cpp, " FileManager::~FileManager() {}\n");
384-
fprintf(cpp, " FileManager & FileManager::getInstance() { static FileManager _mgr; return _mgr; }\n");
385-
fprintf(cpp, " void FileManager::registerFile(t_func func) { functions_.push_back(func); }\n");
386-
fprintf(cpp, " size_t FileManager::getFileCount() const { return functions_.size(); }\n");
387-
fprintf(cpp, " const File * FileManager::getFile(const size_t & index) const\n");
388-
fprintf(cpp, " {\n");
389-
fprintf(cpp, " if (index >= functions_.size())\n");
390-
fprintf(cpp, " return NULL;\n");
391-
fprintf(cpp, " t_func ressource_getter_function = functions_[index];\n");
392-
fprintf(cpp, " const %s::File & resource = ressource_getter_function();\n", mContext.codeNamespace.c_str());
393-
fprintf(cpp, " return &resource;\n");
394-
fprintf(cpp, " }\n");
395-
fprintf(cpp, " bool FileManager::saveFiles(const char * directory) const\n");
396-
fprintf(cpp, " {\n");
397-
fprintf(cpp, " if (directory == NULL)\n");
398-
fprintf(cpp, " return false;\n");
399-
fprintf(cpp, " size_t count = getFileCount();\n");
400-
fprintf(cpp, " for(size_t i=0; i<count; i++)\n");
401-
fprintf(cpp, " {\n");
402-
fprintf(cpp, " const File * f = getFile(i);\n");
403-
fprintf(cpp, " if (!f)\n");
404-
fprintf(cpp, " return false;\n");
405-
fprintf(cpp, " std::string path;\n");
406-
fprintf(cpp, " path.append(directory);\n");
407-
fprintf(cpp, " path.append(1,PATH_SEPARATOR_CHAR);\n");
408-
fprintf(cpp, " path.append(f->getFilePath());\n");
409-
fprintf(cpp, " if (!createParentDirectories(path.c_str()))\n");
410-
fprintf(cpp, " return false;\n");
411-
fprintf(cpp, " bool saved = f->save(path.c_str());\n");
412-
fprintf(cpp, " if (!saved)\n");
413-
fprintf(cpp, " return false;\n");
414-
fprintf(cpp, " }\n");
415-
fprintf(cpp, " return true;\n");
416-
fprintf(cpp, " }\n");
417-
fprintf(cpp, " static inline bool isRootDirectory(const char * path)\n");
418-
fprintf(cpp, " {\n");
419-
fprintf(cpp, " if (path == NULL && path[0] == '\\0')\n");
420-
fprintf(cpp, " return false;\n");
421-
fprintf(cpp, " #if defined(_WIN32)\n");
422-
fprintf(cpp, " bool isDriveLetter = ((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z'));\n");
423-
fprintf(cpp, " if ((isDriveLetter && path[1] == ':' && path[2] == '\\0') || // test for C:\n");
424-
fprintf(cpp, " (isDriveLetter && path[1] == ':' && path[2] == PATH_SEPARATOR_CHAR && path[3] == '\\0')) // test for C:\\ \n");
425-
fprintf(cpp, " return true;\n");
426-
fprintf(cpp, " #else\n");
427-
fprintf(cpp, " if (path[0] == PATH_SEPARATOR_CHAR)\n");
428-
fprintf(cpp, " return true;\n");
429-
fprintf(cpp, " #endif\n");
430-
fprintf(cpp, " return false;\n");
431-
fprintf(cpp, " }\n");
432-
fprintf(cpp, " bool FileManager::createParentDirectories(const char * file_path) const\n");
433-
fprintf(cpp, " {\n");
434-
fprintf(cpp, " if (file_path == NULL)\n");
435-
fprintf(cpp, " return false;\n");
436-
fprintf(cpp, " std::string accumulator;\n");
437-
fprintf(cpp, " size_t length = strlen(file_path);\n");
438-
fprintf(cpp, " for(size_t i=0; i<length; i++)\n");
439-
fprintf(cpp, " {\n");
440-
fprintf(cpp, " if (file_path[i] == PATH_SEPARATOR_CHAR && !accumulator.empty() && !isRootDirectory(accumulator.c_str()))\n");
441-
fprintf(cpp, " {\n");
442-
fprintf(cpp, " int ret = portable_mkdir(accumulator.c_str());\n");
443-
fprintf(cpp, " if (ret != 0 && errno != EEXIST)\n");
444-
fprintf(cpp, " return false;\n");
445-
fprintf(cpp, " }\n");
446-
fprintf(cpp, " accumulator += file_path[i];\n");
447-
fprintf(cpp, " }\n");
448-
fprintf(cpp, " return true;\n");
449-
fprintf(cpp, " }\n");
450-
fprintf(cpp, " bool FileManager::createDirectories(const char * path) const\n");
451-
fprintf(cpp, " {\n");
452-
fprintf(cpp, " if (path == NULL)\n");
453-
fprintf(cpp, " return false;\n");
454-
fprintf(cpp, " std::string path_copy = path;\n");
455-
fprintf(cpp, " path_copy.append(1,PATH_SEPARATOR_CHAR);\n");
456-
fprintf(cpp, " return createParentDirectories(path_copy.c_str());\n");
457-
fprintf(cpp, " }\n");
458-
fprintf(cpp, "}; //%s\n", mContext.codeNamespace.c_str());
459-
460-
fclose(cpp);
461-
462-
return true;
463-
}
464-
465271
bool BaseGenerator::printFileContent()
466272
{
467273
//check if input file exists

src/bin2cpp/BaseGenerator.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ namespace bin2cpp
4545

4646
//same header file for all generators
4747
virtual bool createCppHeaderFile(const char * header_file_path);
48-
virtual bool createManagerHeaderFile(const char * header_file_path);
49-
virtual bool createManagerSourceFile(const char * cpp_file_path);
5048
virtual bool printFileContent();
5149

5250
protected:

src/bin2cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ add_executable(bin2cpp
2222
enums.h
2323
IGenerator.h
2424
main.cpp
25+
ManagerGenerator.cpp
26+
ManagerGenerator.h
2527
SegmentGenerator.cpp
2628
SegmentGenerator.h
2729
StringGenerator.cpp

src/bin2cpp/IGenerator.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,6 @@ 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 FileManager header file.
66-
///</summary>
67-
///<param name="header_file_path">The path of the header file (*.h).</param>
68-
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
69-
virtual bool createManagerHeaderFile(const char * header_file_path) = 0;
70-
71-
///<summary>
72-
///Creates a FileManager cpp file.
73-
///</summary>
74-
///<param name="header_file_path">The path of the cpp file (*.h).</param>
75-
///<returns>Returns true when the file was created. Returns false otherwise.</returns>
76-
virtual bool createManagerSourceFile(const char * cpp_file_path) = 0;
77-
7864
///<summary>
7965
///Print the encoded file content to stdout
8066
///</summary>

0 commit comments

Comments
 (0)