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.

383 lines
11KB

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