-
Notifications
You must be signed in to change notification settings - Fork 1
/
readwrite.cpp
105 lines (91 loc) · 2.98 KB
/
readwrite.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
#include "event.h"
#include "readwrite.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox> //for debug
ReadWrite::ReadWrite(){
//empty constructor
}
/* both methods in this class are static.
* You can make a ReadWrite object if necessary.
* But static methods can be called without an instance of the class.
*/
void ReadWrite::write(const Event& event) {
QFile file("eventlist.txt");
if(file.open(QIODevice::Append | QIODevice::WriteOnly)) {
QTextStream writeStream(&file);
writeStream<< "[event] " + event.getName() +"\n";
writeStream<< "[creator] " + event.getCreator() + "\n";
writeStream<< "[date] " + event.getDate() + "\n";
writeStream<< "[timeSlots] ";
foreach(QString time, event.getSlots())
{
writeStream<<time + ",";
}
writeStream << "\n";
foreach(Attendee att, event.getAttendees())
{
writeStream<< "[attendee] " + att.getName() + "\n";
writeStream<< "[att_timeSlots] ";
foreach(QString time, att.getSlots())
{
writeStream<<time + ",";
}
writeStream << "\n";
}
writeStream<< "\n\n";
}
file.close();
}
void ReadWrite::read(QVector<Event>& eventList) {
QFile file("eventlist.txt");
QString lines = "test";
QString eventName;
QString creatorName;
QString date;
QString times;
QVector<QString> timeSlots;
QString att_name;
QString att_times;
if(file.open(QIODevice::ReadOnly))
{
QTextStream readStream(&file);
while (!readStream.atEnd())
{
lines = readStream.readLine();
if (lines.indexOf("[event]") == 0)
{
eventName = lines.right(lines.size()-8);
lines = readStream.readLine();
creatorName = lines.right(lines.size()-10);
lines = readStream.readLine();
date = lines.right(lines.size()-7);
lines = readStream.readLine();
times = lines.right(lines.size()-12);
foreach (QString time, times.split(",")) {
timeSlots.append(time);
}
timeSlots.removeLast();
Event newEvent(eventName, creatorName, date, timeSlots);
timeSlots.clear();
lines = readStream.readLine();
while (lines.startsWith("[attendee] "))
{
att_name = lines.right(lines.size()-11);
lines = readStream.readLine();
att_times = lines.right(lines.size()-16);
foreach (QString time, times.split(",")) {
timeSlots.append(time);
}
timeSlots.removeLast();
Attendee att(att_name, timeSlots);
newEvent.addAttendee(att);
lines = readStream.readLine();
timeSlots.clear();
}
eventList.append(newEvent);
}
}
file.remove();
}
}