-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
108 lines (92 loc) · 3.26 KB
/
main.cpp
File metadata and controls
108 lines (92 loc) · 3.26 KB
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
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
bool isLevelFileValid();
QString checkPlayerFile();
int main(int argc, char *argv[])
{
//Construct application
QApplication a(argc, argv);
MainWindow w;
MainWindow::userName = checkPlayerFile();
if(isLevelFileValid())
{
w.updateStatusBar(); //updates status bar
w.checkForSetUser(); //Enables start button if valid user
w.show(); //displays mainwindow
}
return a.exec();
}
bool isLevelFileValid()
{
QString file_name = "/Users/Parth/Documents/QT/RvZ/rvz_levels.csv";
QFile level_file(file_name);
//Displays warning if file is not readable and exits program
if(!level_file.open(QIODevice::ReadWrite|QIODevice::Text))
{
QMessageBox::warning(0,"Cannot execute RvZ","rvz_levels.csv does not exist or is unreadable!",
QMessageBox::Ok);
return 0;
}
else
return 1;
}
QString checkPlayerFile()
{
QString file_name = "/Users/Parth/Documents/QT/RvZ/rvz_players.csv";
QFile user_file(file_name);
QString last_user;
//Displays warning if file is not readable and exits program
if(!user_file.open(QIODevice::ReadWrite|QIODevice::Text))
{
MainWindow::userLevel = "1";
return "";
}
else
{
QTextStream verifier(&user_file);
while(!verifier.atEnd())
{
//Creates temporary text manipulation objects
QStringList temp;
QByteArray name_array,level_array;
int level;
//Splits line and saves the level information as char array
temp = verifier.readLine().split(':');
name_array = temp.at(1).toLatin1();
level_array = temp.at(2).toLatin1();
level = temp.at(2).toInt();
//Checking if level is valid
for(int i = 0; i < level_array.length(); i++)
{
//Checks if level is numeric and between 0 and 100
if(!(level_array.at(i) > 47 && level_array.at(i) < 58) || (level < 0 && level > 100))
{
//Displays warning if level is invalid
QMessageBox::warning(0,"User file discarded","Invalid level information! rvz_players.csv will be discarded!",
QMessageBox::Ok);
user_file.resize(0); //clears file
goto close; //goes to close flag
}
}
//Checking if name is valid
for(int i = 0; i < name_array.length(); i++)
{
//Checks if level is numeric and between 0 and 100
if(name_array.length() > 10)
{
//Displays warning if level is invalid
QMessageBox::warning(0,"User file discarded","Invalid user information! rvz_players.csv will be discarded!",
QMessageBox::Ok);
user_file.resize(0); //clears file
goto close; //goes to close flag
}
}
last_user = temp.at(1);
MainWindow::userLevel = temp.at(2);
}
close: //closes file
user_file.close();
return last_user;
}
}