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.

224 lines
7.5KB

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