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.

275 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. s.removeDuplicates (false);
  138. return s;
  139. }
  140. //==============================================================================
  141. static bool keyFoundAndNotSequentialDuplicate (XmlElement* xml, const String& key)
  142. {
  143. forEachXmlChildElementWithTagName (*xml, element, "key")
  144. {
  145. if (element->getAllSubText().trim().equalsIgnoreCase (key))
  146. {
  147. if (element->getNextElement() != nullptr && element->getNextElement()->hasTagName ("key"))
  148. {
  149. // found broken plist format (sequential duplicate), fix by removing
  150. xml->removeChildElement (element, true);
  151. return false;
  152. }
  153. // key found (not sequential duplicate)
  154. return true;
  155. }
  156. }
  157. // key not found
  158. return false;
  159. }
  160. static bool addKeyIfNotFound (XmlElement* xml, const String& key)
  161. {
  162. if (! keyFoundAndNotSequentialDuplicate (xml, key))
  163. {
  164. xml->createNewChildElement ("key")->addTextElement (key);
  165. return true;
  166. }
  167. return false;
  168. }
  169. void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  170. {
  171. if (addKeyIfNotFound (xml, key))
  172. xml->createNewChildElement ("string")->addTextElement (value);
  173. }
  174. void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  175. {
  176. if (addKeyIfNotFound (xml, key))
  177. xml->createNewChildElement (value ? "true" : "false");
  178. }
  179. void addPlistDictionaryKeyInt (XmlElement* xml, const String& key, int value)
  180. {
  181. if (addKeyIfNotFound (xml, key))
  182. xml->createNewChildElement ("integer")->addTextElement (String (value));
  183. }
  184. //==============================================================================
  185. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  186. {
  187. if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
  188. {
  189. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  190. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  191. }
  192. }
  193. //==============================================================================
  194. int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
  195. {
  196. const int len = text.length();
  197. for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
  198. {
  199. if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
  200. text.getCharPointer(), len) == 0)
  201. return index;
  202. ++index;
  203. }
  204. return -1;
  205. }
  206. //==============================================================================
  207. bool fileNeedsCppSyntaxHighlighting (const File& file)
  208. {
  209. if (file.hasFileExtension (sourceOrHeaderFileExtensions))
  210. return true;
  211. // This is a bit of a bodge to deal with libc++ headers with no extension..
  212. char fileStart[128] = { 0 };
  213. FileInputStream fin (file);
  214. fin.read (fileStart, sizeof (fileStart) - 4);
  215. return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
  216. && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
  217. }