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.

421 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../../Application/jucer_Headers.h"
  14. #ifdef BUILDING_JUCE_COMPILEENGINE
  15. const char* getPreferredLineFeed() { return "\r\n"; }
  16. #endif
  17. //==============================================================================
  18. String joinLinesIntoSourceFile (StringArray& lines)
  19. {
  20. while (lines.size() > 10 && lines [lines.size() - 1].isEmpty())
  21. lines.remove (lines.size() - 1);
  22. return lines.joinIntoString (getPreferredLineFeed()) + getPreferredLineFeed();
  23. }
  24. String replaceLineFeeds (const String& content, const String& lineFeed)
  25. {
  26. StringArray lines;
  27. lines.addLines (content);
  28. return lines.joinIntoString (lineFeed);
  29. }
  30. String getLineFeedForFile (const String& fileContent)
  31. {
  32. auto t = fileContent.getCharPointer();
  33. while (! t.isEmpty())
  34. {
  35. switch (t.getAndAdvance())
  36. {
  37. case 0: break;
  38. case '\n': return "\n";
  39. case '\r': if (*t == '\n') return "\r\n";
  40. default: continue;
  41. }
  42. }
  43. return {};
  44. }
  45. String trimCommentCharsFromStartOfLine (const String& line)
  46. {
  47. return line.trimStart().trimCharactersAtStart ("*/").trimStart();
  48. }
  49. String createAlphaNumericUID()
  50. {
  51. String uid;
  52. const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  53. Random r;
  54. uid << chars[r.nextInt (52)]; // make sure the first character is always a letter
  55. for (int i = 5; --i >= 0;)
  56. {
  57. r.setSeedRandomly();
  58. uid << chars [r.nextInt (62)];
  59. }
  60. return uid;
  61. }
  62. String createGUID (const String& seed)
  63. {
  64. auto hex = MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase();
  65. return "{" + hex.substring (0, 8)
  66. + "-" + hex.substring (8, 12)
  67. + "-" + hex.substring (12, 16)
  68. + "-" + hex.substring (16, 20)
  69. + "-" + hex.substring (20, 32)
  70. + "}";
  71. }
  72. String escapeSpaces (const String& s)
  73. {
  74. return s.replace (" ", "\\ ");
  75. }
  76. String addQuotesIfContainsSpaces (const String& text)
  77. {
  78. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  79. }
  80. void setValueIfVoid (Value value, const var& defaultValue)
  81. {
  82. if (value.getValue().isVoid())
  83. value = defaultValue;
  84. }
  85. //==============================================================================
  86. StringPairArray parsePreprocessorDefs (const String& text)
  87. {
  88. StringPairArray result;
  89. auto s = text.getCharPointer();
  90. while (! s.isEmpty())
  91. {
  92. String token, value;
  93. s = s.findEndOfWhitespace();
  94. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  95. token << s.getAndAdvance();
  96. s = s.findEndOfWhitespace();
  97. if (*s == '=')
  98. {
  99. ++s;
  100. while ((! s.isEmpty()) && *s == ' ')
  101. ++s;
  102. while ((! s.isEmpty()) && ! s.isWhitespace())
  103. {
  104. if (*s == ',')
  105. {
  106. ++s;
  107. break;
  108. }
  109. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  110. ++s;
  111. value << s.getAndAdvance();
  112. }
  113. }
  114. if (token.isNotEmpty())
  115. result.set (token, value);
  116. }
  117. return result;
  118. }
  119. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  120. {
  121. for (int i = 0; i < overridingDefs.size(); ++i)
  122. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  123. return inheritedDefs;
  124. }
  125. String createGCCPreprocessorFlags (const StringPairArray& defs)
  126. {
  127. String s;
  128. for (int i = 0; i < defs.size(); ++i)
  129. {
  130. auto def = defs.getAllKeys()[i];
  131. auto value = defs.getAllValues()[i];
  132. if (value.isNotEmpty())
  133. def << "=" << value;
  134. s += " \"" + ("-D" + def).replace ("\"", "\\\"") + "\"";
  135. }
  136. return s;
  137. }
  138. StringArray getSearchPathsFromString (const String& searchPath)
  139. {
  140. StringArray s;
  141. s.addTokens (searchPath, ";\r\n", StringRef());
  142. return getCleanedStringArray (s);
  143. }
  144. StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
  145. {
  146. StringArray s;
  147. s.addTokens (sourceString, ", \t\r\n", StringRef());
  148. return getCleanedStringArray (s);
  149. }
  150. StringArray getCleanedStringArray (StringArray s)
  151. {
  152. s.trim();
  153. s.removeEmptyStrings();
  154. return s;
  155. }
  156. //==============================================================================
  157. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  158. {
  159. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  160. {
  161. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  162. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  163. }
  164. }
  165. //==============================================================================
  166. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  167. {
  168. const int len = text.length();
  169. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  170. {
  171. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  172. text.getCharPointer(), len) == 0)
  173. return index;
  174. ++index;
  175. }
  176. return -1;
  177. }
  178. //==============================================================================
  179. bool fileNeedsCppSyntaxHighlighting (const File& file)
  180. {
  181. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  182. return true;
  183. // This is a bit of a bodge to deal with libc++ headers with no extension..
  184. char fileStart[128] = { 0 };
  185. FileInputStream fin (file);
  186. fin.read (fileStart, sizeof (fileStart) - 4);
  187. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  188. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  189. }
  190. //==============================================================================
  191. StringArray getJUCEModules() noexcept
  192. {
  193. static StringArray juceModuleIds =
  194. {
  195. "juce_analytics",
  196. "juce_audio_basics",
  197. "juce_audio_devices",
  198. "juce_audio_formats",
  199. "juce_audio_plugin_client",
  200. "juce_audio_processors",
  201. "juce_audio_utils",
  202. "juce_blocks_basics",
  203. "juce_box2d",
  204. "juce_core",
  205. "juce_cryptography",
  206. "juce_data_structures",
  207. "juce_dsp",
  208. "juce_events",
  209. "juce_graphics",
  210. "juce_gui_basics",
  211. "juce_gui_extra",
  212. "juce_opengl",
  213. "juce_osc",
  214. "juce_product_unlocking",
  215. "juce_video"
  216. };
  217. return juceModuleIds;
  218. }
  219. bool isJUCEModule (const String& moduleID) noexcept
  220. {
  221. return getJUCEModules().contains (moduleID);
  222. }
  223. StringArray getModulesRequiredForConsole() noexcept
  224. {
  225. return
  226. {
  227. "juce_core",
  228. "juce_data_structures",
  229. "juce_events"
  230. };
  231. }
  232. StringArray getModulesRequiredForComponent() noexcept
  233. {
  234. return
  235. {
  236. "juce_core",
  237. "juce_data_structures",
  238. "juce_events",
  239. "juce_graphics",
  240. "juce_gui_basics"
  241. };
  242. }
  243. StringArray getModulesRequiredForAudioProcessor() noexcept
  244. {
  245. return
  246. {
  247. "juce_audio_basics",
  248. "juce_audio_devices",
  249. "juce_audio_formats",
  250. "juce_audio_plugin_client",
  251. "juce_audio_processors",
  252. "juce_audio_utils",
  253. "juce_core",
  254. "juce_data_structures",
  255. "juce_events",
  256. "juce_graphics",
  257. "juce_gui_basics",
  258. "juce_gui_extra"
  259. };
  260. }
  261. bool isPIPFile (const File& file) noexcept
  262. {
  263. for (auto line : StringArray::fromLines (file.loadFileAsString()))
  264. {
  265. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  266. if (trimmedLine.startsWith ("BEGIN_JUCE_PIP_METADATA"))
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool isValidJUCEExamplesDirectory (const File& directory) noexcept
  272. {
  273. if (! directory.exists() || ! directory.isDirectory() || ! directory.containsSubDirectories())
  274. return false;
  275. return directory.getChildFile ("Assets").getChildFile ("juce_icon.png").existsAsFile();
  276. }
  277. bool isJUCEFolder (const File& f)
  278. {
  279. return isJUCEModulesFolder (f.getChildFile ("modules"));
  280. }
  281. bool isJUCEModulesFolder (const File& f)
  282. {
  283. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  284. }
  285. //==============================================================================
  286. static var parseJUCEHeaderMetadata (const StringArray& lines)
  287. {
  288. auto* o = new DynamicObject();
  289. var result (o);
  290. for (auto& line : lines)
  291. {
  292. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  293. auto colon = trimmedLine.indexOfChar (':');
  294. if (colon >= 0)
  295. {
  296. auto key = trimmedLine.substring (0, colon).trim();
  297. auto value = trimmedLine.substring (colon + 1).trim();
  298. o->setProperty (key, value);
  299. }
  300. }
  301. return result;
  302. }
  303. static String parseMetadataItem (const StringArray& lines, int& index)
  304. {
  305. String result = lines[index++];
  306. while (index < lines.size())
  307. {
  308. auto continuationLine = trimCommentCharsFromStartOfLine (lines[index]);
  309. if (continuationLine.isEmpty() || continuationLine.indexOfChar (':') != -1
  310. || continuationLine.startsWith ("END_JUCE_"))
  311. break;
  312. result += " " + continuationLine;
  313. ++index;
  314. }
  315. return result;
  316. }
  317. var parseJUCEHeaderMetadata (const File& file)
  318. {
  319. StringArray lines;
  320. file.readLines (lines);
  321. for (int i = 0; i < lines.size(); ++i)
  322. {
  323. auto trimmedLine = trimCommentCharsFromStartOfLine (lines[i]);
  324. if (trimmedLine.startsWith ("BEGIN_JUCE_"))
  325. {
  326. StringArray desc;
  327. auto j = i + 1;
  328. while (j < lines.size())
  329. {
  330. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_"))
  331. return parseJUCEHeaderMetadata (desc);
  332. desc.add (parseMetadataItem (lines, j));
  333. }
  334. }
  335. }
  336. return {};
  337. }