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.

277 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. while ((! s.isEmpty()) && *s == ' ')
  77. ++s;
  78. while ((! s.isEmpty()) && ! s.isWhitespace())
  79. {
  80. if (*s == ',')
  81. {
  82. ++s;
  83. break;
  84. }
  85. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  86. ++s;
  87. value << s.getAndAdvance();
  88. }
  89. }
  90. if (token.isNotEmpty())
  91. result.set (token, value);
  92. }
  93. return result;
  94. }
  95. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  96. {
  97. for (int i = 0; i < overridingDefs.size(); ++i)
  98. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  99. return inheritedDefs;
  100. }
  101. String createGCCPreprocessorFlags (const StringPairArray& defs)
  102. {
  103. String s;
  104. for (int i = 0; i < defs.size(); ++i)
  105. {
  106. String def (defs.getAllKeys()[i]);
  107. const String value (defs.getAllValues()[i]);
  108. if (value.isNotEmpty())
  109. def << "=" << value;
  110. s += " -D" + def;
  111. }
  112. return s;
  113. }
  114. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  115. {
  116. for (int i = 0; i < definitions.size(); ++i)
  117. {
  118. const String key (definitions.getAllKeys()[i]);
  119. const String value (definitions.getAllValues()[i]);
  120. sourceString = sourceString.replace ("${" + key + "}", value);
  121. }
  122. return sourceString;
  123. }
  124. StringArray getSearchPathsFromString (const String& searchPath)
  125. {
  126. StringArray s;
  127. s.addTokens (searchPath, ";\r\n", StringRef());
  128. return getCleanedStringArray (s);
  129. }
  130. StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
  131. {
  132. StringArray s;
  133. s.addTokens (sourceString, ", \t\r\n", StringRef());
  134. return getCleanedStringArray (s);
  135. }
  136. StringArray getCleanedStringArray (StringArray s)
  137. {
  138. s.trim();
  139. s.removeEmptyStrings();
  140. return s;
  141. }
  142. //==============================================================================
  143. static bool keyFoundAndNotSequentialDuplicate (XmlElement* xml, const String& key)
  144. {
  145. forEachXmlChildElementWithTagName (*xml, element, "key")
  146. {
  147. if (element->getAllSubText().trim().equalsIgnoreCase (key))
  148. {
  149. if (element->getNextElement() != nullptr && element->getNextElement()->hasTagName ("key"))
  150. {
  151. // found broken plist format (sequential duplicate), fix by removing
  152. xml->removeChildElement (element, true);
  153. return false;
  154. }
  155. // key found (not sequential duplicate)
  156. return true;
  157. }
  158. }
  159. // key not found
  160. return false;
  161. }
  162. static bool addKeyIfNotFound (XmlElement* xml, const String& key)
  163. {
  164. if (! keyFoundAndNotSequentialDuplicate (xml, key))
  165. {
  166. xml->createNewChildElement ("key")->addTextElement (key);
  167. return true;
  168. }
  169. return false;
  170. }
  171. void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  172. {
  173. if (addKeyIfNotFound (xml, key))
  174. xml->createNewChildElement ("string")->addTextElement (value);
  175. }
  176. void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  177. {
  178. if (addKeyIfNotFound (xml, key))
  179. xml->createNewChildElement (value ? "true" : "false");
  180. }
  181. void addPlistDictionaryKeyInt (XmlElement* xml, const String& key, int value)
  182. {
  183. if (addKeyIfNotFound (xml, key))
  184. xml->createNewChildElement ("integer")->addTextElement (String (value));
  185. }
  186. //==============================================================================
  187. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  188. {
  189. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  190. {
  191. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  192. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  193. }
  194. }
  195. //==============================================================================
  196. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  197. {
  198. const int len = text.length();
  199. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  200. {
  201. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  202. text.getCharPointer(), len) == 0)
  203. return index;
  204. ++index;
  205. }
  206. return -1;
  207. }
  208. //==============================================================================
  209. bool fileNeedsCppSyntaxHighlighting (const File& file)
  210. {
  211. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  212. return true;
  213. // This is a bit of a bodge to deal with libc++ headers with no extension..
  214. char fileStart[128] = { 0 };
  215. FileInputStream fin (file);
  216. fin.read (fileStart, sizeof (fileStart) - 4);
  217. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  218. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  219. }