forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
206 lines (170 loc) · 5.38 KB
/
utils.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
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
/*
* Copyright (C) 2015-2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "utils.h"
#include <QTextStream>
#include <QVector>
using namespace Cutelyst;
QByteArray buildTableDivision(const QVector<int> &columnsSize)
{
QByteArray buffer;
QTextStream out(&buffer, QIODevice::WriteOnly);
for (int i = 0; i < columnsSize.size(); ++i) {
if (i) {
out << '+';
} else {
out << '.';
}
out << QByteArray().fill('-', columnsSize[i] + 2).data();
}
out << '.';
return buffer;
}
QByteArray Utils::buildTable(const QVector<QStringList> &table, const QStringList &headers, const QString &title)
{
QByteArray buffer;
QVector<int> columnsSize;
if (!headers.isEmpty()) {
for (const QString &header : headers) {
columnsSize.push_back(header.size());
}
} else {
for (const QStringList &rows : table) {
if (columnsSize.empty()) {
for (const QString &row : rows) {
columnsSize.push_back(row.size());
}
} else if (rows.size() != columnsSize.size()) {
qFatal("Incomplete table");
}
}
}
for (const QStringList &row : table) {
if (row.size() > columnsSize.size()) {
qFatal("Incomplete table");
break;
}
for (int i = 0; i < row.size(); ++i) {
columnsSize[i] = qMax(columnsSize[i], row[i].size());
}
}
// printing
QTextStream out(&buffer, QIODevice::WriteOnly);
out.setFieldAlignment(QTextStream::AlignLeft);
QByteArray div = buildTableDivision(columnsSize);
if (!title.isEmpty()) {
out << title << endl;
}
// Top line
out << div << endl;
if (!headers.isEmpty()) {
// header titles
for (int i = 0; i < headers.size(); ++i) {
out << "| ";
out.setFieldWidth(columnsSize[i]);
out << headers[i];
out.setFieldWidth(0);
out << ' ';
}
out << '|' << endl;
// header bottom line
out << div << endl;
}
for (const QStringList &row : table) {
// content table
for (int i = 0; i < row.size(); ++i) {
out << "| ";
out.setFieldWidth(columnsSize[i]);
out << row[i];
out.setFieldWidth(0);
out << ' ';
}
out << '|' << endl;
}
// table bottom line
out << div;
return buffer;
}
QString Utils::decodePercentEncoding(QString *s)
{
if (s->isEmpty()) {
return *s;
}
QByteArray ba = s->toLatin1();
char *data = ba.data();
const char *inputPtr = data;
const int len = ba.count();
bool skipUtf8 = true;
int outlen = 0;
for (int i = 0 ; i < len; ++i, ++outlen) {
const char c = inputPtr[i];
if (c == '%' && i + 2 < len) {
int a = inputPtr[++i];
int b = inputPtr[++i];
if (a >= '0' && a <= '9') a -= '0';
else if (a >= 'a' && a <= 'f') a = a - 'a' + 10;
else if (a >= 'A' && a <= 'F') a = a - 'A' + 10;
if (b >= '0' && b <= '9') b -= '0';
else if (b >= 'a' && b <= 'f') b = b - 'a' + 10;
else if (b >= 'A' && b <= 'F') b = b - 'A' + 10;
*data++ = (char)((a << 4) | b);
skipUtf8 = false;
} else if (c == '+') {
*data++ = ' ';
} else {
*data++ = c;
}
}
if (skipUtf8) {
return *s;
}
return QString::fromUtf8(ba.data(), outlen);
}
QString Utils::decodePercentEncoding(QByteArray *ba)
{
if (ba->isEmpty())
return QString();
char *data = ba->data();
const char *inputPtr = data;
int len = ba->count();
bool skipUtf8 = true;
int outlen = 0;
for (int i = 0; i < len; ++i, ++outlen) {
const char c = inputPtr[i];
if (c == '%' && i + 2 < len) {
int a = inputPtr[++i];
int b = inputPtr[++i];
if (a >= '0' && a <= '9') a -= '0';
else if (a >= 'a' && a <= 'f') a = a - 'a' + 10;
else if (a >= 'A' && a <= 'F') a = a - 'A' + 10;
if (b >= '0' && b <= '9') b -= '0';
else if (b >= 'a' && b <= 'f') b = b - 'a' + 10;
else if (b >= 'A' && b <= 'F') b = b - 'A' + 10;
*data++ = (char)((a << 4) | b);
skipUtf8 = false;
} else if (c == '+') {
*data++ = ' ';
} else {
*data++ = c;
}
}
if (skipUtf8) {
return QString::fromLatin1(ba->data(), outlen);
} else {
return QString::fromUtf8(ba->data(), outlen);
}
}