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.

276 lines
7.9KB

  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 "../jucer_Headers.h"
  20. //==============================================================================
  21. String createAlphaNumericUID()
  22. {
  23. String uid;
  24. const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  25. Random r;
  26. uid << chars [r.nextInt (52)]; // make sure the first character is always a letter
  27. for (int i = 5; --i >= 0;)
  28. {
  29. r.setSeedRandomly();
  30. uid << chars [r.nextInt (62)];
  31. }
  32. return uid;
  33. }
  34. String hexString8Digits (int value)
  35. {
  36. return String::toHexString (value).paddedLeft ('0', 8);
  37. }
  38. String createGUID (const String& seed)
  39. {
  40. const String hex (MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase());
  41. return "{" + hex.substring (0, 8)
  42. + "-" + hex.substring (8, 12)
  43. + "-" + hex.substring (12, 16)
  44. + "-" + hex.substring (16, 20)
  45. + "-" + hex.substring (20, 32)
  46. + "}";
  47. }
  48. String escapeSpaces (const String& s)
  49. {
  50. return s.replace (" ", "\\ ");
  51. }
  52. String addQuotesIfContainsSpaces (const String& text)
  53. {
  54. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  55. }
  56. void setValueIfVoid (Value value, const var& defaultValue)
  57. {
  58. if (value.getValue().isVoid())
  59. value = defaultValue;
  60. }
  61. //==============================================================================
  62. StringPairArray parsePreprocessorDefs (const String& text)
  63. {
  64. StringPairArray result;
  65. String::CharPointerType s (text.getCharPointer());
  66. while (! s.isEmpty())
  67. {
  68. String token, value;
  69. s = s.findEndOfWhitespace();
  70. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  71. token << s.getAndAdvance();
  72. s = s.findEndOfWhitespace();
  73. if (*s == '=')
  74. {
  75. ++s;
  76. s = s.findEndOfWhitespace();
  77. while ((! s.isEmpty()) && ! s.isWhitespace())
  78. {
  79. if (*s == ',')
  80. {
  81. ++s;
  82. break;
  83. }
  84. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  85. ++s;
  86. value << s.getAndAdvance();
  87. }
  88. }
  89. if (token.isNotEmpty())
  90. result.set (token, value);
  91. }
  92. return result;
  93. }
  94. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  95. {
  96. for (int i = 0; i < overridingDefs.size(); ++i)
  97. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  98. return inheritedDefs;
  99. }
  100. String createGCCPreprocessorFlags (const StringPairArray& defs)
  101. {
  102. String s;
  103. for (int i = 0; i < defs.size(); ++i)
  104. {
  105. String def (defs.getAllKeys()[i]);
  106. const String value (defs.getAllValues()[i]);
  107. if (value.isNotEmpty())
  108. def << "=" << value;
  109. s += " -D" + def;
  110. }
  111. return s;
  112. }
  113. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  114. {
  115. for (int i = 0; i < definitions.size(); ++i)
  116. {
  117. const String key (definitions.getAllKeys()[i]);
  118. const String value (definitions.getAllValues()[i]);
  119. sourceString = sourceString.replace ("${" + key + "}", value);
  120. }
  121. return sourceString;
  122. }
  123. StringArray getSearchPathsFromString (const String& searchPath)
  124. {
  125. StringArray s;
  126. s.addTokens (searchPath, ";\r\n", StringRef());
  127. return getCleanedStringArray (s);
  128. }
  129. StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
  130. {
  131. StringArray s;
  132. s.addTokens (sourceString, ", \t\r\n", StringRef());
  133. return getCleanedStringArray (s);
  134. }
  135. StringArray getCleanedStringArray (StringArray s)
  136. {
  137. s.trim();
  138. s.removeEmptyStrings();
  139. return s;
  140. }
  141. //==============================================================================
  142. static bool keyFoundAndNotSequentialDuplicate (XmlElement* xml, const String& key)
  143. {
  144. forEachXmlChildElementWithTagName (*xml, element, "key")
  145. {
  146. if (element->getAllSubText().trim().equalsIgnoreCase (key))
  147. {
  148. if (element->getNextElement() != nullptr && element->getNextElement()->hasTagName ("key"))
  149. {
  150. // found broken plist format (sequential duplicate), fix by removing
  151. xml->removeChildElement (element, true);
  152. return false;
  153. }
  154. // key found (not sequential duplicate)
  155. return true;
  156. }
  157. }
  158. // key not found
  159. return false;
  160. }
  161. static bool addKeyIfNotFound (XmlElement* xml, const String& key)
  162. {
  163. if (! keyFoundAndNotSequentialDuplicate (xml, key))
  164. {
  165. xml->createNewChildElement ("key")->addTextElement (key);
  166. return true;
  167. }
  168. return false;
  169. }
  170. void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  171. {
  172. if (addKeyIfNotFound (xml, key))
  173. xml->createNewChildElement ("string")->addTextElement (value);
  174. }
  175. void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  176. {
  177. if (addKeyIfNotFound (xml, key))
  178. xml->createNewChildElement (value ? "true" : "false");
  179. }
  180. void addPlistDictionaryKeyInt (XmlElement* xml, const String& key, int value)
  181. {
  182. if (addKeyIfNotFound (xml, key))
  183. xml->createNewChildElement ("integer")->addTextElement (String (value));
  184. }
  185. //==============================================================================
  186. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  187. {
  188. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  189. {
  190. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  191. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  192. }
  193. }
  194. //==============================================================================
  195. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  196. {
  197. const int len = text.length();
  198. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  199. {
  200. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  201. text.getCharPointer(), len) == 0)
  202. return index;
  203. ++index;
  204. }
  205. return -1;
  206. }
  207. //==============================================================================
  208. bool fileNeedsCppSyntaxHighlighting (const File& file)
  209. {
  210. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  211. return true;
  212. // This is a bit of a bodge to deal with libc++ headers with no extension..
  213. char fileStart[128] = { 0 };
  214. FileInputStream fin (file);
  215. fin.read (fileStart, sizeof (fileStart) - 4);
  216. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  217. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  218. }