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.

178 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. jucer_FileUtilities.cpp
  4. Created: 14 May 2010 11:24:09pm
  5. Author: Julian Storer
  6. ==============================================================================
  7. */
  8. #include "../jucer_Headers.h"
  9. #include "jucer_CodeHelpers.h"
  10. //==============================================================================
  11. namespace FileHelpers
  12. {
  13. int64 calculateStreamHashCode (InputStream& in)
  14. {
  15. int64 t = 0;
  16. const int bufferSize = 4096;
  17. HeapBlock <uint8> buffer;
  18. buffer.malloc (bufferSize);
  19. for (;;)
  20. {
  21. const int num = in.read (buffer, bufferSize);
  22. if (num <= 0)
  23. break;
  24. for (int i = 0; i < num; ++i)
  25. t = t * 65599 + buffer[i];
  26. }
  27. return t;
  28. }
  29. int64 calculateFileHashCode (const File& file)
  30. {
  31. ScopedPointer <FileInputStream> stream (file.createInputStream());
  32. return stream != 0 ? calculateStreamHashCode (*stream) : 0;
  33. }
  34. bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes)
  35. {
  36. if (file.getSize() == numBytes)
  37. {
  38. MemoryInputStream newStream (data, numBytes, false);
  39. if (calculateStreamHashCode (newStream) == calculateFileHashCode (file))
  40. return true;
  41. }
  42. TemporaryFile temp (file);
  43. return temp.getFile().appendData (data, numBytes)
  44. && temp.overwriteTargetFileWithTemporary();
  45. }
  46. bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData)
  47. {
  48. return overwriteFileWithNewDataIfDifferent (file, newData.getData(), newData.getDataSize());
  49. }
  50. bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData)
  51. {
  52. return overwriteFileWithNewDataIfDifferent (file, newData.toUTF8(), strlen ((const char*) newData.toUTF8()));
  53. }
  54. bool containsAnyNonHiddenFiles (const File& folder)
  55. {
  56. DirectoryIterator di (folder, false);
  57. while (di.next())
  58. if (! di.getFile().isHidden())
  59. return true;
  60. return false;
  61. }
  62. const String unixStylePath (const String& path)
  63. {
  64. return path.replaceCharacter ('\\', '/');
  65. }
  66. const String windowsStylePath (const String& path)
  67. {
  68. return path.replaceCharacter ('/', '\\');
  69. }
  70. const String appendPath (const String& path, const String& subpath)
  71. {
  72. if (File::isAbsolutePath (subpath)
  73. || subpath.startsWithChar ('$')
  74. || subpath.startsWithChar ('~')
  75. || (CharacterFunctions::isLetter (subpath[0]) && subpath[1] == ':'))
  76. return subpath.replaceCharacter ('\\', '/');
  77. String path1 (path.replaceCharacter ('\\', '/'));
  78. if (! path1.endsWithChar ('/'))
  79. path1 << '/';
  80. return path1 + subpath.replaceCharacter ('\\', '/');
  81. }
  82. bool shouldPathsBeRelative (String path1, String path2)
  83. {
  84. path1 = unixStylePath (path1);
  85. path2 = unixStylePath (path2);
  86. const int len = jmin (path1.length(), path2.length());
  87. int commonBitLength = 0;
  88. for (int i = 0; i < len; ++i)
  89. {
  90. if (CharacterFunctions::toLowerCase (path1[i]) != CharacterFunctions::toLowerCase (path2[i]))
  91. break;
  92. ++commonBitLength;
  93. }
  94. return path1.substring (0, commonBitLength).removeCharacters ("/:").isNotEmpty();
  95. }
  96. //==============================================================================
  97. bool isJuceFolder (const File& folder)
  98. {
  99. return folder.getFileName().containsIgnoreCase ("juce")
  100. && folder.getChildFile ("juce.h").exists()
  101. && folder.getChildFile ("juce_Config.h").exists();
  102. }
  103. static const File lookInFolderForJuceFolder (const File& folder)
  104. {
  105. for (DirectoryIterator di (folder, false, "*juce*", File::findDirectories); di.next();)
  106. {
  107. if (isJuceFolder (di.getFile()))
  108. return di.getFile();
  109. }
  110. return File::nonexistent;
  111. }
  112. const File findParentJuceFolder (const File& file)
  113. {
  114. File f (file);
  115. while (f.exists() && f.getParentDirectory() != f)
  116. {
  117. if (isJuceFolder (f))
  118. return f;
  119. File found = lookInFolderForJuceFolder (f);
  120. if (found.exists())
  121. return found;
  122. f = f.getParentDirectory();
  123. }
  124. return File::nonexistent;
  125. }
  126. const File findDefaultJuceFolder()
  127. {
  128. File f = findParentJuceFolder (File::getSpecialLocation (File::currentApplicationFile));
  129. if (! f.exists())
  130. f = lookInFolderForJuceFolder (File::getSpecialLocation (File::userHomeDirectory));
  131. if (! f.exists())
  132. f = lookInFolderForJuceFolder (File::getSpecialLocation (File::userDocumentsDirectory));
  133. return f;
  134. }
  135. }