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.

314 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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/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 void writeCppData (InputStream& in, OutputStream& out)
  81. {
  82. const int maxCharsOnLine = 250;
  83. MemoryBlock mb;
  84. in.readIntoMemoryBlock (mb);
  85. const unsigned char* data = (const unsigned char*) mb.getData();
  86. int charsOnLine = 0;
  87. bool canUseStringLiteral = mb.getSize() < 65535; // MS compilers can't handle strings bigger than 65536 chars..
  88. if (canUseStringLiteral)
  89. {
  90. for (size_t i = 0; i < mb.getSize(); ++i)
  91. {
  92. const unsigned int num = (unsigned int) data[i];
  93. if (! ((num >= 32 && num < 127) || num == '\t' || num == '\r' || num == '\n'))
  94. {
  95. canUseStringLiteral = false;
  96. break;
  97. }
  98. }
  99. }
  100. if (! canUseStringLiteral)
  101. {
  102. out << "{ ";
  103. for (size_t i = 0; i < mb.getSize(); ++i)
  104. {
  105. const int num = (int) (unsigned int) data[i];
  106. out << num << ',';
  107. charsOnLine += 2;
  108. if (num >= 10)
  109. ++charsOnLine;
  110. if (num >= 100)
  111. ++charsOnLine;
  112. if (charsOnLine >= maxCharsOnLine)
  113. {
  114. charsOnLine = 0;
  115. out << newLine;
  116. }
  117. }
  118. out << "0,0 };";
  119. }
  120. else
  121. {
  122. out << "\"";
  123. for (size_t i = 0; i < mb.getSize(); ++i)
  124. {
  125. const unsigned int num = (unsigned int) data[i];
  126. switch (num)
  127. {
  128. case '\t': out << "\\t"; break;
  129. case '\r': out << "\\r"; break;
  130. case '\n': out << "\\n"; charsOnLine = maxCharsOnLine; break;
  131. case '"': out << "\\\""; break;
  132. case '\\': out << "\\\\"; break;
  133. default: out << (char) num; break;
  134. }
  135. if (++charsOnLine >= maxCharsOnLine && i < mb.getSize() - 1)
  136. {
  137. charsOnLine = 0;
  138. out << "\"" << newLine << "\"";
  139. }
  140. }
  141. out << "\";";
  142. }
  143. }
  144. static int calcResourceHashCode (const String& s)
  145. {
  146. const char* t = s.toUTF8();
  147. int hash = 0;
  148. while (*t != 0)
  149. hash = 31 * hash + *t++;
  150. return hash;
  151. }
  152. bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream& header)
  153. {
  154. String comment;
  155. comment << newLine << newLine
  156. << " This is an auto-generated file, created by " << JUCEApplication::getInstance()->getApplicationName() << newLine
  157. << " Do not edit anything in this file!" << newLine << newLine
  158. << "*/" << newLine << newLine;
  159. header << "/* ========================================================================================="
  160. << comment;
  161. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  162. << comment;
  163. if (juceHeader.exists())
  164. header << createIncludeStatement (juceHeader, cppFile) << newLine;
  165. const String namespaceName (className);
  166. StringArray variableNames;
  167. int i;
  168. for (i = 0; i < files.size(); ++i)
  169. {
  170. String variableNameRoot (makeValidCppIdentifier (files.getUnchecked(i)->getFileName()
  171. .replaceCharacters (" .", "__")
  172. .retainCharacters ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"),
  173. false, true, false));
  174. String variableName (variableNameRoot);
  175. int suffix = 2;
  176. while (variableNames.contains (variableName))
  177. variableName = variableNameRoot + String (suffix++);
  178. variableNames.add (variableName);
  179. }
  180. cpp << createIncludeStatement (cppFile.withFileExtension (".h"), cppFile) << newLine
  181. << newLine
  182. << newLine
  183. << "const char* " << namespaceName << "::getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  184. << "{" << newLine
  185. << " int hash = 0;" << newLine
  186. << " if (resourceNameUTF8 != 0)" << newLine
  187. << " while (*resourceNameUTF8 != 0)" << newLine
  188. << " hash = 31 * hash + *resourceNameUTF8++;" << newLine
  189. << newLine
  190. << " switch (hash)" << newLine
  191. << " {" << newLine;
  192. for (i = 0; i < files.size(); ++i)
  193. {
  194. cpp << " case 0x" << hexString8Digits (calcResourceHashCode (variableNames[i]))
  195. << ": numBytes = " << namespaceName << "::" << variableNames[i] << "Size; return "
  196. << namespaceName << "::" << variableNames[i] << ";" << newLine;
  197. }
  198. cpp << " default: break;" << newLine
  199. << " }" << newLine
  200. << newLine
  201. << " numBytes = 0;" << newLine
  202. << " return 0;" << newLine
  203. << "}" << newLine
  204. << newLine;
  205. header << "namespace " << namespaceName << newLine << "{" << newLine;
  206. for (i = 0; i < files.size(); ++i)
  207. {
  208. const File file (*files.getUnchecked(i));
  209. const int64 dataSize = file.getSize();
  210. ScopedPointer <InputStream> fileStream (file.createInputStream());
  211. jassert (fileStream != 0);
  212. if (fileStream != 0)
  213. {
  214. const String variableName (variableNames[i]);
  215. const String tempVariable ("temp_" + String::toHexString (file.hashCode()));
  216. header << " extern const char* " << variableName << ";" << newLine;
  217. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  218. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  219. << "static const unsigned char " << tempVariable
  220. << "[] =" << newLine;
  221. writeCppData (*fileStream, cpp);
  222. cpp << newLine << newLine
  223. << "const char* " << namespaceName << "::" << variableName << " = (const char*) "
  224. << tempVariable << ";" << newLine;
  225. }
  226. }
  227. header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  228. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  229. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  230. << "}" << newLine;
  231. return true;
  232. }
  233. bool ResourceFile::write (const File& cppFile)
  234. {
  235. TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile);
  236. TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile);
  237. ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768));
  238. ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768));
  239. if (cppOut != 0 && hppOut != 0)
  240. {
  241. if (write (cppFile, *cppOut, *hppOut))
  242. {
  243. cppOut = 0;
  244. hppOut = 0;
  245. return (areFilesIdentical (tempCpp.getFile(), tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary())
  246. && (areFilesIdentical (tempH.getFile(), tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary());
  247. }
  248. }
  249. return false;
  250. }