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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* Copyright (C) 2012-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "utils/POUtils.h"
#include "URL.h"
#include "filesystem/File.h"
#include "utils/log.h"
#include <stdlib.h>
CPODocument::CPODocument()
{
m_CursorPos = 0;
m_nextEntryPos = 0;
m_POfilelength = 0;
m_Entry.msgStrPlural.clear();
m_Entry.msgStrPlural.resize(1);
}
CPODocument::~CPODocument() = default;
bool CPODocument::LoadFile(const std::string &pofilename)
{
CURL poFileUrl(pofilename);
if (!XFILE::CFile::Exists(poFileUrl))
return false;
XFILE::CFile file;
std::vector<uint8_t> buf;
if (file.LoadFile(poFileUrl, buf) < 18) // at least a size of a minimalistic header
{
CLog::Log(LOGERROR, "{}: can't load file \"{}\" or file is too small", __FUNCTION__,
pofilename);
return false;
}
m_strBuffer = '\n';
m_strBuffer.append(reinterpret_cast<char*>(buf.data()), buf.size());
buf.clear();
ConvertLineEnds(pofilename);
// we make sure, to have an LF at the end of buffer
if (*m_strBuffer.rbegin() != '\n')
{
m_strBuffer += "\n";
}
m_POfilelength = m_strBuffer.size();
if (GetNextEntry() && m_Entry.Type == MSGID_FOUND)
return true;
CLog::Log(LOGERROR, "POParser: unable to read PO file header from file: {}", pofilename);
return false;
}
bool CPODocument::GetNextEntry()
{
do
{
// if we don't find LFLF, we reached the end of the buffer and the last entry to check
// we indicate this with setting m_nextEntryPos to the end of the buffer
if ((m_nextEntryPos = m_strBuffer.find("\n\n", m_CursorPos)) == std::string::npos)
m_nextEntryPos = m_POfilelength-1;
// now we read the actual entry into a temp string for further processing
m_Entry.Content.assign(m_strBuffer, m_CursorPos, m_nextEntryPos - m_CursorPos +1);
m_CursorPos = m_nextEntryPos+1; // jump cursor to the second LF character
if (FindLineStart ("\nmsgid ", m_Entry.msgID.Pos))
{
if (FindLineStart ("\nmsgctxt \"#", m_Entry.xIDPos) && ParseNumID())
{
m_Entry.Type = ID_FOUND; // we found an entry with a valid numeric id
return true;
}
size_t plurPos;
if (FindLineStart ("\nmsgid_plural ", plurPos))
{
m_Entry.Type = MSGID_PLURAL_FOUND; // we found a pluralized entry
return true;
}
m_Entry.Type = MSGID_FOUND; // we found a normal entry, with no numeric id
return true;
}
}
while (m_nextEntryPos != m_POfilelength-1);
// we reached the end of buffer AND we have not found a valid entry
return false;
}
void CPODocument::ParseEntry(bool bisSourceLang)
{
if (bisSourceLang)
{
if (m_Entry.Type == ID_FOUND)
GetString(m_Entry.msgID);
else
m_Entry.msgID.Str.clear();
return;
}
if (m_Entry.Type != ID_FOUND)
{
GetString(m_Entry.msgID);
if (FindLineStart ("\nmsgctxt ", m_Entry.msgCtxt.Pos))
GetString(m_Entry.msgCtxt);
else
m_Entry.msgCtxt.Str.clear();
}
if (m_Entry.Type != MSGID_PLURAL_FOUND)
{
if (FindLineStart ("\nmsgstr ", m_Entry.msgStr.Pos))
{
GetString(m_Entry.msgStr);
GetString(m_Entry.msgID);
}
else
{
CLog::Log(LOGERROR, "POParser: missing msgstr line in entry. Failed entry: {}",
m_Entry.Content);
m_Entry.msgStr.Str.clear();
}
return;
}
// We found a plural form entry. We read it into a vector of CStrEntry types
m_Entry.msgStrPlural.clear();
std::string strPattern = "\nmsgstr[0] ";
CStrEntry strEntry;
for (int n=0; n<7 ; n++)
{
strPattern[8] = static_cast<char>(n+'0');
if (FindLineStart (strPattern, strEntry.Pos))
{
GetString(strEntry);
if (strEntry.Str.empty())
break;
m_Entry.msgStrPlural.push_back(strEntry);
}
else
break;
}
if (m_Entry.msgStrPlural.empty())
{
CLog::Log(LOGERROR,
"POParser: msgstr[] plural lines have zero valid strings. "
"Failed entry: {}",
m_Entry.Content);
m_Entry.msgStrPlural.resize(1); // Put 1 element with an empty string into the vector
}
}
const std::string& CPODocument::GetPlurMsgstr(size_t plural) const
{
if (m_Entry.msgStrPlural.size() < plural+1)
{
CLog::Log(LOGERROR,
"POParser: msgstr[{}] plural field requested, but not found in PO file. "
"Failed entry: {}",
static_cast<int>(plural), m_Entry.Content);
plural = m_Entry.msgStrPlural.size()-1;
}
return m_Entry.msgStrPlural[plural].Str;
}
std::string CPODocument::UnescapeString(const std::string &strInput)
{
std::string strOutput;
if (strInput.empty())
return strOutput;
char oescchar;
strOutput.reserve(strInput.size());
std::string::const_iterator it = strInput.begin();
while (it < strInput.end())
{
oescchar = *it++;
if (oescchar == '\\')
{
if (it == strInput.end())
{
CLog::Log(LOGERROR,
"POParser: warning, unhandled escape character "
"at line-end. Problematic entry: {}",
m_Entry.Content);
break;
}
switch (*it++)
{
case 'a': oescchar = '\a'; break;
case 'b': oescchar = '\b'; break;
case 'v': oescchar = '\v'; break;
case 'n': oescchar = '\n'; break;
case 't': oescchar = '\t'; break;
case 'r': oescchar = '\r'; break;
case '"': oescchar = '"' ; break;
case '0': oescchar = '\0'; break;
case 'f': oescchar = '\f'; break;
case '?': oescchar = '\?'; break;
case '\'': oescchar = '\''; break;
case '\\': oescchar = '\\'; break;
default:
{
CLog::Log(LOGERROR,
"POParser: warning, unhandled escape character. Problematic entry: {}",
m_Entry.Content);
continue;
}
}
}
strOutput.push_back(oescchar);
}
return strOutput;
}
bool CPODocument::FindLineStart(const std::string &strToFind, size_t &FoundPos)
{
FoundPos = m_Entry.Content.find(strToFind);
if (FoundPos == std::string::npos || FoundPos + strToFind.size() + 2 > m_Entry.Content.size())
return false; // if we don't find the string or if we don't have at least one char after it
FoundPos += strToFind.size(); // to set the pos marker to the exact start of the real data
return true;
}
bool CPODocument::ParseNumID()
{
if (isdigit(m_Entry.Content.at(m_Entry.xIDPos))) // verify if the first char is digit
{
// we check for the numeric id for the fist 10 chars (uint32)
m_Entry.xID = strtol(&m_Entry.Content[m_Entry.xIDPos], NULL, 10);
return true;
}
CLog::Log(LOGERROR, "POParser: found numeric id descriptor, but no valid id can be read, "
"entry was handled as normal msgid entry");
CLog::Log(LOGERROR, "POParser: The problematic entry: {}", m_Entry.Content);
return false;
}
void CPODocument::GetString(CStrEntry &strEntry)
{
size_t nextLFPos;
size_t startPos = strEntry.Pos;
strEntry.Str.clear();
while (startPos < m_Entry.Content.size())
{
nextLFPos = m_Entry.Content.find('\n', startPos);
if (nextLFPos == std::string::npos)
nextLFPos = m_Entry.Content.size();
// check syntax, if it really is a valid quoted string line
if (nextLFPos-startPos < 2 || m_Entry.Content[startPos] != '\"' ||
m_Entry.Content[nextLFPos-1] != '\"')
break;
strEntry.Str.append(m_Entry.Content, startPos+1, nextLFPos-2-startPos);
startPos = nextLFPos+1;
}
strEntry.Str = UnescapeString(strEntry.Str);
}
void CPODocument::ConvertLineEnds(const std::string &filename)
{
size_t foundPos = m_strBuffer.find_first_of('\r');
if (foundPos == std::string::npos)
return; // We have only Linux style line endings in the file, nothing to do
if (foundPos+1 >= m_strBuffer.size() || m_strBuffer[foundPos+1] != '\n')
CLog::Log(LOGDEBUG,
"POParser: PO file has Mac Style Line Endings. "
"Converted in memory to Linux LF for file: {}",
filename);
else
CLog::Log(LOGDEBUG,
"POParser: PO file has Win Style Line Endings. "
"Converted in memory to Linux LF for file: {}",
filename);
std::string strTemp;
strTemp.reserve(m_strBuffer.size());
for (std::string::const_iterator it = m_strBuffer.begin(); it < m_strBuffer.end(); ++it)
{
if (*it == '\r')
{
if (it+1 == m_strBuffer.end() || *(it+1) != '\n')
strTemp.push_back('\n'); // convert Mac style line ending and continue
continue; // we have Win style line ending so we exclude this CR now
}
strTemp.push_back(*it);
}
m_strBuffer.swap(strTemp);
m_POfilelength = m_strBuffer.size();
}
|