forked from AtomicGameEngine/AtomicGameEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cs
More file actions
201 lines (159 loc) · 5.42 KB
/
File.cs
File metadata and controls
201 lines (159 loc) · 5.42 KB
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
195
196
197
198
199
200
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace AtomicEngine
{
public partial class File : AObject, AbstractFile
{
private class Stream : System.IO.Stream
{
private File file;
public override bool CanRead
{
get
{
return file.Mode == FileMode.FILE_READ || file.Mode == FileMode.FILE_READWRITE;
}
}
public override bool CanSeek
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return file.Mode == FileMode.FILE_WRITE ||
file.Mode == FileMode.FILE_READWRITE ||
file.Mode == FileMode.FILE_APPEND;
}
}
public override long Length
{
get
{
return file.GetSize();
}
}
public override long Position
{
get
{
return file.GetPosition();
}
set
{
file.Seek((uint)value);
}
}
public Stream(File file)
{
if (file == null)
throw new ArgumentNullException(nameof(file));
this.file = file;
}
public override void Flush()
{
file.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (offset + count > buffer.Length)
throw new IndexOutOfRangeException("buffer.Length");
byte[] readData = file.Read(count);
int limit = Math.Min(count, readData.Length);
Array.Copy(readData, 0, buffer, offset, limit);
return limit;
}
public override long Seek(long offset, SeekOrigin origin)
{
if (origin == SeekOrigin.Current)
{
offset += Position;
}
else if (origin == SeekOrigin.End)
{
offset += Length;
}
return file.Seek((uint)offset);
}
public override void SetLength(long value)
{
throw new InvalidOperationException();
}
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (offset + count >= buffer.Length)
throw new IndexOutOfRangeException("buffer.Length");
file.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
if (file != null)
{
file.Close();
file = null;
}
base.Dispose(disposing);
}
}
public System.IO.Stream ToStream()
{
return new Stream(this);
}
/// <summary>
/// Read bytes from the file. Return array of bytes of the length actually read (can be 0 length)
/// </summary>
unsafe public byte[] Read(int size)
{
byte[] bytes = null;
byte[] buffer = new byte[size];
int sizeRead = 0;
fixed (byte* p = buffer)
{
IntPtr ptr = (IntPtr)p;
sizeRead = (int)csi_Atomic_File_Read(nativeInstance, ptr, (uint)size);
bytes = new byte[sizeRead];
if (sizeRead > 0)
{
Marshal.Copy(ptr, bytes, 0, (int)sizeRead);
}
}
return bytes;
}
[DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern uint csi_Atomic_File_Read(IntPtr self, IntPtr dest, uint size);
/// <summary>
/// Write bytes to the file, with optional offset into array and count of bytes to write.
/// Return number of bytes actually written.
/// </summary>
unsafe public int Write(byte[] bytes, int offset = 0, int count = 0)
{
if (bytes == null || bytes.Length == 0)
return 0;
if (count == 0)
count = bytes.Length;
if (offset >= bytes.Length)
return 0;
// should this truncate?
if ((offset + count) > bytes.Length)
return 0;
int bytesWritten = 0;
fixed (byte* p = bytes)
{
IntPtr ptr = (IntPtr)p;
bytesWritten = (int)csi_Atomic_File_Write(nativeInstance, ptr, (uint) offset, (uint)count);
}
return bytesWritten;
}
[DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern uint csi_Atomic_File_Write(IntPtr self, IntPtr data, uint offset, uint size);
};
}