-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfile.js
56 lines (46 loc) · 1.09 KB
/
file.js
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
import StormLib from './storm-lib';
class File {
static FILE_BEGIN = 0;
static FILE_CURRENT = 1;
static FILE_END = 2;
static MAX_PATH = 260;
constructor(handle) {
this.handle = handle;
}
close() {
const handle = this.handle;
if (handle) {
this.handle = null;
return StormLib.SFileCloseFile(handle);
}
}
get opened() {
return !!this.handle;
}
get name() {
if (this.handle) {
const name = new Buffer(this.constructor.MAX_PATH);
if (!StormLib.SFileGetFileName(this.handle, name)) {
return null;
}
return name.readCString();
}
}
get size() {
return this.handle && StormLib.SFileGetFileSize(this.handle, null);
}
get data() {
if (this.handle) {
const data = new Buffer(this.size);
this.position = 0;
if (!StormLib.SFileReadFile(this.handle, data, this.size, null, null)) {
return null;
}
return data;
}
}
set position(offset) {
return StormLib.SFileSetFilePointer(this.handle, offset, null, this.constructor.FILE_BEGIN);
}
}
export default File;