-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
204 lines (168 loc) · 4.3 KB
/
Parser.cpp
File metadata and controls
204 lines (168 loc) · 4.3 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
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
#include "Parser.h"
#include "Helpers.h"
#include <fstream>
#include <cassert>
#include <sstream>
#include <iomanip>
char const EXISTS_HEADER = char(127);
#define cOFFSET_LEN 10
Piece * Parser::parseFile(std::string const& file, int & length)
{
Piece * first = nullptr;
std::ifstream f(file);
if (f.is_open())
{
// read first character
char in = f.get();
if (in == EXISTS_HEADER)
{
// read offset
int offset = parseNextInt(f);
// read pieces
first = new Piece("", 0, 0, nullptr, true);
Piece * previous = first;
int pos = offset;
while ((int)f.tellg() < offset)
{
int len = parseNextInt(f);
std::string font = parseNextString(f);
std::string style = parseNextString(f);
QFont qfont = parseFont(font, style);
// append new piece
Piece * piece = new Piece(file, len, pos, nullptr, qfont);
pos += len;
previous->setNext(piece);
previous = piece;
}
// write length
length = Helpers::GetFileSize(file) - offset;
}
else
{
// file doesn't have a header
// actual first piece
length = Helpers::GetFileSize(file);
Piece * second = new Piece(file, length, 0, nullptr);
// dummy piece
first = new Piece("", 0, 0, second, true);
}
}
else
{
first = nullptr;
length = 0;
}
f.close();
return first;
}
bool Parser::writeFile(std::string const& file, Piece * first)
{
// check if file can be openend
std::ofstream out;
out.open(file, std::ofstream::out | std::ofstream::trunc);
if (!out.is_open())
return false;
std::stringstream text;
std::stringstream info;
std::stringstream header;
std::string output;
Piece * piece = first;
if (first != nullptr)
piece = first->getNext();
info << ";";
while (piece != nullptr)
{
// add text
text << piece->getText();
// add meta info
info << piece->getLength() << ";";
info << qfontToFontString(piece->getFont()) << ";";
info << qfontToStyleString(piece->getFont()) << ";";
piece = piece->getNext();
}
// set header flag
header << EXISTS_HEADER;
// write offset
int offset = int(header.str().size() + info.str().size() + cOFFSET_LEN);
header << std::setw(cOFFSET_LEN) << offset;
// put it all together
output = header.str() + info.str() + text.str();
out << output;
out.close();
return true;
}
int Parser::parseNextInt(std::ifstream & file)
{
return std::stoi(parseNextString(file));
}
std::string Parser::parseNextString(std::ifstream & file)
{
std::stringstream s;
char c = file.get();
while (c != ';' && c != EOF)
{
s << c;
c = file.get();
}
return s.str();
}
std::string Parser::qfontToFontString(QFont font)
{
std::string fontStr;
fontStr.append(std::to_string(font.pixelSize()));
fontStr.append("px ");
fontStr.append(font.family().toStdString());
return std::string(fontStr);
}
std::string Parser::qfontToStyleString(QFont font)
{
std::string style;
if(font.bold())
style.push_back('b');
if(font.italic())
style.push_back('i');
if(font.underline())
style.push_back('u');
return style;
}
QFont Parser::parseFont(std::string const& font, std::string const& style)
{
QFont qFont;
int idx = int(font.find("px"));
int size = std::stoi(font.substr(0, idx));
qFont.setPixelSize(size);
idx = int(font.find(' ')) + 1;
std::string family = font.substr(idx, font.size()-1);
qFont.setFamily(QString::fromStdString(family));
if (style.find('b') != std::string::npos)
qFont.setBold(true);
if (style.find('i') != std::string::npos)
qFont.setItalic(true);
if (style.find('u') != std::string::npos)
qFont.setUnderline(true);
return qFont;
}
std::string Parser::getFontAsString(QFont const& font)
{
std::string fontStr;
if (font.italic())
fontStr.append("italic ");
if (font.bold())
fontStr.append("bold ");
/* underline not supported */
fontStr.append(std::to_string(font.pixelSize()) + "px ");
fontStr.append(font.family().toStdString());
return fontStr;
}
bool Parser::fileHasHeader(std::string const& file)
{
std::ifstream f(file);
bool ret = false;
if (f.is_open())
{
char in = f.get();
ret = (in == EXISTS_HEADER);
f.close();
}
return ret;
}