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.

497 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. //==============================================================================
  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 addQuotesIfContainsSpaces (const String& text)
  79. {
  80. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  81. }
  82. void setValueIfVoid (Value value, const var& defaultValue)
  83. {
  84. if (value.getValue().isVoid())
  85. value = defaultValue;
  86. }
  87. //==============================================================================
  88. StringPairArray parsePreprocessorDefs (const String& text)
  89. {
  90. StringPairArray result;
  91. auto s = text.getCharPointer();
  92. while (! s.isEmpty())
  93. {
  94. String token, value;
  95. s.incrementToEndOfWhitespace();
  96. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  97. token << s.getAndAdvance();
  98. s.incrementToEndOfWhitespace();
  99. if (*s == '=')
  100. {
  101. ++s;
  102. while ((! s.isEmpty()) && *s == ' ')
  103. ++s;
  104. while ((! s.isEmpty()) && ! s.isWhitespace())
  105. {
  106. if (*s == ',')
  107. {
  108. ++s;
  109. break;
  110. }
  111. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  112. ++s;
  113. value << s.getAndAdvance();
  114. }
  115. }
  116. if (token.isNotEmpty())
  117. result.set (token, value);
  118. }
  119. return result;
  120. }
  121. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  122. {
  123. for (int i = 0; i < overridingDefs.size(); ++i)
  124. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  125. return inheritedDefs;
  126. }
  127. String createGCCPreprocessorFlags (const StringPairArray& defs)
  128. {
  129. String s;
  130. for (int i = 0; i < defs.size(); ++i)
  131. {
  132. auto def = defs.getAllKeys()[i];
  133. auto value = defs.getAllValues()[i];
  134. if (value.isNotEmpty())
  135. def << "=" << value;
  136. s += " \"" + ("-D" + def).replace ("\"", "\\\"") + "\"";
  137. }
  138. return s;
  139. }
  140. StringArray getSearchPathsFromString (const String& searchPath)
  141. {
  142. StringArray s;
  143. s.addTokens (searchPath, ";\r\n", StringRef());
  144. return getCleanedStringArray (s);
  145. }
  146. StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
  147. {
  148. StringArray s;
  149. s.addTokens (sourceString, ", \t\r\n", StringRef());
  150. return getCleanedStringArray (s);
  151. }
  152. StringArray getCleanedStringArray (StringArray s)
  153. {
  154. s.trim();
  155. s.removeEmptyStrings();
  156. return s;
  157. }
  158. //==============================================================================
  159. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  160. {
  161. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  162. {
  163. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  164. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  165. }
  166. }
  167. //==============================================================================
  168. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  169. {
  170. const int len = text.length();
  171. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  172. {
  173. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  174. text.getCharPointer(), len) == 0)
  175. return index;
  176. ++index;
  177. }
  178. return -1;
  179. }
  180. //==============================================================================
  181. bool fileNeedsCppSyntaxHighlighting (const File& file)
  182. {
  183. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  184. return true;
  185. // This is a bit of a bodge to deal with libc++ headers with no extension..
  186. char fileStart[128] = { 0 };
  187. FileInputStream fin (file);
  188. fin.read (fileStart, sizeof (fileStart) - 4);
  189. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  190. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  191. }
  192. //==============================================================================
  193. void writeAutoGenWarningComment (OutputStream& outStream)
  194. {
  195. outStream << "/*" << newLine << newLine
  196. << " IMPORTANT! This file is auto-generated each time you save your" << newLine
  197. << " project - if you alter its contents, your changes may be overwritten!" << newLine
  198. << newLine;
  199. }
  200. //==============================================================================
  201. StringArray getJUCEModules() noexcept
  202. {
  203. static StringArray juceModuleIds =
  204. {
  205. "juce_analytics",
  206. "juce_audio_basics",
  207. "juce_audio_devices",
  208. "juce_audio_formats",
  209. "juce_audio_plugin_client",
  210. "juce_audio_processors",
  211. "juce_audio_utils",
  212. "juce_box2d",
  213. "juce_core",
  214. "juce_cryptography",
  215. "juce_data_structures",
  216. "juce_dsp",
  217. "juce_events",
  218. "juce_graphics",
  219. "juce_gui_basics",
  220. "juce_gui_extra",
  221. "juce_opengl",
  222. "juce_osc",
  223. "juce_product_unlocking",
  224. "juce_video"
  225. };
  226. return juceModuleIds;
  227. }
  228. bool isJUCEModule (const String& moduleID) noexcept
  229. {
  230. return getJUCEModules().contains (moduleID);
  231. }
  232. StringArray getModulesRequiredForConsole() noexcept
  233. {
  234. return
  235. {
  236. "juce_core",
  237. "juce_data_structures",
  238. "juce_events"
  239. };
  240. }
  241. StringArray getModulesRequiredForComponent() noexcept
  242. {
  243. return
  244. {
  245. "juce_core",
  246. "juce_data_structures",
  247. "juce_events",
  248. "juce_graphics",
  249. "juce_gui_basics"
  250. };
  251. }
  252. StringArray getModulesRequiredForAudioProcessor() noexcept
  253. {
  254. return
  255. {
  256. "juce_audio_basics",
  257. "juce_audio_devices",
  258. "juce_audio_formats",
  259. "juce_audio_plugin_client",
  260. "juce_audio_processors",
  261. "juce_audio_utils",
  262. "juce_core",
  263. "juce_data_structures",
  264. "juce_events",
  265. "juce_graphics",
  266. "juce_gui_basics",
  267. "juce_gui_extra"
  268. };
  269. }
  270. bool isPIPFile (const File& file) noexcept
  271. {
  272. for (auto line : StringArray::fromLines (file.loadFileAsString()))
  273. {
  274. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  275. if (trimmedLine.startsWith ("BEGIN_JUCE_PIP_METADATA"))
  276. return true;
  277. }
  278. return false;
  279. }
  280. bool isValidJUCEExamplesDirectory (const File& directory) noexcept
  281. {
  282. if (! directory.exists() || ! directory.isDirectory() || ! directory.containsSubDirectories())
  283. return false;
  284. return directory.getChildFile ("Assets").getChildFile ("juce_icon.png").existsAsFile();
  285. }
  286. bool isJUCEFolder (const File& f)
  287. {
  288. return isJUCEModulesFolder (f.getChildFile ("modules"));
  289. }
  290. bool isJUCEModulesFolder (const File& f)
  291. {
  292. return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
  293. }
  294. //==============================================================================
  295. static bool isDivider (const String& line)
  296. {
  297. auto afterIndent = line.trim();
  298. if (afterIndent.startsWith ("//") && afterIndent.length() > 20)
  299. {
  300. afterIndent = afterIndent.substring (5);
  301. if (afterIndent.containsOnly ("=")
  302. || afterIndent.containsOnly ("/")
  303. || afterIndent.containsOnly ("-"))
  304. {
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. static int getIndexOfCommentBlockStart (const StringArray& lines, int endIndex)
  311. {
  312. auto endLine = lines[endIndex];
  313. if (endLine.contains ("*/"))
  314. {
  315. for (int i = endIndex; i >= 0; --i)
  316. if (lines[i].contains ("/*"))
  317. return i;
  318. }
  319. if (endLine.trim().startsWith ("//") && ! isDivider (endLine))
  320. {
  321. for (int i = endIndex; i >= 0; --i)
  322. if (! lines[i].startsWith ("//") || isDivider (lines[i]))
  323. return i + 1;
  324. }
  325. return -1;
  326. }
  327. int findBestLineToScrollToForClass (StringArray lines, const String& className, bool isPlugin)
  328. {
  329. for (auto line : lines)
  330. {
  331. if (line.contains ("struct " + className) || line.contains ("class " + className)
  332. || (isPlugin && line.contains ("public AudioProcessor") && ! line.contains ("AudioProcessorEditor")))
  333. {
  334. auto index = lines.indexOf (line);
  335. auto commentBlockStartIndex = getIndexOfCommentBlockStart (lines, index - 1);
  336. if (commentBlockStartIndex != -1)
  337. index = commentBlockStartIndex;
  338. if (isDivider (lines[index - 1]))
  339. index -= 1;
  340. return index;
  341. }
  342. }
  343. return 0;
  344. }
  345. //==============================================================================
  346. static var parseJUCEHeaderMetadata (const StringArray& lines)
  347. {
  348. auto* o = new DynamicObject();
  349. var result (o);
  350. for (auto& line : lines)
  351. {
  352. auto trimmedLine = trimCommentCharsFromStartOfLine (line);
  353. auto colon = trimmedLine.indexOfChar (':');
  354. if (colon >= 0)
  355. {
  356. auto key = trimmedLine.substring (0, colon).trim();
  357. auto value = trimmedLine.substring (colon + 1).trim();
  358. o->setProperty (key, value);
  359. }
  360. }
  361. return result;
  362. }
  363. static String parseMetadataItem (const StringArray& lines, int& index)
  364. {
  365. String result = lines[index++];
  366. while (index < lines.size())
  367. {
  368. auto continuationLine = trimCommentCharsFromStartOfLine (lines[index]);
  369. if (continuationLine.isEmpty() || continuationLine.indexOfChar (':') != -1
  370. || continuationLine.startsWith ("END_JUCE_"))
  371. break;
  372. result += " " + continuationLine;
  373. ++index;
  374. }
  375. return result;
  376. }
  377. var parseJUCEHeaderMetadata (const File& file)
  378. {
  379. StringArray lines;
  380. file.readLines (lines);
  381. for (int i = 0; i < lines.size(); ++i)
  382. {
  383. auto trimmedLine = trimCommentCharsFromStartOfLine (lines[i]);
  384. if (trimmedLine.startsWith ("BEGIN_JUCE_"))
  385. {
  386. StringArray desc;
  387. auto j = i + 1;
  388. while (j < lines.size())
  389. {
  390. if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_"))
  391. return parseJUCEHeaderMetadata (desc);
  392. desc.add (parseMetadataItem (lines, j));
  393. }
  394. }
  395. }
  396. return {};
  397. }