forked from halexandru11/InterSchem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_export.hpp
238 lines (217 loc) · 6.15 KB
/
import_export.hpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once
#include "inputText.hpp"
#define Dad v[i]
#define Kid v[a]
#define caracter E.text.unicode
string filePath = "output.txt";
void PrintNode(Node* p, ofstream &g)
{
g << p->nodeType << '\n';
g << p->getNodeCoordonates(Constants::CoordNode).x << ' ' << p->getNodeCoordonates(Constants::CoordNode).y << '\n';
g << p->content << '\n';
}
void PrintEdges(vector <Node*> v, ofstream &g)
{
for(auto p : v)
{
int q = -1;
for(int i = 0; i < int(v.size()); ++i)
if(p->urm == v[i])
{
q = i;
break;
}
g << q << ' ';
q = -1;
for(int i = 0; i < int(v.size()); ++i)
if(p->urmTrue == v[i])
{
q = i;
break;
}
g << q << ' ';
q = -1;
for(int i = 0; i < int(v.size()); ++i)
if(p->urmFalse == v[i])
{
q = i;
break;
}
g << q << '\n';
}
}
void ExportToFile(vector <Node*> v)
{
RenderWindow fereastra_citire(VideoMode(1000, 150), "Export", Style::Close | Style::Titlebar);
isPOPup = true;
filePath = "";
string taskk = "Introduceti numele fisierului + extenisa(*.sch, *.cpp):";
setPopupText(taskk);
while(fereastra_citire.isOpen())
{
Event E;
while(fereastra_citire.pollEvent(E))
{
if(E.type == Event::Closed)
{
fereastra_citire.close();
}
else if(E.type == Event::TextEntered)
{
getInputPop(E);
if(caracter == 13)
{
filePath = inputPopUp.getString();
setPopupInputText("");
fereastra_citire.close();
}
}
}
fereastra_citire.clear();
afiseazaPopup(fereastra_citire);
fereastra_citire.display();
}
isPOPup = false;
if(filePath.size() < 5) return;
if(filePath.substr(filePath.size()-4) != ".cpp" && filePath.substr(filePath.size()-4) != ".sch") {
throwError("Ati gresit extensia fisierului.\nDoar *.sch si *.cpp sunt\nacceptate.");
changeTab(1);
OutputText.setString(errorString);
OutputText.setFillColor(Color(255, 160, 0));
return;
}
ofstream g(filePath);
g << v.size() << '\n';
for(int i = 0; i < int(v.size()); ++i)
PrintNode(v[i],g);
PrintEdges(v,g);
g.close();
}
void EnterNode(ifstream &f, vector <Node*> &v)
{
int type;
float x,y;
char buff[502];
string ct;
f >> type >> x >> y;
f.get();
f.get(buff, 501);
for(int i = 0; i < int(strlen(buff)); ++i)
ct.push_back(buff[i]);
Node*p;
if(type == Constants::StartNode)
{
p = new Node(Constants::StartNode, font);
}
else if(type == Constants::AssignNode)
p = new Node(Constants::AssignNode, font);
else if(type == Constants::ConditionalNode)
p = new Node(Constants::ConditionalNode, font);
else if(type == Constants::OutputNode)
p = new Node(Constants::OutputNode, font);
else if(type == Constants::StopNode)
{
isStopNode = true;
p = new Node(Constants::StopNode, font);
}
else if(type == Constants::ReadNode)
p = new Node(Constants::ReadNode, font);
p->setNodeCoordonates(Vector2f(x,y));
p->setTextString(ct);
//cout << type << ' ' << ' ' << y << ct << '\n';
v.push_back(p);
if(type == Constants::StartNode)
{
isStartNode = true;
StartSchema = v.back();
}
}
void setLines(ifstream &f, vector <Node*> &v, vector<Line>& linii)
{
for(int i = 0; i < int(v.size()); ++i)
{
int a;
f >> a ;
// cout << a << ' ';
/*
*/
if(a != -1)
{
// cout << v[i]->content << ' ' << v[a]->content << '\n';
linii.push_back(Line(*Dad, *Kid, Constants::CoordType::CoordOut, Kid));
}
f >> a ;
// cout << a << ' ';
if(a != -1)
{
// cout << v[i]->content << ' ' << v[a]->content << '\n';
linii.push_back(Line(*Dad,*Kid,Constants::CoordType::CoordOutTrue,Kid));
}
f >> a ;
// cout << a << ' ';
if(a != -1)
{
// cout << v[i]->content << ' ' << v[a]->content << '\n';
linii.push_back(Line(*Dad,*Kid,Constants::CoordType::CoordOutFalse,Kid));
}
// cout << '\n';
}
}
void ImportFromFile(vector <Node*> &v, vector<Line>& linii)
{
RenderWindow fereastra_citire(VideoMode(1000, 150), "Import", Style::Close | Style::Titlebar);
isPOPup = true;
filePath = "";
string taskk = "Introduceti numele fisierului + extenisa(*.sch):";
setPopupText(taskk);
while(fereastra_citire.isOpen())
{
Event E;
while(fereastra_citire.pollEvent(E))
{
if(E.type == Event::Closed)
{
fereastra_citire.close();
}
else if(E.type == Event::TextEntered)
{
getInputPop(E);
if(caracter == 13)
{
filePath = inputPopUp.getString();
setPopupInputText("");
fereastra_citire.close();
}
}
}
fereastra_citire.clear();
afiseazaPopup(fereastra_citire);
fereastra_citire.display();
}
isPOPup = false;
if(filePath.size() < 5) {
return;
}
if(filePath.substr(filePath.size()-4) != ".sch") {
throwError("Fisierul nu a putut fi deschis.\nAsigurati-va ca ati scris\nextensia corect(*.sch).");
changeTab(1);
OutputText.setString(errorString);
OutputText.setFillColor(Color(255, 160, 0));
return;
}
ifstream f(filePath);
if(!f) {
changeTab(1);
throwError("Acest fisier nu exista.");
OutputText.setString(errorString);
OutputText.setFillColor(Color(255, 160, 0));
return;
}
ClearScreen(v, linii);
int lg;
f >> lg;
for(int i = 0; i < lg; ++i)
EnterNode(f,v);
setLines(f,v,linii);
f.close();
}