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.

274 lines
7.9KB

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