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.

256 lines
6.6KB

  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. int64 juce_fileSetPosition (void* handle, int64 pos);
  24. //==============================================================================
  25. FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
  26. : file (f),
  27. fileHandle (nullptr),
  28. status (Result::ok()),
  29. currentPosition (0),
  30. bufferSize (bufferSizeToUse),
  31. bytesInBuffer (0),
  32. buffer (jmax (bufferSizeToUse, (size_t) 16))
  33. {
  34. openHandle();
  35. }
  36. FileOutputStream::~FileOutputStream()
  37. {
  38. flushBuffer();
  39. closeHandle();
  40. }
  41. int64 FileOutputStream::getPosition()
  42. {
  43. return currentPosition;
  44. }
  45. bool FileOutputStream::setPosition (int64 newPosition)
  46. {
  47. if (newPosition != currentPosition)
  48. {
  49. flushBuffer();
  50. currentPosition = juce_fileSetPosition (fileHandle, newPosition);
  51. }
  52. return newPosition == currentPosition;
  53. }
  54. bool FileOutputStream::flushBuffer()
  55. {
  56. bool ok = true;
  57. if (bytesInBuffer > 0)
  58. {
  59. ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
  60. bytesInBuffer = 0;
  61. }
  62. return ok;
  63. }
  64. void FileOutputStream::flush()
  65. {
  66. flushBuffer();
  67. flushInternal();
  68. }
  69. bool FileOutputStream::write (const void* const src, const size_t numBytes)
  70. {
  71. jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
  72. if (bytesInBuffer + numBytes < bufferSize)
  73. {
  74. memcpy (buffer + bytesInBuffer, src, numBytes);
  75. bytesInBuffer += numBytes;
  76. currentPosition += (int64) numBytes;
  77. }
  78. else
  79. {
  80. if (! flushBuffer())
  81. return false;
  82. if (numBytes < bufferSize)
  83. {
  84. memcpy (buffer + bytesInBuffer, src, numBytes);
  85. bytesInBuffer += numBytes;
  86. currentPosition += (int64) numBytes;
  87. }
  88. else
  89. {
  90. const ssize_t bytesWritten = writeInternal (src, numBytes);
  91. if (bytesWritten < 0)
  92. return false;
  93. currentPosition += (int64) bytesWritten;
  94. return bytesWritten == (ssize_t) numBytes;
  95. }
  96. }
  97. return true;
  98. }
  99. bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
  100. {
  101. jassert (((ssize_t) numBytes) >= 0);
  102. if (bytesInBuffer + numBytes < bufferSize)
  103. {
  104. memset (buffer + bytesInBuffer, byte, numBytes);
  105. bytesInBuffer += numBytes;
  106. currentPosition += (int64) numBytes;
  107. return true;
  108. }
  109. return OutputStream::writeRepeatedByte (byte, numBytes);
  110. }
  111. #ifdef CARLA_OS_WIN
  112. void FileOutputStream::openHandle()
  113. {
  114. HANDLE h = CreateFile (file.getFullPathName().toUTF8(), GENERIC_WRITE, FILE_SHARE_READ, 0,
  115. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  116. if (h != INVALID_HANDLE_VALUE)
  117. {
  118. LARGE_INTEGER li;
  119. li.QuadPart = 0;
  120. li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_END);
  121. if (li.LowPart != INVALID_SET_FILE_POINTER)
  122. {
  123. fileHandle = (void*) h;
  124. currentPosition = li.QuadPart;
  125. return;
  126. }
  127. }
  128. status = getResultForLastError();
  129. }
  130. void FileOutputStream::closeHandle()
  131. {
  132. CloseHandle ((HANDLE) fileHandle);
  133. }
  134. ssize_t FileOutputStream::writeInternal (const void* bufferToWrite, size_t numBytes)
  135. {
  136. if (fileHandle != nullptr)
  137. {
  138. DWORD actualNum = 0;
  139. if (! WriteFile ((HANDLE) fileHandle, bufferToWrite, (DWORD) numBytes, &actualNum, 0))
  140. status = getResultForLastError();
  141. return (ssize_t) actualNum;
  142. }
  143. return 0;
  144. }
  145. void FileOutputStream::flushInternal()
  146. {
  147. if (fileHandle != nullptr)
  148. if (! FlushFileBuffers ((HANDLE) fileHandle))
  149. status = getResultForLastError();
  150. }
  151. #else
  152. void FileOutputStream::openHandle()
  153. {
  154. if (file.exists())
  155. {
  156. const int f = open (file.getFullPathName().toUTF8(), O_RDWR, 00644);
  157. if (f != -1)
  158. {
  159. currentPosition = lseek (f, 0, SEEK_END);
  160. if (currentPosition >= 0)
  161. {
  162. fileHandle = fdToVoidPointer (f);
  163. }
  164. else
  165. {
  166. status = getResultForErrno();
  167. close (f);
  168. }
  169. }
  170. else
  171. {
  172. status = getResultForErrno();
  173. }
  174. }
  175. else
  176. {
  177. const int f = open (file.getFullPathName().toUTF8(), O_RDWR + O_CREAT, 00644);
  178. if (f != -1)
  179. fileHandle = fdToVoidPointer (f);
  180. else
  181. status = getResultForErrno();
  182. }
  183. }
  184. void FileOutputStream::closeHandle()
  185. {
  186. if (fileHandle != 0)
  187. {
  188. close (getFD (fileHandle));
  189. fileHandle = 0;
  190. }
  191. }
  192. ssize_t FileOutputStream::writeInternal (const void* const data, const size_t numBytes)
  193. {
  194. ssize_t result = 0;
  195. if (fileHandle != 0)
  196. {
  197. result = ::write (getFD (fileHandle), data, numBytes);
  198. if (result == -1)
  199. status = getResultForErrno();
  200. }
  201. return result;
  202. }
  203. void FileOutputStream::flushInternal()
  204. {
  205. if (fileHandle != 0)
  206. {
  207. if (fsync (getFD (fileHandle)) == -1)
  208. status = getResultForErrno();
  209. }
  210. }
  211. #endif