summaryrefslogtreecommitdiff
path: root/Source/Library/Userland/Binding/File.class.h
blob: 4d1ac16ebf991b5a12dc8502d27644027bb9c486 (plain) (blame)
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
#ifndef DEF_FILE_CLASS_H
#define DEF_FILE_CLASS_H

#include <File.iface.h>
#include <Binding/FSNode.class.h>
#include <Syscall/RessourceCaller.class.h>
#include <Binding/VirtualTerminal.class.h>
#include <ByteArray.class.h>

class File : public RessourceCaller {
	public:
	File(String name, u8int mode, FSNode start = FSNode(0)) :
   		RessourceCaller(sCall(FLIF_OBJTYPE, FLIF_SOPEN, (u32int)&name, mode, start.resId()), FLIF_OBJTYPE) {
	}
	File(u32int id) : RessourceCaller(id, FLIF_OBJTYPE) {}

	void close() {
		doCall(FLIF_CLOSE);
	}
	bool validOpened() {
		return (doCall(FLIF_VALID) != 0);
	}
	u32int read(u32int max_length, u8int *ptr) {
		return (doCall(FLIF_READ, max_length, (u32int)ptr));
	}
	bool write(u32int length, u8int* ptr) {
		return (doCall(FLIF_WRITE, length, (u32int)ptr) != 0);
	}
	u32int read(ByteArray &data) {
		if (!valid()) {
			data.clear();
			return 0;
		}
		u32int l = read(data.size(), (u8int*)data);
		if (l != data.size()) data.resize(l);
		return l;
	}
	bool write(ByteArray &data) {
		if (!valid()) return false;
		return write(data.size(), (u8int*)data);
	}

	template <typename T> bool read(T* elem) {
		return (read(sizeof(T), (u8int*)elem) == sizeof(T));
	}
	template <typename T> bool write(T* elem) {
		return write(sizeof(T), (u8int*)elem);
	}

	bool seek(u64int count, u8int mode) {
		union {
			u64int x;
			u32int a[2];
		} wat = {count};
		return (doCall(FLIF_SEEK, wat.a[0], wat.a[1], mode) != 0);
	}
	u64int position() {
		return *((u64int*)doCall(FLIF_POSITION));
	}
	u64int length() {
		return *((u64int*)doCall(FLIF_LENGTH));
	}
	bool eof() {
		return (doCall(FLIF_EOF) != 0);
	}
};

#endif