Website Summary Files Commits Branches @commit @diff @stats
path: root / Utils / Kindle / serialize.cpp
blob: bd75872f9857e61f7e0e5d8c721abd4c64f61259 (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
69
70
71
#include"serialize.h"

void writeString(std::ofstream& outFile, const string& str) {
	int len = str.getSize();
	outFile.write((const char*)&len, sizeof(int));

	outFile.write(str.c_str(), sizeof(char) * len);
}

string readString(std::ifstream& inFile) {
	int len = 0;
	inFile.read((char*)&len, sizeof(len));

	char* buff = new char[len + 1];
	inFile.read(buff, sizeof(char) * len);

	buff[len] = '\0';

	string toReturn(buff);

	delete[] buff;

	return toReturn;
}

void writeVector(std::ofstream& outFile, const Vector<size_t>& v) {
	int len = v.getSize();
	outFile.write((const char*)&len, sizeof(int));

	for (size_t i = 0; i < v.getSize(); i++)
		outFile.write((const char*)&v[i], sizeof(size_t));
}

Vector<size_t> readVector(std::ifstream& inFile) {
	int len = 0;
	inFile.read((char*)&len, sizeof(int));
	Vector<size_t> toReturn;
	size_t read = 0;

	for (size_t i = 0; i < len; i++) {
		inFile.read((char*)&read, sizeof(size_t));
		toReturn.pushBack(read);
	}

	return toReturn;
}

void writeCommentsToFile(std::ofstream& outFile, const Pair<string, string>& p) {
	writeString(outFile, p.getFirst());
	writeString(outFile, p.getSecond());
}

Pair<string, string> readCommentsFromFile(std::ifstream& inFile) {
	string fst = readString(inFile);
	string snd = readString(inFile);

	return { fst, snd };
}

void writeRatesToFile(std::ofstream& outFile, const Pair<string, unsigned>& comments) {
	writeString(outFile, comments.getFirst());
	outFile.write((const char*)&comments.getSecond(), sizeof(comments.getSecond()));
}

Pair<string, unsigned> readRatesFromFile(std::ifstream& inFile) {
	string fst = readString(inFile);
	unsigned rate = 0;
	inFile.read((char*)&rate, sizeof(rate));

	return { fst, rate };
}