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.

137 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_FileOutputStream.h"
  21. void* juce_fileOpen (const String& path, bool forWriting);
  22. void juce_fileClose (void* handle);
  23. int juce_fileWrite (void* handle, const void* buffer, int size);
  24. void juce_fileFlush (void* handle);
  25. int64 juce_fileGetPosition (void* handle);
  26. int64 juce_fileSetPosition (void* handle, int64 pos);
  27. //==============================================================================
  28. FileOutputStream::FileOutputStream (const File& f,
  29. const int bufferSize_)
  30. : file (f),
  31. bufferSize (bufferSize_),
  32. bytesInBuffer (0)
  33. {
  34. fileHandle = juce_fileOpen (f.getFullPathName(), true);
  35. if (fileHandle != 0)
  36. {
  37. currentPosition = juce_fileGetPosition (fileHandle);
  38. if (currentPosition < 0)
  39. {
  40. jassertfalse
  41. juce_fileClose (fileHandle);
  42. fileHandle = 0;
  43. }
  44. }
  45. buffer.malloc (jmax (bufferSize_, 16));
  46. }
  47. FileOutputStream::~FileOutputStream()
  48. {
  49. flush();
  50. juce_fileClose (fileHandle);
  51. }
  52. int64 FileOutputStream::getPosition()
  53. {
  54. return currentPosition;
  55. }
  56. bool FileOutputStream::setPosition (int64 newPosition)
  57. {
  58. if (newPosition != currentPosition)
  59. {
  60. flush();
  61. currentPosition = juce_fileSetPosition (fileHandle, newPosition);
  62. }
  63. return newPosition == currentPosition;
  64. }
  65. void FileOutputStream::flush()
  66. {
  67. if (bytesInBuffer > 0)
  68. {
  69. juce_fileWrite (fileHandle, buffer, bytesInBuffer);
  70. bytesInBuffer = 0;
  71. }
  72. juce_fileFlush (fileHandle);
  73. }
  74. bool FileOutputStream::write (const void* const src, const int numBytes)
  75. {
  76. if (bytesInBuffer + numBytes < bufferSize)
  77. {
  78. memcpy (buffer + bytesInBuffer, src, numBytes);
  79. bytesInBuffer += numBytes;
  80. currentPosition += numBytes;
  81. }
  82. else
  83. {
  84. if (bytesInBuffer > 0)
  85. {
  86. // flush the reservoir
  87. const bool wroteOk = (juce_fileWrite (fileHandle, buffer, bytesInBuffer) == bytesInBuffer);
  88. bytesInBuffer = 0;
  89. if (! wroteOk)
  90. return false;
  91. }
  92. if (numBytes < bufferSize)
  93. {
  94. memcpy (buffer + bytesInBuffer, src, numBytes);
  95. bytesInBuffer += numBytes;
  96. currentPosition += numBytes;
  97. }
  98. else
  99. {
  100. const int bytesWritten = juce_fileWrite (fileHandle, src, numBytes);
  101. currentPosition += bytesWritten;
  102. return bytesWritten == numBytes;
  103. }
  104. }
  105. return true;
  106. }
  107. END_JUCE_NAMESPACE