1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#ifndef CORE_LIB_STREAM_H
#define CORE_LIB_STREAM_H
#include "basic.h"
namespace Slang
{
class IOException : public Exception
{
public:
IOException()
{}
IOException(const String & message)
: Slang::Exception(message)
{
}
};
class EndOfStreamException : public IOException
{
public:
EndOfStreamException()
{}
EndOfStreamException(const String & message)
: IOException(message)
{
}
};
enum class SeekOrigin
{
Start, End, Current
};
class Stream : public RefObject
{
public:
virtual ~Stream() {}
virtual Int64 GetPosition()=0;
virtual void Seek(SeekOrigin origin, Int64 offset)=0;
virtual Int64 Read(void * buffer, Int64 length) = 0;
virtual Int64 Write(const void * buffer, Int64 length) = 0;
virtual bool IsEnd() = 0;
virtual bool CanRead() = 0;
virtual bool CanWrite() = 0;
virtual void Close() = 0;
};
enum class FileMode
{
Create, Open, CreateNew, Append
};
enum class FileAccess
{
None = 0, Read = 1, Write = 2, ReadWrite = 3
};
enum class FileShare
{
None, ReadOnly, WriteOnly, ReadWrite
};
class MemoryStream : public Stream
{
public:
virtual Int64 GetPosition() SLANG_OVERRIDE { return m_position; }
virtual void Seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE;
virtual Int64 Read(void * buffer, Int64 length) SLANG_OVERRIDE;
virtual Int64 Write(const void * buffer, Int64 length) SLANG_OVERRIDE;
virtual bool IsEnd() SLANG_OVERRIDE { return m_atEnd; }
virtual bool CanRead() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Read)) != 0; }
virtual bool CanWrite() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Write)) != 0; }
virtual void Close() SLANG_OVERRIDE { m_access = FileAccess::None; }
MemoryStream(FileAccess access) :
m_access(access),
m_position(0),
m_atEnd(false)
{}
UInt m_position;
bool m_atEnd; ///< Happens when a read is done and nothing can be returned because already at end
FileAccess m_access;
List<uint8_t> m_contents;
};
class FileStream : public Stream
{
private:
FILE * handle;
FileAccess fileAccess;
bool endReached = false;
void Init(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
public:
FileStream(const Slang::String & fileName, FileMode fileMode = FileMode::Open);
FileStream(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
~FileStream();
public:
virtual Int64 GetPosition();
virtual void Seek(SeekOrigin origin, Int64 offset);
virtual Int64 Read(void * buffer, Int64 length);
virtual Int64 Write(const void * buffer, Int64 length);
virtual bool CanRead();
virtual bool CanWrite();
virtual void Close();
virtual bool IsEnd();
};
}
#endif
|