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.

241 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_ResourceFile.h"
  19. #include "../../ui/Project Editor/jucer_ProjectTreeViewBase.h"
  20. #include "../../ui/jucer_OpenDocumentManager.h"
  21. static const char* resourceFileIdentifierString = "JUCER_BINARY_RESOURCE";
  22. //==============================================================================
  23. ResourceFile::ResourceFile (Project& project_)
  24. : project (project_),
  25. className ("BinaryData")
  26. {
  27. addResourcesFromProjectItem (project.getMainGroup());
  28. }
  29. ResourceFile::~ResourceFile()
  30. {
  31. }
  32. bool ResourceFile::isResourceFile (const File& file)
  33. {
  34. if (file.hasFileExtension ("cpp;h"))
  35. {
  36. ScopedPointer <InputStream> in (file.createInputStream());
  37. if (in != 0)
  38. {
  39. MemoryBlock mb;
  40. in->readIntoMemoryBlock (mb, 256);
  41. return mb.toString().contains (resourceFileIdentifierString);
  42. }
  43. }
  44. return false;
  45. }
  46. //==============================================================================
  47. void ResourceFile::addResourcesFromProjectItem (const Project::Item& projectItem)
  48. {
  49. if (projectItem.isGroup())
  50. {
  51. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  52. addResourcesFromProjectItem (projectItem.getChild(i));
  53. }
  54. else
  55. {
  56. if (projectItem.shouldBeAddedToBinaryResources())
  57. addFile (projectItem.getFile());
  58. }
  59. }
  60. //==============================================================================
  61. void ResourceFile::setJuceHeaderToInclude (const File& header)
  62. {
  63. juceHeader = header;
  64. }
  65. void ResourceFile::setClassName (const String& className_)
  66. {
  67. className = className_;
  68. }
  69. void ResourceFile::addFile (const File& file)
  70. {
  71. files.add (new File (file));
  72. }
  73. int64 ResourceFile::getTotalDataSize() const
  74. {
  75. int64 total = 0;
  76. for (int i = 0; i < files.size(); ++i)
  77. total += files.getUnchecked(i)->getSize();
  78. return total;
  79. }
  80. static int calcResourceHashCode (const String& s)
  81. {
  82. const char* t = s.toUTF8();
  83. int hash = 0;
  84. while (*t != 0)
  85. hash = 31 * hash + *t++;
  86. return hash;
  87. }
  88. bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream& header)
  89. {
  90. String comment;
  91. comment << newLine << newLine
  92. << " This is an auto-generated file, created by " << JUCEApplication::getInstance()->getApplicationName() << newLine
  93. << " Do not edit anything in this file!" << newLine << newLine
  94. << "*/" << newLine << newLine;
  95. header << "/* ========================================================================================="
  96. << comment;
  97. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  98. << comment;
  99. if (juceHeader.exists())
  100. header << CodeFormatting::createIncludeStatement (juceHeader, cppFile) << newLine;
  101. const String namespaceName (className);
  102. StringArray variableNames;
  103. int i;
  104. for (i = 0; i < files.size(); ++i)
  105. {
  106. String variableNameRoot (CodeFormatting::makeValidIdentifier (files.getUnchecked(i)->getFileName()
  107. .replaceCharacters (" .", "__")
  108. .retainCharacters ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"),
  109. false, true, false));
  110. String variableName (variableNameRoot);
  111. int suffix = 2;
  112. while (variableNames.contains (variableName))
  113. variableName = variableNameRoot + String (suffix++);
  114. variableNames.add (variableName);
  115. }
  116. cpp << CodeFormatting::createIncludeStatement (cppFile.withFileExtension (".h"), cppFile) << newLine
  117. << newLine
  118. << newLine
  119. << "const char* " << namespaceName << "::getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  120. << "{" << newLine
  121. << " int hash = 0;" << newLine
  122. << " if (resourceNameUTF8 != 0)" << newLine
  123. << " while (*resourceNameUTF8 != 0)" << newLine
  124. << " hash = 31 * hash + *resourceNameUTF8++;" << newLine
  125. << newLine
  126. << " switch (hash)" << newLine
  127. << " {" << newLine;
  128. for (i = 0; i < files.size(); ++i)
  129. {
  130. cpp << " case 0x" << hexString8Digits (calcResourceHashCode (variableNames[i]))
  131. << ": numBytes = " << namespaceName << "::" << variableNames[i] << "Size; return "
  132. << namespaceName << "::" << variableNames[i] << ";" << newLine;
  133. }
  134. cpp << " default: break;" << newLine
  135. << " }" << newLine
  136. << newLine
  137. << " numBytes = 0;" << newLine
  138. << " return 0;" << newLine
  139. << "}" << newLine
  140. << newLine;
  141. header << "namespace " << namespaceName << newLine << "{" << newLine;
  142. for (i = 0; i < files.size(); ++i)
  143. {
  144. const File file (*files.getUnchecked(i));
  145. const int64 dataSize = file.getSize();
  146. ScopedPointer <InputStream> fileStream (file.createInputStream());
  147. jassert (fileStream != 0);
  148. if (fileStream != 0)
  149. {
  150. const String variableName (variableNames[i]);
  151. const String tempVariable ("temp_" + String::toHexString (file.hashCode()));
  152. header << " extern const char* " << variableName << ";" << newLine;
  153. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  154. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  155. << "static const unsigned char " << tempVariable
  156. << "[] =" << newLine;
  157. {
  158. MemoryBlock data;
  159. fileStream->readIntoMemoryBlock (data);
  160. CodeFormatting::writeDataAsCppLiteral (data, cpp);
  161. }
  162. cpp << newLine << newLine
  163. << "const char* " << namespaceName << "::" << variableName << " = (const char*) "
  164. << tempVariable << ";" << newLine;
  165. }
  166. }
  167. header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  168. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  169. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  170. << "}" << newLine;
  171. return true;
  172. }
  173. bool ResourceFile::write (const File& cppFile)
  174. {
  175. TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile);
  176. TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile);
  177. ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768));
  178. ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768));
  179. if (cppOut != 0 && hppOut != 0)
  180. {
  181. if (write (cppFile, *cppOut, *hppOut))
  182. {
  183. cppOut = 0;
  184. hppOut = 0;
  185. return (FileUtils::areFilesIdentical (tempCpp.getFile(), tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary())
  186. && (FileUtils::areFilesIdentical (tempH.getFile(), tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary());
  187. }
  188. }
  189. return false;
  190. }