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.

85 lines
2.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace build_tools
  16. {
  17. uint64 calculateStreamHashCode (InputStream& in)
  18. {
  19. uint64 t = 0;
  20. const int bufferSize = 4096;
  21. HeapBlock<uint8> buffer;
  22. buffer.malloc (bufferSize);
  23. for (;;)
  24. {
  25. auto num = in.read (buffer, bufferSize);
  26. if (num <= 0)
  27. break;
  28. for (int i = 0; i < num; ++i)
  29. t = t * 65599 + buffer[i];
  30. }
  31. return t;
  32. }
  33. uint64 calculateFileHashCode (const File& file)
  34. {
  35. std::unique_ptr<FileInputStream> stream (file.createInputStream());
  36. return stream != nullptr ? calculateStreamHashCode (*stream) : 0;
  37. }
  38. uint64 calculateMemoryHashCode (const void* data, size_t numBytes)
  39. {
  40. uint64 t = 0;
  41. for (size_t i = 0; i < numBytes; ++i)
  42. t = t * 65599 + static_cast<const uint8*> (data)[i];
  43. return t;
  44. }
  45. bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, size_t numBytes)
  46. {
  47. if (file.getSize() == (int64) numBytes
  48. && calculateMemoryHashCode (data, numBytes) == calculateFileHashCode (file))
  49. return true;
  50. if (file.exists())
  51. return file.replaceWithData (data, numBytes);
  52. return file.getParentDirectory().createDirectory() && file.appendData (data, numBytes);
  53. }
  54. bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData)
  55. {
  56. return overwriteFileWithNewDataIfDifferent (file, newData.getData(), newData.getDataSize());
  57. }
  58. bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData)
  59. {
  60. const char* const utf8 = newData.toUTF8();
  61. return overwriteFileWithNewDataIfDifferent (file, utf8, strlen (utf8));
  62. }
  63. }
  64. }