-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursus.cpp
166 lines (153 loc) · 6.2 KB
/
Cursus.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "UTProfiler.h"
#include <sstream>
#include <QFile>
#include <QTextCodec>
#include <QtXml>
#include <QMessageBox>
#include "Cursus.h"
void Cursus::retirerUV(unsigned int x){
for(unsigned int j=x; j<nbUv-1;j++){
Cuvs[j]=Cuvs[j+1];}
nbUv--;
}
CursusManager::CursusManager():Curs(0),nbCursus(0),nbMaxCursus(0),file(""),modification(false){
}
void CursusManager::AjouterCursus(Cursus* cur){
if (nbCursus==nbMaxCursus){
Cursus** newtab=new Cursus*[nbMaxCursus+10];
for(unsigned int i=0; i<nbCursus; i++) newtab[i]=Curs[i];
nbMaxCursus+=10;
Cursus** old=Curs;
Curs=newtab;
delete[] old;
}
Curs[nbCursus++]=cur;
}
void CursusManager::saveC(const QString& f){
file=f;
QFile newfile( file);
if (!newfile.open(QIODevice::WriteOnly | QIODevice::Text)) throw UTProfilerException(QString("erreur ouverture fichier xml"));
QXmlStreamWriter stream(&newfile);
stream.setAutoFormatting(true);
stream.writeStartDocument();
stream.writeStartElement("Cursus");
for(unsigned int i=0; i<nbCursus; i++){
stream.writeStartElement("Cur");
stream.writeTextElement("titre",Curs[i]->getTitle());
stream.writeTextElement("CreditCS",QString::number(Curs[i]->getCreditCS()));
stream.writeTextElement("CreditTM",QString::number(Curs[i]->getCreditTM()));
stream.writeTextElement("CreditTSH",QString::number(Curs[i]->getCreditTSH()));
stream.writeTextElement("CreditLibre",QString::number(Curs[i]->getCreditCL()));
for(unsigned int j=0; j<Curs[i]->getNbUV(); j++){
stream.writeTextElement("Cuv",Curs[i]->getUV(j)->getCode());
}
stream.writeEndElement();
}
stream.writeEndElement();
stream.writeEndDocument();
newfile.close();
}
void CursusManager::load(const QString& f){
if (file!=f) this->~CursusManager();
file=f;
QFile fin(file);
// If we can't open it, let's show an error message.
if (!fin.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw UTProfilerException("Erreur ouverture fichier Cursus");
}
// QXmlStreamReader takes any QIODevice.
QXmlStreamReader xml(&fin);
// We'll parse the XML until we reach end of it.
while(!xml.atEnd() && !xml.hasError()) {
// Read next element.
QXmlStreamReader::TokenType token = xml.readNext();
// If token is just StartDocument, we'll go to next.
if(token == QXmlStreamReader::StartDocument) continue;
// If token is StartElement, we'll see if we can read it.
if(token == QXmlStreamReader::StartElement) {
// If it's named Cursus, we'll go to the next.
if(xml.name() == "Cursus") continue;
// If it's named cur, we'll dig the information from there.
if(xml.name() == "Cur") {
QString titre;
unsigned int NBCS;
unsigned int NBTM;
unsigned int NBTSH;
unsigned int NBCL;
UV* currUv;
Cursus* Cur;
xml.readNext();
//We'll continue the loop until we hit an EndElement named Cur.
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "Cur")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
// We've found titre.
if(xml.name() == "titre") {
xml.readNext();
titre=xml.text().toString();
Cur = new Cursus(titre);
}
if(xml.name() == "CreditCS") {
xml.readNext();
NBCS=xml.text().toString().toUInt();
Cur->setCreditCS(NBCS);
}
if(xml.name() == "CreditTM") {
xml.readNext();
NBTM=xml.text().toString().toUInt();
Cur->setCreditTM(NBTM);
}
if(xml.name() == "CreditTSH") {
xml.readNext();
NBTSH=xml.text().toString().toUInt();
Cur->setCreditTSH(NBTSH);
}
if(xml.name() == "CreditLibre") {
xml.readNext();
NBCL=xml.text().toString().toUInt();
Cur->setCreditLibre(NBCL);
}
// We've found List of uv.
if(xml.name() == "Cuv") {
xml.readNext();
QString texte=xml.text().toString();
currUv=UVManager::getInstance().trouverUV(texte);
Cur->AjouterUv(currUv);
}
}
// ...and next...
xml.readNext();
}
AjouterCursus(Cur);
}
}
}
// Error handling.
if(xml.hasError()) {
throw UTProfilerException("Erreur lecteur fichier UV, parser xml");
}
// Removes any device() or data from the reader * and resets its internal state to the initial state.
xml.clear();
}
CursusManager::~CursusManager(){
if (file!="") saveC(file);
for(unsigned int i=0; i<nbCursus; i++) delete Curs[i];
delete[] Curs;
}
Cursus* CursusManager::trouverCursus( QString& c){
for(unsigned int i=0; i<nbCursus; i++)
if (c==Curs[i]->getTitle()) return Curs[i];
return 0;
}
Cursus& CursusManager::getCursus(QString& code){
Cursus* Cur=trouverCursus(code);
if (!Cur) throw UTProfilerException("erreur, UVManager, UV inexistante",__FILE__,__LINE__);
return *Cur;
}
CursusManager::Handler CursusManager::handler=Handler();
CursusManager& CursusManager::getInstance(){
if (!handler.instance) handler.instance = new CursusManager; /* instance créée une seule fois lors de la première utilisation*/
return *handler.instance;
}
void CursusManager::libererInstance(){
if (handler.instance) { delete handler.instance; handler.instance=0; }
}