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.

153 lines
4.3KB

  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. FileInputStream::FileInputStream (const File& f)
  26. : file (f),
  27. fileHandle (nullptr),
  28. currentPosition (0),
  29. status (Result::ok())
  30. {
  31. openHandle();
  32. }
  33. int64 FileInputStream::getTotalLength()
  34. {
  35. // You should always check that a stream opened successfully before using it!
  36. jassert (openedOk());
  37. return file.getSize();
  38. }
  39. int FileInputStream::read (void* buffer, int bytesToRead)
  40. {
  41. // You should always check that a stream opened successfully before using it!
  42. jassert (openedOk());
  43. // The buffer should never be null, and a negative size is probably a
  44. // sign that something is broken!
  45. jassert (buffer != nullptr && bytesToRead >= 0);
  46. const size_t num = readInternal (buffer, (size_t) bytesToRead);
  47. currentPosition += (int64) num;
  48. return (int) num;
  49. }
  50. bool FileInputStream::isExhausted()
  51. {
  52. return currentPosition >= getTotalLength();
  53. }
  54. int64 FileInputStream::getPosition()
  55. {
  56. return currentPosition;
  57. }
  58. bool FileInputStream::setPosition (int64 pos)
  59. {
  60. // You should always check that a stream opened successfully before using it!
  61. jassert (openedOk());
  62. if (pos != currentPosition)
  63. currentPosition = juce_fileSetPosition (fileHandle, pos);
  64. return currentPosition == pos;
  65. }
  66. #ifdef CARLA_OS_WIN
  67. FileInputStream::~FileInputStream()
  68. {
  69. CloseHandle ((HANDLE) fileHandle);
  70. }
  71. void FileInputStream::openHandle()
  72. {
  73. HANDLE h = CreateFile (file.getFullPathName().toUTF8(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  74. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
  75. if (h != INVALID_HANDLE_VALUE)
  76. fileHandle = (void*) h;
  77. else
  78. status = getResultForLastError();
  79. }
  80. size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
  81. {
  82. if (fileHandle != 0)
  83. {
  84. DWORD actualNum = 0;
  85. if (! ReadFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
  86. status = getResultForLastError();
  87. return (size_t) actualNum;
  88. }
  89. return 0;
  90. }
  91. #else
  92. FileInputStream::~FileInputStream()
  93. {
  94. if (fileHandle != 0)
  95. close (getFD (fileHandle));
  96. }
  97. void FileInputStream::openHandle()
  98. {
  99. const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644);
  100. if (f != -1)
  101. fileHandle = fdToVoidPointer (f);
  102. else
  103. status = getResultForErrno();
  104. }
  105. size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes)
  106. {
  107. ssize_t result = 0;
  108. if (fileHandle != 0)
  109. {
  110. result = ::read (getFD (fileHandle), buffer, numBytes);
  111. if (result < 0)
  112. {
  113. status = getResultForErrno();
  114. result = 0;
  115. }
  116. }
  117. return (size_t) result;
  118. }
  119. #endif