summaryrefslogtreecommitdiff
path: root/rec.h
blob: 3406f02f15d4f03e0969cd0a153360bfcff9f3af (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
inline void *operator new(size_t s) { return malloc(s); }
inline void operator delete(void *p) { free(p); }

class Record
{
public:
	enum FieldsType { Nothing, X, Y, State, Detail, Window, Width, Height, Keysym, MAX_FIELD };
	enum RecordType { WindowMove = 256, TargetWindow };
	Record(unsigned _type, unsigned _time):
		fields_present(0), type(_type), time(_time) 
	{
	}
	~Record() { }
	unsigned get_type() const { return type; }
	unsigned get_time() const { return time; }
	bool has_field(FieldsType type) const { return ((fields_present >> type) & 1) != 0; }
	unsigned get_field(FieldsType type) const {
		if (!has_field(type)) {
			fprintf(stderr, "record has no field %d\n", type);
			exit(EXIT_FAILURE);
		}
		return fields[type];
	}
	unsigned get_field(FieldsType type, unsigned def) const {
		if (!has_field(type))
			return def;
		return fields[type];
	}
	void set_field(FieldsType type, unsigned value) {
		fields_present |= 1u << type;
		fields[type] = value;
	}
private:
	unsigned type, time;
	unsigned fields_present;
	unsigned fields[MAX_FIELD];
};

class RecordsFile
{
protected:
	RecordsFile(FILE *_f);
	~RecordsFile() { fclose(f); }

	enum { START_RECORD = 1, END_RECORD = 2, FIELD = 3 };
	FILE *f;
};

class ReadRecordsFile: public RecordsFile
{
public:
	ReadRecordsFile(const char *fn);
	bool at_eof() const { return eof; }
	Record get();
private:
	unsigned get_number();
	void get_number(unsigned num);

	bool eof;
	unsigned next;
};

class WriteRecordsFile: public RecordsFile
{
public:
	WriteRecordsFile(const char *fn);
	void put(const Record &rec);
private:
	void add_number(unsigned num);
};