FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/lang/lang_file_parser.cpp at master · JavaDevl/tdesktop · GitHub
JavaDevl
/
tdesktop
Public
forked from
telegramdesktop/tdesktop
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
lang
/
lang_file_parser.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
214 lines (185 loc) · 6.27 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
lang
/
lang_file_parser.cpp
Copy path
File metadata and controls
214 lines (185 loc) · 6.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It 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 General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#
include
"
lang/lang_file_parser.h
"
#
include
"
base/parse_helper.h
"
namespace
Lang
{
namespace
{
constexpr
auto
kLangFileLimit
=
1024
*
1024
;
}
//
namespace
FileParser::FileParser
(
const
QString &file,
const
std::set<LangKey> &request)
: _content(base::parse::stripComments(ReadFile(file, file)))
, _request(request) {
parse
();
}
FileParser::FileParser
(
const
QByteArray &content, base::lambda<
void
(QLatin1String key,
const
QByteArray &value)> callback)
: _content(base::parse::stripComments(content))
, _callback(std::move(callback)) {
parse
();
}
void
FileParser::parse
() {
if
(_content.
isEmpty
()) {
error
(
qsl
(
"
Got empty lang file content
"
));
return
;
}
auto
text = _content.
constData
(), end = text + _content.
size
();
while
(text != end) {
if
(!
readKeyValue
(text, end)) {
break
;
}
}
}
const
QString &
FileParser::errors
()
const
{
if
(_errors.
isEmpty
() && !_errorsList.
isEmpty
()) {
_errors = _errorsList.
join
(
'
\n
'
);
}
return
_errors;
}
const
QString &
FileParser::warnings
()
const
{
if
(_warnings.
isEmpty
() && !_warningsList.
isEmpty
()) {
_warnings = _warningsList.
join
(
'
\n
'
);
}
return
_warnings;
}
bool
FileParser::readKeyValue
(
const
char
*&from,
const
char
*end) {
using
base::parse::skipWhitespaces;
if
(!
skipWhitespaces
(from, end))
return
false
;
if
(*from !=
'
"
'
) {
return
error
(
"
Expected quote before key name!
"
);
}
++from;
const
char
*nameStart = from;
while
(from < end && ((*from >=
'
a
'
&& *from <=
'
z
'
) || (*from >=
'
A
'
&& *from <=
'
Z
'
) || *from ==
'
_
'
|| (*from >=
'
0
'
&& *from <=
'
9
'
) || *from ==
'
#
'
)) {
++from;
}
auto
key =
QLatin1String
(nameStart, from - nameStart);
if
(from == end || *from !=
'
"
'
) {
return
error
(
qsl
(
"
Expected quote after key name '%1'!
"
).
arg
(key));
}
++from;
if
(!
skipWhitespaces
(from, end)) {
return
error
(
qsl
(
"
Unexpected end of file in key '%1'!
"
).
arg
(key));
}
if
(*from !=
'
=
'
) {
return
error
(
qsl
(
"
'=' expected in key '%1'!
"
).
arg
(key));
}
if
(!
skipWhitespaces
(++from, end)) {
return
error
(
qsl
(
"
Unexpected end of file in key '%1'!
"
).
arg
(key));
}
if
(*from !=
'
"
'
) {
return
error
(
qsl
(
"
Expected string after '=' in key '%1'!
"
).
arg
(key));
}
auto
skipping =
false
;
auto
keyIndex =
kLangKeysCount
;
if
(!_callback) {
keyIndex =
GetKeyIndex
(key);
skipping = (_request.
find
(keyIndex) == _request.
end
());
}
auto
value =
QByteArray
();
auto
appendValue = [&value, skipping](
auto
&&... args) {
if
(!skipping) {
value.
append
(std::forward<
decltype
(args)>(args)...);
}
};
const
char
*start = ++from;
while
(from < end && *from !=
'
"
'
) {
if
(*from ==
'
\n
'
) {
return
error
(
qsl
(
"
Unexpected end of string in key '%1'!
"
).
arg
(key));
}
if
(*from ==
'
\\
'
) {
if
(from +
1
>= end) {
return
error
(
qsl
(
"
Unexpected end of file in key '%1'!
"
).
arg
(key));
}
if
(*(from +
1
) ==
'
"
'
|| *(from +
1
) ==
'
\\
'
) {
if
(from > start)
appendValue
(start, from - start);
start = ++from;
}
else
if
(*(from +
1
) ==
'
n
'
) {
if
(from > start)
appendValue
(start, from - start);
appendValue
(
'
\n
'
);
start = (++from) +
1
;
}
}
++from;
}
if
(from >= end) {
return
error
(
qsl
(
"
Unexpected end of file in key '%1'!
"
).
arg
(key));
}
if
(from > start) {
appendValue
(start, from - start);
}
if
(!
skipWhitespaces
(++from, end)) {
return
error
(
qsl
(
"
Unexpected end of file in key '%1'!
"
).
arg
(key));
}
if
(*from !=
'
;
'
) {
return
error
(
qsl
(
"
';' expected after
\"
value
\"
in key '%1'!
"
).
arg
(key));
}
skipWhitespaces
(++from, end);
if
(_callback) {
_callback
(key, value);
}
else
if
(!skipping) {
_result.
insert
(keyIndex,
QString::fromUtf8
(value));
}
return
true
;
}
QByteArray
FileParser::ReadFile
(
const
QString &absolutePath,
const
QString &relativePath) {
QFile
file
(
QFileInfo
(relativePath).
exists
() ? relativePath : absolutePath);
if
(!file.
open
(QIODevice::ReadOnly)) {
LOG
((
"
Lang Error: Could not open file at '%1' ('%2')
"
).
arg
(relativePath).
arg
(absolutePath));
return
QByteArray
();
}
if
(file.
size
() >
kLangFileLimit
) {
LOG
((
"
Lang Error: File is too big: %1
"
).
arg
(file.
size
()));
return
QByteArray
();
}
constexpr
auto
kCodecMagicSize
=
3
;
auto
codecMagic = file.
read
(
kCodecMagicSize
);
if
(codecMagic.
size
() <
kCodecMagicSize
) {
LOG
((
"
Lang Error: Found bad file at '%1' ('%2')
"
).
arg
(relativePath).
arg
(absolutePath));
return
QByteArray
();
}
file.
seek
(
0
);
QByteArray data;
int
skip =
0
;
auto
readUtf16Stream = [relativePath, absolutePath](
auto
&&stream) {
stream.
setCodec
(
"
UTF-16
"
);
auto
string = stream.
readAll
();
if
(stream.
status
() != QTextStream::Ok) {
LOG
((
"
Lang Error: Could not read UTF-16 data from '%1' ('%2')
"
).
arg
(relativePath).
arg
(absolutePath));
return
QByteArray
();
}
if
(string.
isEmpty
()) {
LOG
((
"
Lang Error: Empty UTF-16 content in '%1' ('%2')
"
).
arg
(relativePath).
arg
(absolutePath));
return
QByteArray
();
}
return
string.
toUtf8
();
};
if
((codecMagic.
at
(
0
) ==
'
\xFF
'
&& codecMagic.
at
(
1
) ==
'
\xFE
'
) || (codecMagic.
at
(
0
) ==
'
\xFE
'
&& codecMagic.
at
(
1
) ==
'
\xFF
'
) || (codecMagic.
at
(
1
) ==
0
)) {
return
readUtf16Stream
(
QTextStream
(&file));
}
else
if
(codecMagic.
at
(
0
) ==
0
) {
auto
utf16WithBOM =
"
\xFE\xFF
"
+ file.
readAll
();
return
readUtf16Stream
(
QTextStream
(utf16WithBOM));
}
data = file.
readAll
();
if
(codecMagic.
at
(
0
) ==
'
\xEF
'
&& codecMagic.
at
(
1
) ==
'
\xBB
'
&& codecMagic.
at
(
2
) ==
'
\xBF
'
) {
data = data.
mid
(
3
);
//
skip UTF-8 BOM
}
if
(data.
isEmpty
()) {
LOG
((
"
Lang Error: Empty UTF-8 content in '%1' ('%2')
"
).
arg
(relativePath).
arg
(absolutePath));
return
QByteArray
();
}
return
data;
}
}
//
namespace Lang
Back
|
FazBrowse Home
|
New Git URL