1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #include "fileresourceinputstream.h"
6 
7 #if WINDOWS
8 	#define fseeko _fseeki64
9 	#define ftello _ftelli64
10 #endif
11 
12 //-----------------------------------------------------------------------------
13 namespace VSTGUI {
14 
create(const std::string & path)15 FileResourceInputStream::Ptr FileResourceInputStream::create (const std::string& path)
16 {
17 	auto cstr = path.data ();
18 	if (auto handle = fopen (cstr, "rb"))
19 		return Ptr (new FileResourceInputStream (handle));
20 	return nullptr;
21 }
22 
23 //-----------------------------------------------------------------------------
FileResourceInputStream(FILE * handle)24 FileResourceInputStream::FileResourceInputStream (FILE* handle) : fileHandle (handle) {}
25 
26 //-----------------------------------------------------------------------------
~FileResourceInputStream()27 FileResourceInputStream::~FileResourceInputStream () noexcept
28 {
29 	fclose (fileHandle);
30 }
31 
32 //-----------------------------------------------------------------------------
readRaw(void * buffer,uint32_t size)33 uint32_t FileResourceInputStream::readRaw (void* buffer, uint32_t size)
34 {
35 	uint32_t readResult = static_cast<uint32_t> (fread (buffer, 1, size, fileHandle));
36 	if (readResult == 0)
37 	{
38 		if (ferror (fileHandle) != 0)
39 		{
40 			readResult = kStreamIOError;
41 			clearerr (fileHandle);
42 		}
43 	}
44 	return readResult;
45 }
46 
47 //-----------------------------------------------------------------------------
seek(int64_t pos,SeekMode mode)48 int64_t FileResourceInputStream::seek (int64_t pos, SeekMode mode)
49 {
50 	int whence;
51 	switch (mode)
52 	{
53 		case SeekMode::Set:
54 			whence = SEEK_SET;
55 			break;
56 		case SeekMode::Current:
57 			whence = SEEK_CUR;
58 			break;
59 		case SeekMode::End:
60 			whence = SEEK_END;
61 			break;
62 	}
63 	if (fseeko (fileHandle, pos, whence) == 0)
64 		return tell ();
65 	return kStreamSeekError;
66 }
67 
68 //-----------------------------------------------------------------------------
tell()69 int64_t FileResourceInputStream::tell ()
70 {
71 	return ftello (fileHandle);
72 }
73 
74 //-----------------------------------------------------------------------------
75 } // VSTGUI
76