aboutsummaryrefslogtreecommitdiff
path: root/devtools/create_supernova/file.cpp
blob: 5fb842aff6942890ff476681d54d546b26d626a2 (plain)
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
#include "file.h"

bool File::open(const char *filename, AccessMode mode) {
	f = fopen(filename, (mode == kFileReadMode) ? "rb" : "wb");
	return (f != NULL);
}

void File::close() {
	fclose(f);
	f = NULL;
}

int File::seek(int32 offset, int whence) {
	return fseek(f, offset, whence);
}

long File::read(void *buffer, int len) {
	return fread(buffer, 1, len, f);
}
void File::write(const void *buffer, int len) {
	fwrite(buffer, 1, len, f);
}

byte File::readByte() {
	byte v;
	read(&v, sizeof(byte));
	return v;
}

uint16 File::readWord() {
	uint16 v;
	read(&v, sizeof(uint16));
	return FROM_LE_16(v);
}

uint32 File::readLong() {
	uint32 v;
	read(&v, sizeof(uint32));
	return FROM_LE_32(v);
}

void File::readString(char* string, int bufferLength) {
	int i = 0;
	while (i < bufferLength - 1 && fread(string + i, 1, 1, f) == 1) {
		if (string[i] == '\n' || string[i] == 0)
			break;
		++ i;
	}
	string[i] = 0;
}

void File::writeByte(byte v) {
	write(&v, sizeof(byte));
}

void File::writeWord(uint16 v) {
	uint16 vTemp = TO_LE_16(v);
	write(&vTemp, sizeof(uint16));
}

void File::writeLong(uint32 v) {
	uint32 vTemp = TO_LE_32(v);
	write(&vTemp, sizeof(uint32));
}

void File::writeString(const char *s) {
	write(s, strlen(s) + 1);
}

uint32 File::pos() {
	return ftell(f);
}