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.

200 lines
6.8KB

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