-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamerecord.cpp
120 lines (98 loc) · 2.48 KB
/
gamerecord.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "gamerecord.h"
#include <QDir>
#include <QFile>
GameRecord::GameRecord()
{
// Set the path
recordPath = QDir::homePath() + "/.HexGame/";
QDir *dir = new QDir();
// Make the path if it hasn't be created
dir->mkpath(recordPath);
}
bool GameRecord::createFile(const QString &filename, int intCount)
{
// Create the file
QFile recordFile(recordPath + filename);
if (!recordFile.open(QIODevice::WriteOnly))
return false;
// Set the values to 0
QDataStream out(&recordFile);
for (int i = 0 ; i < intCount; ++i)
out << 0;
return true;
}
bool GameRecord::resizeFile(const QString &filename, int intCount)
{
// Resize the file
QFile recordFile(recordPath + filename);
return recordFile.resize(intCount * 4);
}
bool GameRecord::exists(const QString &filename)
{
return QFile::exists(recordPath + filename);
}
bool GameRecord::remove(const QString &filename)
{
return QFile::remove(recordPath + filename);
}
int GameRecord::sizeOfInt(const QString &filename)
{
QFile recordFile(recordPath + filename);
return recordFile.size() / sizeof(int);
}
bool GameRecord::writeData(const QString &filename, int pos, int data)
{
//TODO
// Open the file
QFile recordFile(recordPath + filename);
if (!recordFile.open(QIODevice::ReadWrite))
return false;
// Write the data
QDataStream out(&recordFile);
recordFile.seek(pos * sizeof(int));
out << data;
return true;
}
int GameRecord::readData(const QString &filename, int pos)
{
//TODO
// Open the file
QFile recordFile(recordPath + filename);
if (!recordFile.open(QIODevice::ReadOnly))
return false;
// Read the data
QDataStream in(&recordFile);
recordFile.seek(pos * sizeof(int));
int result;
in >> result;
return result;
}
bool GameRecord::writeDataArr(const QString &filename, int *dataArr, int size)
{
//TODO
// Open the file
QFile recordFile(recordPath + filename);
if (!recordFile.open(QIODevice::WriteOnly))
return false;
// Write the data
QDataStream out(&recordFile);
for (int i = 0; i < size; ++i)
out << dataArr[i];
return true;
}
bool GameRecord::readDataArr(const QString &filename, int *&dataArr, int &size)
{
//TODO
// Open the file
QFile recordFile(recordPath + filename);
if (!recordFile.open(QIODevice::ReadOnly))
return false;
// Prepare space for the data
size = sizeOfInt(filename);
dataArr = new int[size];
// Read the data
QDataStream in(&recordFile);
for (int i = 0; i < size; ++i)
in >> dataArr[i];
return true;
}