-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVGParser.h
183 lines (164 loc) · 4.17 KB
/
SVGParser.h
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
#pragma once
#include "ShapesContainer.h"
#include <string>
void Open(string filename)
{
ifstream input;
input.open(filename);
if (!input.is_open())
{
cout << "Couldn't open the file.";
}
}
int LinesCount(string filename)
{
ifstream input;
input.open(filename);
string line;
unsigned int curLine = 0;
while (getline(input, line))
{
curLine++;
}
input.close();
return curLine;
}
ShapesContainer ParseFile(string filename)
{
ShapesContainer array;
int currsor=LinesCount(filename);//the total lines in the file
ifstream input;
string line; // I'm using that string to get rid of chars I do not need
string token = "<svg>";
input.open(filename);
unsigned int curLine = 0;
while (getline(input, line)) // curLine is the index of the start of the <svg> tag
{
curLine++;
if (line.find(token, 0) != string::npos)
{
break;
}
} // I'm finding curLine by searching for the <svg> tag
string shape;
int LinesLeft = currsor - curLine + 1;
int startX = 0, startY = 0, endX = 0, endY = 0;
string line2;
string color;
input.seekg(0);
for (int n = 1; n < curLine; n += 1) // skipping the first lines before the opening tag
{
if (!getline(input, line2))
{
cout << "error";
}
}
while (getline(input, line2)) //building the Container
{
if (LinesLeft==currsor)
{
break; // I break before the closing </svg> tag
}
input.ignore(3);
getline(input, shape, ' ');
if (shape.compare("rect") == 0)
{
getline(input, line, '"');
input >> startX;
getline(input, line, '=');
input.ignore(1);
input >> startY;
getline(input, line, '=');
input.ignore(1);
input >> endX;
getline(input, line, '=');
input.ignore(1);
input >> endY;
getline(input, line, '=');
input.ignore(1);
getline(input, color, '"');
int len = color.length() + 1;
char* ColorChar = new char[len];
for (int i = 0; i < len; i++) {
ColorChar[i] = color[i];
}
array.AddShape("rectangle", startX, startY, ColorChar, endX, endY);
delete[] ColorChar;
}
else if(shape.compare("circle")==0)
{
getline(input, line, '"');
input >> startX;
getline(input, line, '=');
input.ignore(1);
input >> startY;
getline(input, line, '=');
input.ignore(1);
input >> endX;
getline(input, line, '=');
input.ignore(1);
getline(input, color, '"');
int len = color.length() + 1;
char* ColorChar = new char[len];
for (int i = 0; i < len; i++) {
ColorChar[i] = color[i];
}
array.AddShape("circle", startX, startY, ColorChar, endX, endY);
delete[] ColorChar;
}
else if (shape.compare("line"))
{
getline(input, line, '"');
input >> startX;
getline(input, line, '=');
input.ignore(1);
input >> startY;
getline(input, line, '=');
input.ignore(1);
input >> endX;
getline(input, line, '=');
input.ignore(1);
input >> endY;
getline(input, line, '=');
input.ignore(1);
getline(input, color, '"');
int len = color.length() + 1;
char* ColorChar = new char[len];
for (int i = 0; i < len; i++) {
ColorChar[i] = color[i];
}
array.AddShape("line", startX, startY, ColorChar, endX, endY);
delete[] ColorChar;
}
LinesLeft++;
}
return array;
}
void WritingBackInSVGFile(const char* filename, ShapesContainer& array)// this function is building new svg file with changed information
{
ofstream ofs;
ofs.open("temp.svg", ofstream::out);// using this file to build the whole file changing only the <svg> tagged section
int currsor = LinesCount(filename);//the total lines in the file
ifstream input;
string line; // I'm using that string to get rid of chars I do not need
string token = "<svg>";
input.open(filename);
unsigned int curLine = 0;
while (getline(input, line)) // curLine is the index of the start of the <svg> tag
{
curLine++;
if (line.find(token, 0) != string::npos)
{
break;
}
} // I'm finding curLine by searching for the <svg> tag
input.seekg(0);
int LinesLeft = currsor - curLine + 1;
for (int i = 0; i < curLine; i++)
{
getline(input, line);
ofs << line << endl;
}
array.PrintAllInStrm(ofs);
ofs << "</svg>";
}