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.

278 lines
8.0KB

  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. if (! def.endsWithChar ('"'))
  108. def = def.quoted();
  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. s.removeDuplicates (false);
  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. }