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.

FileInputStream.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "FileInputStream.h"
  21. namespace water {
  22. static int64 water_fileSetPosition (void* handle, int64 pos);
  23. //==============================================================================
  24. FileInputStream::FileInputStream (const File& f)
  25. : file (f),
  26. fileHandle (nullptr),
  27. currentPosition (0),
  28. status (Result::ok())
  29. {
  30. openHandle();
  31. }
  32. int64 FileInputStream::getTotalLength()
  33. {
  34. // You should always check that a stream opened successfully before using it!
  35. wassert (openedOk());
  36. return file.getSize();
  37. }
  38. int FileInputStream::read (void* buffer, int bytesToRead)
  39. {
  40. // You should always check that a stream opened successfully before using it!
  41. wassert (openedOk());
  42. // The buffer should never be null, and a negative size is probably a
  43. // sign that something is broken!
  44. wassert (buffer != nullptr && bytesToRead >= 0);
  45. const size_t num = readInternal (buffer, (size_t) bytesToRead);
  46. currentPosition += (int64) num;
  47. return (int) num;
  48. }
  49. bool FileInputStream::isExhausted()
  50. {
  51. return currentPosition >= getTotalLength();
  52. }
  53. int64 FileInputStream::getPosition()
  54. {
  55. return currentPosition;
  56. }
  57. bool FileInputStream::setPosition (int64 pos)
  58. {
  59. // You should always check that a stream opened successfully before using it!
  60. wassert (openedOk());
  61. if (pos != currentPosition)
  62. currentPosition = water_fileSetPosition (fileHandle, pos);
  63. return currentPosition == pos;
  64. }
  65. #ifdef CARLA_OS_WIN
  66. FileInputStream::~FileInputStream()
  67. {
  68. CloseHandle ((HANDLE) fileHandle);
  69. }
  70. void FileInputStream::openHandle()
  71. {
  72. HANDLE h = CreateFile (file.getFullPathName().toUTF8(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  73. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
  74. if (h != INVALID_HANDLE_VALUE)
  75. fileHandle = (void*) h;
  76. else
  77. status = getResultForLastError();
  78. }
  79. size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
  80. {
  81. if (fileHandle != 0)
  82. {
  83. DWORD actualNum = 0;
  84. if (! ReadFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
  85. status = getResultForLastError();
  86. return (size_t) actualNum;
  87. }
  88. return 0;
  89. }
  90. #else
  91. FileInputStream::~FileInputStream()
  92. {
  93. if (fileHandle != 0)
  94. close (getFD (fileHandle));
  95. }
  96. void FileInputStream::openHandle()
  97. {
  98. const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644);
  99. if (f != -1)
  100. fileHandle = fdToVoidPointer (f);
  101. else
  102. status = getResultForErrno();
  103. }
  104. size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes)
  105. {
  106. ssize_t result = 0;
  107. if (fileHandle != 0)
  108. {
  109. result = ::read (getFD (fileHandle), buffer, numBytes);
  110. if (result < 0)
  111. {
  112. status = getResultForErrno();
  113. result = 0;
  114. }
  115. }
  116. return (size_t) result;
  117. }
  118. #endif
  119. }