Cleanup
[carla.git] / source / modules / water / files / FileInputStream.cpp
blob22fe2499e7dd05259629ade0d95f869be209f169
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #include "FileInputStream.h"
28 namespace water {
30 static int64 water_fileSetPosition (void* handle, int64 pos);
32 //==============================================================================
33 FileInputStream::FileInputStream (const File& f)
34 : file (f),
35 fileHandle (nullptr),
36 currentPosition (0),
37 status (Result::ok())
39 openHandle();
42 int64 FileInputStream::getTotalLength()
44 // You should always check that a stream opened successfully before using it!
45 wassert (openedOk());
47 return file.getSize();
50 int FileInputStream::read (void* buffer, int bytesToRead)
52 // You should always check that a stream opened successfully before using it!
53 wassert (openedOk());
55 // The buffer should never be null, and a negative size is probably a
56 // sign that something is broken!
57 wassert (buffer != nullptr && bytesToRead >= 0);
59 const size_t num = readInternal (buffer, (size_t) bytesToRead);
60 currentPosition += (int64) num;
62 return (int) num;
65 bool FileInputStream::isExhausted()
67 return currentPosition >= getTotalLength();
70 int64 FileInputStream::getPosition()
72 return currentPosition;
75 bool FileInputStream::setPosition (int64 pos)
77 // You should always check that a stream opened successfully before using it!
78 wassert (openedOk());
80 if (pos != currentPosition)
81 currentPosition = water_fileSetPosition (fileHandle, pos);
83 return currentPosition == pos;
86 #ifdef CARLA_OS_WIN
87 FileInputStream::~FileInputStream()
89 CloseHandle ((HANDLE) fileHandle);
92 void FileInputStream::openHandle()
94 HANDLE h = CreateFileA (file.getFullPathName().toUTF8(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
95 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
97 if (h != INVALID_HANDLE_VALUE)
98 fileHandle = (void*) h;
99 else
100 status = getResultForLastError();
103 size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
105 if (fileHandle != 0)
107 DWORD actualNum = 0;
108 if (! ReadFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
110 status = getResultForLastError();
111 actualNum = 0;
114 return (size_t) actualNum;
117 return 0;
119 #else
120 FileInputStream::~FileInputStream()
122 if (fileHandle != 0)
123 close (getFD (fileHandle));
126 void FileInputStream::openHandle()
128 const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644);
130 if (f != -1)
131 fileHandle = fdToVoidPointer (f);
132 else
133 status = getResultForErrno();
136 size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes)
138 ssize_t result = 0;
140 if (fileHandle != 0)
142 result = ::read (getFD (fileHandle), buffer, numBytes);
144 if (result < 0)
146 status = getResultForErrno();
147 result = 0;
151 return (size_t) result;
153 #endif