Audio plugin host https://kx.studio/carla
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.

120 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. static File createTempFile (const File& parentDirectory, String name,
  24. const String& suffix, const int optionFlags)
  25. {
  26. if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
  27. name = "." + name;
  28. return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
  29. }
  30. TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
  31. : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
  32. "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
  33. suffix, optionFlags))
  34. {
  35. }
  36. TemporaryFile::TemporaryFile (const File& target, const int optionFlags)
  37. : temporaryFile (createTempFile (target.getParentDirectory(),
  38. target.getFileNameWithoutExtension()
  39. + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()),
  40. target.getFileExtension(), optionFlags)),
  41. targetFile (target)
  42. {
  43. // If you use this constructor, you need to give it a valid target file!
  44. jassert (targetFile != File());
  45. }
  46. TemporaryFile::TemporaryFile (const File& target, const File& temporary)
  47. : temporaryFile (temporary), targetFile (target)
  48. {
  49. }
  50. TemporaryFile::~TemporaryFile()
  51. {
  52. if (! deleteTemporaryFile())
  53. {
  54. /* Failed to delete our temporary file! The most likely reason for this would be
  55. that you've not closed an output stream that was being used to write to file.
  56. If you find that something beyond your control is changing permissions on
  57. your temporary files and preventing them from being deleted, you may want to
  58. call TemporaryFile::deleteTemporaryFile() to detect those error cases and
  59. handle them appropriately.
  60. */
  61. jassertfalse;
  62. }
  63. }
  64. //==============================================================================
  65. bool TemporaryFile::overwriteTargetFileWithTemporary() const
  66. {
  67. // This method only works if you created this object with the constructor
  68. // that takes a target file!
  69. jassert (targetFile != File());
  70. if (temporaryFile.exists())
  71. {
  72. // Have a few attempts at overwriting the file before giving up..
  73. for (int i = 5; --i >= 0;)
  74. {
  75. if (temporaryFile.replaceFileIn (targetFile))
  76. return true;
  77. Thread::sleep (100);
  78. }
  79. }
  80. else
  81. {
  82. // There's no temporary file to use. If your write failed, you should
  83. // probably check, and not bother calling this method.
  84. jassertfalse;
  85. }
  86. return false;
  87. }
  88. bool TemporaryFile::deleteTemporaryFile() const
  89. {
  90. // Have a few attempts at deleting the file before giving up..
  91. for (int i = 5; --i >= 0;)
  92. {
  93. if (temporaryFile.deleteFile())
  94. return true;
  95. Thread::sleep (50);
  96. }
  97. return false;
  98. }