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) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. //==============================================================================
  20. String joinLinesIntoSourceFile (StringArray& lines)
  21. {
  22. while (lines.size() > 10 && lines [lines.size() - 1].isEmpty())
  23. lines.remove (lines.size() - 1);
  24. return lines.joinIntoString (getPreferredLineFeed()) + getPreferredLineFeed();
  25. }
  26. String replaceLineFeeds (const String& content, const String& lineFeed)
  27. {
  28. StringArray lines;
  29. lines.addLines (content);
  30. return lines.joinIntoString (lineFeed);
  31. }
  32. String getLineFeedForFile (const String& fileContent)
  33. {
  34. auto t = fileContent.getCharPointer();
  35. while (! t.isEmpty())
  36. {
  37. switch (t.getAndAdvance())
  38. {
  39. case 0: break;
  40. case '\n': return "\n";
  41. case '\r': if (*t == '\n') return "\r\n";
  42. default: continue;
  43. }
  44. }
  45. return {};
  46. }
  47. String trimCommentCharsFromStartOfLine (const String& line)
  48. {
  49. return line.trimStart().trimCharactersAtStart ("*/").trimStart();
  50. }
  51. String createAlphaNumericUID()
  52. {
  53. String uid;
  54. const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  55. Random r;
  56. uid << chars[r.nextInt (52)]; // make sure the first character is always a letter
  57. for (int i = 5; --i >= 0;)
  58. {
  59. r.setSeedRandomly();
  60. uid << chars [r.nextInt (62)];
  61. }
  62. return uid;
  63. }
  64. String createGUID (const String& seed)
  65. {
  66. auto hex = MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase();
  67. return "{" + hex.substring (0, 8)
  68. + "-" + hex.substring (8, 12)
  69. + "-" + hex.substring (12, 16)
  70. + "-" + hex.substring (16, 20)
  71. + "-" + hex.substring (20, 32)
  72. + "}";
  73. }
  74. String escapeSpaces (const String& s)
  75. {
  76. return s.replace (" ", "\\ ");
  77. }
  78. String escapeQuotesAndSpaces (const String& s)
  79. {
  80. return escapeSpaces (s).replace ("'", "\\'").replace ("\"", "\\\"");
  81. }
  82. String addQuotesIfContainsSpaces (const String& text)
  83. {
  84. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  85. }
  86. void setValueIfVoid (Value value, const var& defaultValue)
  87. {
  88. if (value.getValue().isVoid())
  89. value = defaultValue;
  90. }
  91. //==============================================================================
  92. StringPairArray parsePreprocessorDefs (const String& text)
  93. {
  94. StringPairArray result;
  95. auto s = text.getCharPointer();
  96. while (! s.isEmpty())
  97. {
  98. String token, value;
  99. s.incrementToEndOfWhitespace();
  100. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  101. token << s.getAndAdvance();
  102. s.incrementToEndOfWhitespace();
  103. if (*s == '=')
  104. {
  105. ++s;
  106. while ((! s.isEmpty()) && *s == ' ')
  107. ++s;
  108. while ((! s.isEmpty()) && ! s.isWhitespace())
  109. {
  110. if (*s == ',')
  111. {
  112. ++s;
  113. break;
  114. }
  115. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  116. ++s;
  117. value << s.getAndAdvance();
  118. }
  119. }
  120. if (token.isNotEmpty())
  121. result.set (token, value);
  122. }
  123. return result;
  124. }
  125. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  126. {
  127. for (int i = 0; i < overridingDefs.size(); ++i)
  128. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  129. return inheritedDefs;
  130. }
  131. String createGCCPreprocessorFlags (const StringPairArray& defs)
  132. {
  133. String s;
  134. for (int i = 0; i < defs.size(); ++i)
  135. {
  136. auto def = defs.getAllKeys()[i];
  137. auto value = defs.getAllValues()[i];
  138. if (value.isNotEmpty())
  139. def << "=" << value;
  140. s += " \"" + ("-D" + def).replace ("\"", "\\\"") + "\"";
  141. }
  142. return s;
  143. }
  144. StringArray getSearchPathsFromString (const String& searchPath)
  145. {
  146. StringArray s;
  147. s.addTokens (searchPath, ";\r\n", StringRef());
  148. return getCleanedStringArray (s);
  149. }
  150. StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
  151. {
  152. StringArray s;
  153. s.addTokens (sourceString, ", \t\r\n", StringRef());
  154. return getCleanedStringArray (s);
  155. }
  156. StringArray getCleanedStringArray (StringArray s)
  157. {
  158. s.trim();
  159. s.removeEmptyStrings();
  160. return s;
  161. }
  162. //==============================================================================
  163. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  164. {
  165. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  166. {
  167. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  168. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  169. }
  170. }
  171. //==============================================================================
  172. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  173. {
  174. const int len = text.length();
  175. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  176. {
  177. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  178. text.getCharPointer(), len) == 0)
  179. return index;
  180. ++index;
  181. }
  182. return -1;
  183. }
  184. //==============================================================================
  185. bool fileNeedsCppSyntaxHighlighting (const File& file)
  186. {
  187. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  188. return true;
  189. // This is a bit of a bodge to deal with libc++ headers with no extension..
  190. char fileStart[128] = { 0 };
  191. FileInputStream fin (file);
  192. fin.read (fileStart, sizeof (fileStart) - 4);
  193. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  194. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  195. }
  196. //==============================================================================
  197. void writeAutoGenWarningComment (OutputStream& outStream)
  198. {
  199. outStream << "/*" << newLine << newLine
  200. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  201. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  202. << newLine;
  203. }
  204. //==============================================================================
  205. StringArray getJUCEModules() noexcept
  206. {
  207. static StringArray juceModuleIds =
  208. {
  209. "juce_analytics",
  210. "juce_audio_basics",
  211. "juce_audio_devices",
  212. "juce_audio_formats",
  213. "juce_audio_plugin_client",
  214. "juce_audio_processors",
  215. "juce_audio_utils",
  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. }