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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <string>
#include <vector>
#include "gL10nMem.hxx"
#include "gConv.hxx"
#include "gConvPo.hxx"
#include "gConvProp.hxx"
#include "gConvSrc.hxx"
#include "gConvTree.hxx"
#include "gConvUlf.hxx"
#include "gConvXcs.hxx"
#include "gConvXcu.hxx"
#include "gConvXhp.hxx"
#include "gConvXrm.hxx"
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#define OS_ACCESS(x,y) _access(x,y)
#define OS_MKDIR(x) _mkdir(x)
#else
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define OS_ACCESS(x,y) access(x,y)
#define OS_MKDIR(x) mkdir(x,0777)
#endif
convert_gen * convert_gen::mcImpl = NULL;
convert_gen::convert_gen(l10nMem& cMemory)
: mcMemory(cMemory)
{
mcImpl = this;
}
convert_gen::~convert_gen()
{
}
convert_gen& convert_gen::createInstance(l10nMem& cMemory,
const std::string& sSourceDir,
const std::string& sTargetDir,
const std::string& sSourceFile)
{
// did the user give a .xxx with the source file ?
int nInx = sSourceFile.rfind(".");
if (nInx == (int)std::string::npos)
throw l10nMem::showError("source file: "+sSourceFile+" missing extension");
// find correct conversion class and create correct object
std::string sExtension = sSourceFile.substr(nInx+1);
convert_gen *x;
if (sExtension == "hrc") x = new convert_src(cMemory);
else if (sExtension == "src") x = new convert_src(cMemory);
else if (sExtension == "po") x = new convert_po(cMemory);
else if (sExtension == "pot") x = new convert_po(cMemory);
else if (sExtension == "tree") x = new convert_tree(cMemory);
else if (sExtension == "ulf") x = new convert_ulf(cMemory);
else if (sExtension == "xcu") x = new convert_xcu(cMemory);
else if (sExtension == "xhp") x = new convert_xhp(cMemory);
else if (sExtension == "xrm") x = new convert_xrm(cMemory);
else if (sExtension == "properties") x = new convert_prop(cMemory);
else throw l10nMem::showError("unknown extension on source file: "+sSourceFile);
// and set environment
x->msSourceFile = sSourceFile;
x->msTargetPath = sTargetDir;
x->msSourcePath = sSourceDir + sSourceFile;
return *x;
}
bool convert_gen::execute(const bool bMerge, const bool bKid)
{
mbMergeMode = bMerge;
if (bKid)
throw l10nMem::showError("not implemented");
// and load file
if (!prepareFile())
return false;
// and execute conversion
execute();
return true;
}
bool convert_gen::checkAccess(std::string& sFile)
{
return (OS_ACCESS(sFile.c_str(), 0) == 0);
}
bool convert_gen::createDir(std::string& sDir, std::string& sFile)
{
std::string sNewDir(sDir);
int newPos, oldPos;
for (oldPos = 0;; oldPos = newPos +1) {
newPos = sFile.find_first_of("/\\", oldPos);
if (newPos == (int)std::string::npos)
break;
sNewDir += sFile.substr(oldPos, newPos-oldPos) + "/";
if (!checkAccess(sNewDir)) {
OS_MKDIR(sNewDir.c_str());
}
}
return true;
}
bool convert_gen::prepareFile()
{
std::ifstream inputFile(msSourcePath.c_str(), std::ios::binary);
if (!inputFile.is_open()) {
if (mbLoadMode) {
l10nMem::showWarning("Cannot open file (" + msSourcePath + ")");
return false;
}
else
throw l10nMem::showError("Cannot open file (" + msSourcePath + ") for reading");
}
// get length of file:
miSourceReadIndex = 0;
inputFile.seekg (0, std::ios::end);
msSourceBuffer.resize((unsigned int)inputFile.tellg());
inputFile.seekg (0, std::ios::beg);
// get size, prepare std::string and read whole file
inputFile.read(const_cast<char *>(msSourceBuffer.c_str()), msSourceBuffer.size());
if ((unsigned int)inputFile.gcount() != msSourceBuffer.size())
throw l10nMem::showError("cannot read whole file");
inputFile.close();
if (mbMergeMode && !mbLoadMode) {
// close previous file
if (mcOutputFile.is_open())
mcOutputFile.close();
// open output file
mcOutputFile.open((msTargetPath+msSourceFile).c_str(), std::ios::binary);
if (mcOutputFile.is_open())
return true;
if (convert_gen::createDir(msTargetPath, msSourceFile)) {
mcOutputFile.open((msTargetPath+msSourceFile).c_str(), std::ios::binary);
if (mcOutputFile.is_open())
return true;
}
throw l10nMem::showError("Cannot open file (" + msTargetPath+msSourceFile + ") for writing");
}
return true;
}
void convert_gen::lexRead(char *sBuf, int *nResult, int nMax_size)
{
// did we hit eof
if (miSourceReadIndex == -1) {
*nResult = 0;
return;
}
// assume we can copy all that are left.
*nResult = msSourceBuffer.size() - miSourceReadIndex;
// space enough for the whole line ?
if (*nResult <= nMax_size) {
msSourceBuffer.copy(sBuf, *nResult, miSourceReadIndex);
l10nMem::showDebug(sBuf);
miSourceReadIndex = -1;
}
else {
msSourceBuffer.copy(sBuf, nMax_size, miSourceReadIndex);
l10nMem::showDebug(sBuf);
*nResult = nMax_size;
miSourceReadIndex += nMax_size;
}
}
void convert_gen::writeSourceFile(const std::string& line)
{
if (!line.size())
return;
if (mcOutputFile.is_open())
mcOutputFile.write(line.c_str(), line.size());
}
std::string& convert_gen::copySource(char const *yyText, bool bDoClear)
{
int nL;
if (!yyText) {
msCopyText.clear();
return msCopyText;
}
msCopyText = yyText;
// write text for merge
if (mbMergeMode)
writeSourceFile(msCopyText);
if (bDoClear)
msCollector.clear();
else
msCollector += msCopyText;
// remove any CR
for (nL = 0; nL < (int)msCopyText.size(); ++nL) {
if (msCopyText[nL] == '\r') {
msCopyText.erase(nL, 1);
--nL;
continue;
}
if (msCopyText[nL] == '\n')
++miLineNo;
}
return msCopyText;
}
|