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.

218 lines
6.6KB

  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 "../jucer_Headers.h"
  20. #include "jucer_CodeHelpers.h"
  21. //==============================================================================
  22. namespace FileHelpers
  23. {
  24. static int64 calculateMemoryHashCode (const void* data, const size_t numBytes)
  25. {
  26. int64 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. int64 calculateStreamHashCode (InputStream& in)
  32. {
  33. int64 t = 0;
  34. const int bufferSize = 4096;
  35. HeapBlock<uint8> buffer;
  36. buffer.malloc (bufferSize);
  37. for (;;)
  38. {
  39. const int 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. int64 calculateFileHashCode (const File& file)
  48. {
  49. ScopedPointer<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. String appendPath (const String& path, const String& subpath)
  98. {
  99. if (isAbsolutePath (subpath))
  100. return unixStylePath (subpath);
  101. String path1 (unixStylePath (path));
  102. if (! path1.endsWithChar ('/'))
  103. path1 << '/';
  104. return path1 + unixStylePath (subpath);
  105. }
  106. bool shouldPathsBeRelative (String path1, String path2)
  107. {
  108. path1 = unixStylePath (path1);
  109. path2 = unixStylePath (path2);
  110. const int len = jmin (path1.length(), path2.length());
  111. int commonBitLength = 0;
  112. for (int i = 0; i < len; ++i)
  113. {
  114. if (CharacterFunctions::toLowerCase (path1[i]) != CharacterFunctions::toLowerCase (path2[i]))
  115. break;
  116. ++commonBitLength;
  117. }
  118. return path1.substring (0, commonBitLength).removeCharacters ("/:").isNotEmpty();
  119. }
  120. String getRelativePathFrom (const File& file, const File& sourceFolder)
  121. {
  122. #if ! JUCE_WINDOWS
  123. // On a non-windows machine, we can't know if a drive-letter path may be relative or not.
  124. if (CharacterFunctions::isLetter (file.getFullPathName()[0]) && file.getFullPathName()[1] == ':')
  125. return file.getFullPathName();
  126. #endif
  127. return file.getRelativePathFrom (sourceFolder);
  128. }
  129. // removes "/../" bits from the middle of the path
  130. String simplifyPath (String::CharPointerType p)
  131. {
  132. #if JUCE_WINDOWS
  133. if (CharacterFunctions::indexOf (p, CharPointer_ASCII ("/../")) >= 0
  134. || CharacterFunctions::indexOf (p, CharPointer_ASCII ("\\..\\")) >= 0)
  135. #else
  136. if (CharacterFunctions::indexOf (p, CharPointer_ASCII ("/../")) >= 0)
  137. #endif
  138. {
  139. StringArray toks;
  140. #if JUCE_WINDOWS
  141. toks.addTokens (p, "\\/", StringRef());
  142. #else
  143. toks.addTokens (p, "/", StringRef());
  144. #endif
  145. while (toks[0] == ".")
  146. toks.remove (0);
  147. for (int i = 1; i < toks.size(); ++i)
  148. {
  149. if (toks[i] == ".." && toks [i - 1] != "..")
  150. {
  151. toks.removeRange (i - 1, 2);
  152. i = jmax (0, i - 2);
  153. }
  154. }
  155. return toks.joinIntoString ("/");
  156. }
  157. return p;
  158. }
  159. String simplifyPath (const String& path)
  160. {
  161. #if JUCE_WINDOWS
  162. if (path.contains ("\\..\\") || path.contains ("/../"))
  163. #else
  164. if (path.contains ("/../"))
  165. #endif
  166. return simplifyPath (path.getCharPointer());
  167. return path;
  168. }
  169. }