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.

205 lines
6.3KB

  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 "../../Application/jucer_Headers.h"
  20. #include "jucer_CodeHelpers.h"
  21. //==============================================================================
  22. namespace FileHelpers
  23. {
  24. static uint64 calculateMemoryHashCode (const void* data, size_t numBytes)
  25. {
  26. uint64 t = 0;
  27. for (size_t i = 0; i < numBytes; ++i)
  28. t = t * 65599 + static_cast<const uint8*> (data)[i];
  29. return t;
  30. }
  31. uint64 calculateStreamHashCode (InputStream& in)
  32. {
  33. uint64 t = 0;
  34. const int bufferSize = 4096;
  35. HeapBlock<uint8> buffer;
  36. buffer.malloc (bufferSize);
  37. for (;;)
  38. {
  39. auto num = in.read (buffer, bufferSize);
  40. if (num <= 0)
  41. break;
  42. for (int i = 0; i < num; ++i)
  43. t = t * 65599 + buffer[i];
  44. }
  45. return t;
  46. }
  47. uint64 calculateFileHashCode (const File& file)
  48. {
  49. std::unique_ptr<FileInputStream> stream (file.createInputStream());
  50. return stream != nullptr ? calculateStreamHashCode (*stream) : 0;
  51. }
  52. bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, size_t numBytes)
  53. {
  54. if (file.getSize() == (int64) numBytes
  55. && calculateMemoryHashCode (data, numBytes) == calculateFileHashCode (file))
  56. return true;
  57. if (file.exists())
  58. return file.replaceWithData (data, numBytes);
  59. return file.getParentDirectory().createDirectory() && file.appendData (data, numBytes);
  60. }
  61. bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData)
  62. {
  63. return overwriteFileWithNewDataIfDifferent (file, newData.getData(), newData.getDataSize());
  64. }
  65. bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData)
  66. {
  67. const char* const utf8 = newData.toUTF8();
  68. return overwriteFileWithNewDataIfDifferent (file, utf8, strlen (utf8));
  69. }
  70. bool containsAnyNonHiddenFiles (const File& folder)
  71. {
  72. DirectoryIterator di (folder, false);
  73. while (di.next())
  74. if (! di.getFile().isHidden())
  75. return true;
  76. return false;
  77. }
  78. String unixStylePath (const String& path) { return path.replaceCharacter ('\\', '/'); }
  79. String windowsStylePath (const String& path) { return path.replaceCharacter ('/', '\\'); }
  80. String currentOSStylePath (const String& path)
  81. {
  82. #if JUCE_WINDOWS
  83. return windowsStylePath (path);
  84. #else
  85. return unixStylePath (path);
  86. #endif
  87. }
  88. bool isAbsolutePath (const String& path)
  89. {
  90. return File::isAbsolutePath (path)
  91. || path.startsWithChar ('/') // (needed because File::isAbsolutePath will ignore forward-slashes on Windows)
  92. || path.startsWithChar ('$')
  93. || path.startsWithChar ('~')
  94. || (CharacterFunctions::isLetter (path[0]) && path[1] == ':')
  95. || path.startsWithIgnoreCase ("smb:");
  96. }
  97. bool shouldPathsBeRelative (String path1, String path2)
  98. {
  99. path1 = unixStylePath (path1);
  100. path2 = unixStylePath (path2);
  101. const int len = jmin (path1.length(), path2.length());
  102. int commonBitLength = 0;
  103. for (int i = 0; i < len; ++i)
  104. {
  105. if (CharacterFunctions::toLowerCase (path1[i]) != CharacterFunctions::toLowerCase (path2[i]))
  106. break;
  107. ++commonBitLength;
  108. }
  109. return path1.substring (0, commonBitLength).removeCharacters ("/:").isNotEmpty();
  110. }
  111. String getRelativePathFrom (const File& file, const File& sourceFolder)
  112. {
  113. #if ! JUCE_WINDOWS
  114. // On a non-windows machine, we can't know if a drive-letter path may be relative or not.
  115. if (CharacterFunctions::isLetter (file.getFullPathName()[0]) && file.getFullPathName()[1] == ':')
  116. return file.getFullPathName();
  117. #endif
  118. return file.getRelativePathFrom (sourceFolder);
  119. }
  120. // removes "/../" bits from the middle of the path
  121. String simplifyPath (String::CharPointerType p)
  122. {
  123. #if JUCE_WINDOWS
  124. if (CharacterFunctions::indexOf (p, CharPointer_ASCII ("/../")) >= 0
  125. || CharacterFunctions::indexOf (p, CharPointer_ASCII ("\\..\\")) >= 0)
  126. #else
  127. if (CharacterFunctions::indexOf (p, CharPointer_ASCII ("/../")) >= 0)
  128. #endif
  129. {
  130. StringArray toks;
  131. #if JUCE_WINDOWS
  132. toks.addTokens (p, "\\/", StringRef());
  133. #else
  134. toks.addTokens (p, "/", StringRef());
  135. #endif
  136. while (toks[0] == ".")
  137. toks.remove (0);
  138. for (int i = 1; i < toks.size(); ++i)
  139. {
  140. if (toks[i] == ".." && toks [i - 1] != "..")
  141. {
  142. toks.removeRange (i - 1, 2);
  143. i = jmax (0, i - 2);
  144. }
  145. }
  146. return toks.joinIntoString ("/");
  147. }
  148. return p;
  149. }
  150. String simplifyPath (const String& path)
  151. {
  152. #if JUCE_WINDOWS
  153. if (path.contains ("\\..\\") || path.contains ("/../"))
  154. #else
  155. if (path.contains ("/../"))
  156. #endif
  157. return simplifyPath (path.getCharPointer());
  158. return path;
  159. }
  160. }