The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. Utility to turn a bunch of binary files into a .cpp file and .h file full of
  4. data so they can be built directly into an executable.
  5. Copyright 2007 by Julian Storer.
  6. Use this code at your own risk! It carries no warranty!
  7. ==============================================================================
  8. */
  9. #include "juce_AppConfig.h"
  10. #include "../../juce_amalgamated.h"
  11. //==============================================================================
  12. static int addFile (const File& file,
  13. const String& classname,
  14. OutputStream& headerStream,
  15. OutputStream& cppStream)
  16. {
  17. MemoryBlock mb;
  18. file.loadFileAsData (mb);
  19. const String name (file.getFileName().toLowerCase()
  20. .replaceCharacter (' ', '_')
  21. .replaceCharacter ('.', '_')
  22. .retainCharacters (T("abcdefghijklmnopqrstuvwxyz_0123456789")));
  23. std::cout << "Adding " << (const char*) name << ": "
  24. << (int) mb.getSize() << " bytes" << std::endl;
  25. headerStream << " extern const char* " << name << ";\r\n"
  26. " const int " << name << "Size = "
  27. << (int) mb.getSize() << ";\r\n\r\n";
  28. static int tempNum = 0;
  29. cppStream << "static const unsigned char temp" << ++tempNum << "[] = {";
  30. int i = 0;
  31. const uint8* const data = (const uint8*) mb.getData();
  32. while (i < mb.getSize() - 1)
  33. {
  34. if ((i % 40) != 39)
  35. cppStream << (int) data[i] << ",";
  36. else
  37. cppStream << (int) data[i] << ",\r\n ";
  38. ++i;
  39. }
  40. cppStream << (int) data[i] << ",0,0};\r\n";
  41. cppStream << "const char* " << classname << "::" << name
  42. << " = (const char*) temp" << tempNum << ";\r\n\r\n";
  43. return mb.getSize();
  44. }
  45. static bool isHiddenFile (const File& f, const File& root)
  46. {
  47. return f.getFileName().endsWithIgnoreCase (T(".scc"))
  48. || f.getFileName() == T(".svn")
  49. || f.getFileName().startsWithChar (T('.'))
  50. || (f.getSize() == 0 && ! f.isDirectory())
  51. || (f.getParentDirectory() != root && isHiddenFile (f.getParentDirectory(), root));
  52. }
  53. //==============================================================================
  54. int main (int argc, char* argv[])
  55. {
  56. // If you're running a command-line app, you need to initialise juce manually
  57. // before calling any Juce functionality..
  58. initialiseJuce_NonGUI();
  59. std::cout << "\n BinaryBuilder! Copyright 2007 by Julian Storer - www.rawmaterialsoftware.com\n\n";
  60. if (argc < 4 || argc > 5)
  61. {
  62. std::cout << " Usage: BinaryBuilder sourcedirectory targetdirectory targetclassname [optional wildcard pattern]\n\n"
  63. " BinaryBuilder will find all files in the source directory, and encode them\n"
  64. " into two files called (targetclassname).cpp and (targetclassname).h, which it\n"
  65. " will write into the target directory supplied.\n\n"
  66. " Any files in sub-directories of the source directory will be put into the\n"
  67. " resultant class, but #ifdef'ed out using the name of the sub-directory (hard to\n"
  68. " explain, but obvious when you try it...)\n";
  69. return 0;
  70. }
  71. const File sourceDirectory (File::getCurrentWorkingDirectory()
  72. .getChildFile (String (argv[1]).unquoted()));
  73. if (! sourceDirectory.isDirectory())
  74. {
  75. std::cout << "Source directory doesn't exist: "
  76. << (const char*) sourceDirectory.getFullPathName()
  77. << std::endl << std::endl;
  78. return 0;
  79. }
  80. const File destDirectory (File::getCurrentWorkingDirectory()
  81. .getChildFile (String (argv[2]).unquoted()));
  82. if (! destDirectory.isDirectory())
  83. {
  84. std::cout << "Destination directory doesn't exist: "
  85. << (const char*) destDirectory.getFullPathName() << std::endl << std::endl;
  86. return 0;
  87. }
  88. String className (argv[3]);
  89. className = className.trim();
  90. const File headerFile (destDirectory.getChildFile (className).withFileExtension (T(".h")));
  91. const File cppFile (destDirectory.getChildFile (className).withFileExtension (T(".cpp")));
  92. std::cout << "Creating " << (const char*) headerFile.getFullPathName()
  93. << " and " << (const char*) cppFile.getFullPathName()
  94. << " from files in " << (const char*) sourceDirectory.getFullPathName()
  95. << "..." << std::endl << std::endl;
  96. Array <File> files;
  97. sourceDirectory.findChildFiles (files, File::findFiles, true,
  98. (argc > 4) ? argv[4] : "*");
  99. if (files.size() == 0)
  100. {
  101. std::cout << "Didn't find any source files in: "
  102. << (const char*) sourceDirectory.getFullPathName() << std::endl << std::endl;
  103. return 0;
  104. }
  105. headerFile.deleteFile();
  106. cppFile.deleteFile();
  107. ScopedPointer <OutputStream> header (headerFile.createOutputStream());
  108. if (header == 0)
  109. {
  110. std::cout << "Couldn't open "
  111. << (const char*) headerFile.getFullPathName() << " for writing" << std::endl << std::endl;
  112. return 0;
  113. }
  114. ScopedPointer <OutputStream> cpp (cppFile.createOutputStream());
  115. if (cpp == 0)
  116. {
  117. std::cout << "Couldn't open "
  118. << (const char*) cppFile.getFullPathName() << " for writing" << std::endl << std::endl;
  119. return 0;
  120. }
  121. *header << "/* (Auto-generated binary data file). */\r\n\r\n"
  122. "#ifndef BINARY_" << className.toUpperCase() << "_H\r\n"
  123. "#define BINARY_" << className.toUpperCase() << "_H\r\n\r\n"
  124. "namespace " << className << "\r\n"
  125. "{\r\n";
  126. *cpp << "/* (Auto-generated binary data file). */\r\n\r\n"
  127. "#include \"" << className << ".h\"\r\n\r\n";
  128. int totalBytes = 0;
  129. for (int i = 0; i < files.size(); ++i)
  130. {
  131. const File file (files[i]);
  132. // (avoid source control files and hidden files..)
  133. if (! isHiddenFile (file, sourceDirectory))
  134. {
  135. if (file.getParentDirectory() != sourceDirectory)
  136. {
  137. *header << " #ifdef " << file.getParentDirectory().getFileName().toUpperCase() << "\r\n";
  138. *cpp << "#ifdef " << file.getParentDirectory().getFileName().toUpperCase() << "\r\n";
  139. totalBytes += addFile (file, className, *header, *cpp);
  140. *header << " #endif\r\n";
  141. *cpp << "#endif\r\n";
  142. }
  143. else
  144. {
  145. totalBytes += addFile (file, className, *header, *cpp);
  146. }
  147. }
  148. }
  149. *header << "};\r\n\r\n"
  150. "#endif\r\n";
  151. header = 0;
  152. cpp = 0;
  153. std::cout << std::endl << " Total size of binary data: " << totalBytes << " bytes" << std::endl;
  154. shutdownJuce_NonGUI();
  155. return 0;
  156. }