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.

493 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 = s.findEndOfWhitespace();
  99. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  100. token << s.getAndAdvance();
  101. s = s.findEndOfWhitespace();
  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. StringArray getJUCEModules() noexcept
  197. {
  198. static StringArray juceModuleIds =
  199. {
  200. "juce_analytics",
  201. "juce_audio_basics",
  202. "juce_audio_devices",
  203. "juce_audio_formats",
  204. "juce_audio_plugin_client",
  205. "juce_audio_processors",
  206. "juce_audio_utils",
  207. "juce_blocks_basics",
  208. "juce_box2d",
  209. "juce_core",
  210. "juce_cryptography",
  211. "juce_data_structures",
  212. "juce_dsp",
  213. "juce_events",
  214. "juce_graphics",
  215. "juce_gui_basics",
  216. "juce_gui_extra",
  217. "juce_opengl",
  218. "juce_osc",
  219. "juce_product_unlocking",
  220. "juce_video"
  221. };
  222. return juceModuleIds;
  223. }
  224. bool isJUCEModule (const String& moduleID) noexcept
  225. {
  226. return getJUCEModules().contains (moduleID);
  227. }
  228. StringArray getModulesRequiredForConsole() noexcept
  229. {
  230. return
  231. {
  232. "juce_core",
  233. "juce_data_structures",
  234. "juce_events"
  235. };
  236. }
  237. StringArray getModulesRequiredForComponent() noexcept
  238. {
  239. return
  240. {
  241. "juce_core",
  242. "juce_data_structures",
  243. "juce_events",
  244. "juce_graphics",
  245. "juce_gui_basics"
  246. };
  247. }
  248. StringArray getModulesRequiredForAudioProcessor() noexcept
  249. {
  250. return
  251. {
  252. "juce_audio_basics",
  253. "juce_audio_devices",
  254. "juce_audio_formats",
  255. "juce_audio_plugin_client",
  256. "juce_audio_processors",
  257. "juce_audio_utils",
  258. "juce_core",
  259. "juce_data_structures",
  260. "juce_events",
  261. "juce_graphics",
  262. "juce_gui_basics",
  263. "juce_gui_extra"
  264. };
  265. }
  266. bool isPIPFile (const File& file) noexcept
  267. {
  268. for (auto line : StringArray::fromLines (file.loadFileAsString()))
  269. {
  270. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  271. if (trimmedLine.startsWith ("BEGIN_JUCE_PIP_METADATA"))
  272. return true;
  273. }
  274. return false;
  275. }
  276. bool isValidJUCEExamplesDirectory (const File& directory) noexcept
  277. {
  278. if (! directory.exists() || ! directory.isDirectory() || ! directory.containsSubDirectories())
  279. return false;
  280. return directory.getChildFile ("Assets").getChildFile ("juce_icon.png").existsAsFile();
  281. }
  282. bool isJUCEFolder (const File& f)
  283. {
  284. return isJUCEModulesFolder (f.getChildFile ("modules"));
  285. }
  286. bool isJUCEModulesFolder (const File& f)
  287. {
  288. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  289. }
  290. //==============================================================================
  291. bool isDivider (const String& line)
  292. {
  293. auto afterIndent = line.trim();
  294. if (afterIndent.startsWith ("//") && afterIndent.length() > 20)
  295. {
  296. afterIndent = afterIndent.substring (5);
  297. if (afterIndent.containsOnly ("=")
  298. || afterIndent.containsOnly ("/")
  299. || afterIndent.containsOnly ("-"))
  300. {
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. int getIndexOfCommentBlockStart (const StringArray& lines, int endIndex)
  307. {
  308. auto endLine = lines[endIndex];
  309. if (endLine.contains ("*/"))
  310. {
  311. for (int i = endIndex; i >= 0; --i)
  312. if (lines[i].contains ("/*"))
  313. return i;
  314. }
  315. if (endLine.trim().startsWith ("//") && ! isDivider (endLine))
  316. {
  317. for (int i = endIndex; i >= 0; --i)
  318. if (! lines[i].startsWith ("//") || isDivider (lines[i]))
  319. return i + 1;
  320. }
  321. return -1;
  322. }
  323. int findBestLineToScrollToForClass (StringArray lines, const String& className, bool isPlugin)
  324. {
  325. for (auto line : lines)
  326. {
  327. if (line.contains ("struct " + className) || line.contains ("class " + className)
  328. || (isPlugin && line.contains ("public AudioProcessor") && ! line.contains ("AudioProcessorEditor")))
  329. {
  330. auto index = lines.indexOf (line);
  331. auto commentBlockStartIndex = getIndexOfCommentBlockStart (lines, index - 1);
  332. if (commentBlockStartIndex != -1)
  333. index = commentBlockStartIndex;
  334. if (isDivider (lines[index - 1]))
  335. index -= 1;
  336. return index;
  337. }
  338. }
  339. return 0;
  340. }
  341. //==============================================================================
  342. var parseJUCEHeaderMetadata (const StringArray& lines)
  343. {
  344. auto* o = new DynamicObject();
  345. var result (o);
  346. for (auto& line : lines)
  347. {
  348. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  349. auto colon = trimmedLine.indexOfChar (':');
  350. if (colon >= 0)
  351. {
  352. auto key = trimmedLine.substring (0, colon).trim();
  353. auto value = trimmedLine.substring (colon + 1).trim();
  354. o->setProperty (key, value);
  355. }
  356. }
  357. return result;
  358. }
  359. String parseMetadataItem (const StringArray& lines, int& index)
  360. {
  361. String result = lines[index++];
  362. while (index < lines.size())
  363. {
  364. auto continuationLine = trimCommentCharsFromStartOfLine (lines[index]);
  365. if (continuationLine.isEmpty() || continuationLine.indexOfChar (':') != -1
  366. || continuationLine.startsWith ("END_JUCE_"))
  367. break;
  368. result += " " + continuationLine;
  369. ++index;
  370. }
  371. return result;
  372. }
  373. var parseJUCEHeaderMetadata (const File& file)
  374. {
  375. StringArray lines;
  376. file.readLines (lines);
  377. for (int i = 0; i < lines.size(); ++i)
  378. {
  379. auto trimmedLine = trimCommentCharsFromStartOfLine (lines[i]);
  380. if (trimmedLine.startsWith ("BEGIN_JUCE_"))
  381. {
  382. StringArray desc;
  383. auto j = i + 1;
  384. while (j < lines.size())
  385. {
  386. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_"))
  387. return parseJUCEHeaderMetadata (desc);
  388. desc.add (parseMetadataItem (lines, j));
  389. }
  390. }
  391. }
  392. return {};
  393. }