Skip to content

Latest commit

 

History

History
executable file
·
194 lines (163 loc) · 5.56 KB

PhysFSFileStream.cs

File metadata and controls

executable file
·
194 lines (163 loc) · 5.56 KB
 
Jan 7, 2003
Jan 7, 2003
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* PhysFSFileStream.cs - (c)2003 Gregory S. Read */
using System;
using System.Collections;
using System.IO;
namespace PhysFS_NET
{
public enum PhysFSFileMode {Read, Write, Append};
// Our exception class we'll use for throwing all PhysFS API related exception
public class PhysFSException : IOException
{
public PhysFSException(string Message) : base(Message) {}
public PhysFSException() : base(PhysFS_DLL.PHYSFS_getLastError()) {}
}
public unsafe class PhysFSFileStream : Stream
{
// ***Public properties***
public override bool CanRead
{
get
{
// Reading is supported
return true;
}
}
public override bool CanSeek
{
get
{
// Seek is supported
return true;
}
}
public override bool CanWrite
{
get
{
// Writing is supported
return true;
}
}
public override long Length
{
get
{
long TempLength;
TempLength = PhysFS_DLL.PHYSFS_fileLength(pHandle);
// If call returned an error, throw an exception
if(TempLength == -1)
throw new PhysFSException();
return TempLength;
}
}
public override long Position
{
get
{
long TempPosition;
TempPosition = PhysFS_DLL.PHYSFS_tell(pHandle);
// If call returned an error, throw an exception
if(TempPosition == -1)
throw new PhysFSException();
return TempPosition;
}
set
{
// Seek from beginning of file using the position value
Seek(value, SeekOrigin.Begin);
}
}
// ***Public methods***
public PhysFSFileStream(string FileName, PhysFSFileMode FileMode, ulong BufferSize)
{
// Open the specified file with the appropriate file access
switch(FileMode)
{
case PhysFSFileMode.Read:
pHandle = PhysFS_DLL.PHYSFS_openRead(FileName);
break;
case PhysFSFileMode.Write:
pHandle = PhysFS_DLL.PHYSFS_openWrite(FileName);
break;
case PhysFSFileMode.Append:
pHandle = PhysFS_DLL.PHYSFS_openAppend(FileName);
break;
default:
throw new PhysFSException("Invalid FileMode specified");
}
// If handle is null, an error occured, so raise an exception
//!!! Does object get created if exception is thrown?
if(pHandle == null)
throw new PhysFSException();
// Set buffer size, raise an exception if an error occured
if(PhysFS_DLL.PHYSFS_setBuffer(pHandle, BufferSize) == 0)
throw new PhysFSException();
}
// This constructor sets the buffer size to 0 if not specified
public PhysFSFileStream(string FileName, PhysFSFileMode FileMode) : this(FileName, FileMode, 0) {}
~PhysFSFileStream()
{
// Don't close the handle if they've specifically closed it already
if(!Closed)
Close();
}
public override void Flush()
{
if(PhysFS_DLL.PHYSFS_flush(pHandle) == 0)
throw new PhysFSException();
}
public override int Read(byte[] buffer, int offset, int count)
{
long RetValue;
fixed(byte *pbytes = &buffer[offset])
{
// Read into our allocated pointer
RetValue = PhysFS_DLL.PHYSFS_read(pHandle, pbytes, sizeof(byte), (uint)count);
}
if(RetValue == -1)
throw new PhysFSException();
// Return number of bytes read
// Note: This cast should be safe since we are only reading 'count' items, which
// is of type 'int'.
return (int)RetValue;
}
public override void Write(byte[] buffer, int offset, int count)
{
long RetValue;
fixed(byte* pbytes = &buffer[offset])
{
// Write buffer
RetValue = PhysFS_DLL.PHYSFS_write(pHandle, pbytes, sizeof(byte), (uint)count);
}
if(RetValue == -1)
throw new PhysFSException();
}
public override long Seek(long offset, SeekOrigin origin)
{
// Only seeking from beginning is supported by PhysFS API
if(origin != SeekOrigin.Begin)
throw new PhysFSException("Only seek origin of \"Begin\" is supported");
// Seek to specified offset, raise an exception if error occured
if(PhysFS_DLL.PHYSFS_seek(pHandle, (ulong)offset) == 0)
throw new PhysFSException();
// Since we always seek from beginning, the offset is always
// the absolute position.
return offset;
}
public override void SetLength(long value)
{
throw new NotSupportedException("SetLength method not supported in PhysFSFileStream objects.");
}
public override void Close()
{
// Close the handle
if(PhysFS_DLL.PHYSFS_close(pHandle) == 0)
throw new PhysFSException();
// File has been closed. Rock.
Closed = true;
}
// ***Private variables***
private void *pHandle;
private bool Closed = false;
}
}