1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 #include "../../Application/jucer_Headers.h"
27 
28 #ifdef BUILDING_JUCE_COMPILEENGINE
getPreferredLineFeed()29  const char* getPreferredLineFeed() { return "\r\n"; }
30 #endif
31 
32 //==============================================================================
joinLinesIntoSourceFile(StringArray & lines)33 String joinLinesIntoSourceFile (StringArray& lines)
34 {
35     while (lines.size() > 10 && lines [lines.size() - 1].isEmpty())
36         lines.remove (lines.size() - 1);
37 
38     return lines.joinIntoString (getPreferredLineFeed()) + getPreferredLineFeed();
39 }
40 
replaceLineFeeds(const String & content,const String & lineFeed)41 String replaceLineFeeds (const String& content, const String& lineFeed)
42 {
43     StringArray lines;
44     lines.addLines (content);
45 
46     return lines.joinIntoString (lineFeed);
47 }
48 
getLineFeedForFile(const String & fileContent)49 String getLineFeedForFile (const String& fileContent)
50 {
51     auto t = fileContent.getCharPointer();
52 
53     while (! t.isEmpty())
54     {
55         switch (t.getAndAdvance())
56         {
57             case 0:     break;
58             case '\n':  return "\n";
59             case '\r':  if (*t == '\n') return "\r\n";
60             default:    continue;
61         }
62     }
63 
64     return {};
65 }
66 
trimCommentCharsFromStartOfLine(const String & line)67 String trimCommentCharsFromStartOfLine (const String& line)
68 {
69     return line.trimStart().trimCharactersAtStart ("*/").trimStart();
70 }
71 
createAlphaNumericUID()72 String createAlphaNumericUID()
73 {
74     String uid;
75     const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
76     Random r;
77 
78     uid << chars[r.nextInt (52)]; // make sure the first character is always a letter
79 
80     for (int i = 5; --i >= 0;)
81     {
82         r.setSeedRandomly();
83         uid << chars [r.nextInt (62)];
84     }
85 
86     return uid;
87 }
88 
createGUID(const String & seed)89 String createGUID (const String& seed)
90 {
91     auto hex = MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase();
92 
93     return "{" + hex.substring (0, 8)
94          + "-" + hex.substring (8, 12)
95          + "-" + hex.substring (12, 16)
96          + "-" + hex.substring (16, 20)
97          + "-" + hex.substring (20, 32)
98          + "}";
99 }
100 
escapeSpaces(const String & s)101 String escapeSpaces (const String& s)
102 {
103     return s.replace (" ", "\\ ");
104 }
105 
addQuotesIfContainsSpaces(const String & text)106 String addQuotesIfContainsSpaces (const String& text)
107 {
108     return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
109 }
110 
setValueIfVoid(Value value,const var & defaultValue)111 void setValueIfVoid (Value value, const var& defaultValue)
112 {
113     if (value.getValue().isVoid())
114         value = defaultValue;
115 }
116 
117 //==============================================================================
parsePreprocessorDefs(const String & text)118 StringPairArray parsePreprocessorDefs (const String& text)
119 {
120     StringPairArray result;
121     auto s = text.getCharPointer();
122 
123     while (! s.isEmpty())
124     {
125         String token, value;
126         s.incrementToEndOfWhitespace();
127 
128         while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
129             token << s.getAndAdvance();
130 
131         s.incrementToEndOfWhitespace();
132 
133         if (*s == '=')
134         {
135             ++s;
136 
137             while ((! s.isEmpty()) && *s == ' ')
138                 ++s;
139 
140             while ((! s.isEmpty()) && ! s.isWhitespace())
141             {
142                 if (*s == ',')
143                 {
144                     ++s;
145                     break;
146                 }
147 
148                 if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
149                     ++s;
150 
151                 value << s.getAndAdvance();
152             }
153         }
154 
155         if (token.isNotEmpty())
156             result.set (token, value);
157     }
158 
159     return result;
160 }
161 
mergePreprocessorDefs(StringPairArray inheritedDefs,const StringPairArray & overridingDefs)162 StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
163 {
164     for (int i = 0; i < overridingDefs.size(); ++i)
165         inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
166 
167     return inheritedDefs;
168 }
169 
createGCCPreprocessorFlags(const StringPairArray & defs)170 String createGCCPreprocessorFlags (const StringPairArray& defs)
171 {
172     String s;
173 
174     for (int i = 0; i < defs.size(); ++i)
175     {
176         auto def = defs.getAllKeys()[i];
177         auto value = defs.getAllValues()[i];
178 
179         if (value.isNotEmpty())
180             def << "=" << value;
181 
182         s += " \"" + ("-D" + def).replace ("\"", "\\\"") + "\"";
183     }
184 
185     return s;
186 }
187 
getSearchPathsFromString(const String & searchPath)188 StringArray getSearchPathsFromString (const String& searchPath)
189 {
190     StringArray s;
191     s.addTokens (searchPath, ";\r\n", StringRef());
192     return getCleanedStringArray (s);
193 }
194 
getCommaOrWhitespaceSeparatedItems(const String & sourceString)195 StringArray getCommaOrWhitespaceSeparatedItems (const String& sourceString)
196 {
197     StringArray s;
198     s.addTokens (sourceString, ", \t\r\n", StringRef());
199     return getCleanedStringArray (s);
200 }
201 
getCleanedStringArray(StringArray s)202 StringArray getCleanedStringArray (StringArray s)
203 {
204     s.trim();
205     s.removeEmptyStrings();
206     return s;
207 }
208 
209 //==============================================================================
autoScrollForMouseEvent(const MouseEvent & e,bool scrollX,bool scrollY)210 void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
211 {
212     if (Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>())
213     {
214         const MouseEvent e2 (e.getEventRelativeTo (viewport));
215         viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
216     }
217 }
218 
219 //==============================================================================
indexOfLineStartingWith(const StringArray & lines,const String & text,int index)220 int indexOfLineStartingWith (const StringArray& lines, const String& text, int index)
221 {
222     const int len = text.length();
223 
224     for (const String* i = lines.begin() + index, * const e = lines.end(); i < e; ++i)
225     {
226         if (CharacterFunctions::compareUpTo (i->getCharPointer().findEndOfWhitespace(),
227                                              text.getCharPointer(), len) == 0)
228             return index;
229 
230         ++index;
231     }
232 
233     return -1;
234 }
235 
236 //==============================================================================
fileNeedsCppSyntaxHighlighting(const File & file)237 bool fileNeedsCppSyntaxHighlighting (const File& file)
238 {
239     if (file.hasFileExtension (sourceOrHeaderFileExtensions))
240         return true;
241 
242     // This is a bit of a bodge to deal with libc++ headers with no extension..
243     char fileStart[128] = { 0 };
244     FileInputStream fin (file);
245     fin.read (fileStart, sizeof (fileStart) - 4);
246 
247     return CharPointer_UTF8::isValidString (fileStart, sizeof (fileStart))
248              && String (fileStart).trimStart().startsWith ("// -*- C++ -*-");
249 }
250 
251 //==============================================================================
writeAutoGenWarningComment(OutputStream & outStream)252 void writeAutoGenWarningComment (OutputStream& outStream)
253 {
254     outStream << "/*" << newLine << newLine
255               << "    IMPORTANT! This file is auto-generated each time you save your" << newLine
256               << "    project - if you alter its contents, your changes may be overwritten!" << newLine
257               << newLine;
258 }
259 
260 //==============================================================================
getJUCEModules()261 StringArray getJUCEModules() noexcept
262 {
263     static StringArray juceModuleIds =
264     {
265         "juce_analytics",
266         "juce_audio_basics",
267         "juce_audio_devices",
268         "juce_audio_formats",
269         "juce_audio_plugin_client",
270         "juce_audio_processors",
271         "juce_audio_utils",
272         "juce_blocks_basics",
273         "juce_box2d",
274         "juce_core",
275         "juce_cryptography",
276         "juce_data_structures",
277         "juce_dsp",
278         "juce_events",
279         "juce_graphics",
280         "juce_gui_basics",
281         "juce_gui_extra",
282         "juce_opengl",
283         "juce_osc",
284         "juce_product_unlocking",
285         "juce_video"
286     };
287 
288     return juceModuleIds;
289 }
290 
isJUCEModule(const String & moduleID)291 bool isJUCEModule (const String& moduleID) noexcept
292 {
293     return getJUCEModules().contains (moduleID);
294 }
295 
getModulesRequiredForConsole()296 StringArray getModulesRequiredForConsole() noexcept
297 {
298     return
299     {
300         "juce_core",
301         "juce_data_structures",
302         "juce_events"
303     };
304 }
305 
getModulesRequiredForComponent()306 StringArray getModulesRequiredForComponent() noexcept
307 {
308     return
309     {
310         "juce_core",
311         "juce_data_structures",
312         "juce_events",
313         "juce_graphics",
314         "juce_gui_basics"
315     };
316 }
317 
getModulesRequiredForAudioProcessor()318 StringArray getModulesRequiredForAudioProcessor() noexcept
319 {
320     return
321     {
322         "juce_audio_basics",
323         "juce_audio_devices",
324         "juce_audio_formats",
325         "juce_audio_plugin_client",
326         "juce_audio_processors",
327         "juce_audio_utils",
328         "juce_core",
329         "juce_data_structures",
330         "juce_events",
331         "juce_graphics",
332         "juce_gui_basics",
333         "juce_gui_extra"
334     };
335 }
336 
isPIPFile(const File & file)337 bool isPIPFile (const File& file) noexcept
338 {
339     for (auto line : StringArray::fromLines (file.loadFileAsString()))
340     {
341         auto trimmedLine = trimCommentCharsFromStartOfLine (line);
342 
343         if (trimmedLine.startsWith ("BEGIN_JUCE_PIP_METADATA"))
344             return true;
345     }
346 
347     return false;
348 }
349 
isValidJUCEExamplesDirectory(const File & directory)350 bool isValidJUCEExamplesDirectory (const File& directory) noexcept
351 {
352     if (! directory.exists() || ! directory.isDirectory() || ! directory.containsSubDirectories())
353         return false;
354 
355     return directory.getChildFile ("Assets").getChildFile ("juce_icon.png").existsAsFile();
356 }
357 
isJUCEFolder(const File & f)358 bool isJUCEFolder (const File& f)
359 {
360     return isJUCEModulesFolder (f.getChildFile ("modules"));
361 }
362 
isJUCEModulesFolder(const File & f)363 bool isJUCEModulesFolder (const File& f)
364 {
365     return f.isDirectory() && f.getChildFile ("juce_core").isDirectory();
366 }
367 
368 //==============================================================================
isDivider(const String & line)369 static bool isDivider (const String& line)
370 {
371     auto afterIndent = line.trim();
372 
373     if (afterIndent.startsWith ("//") && afterIndent.length() > 20)
374     {
375         afterIndent = afterIndent.substring (5);
376 
377         if (afterIndent.containsOnly ("=")
378             || afterIndent.containsOnly ("/")
379             || afterIndent.containsOnly ("-"))
380         {
381             return true;
382         }
383     }
384 
385     return false;
386 }
387 
getIndexOfCommentBlockStart(const StringArray & lines,int endIndex)388 static int getIndexOfCommentBlockStart (const StringArray& lines, int endIndex)
389 {
390     auto endLine = lines[endIndex];
391 
392     if (endLine.contains ("*/"))
393     {
394         for (int i = endIndex; i >= 0; --i)
395             if (lines[i].contains ("/*"))
396                 return i;
397     }
398 
399      if (endLine.trim().startsWith ("//") && ! isDivider (endLine))
400      {
401          for (int i = endIndex; i >= 0; --i)
402              if (! lines[i].startsWith ("//") || isDivider (lines[i]))
403                  return i + 1;
404      }
405 
406     return -1;
407 }
408 
findBestLineToScrollToForClass(StringArray lines,const String & className,bool isPlugin)409 int findBestLineToScrollToForClass (StringArray lines, const String& className, bool isPlugin)
410 {
411     for (auto line : lines)
412     {
413         if (line.contains ("struct " + className) || line.contains ("class " + className)
414             || (isPlugin && line.contains ("public AudioProcessor") && ! line.contains ("AudioProcessorEditor")))
415         {
416             auto index = lines.indexOf (line);
417 
418             auto commentBlockStartIndex = getIndexOfCommentBlockStart (lines, index - 1);
419 
420             if (commentBlockStartIndex != -1)
421                 index = commentBlockStartIndex;
422 
423             if (isDivider (lines[index - 1]))
424                 index -= 1;
425 
426             return index;
427         }
428     }
429 
430     return 0;
431 }
432 
433 //==============================================================================
parseJUCEHeaderMetadata(const StringArray & lines)434 static var parseJUCEHeaderMetadata (const StringArray& lines)
435 {
436     auto* o = new DynamicObject();
437     var result (o);
438 
439     for (auto& line : lines)
440     {
441         auto trimmedLine = trimCommentCharsFromStartOfLine (line);
442 
443         auto colon = trimmedLine.indexOfChar (':');
444 
445         if (colon >= 0)
446         {
447             auto key = trimmedLine.substring (0, colon).trim();
448             auto value = trimmedLine.substring (colon + 1).trim();
449 
450             o->setProperty (key, value);
451         }
452     }
453 
454     return result;
455 }
456 
parseMetadataItem(const StringArray & lines,int & index)457 static String parseMetadataItem (const StringArray& lines, int& index)
458 {
459     String result = lines[index++];
460 
461     while (index < lines.size())
462     {
463         auto continuationLine = trimCommentCharsFromStartOfLine (lines[index]);
464 
465         if (continuationLine.isEmpty() || continuationLine.indexOfChar (':') != -1
466             || continuationLine.startsWith ("END_JUCE_"))
467             break;
468 
469         result += " " + continuationLine;
470         ++index;
471     }
472 
473     return result;
474 }
475 
parseJUCEHeaderMetadata(const File & file)476 var parseJUCEHeaderMetadata (const File& file)
477 {
478     StringArray lines;
479     file.readLines (lines);
480 
481     for (int i = 0; i < lines.size(); ++i)
482     {
483         auto trimmedLine = trimCommentCharsFromStartOfLine (lines[i]);
484 
485         if (trimmedLine.startsWith ("BEGIN_JUCE_"))
486         {
487             StringArray desc;
488             auto j = i + 1;
489 
490             while (j < lines.size())
491             {
492                 if (trimCommentCharsFromStartOfLine (lines[j]).startsWith ("END_JUCE_"))
493                     return parseJUCEHeaderMetadata (desc);
494 
495                 desc.add (parseMetadataItem (lines, j));
496             }
497         }
498     }
499 
500     return {};
501 }
502