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.

502 lines
13KB

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