LMMS
Loading...
Searching...
No Matches
FileInputStream.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
7
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/
10
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.
14
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.
22
23 ==============================================================================
24*/
25
26#include "FileInputStream.h"
27
28namespace water {
29
30static int64 water_fileSetPosition (void* handle, int64 pos);
31
32//==============================================================================
34 : file (f),
37 status (Result::ok())
38{
39 openHandle();
40}
41
43{
44 // You should always check that a stream opened successfully before using it!
45 wassert (openedOk());
46
47 return file.getSize();
48}
49
50int FileInputStream::read (void* buffer, int bytesToRead)
51{
52 // You should always check that a stream opened successfully before using it!
53 wassert (openedOk());
54
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);
58
59 const size_t num = readInternal (buffer, (size_t) bytesToRead);
60 currentPosition += (int64) num;
61
62 return (int) num;
63}
64
69
74
76{
77 // You should always check that a stream opened successfully before using it!
78 wassert (openedOk());
79
80 if (pos != currentPosition)
82
83 return currentPosition == pos;
84}
85
86#ifdef CARLA_OS_WIN
88{
90}
91
93{
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);
96
97 if (h != INVALID_HANDLE_VALUE)
98 fileHandle = (void*) h;
99 else
100 status = getResultForLastError();
101}
102
103size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
104{
105 if (fileHandle != 0)
106 {
107 DWORD actualNum = 0;
108 if (! ReadFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
109 {
110 status = getResultForLastError();
111 actualNum = 0;
112 }
113
114 return (size_t) actualNum;
115 }
116
117 return 0;
118}
119#else
121{
122 if (fileHandle != 0)
123 close (getFD (fileHandle));
124}
125
127{
128 const int f = open (file.getFullPathName().toUTF8(), O_RDONLY, 00644);
129
130 if (f != -1)
131 fileHandle = fdToVoidPointer (f);
132 else
133 status = getResultForErrno();
134}
135
136size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes)
137{
138 ssize_t result = 0;
139
140 if (fileHandle != 0)
141 {
142 result = ::read (getFD (fileHandle), buffer, numBytes);
143
144 if (result < 0)
145 {
146 status = getResultForErrno();
147 result = 0;
148 }
149 }
150
151 return (size_t) result;
152}
153#endif
154
155}
#define nullptr
Definition DistrhoDefines.h:75
Definition File.h:50
FileInputStream(const File &fileToRead)
Definition FileInputStream.cpp:33
int64 getPosition() override
Definition FileInputStream.cpp:70
~FileInputStream()
Definition FileInputStream.cpp:120
const File file
Definition FileInputStream.h:85
int64 currentPosition
Definition FileInputStream.h:87
bool openedOk() const noexcept
Definition FileInputStream.h:73
int read(void *, int) override
Definition FileInputStream.cpp:50
Result status
Definition FileInputStream.h:88
void * fileHandle
Definition FileInputStream.h:86
void openHandle()
Definition FileInputStream.cpp:126
bool setPosition(int64) override
Definition FileInputStream.cpp:75
int64 getTotalLength() override
Definition FileInputStream.cpp:42
bool isExhausted() override
Definition FileInputStream.cpp:65
size_t readInternal(void *, size_t)
Definition FileInputStream.cpp:136
Definition Result.h:64
unsigned f
Definition inflate.c:1572
#define wassert(expression)
Definition AudioSampleBuffer.h:33
static int64 water_fileSetPosition(void *handle, int64 pos)
long long int64
Definition water.h:100
unsigned int DWORD
Definition swell-types.h:164
void * HANDLE
Definition swell-types.h:212
BOOL CloseHandle(HANDLE hand)
Definition swell.cpp:157
uch h[RAND_HEAD_LEN]
Definition crypt.c:459
int result
Definition process.c:1455
struct zdirent * file
Definition win32.c:1500