summaryrefslogtreecommitdiff
path: root/l10ntools/source/gConv.cxx
blob: 164b245addcf272bc51b7dfd08ea1f9d97f66a5e (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- 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 "gConv.hxx"
#include "gConvDB.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



/*****************************************************************************
 ****************************   G C O N . C X X   ****************************
 *****************************************************************************
 * This is the generic conversion module, it handles all generic work of the
 * conversion, and offer utility functions to the specific conversion classes
 *****************************************************************************/



/*******************   G L O B A L   D E F I N I T I O N   *******************/
convert_gen_impl * convert_gen_impl::mcImpl = nullptr;



/**********************   I M P L E M E N T A T I O N   **********************/
convert_gen::convert_gen(l10nMem&           cMemory,
                         const std::string& sSourceDir,
                         const std::string& sTargetDir,
                         const std::string& sSourceFile)
{
  // do we have an old object
  if (convert_gen_impl::mcImpl)
    delete convert_gen_impl::mcImpl;

  // 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);
  if      (sExtension == "hrc")        convert_gen_impl::mcImpl = new convert_src(cMemory);
  else if (sExtension == "src")        convert_gen_impl::mcImpl = new convert_src(cMemory);
  else if (sExtension == "po")         convert_gen_impl::mcImpl = new convert_po(cMemory);
  else if (sExtension == "pot")        convert_gen_impl::mcImpl = new convert_po(cMemory);
  else if (sExtension == "tree")       convert_gen_impl::mcImpl = new convert_tree(cMemory);
  else if (sExtension == "ulf")        convert_gen_impl::mcImpl = new convert_ulf(cMemory);
  else if (sExtension == "xcu")        convert_gen_impl::mcImpl = new convert_xcu(cMemory);
  else if (sExtension == "xhp")        convert_gen_impl::mcImpl = new convert_xhp(cMemory);
  else if (sExtension == "xrm")        convert_gen_impl::mcImpl = new convert_xrm(cMemory);
  else if (sExtension == "properties") convert_gen_impl::mcImpl = new convert_prop(cMemory);
  else throw l10nMem::showError("unknown extension on source file: "+sSourceFile);

  // and set environment
  convert_gen_impl::mcImpl->msSourceFile = sSourceFile;
  convert_gen_impl::mcImpl->msTargetPath = sTargetDir;
  convert_gen_impl::mcImpl->msSourcePath = sSourceDir + sSourceFile;
}



/**********************   I M P L E M E N T A T I O N   **********************/
convert_gen::~convert_gen()
{
  delete convert_gen_impl::mcImpl;
}



/**********************   I M P L E M E N T A T I O N   **********************/
bool convert_gen::execute(const bool bMerge, const bool bKid)
{
  convert_gen_impl::mcImpl->mbMergeMode  = bMerge;

  if (bKid)
      throw l10nMem::showError("not implemented");

  // and load file
  if (!convert_gen_impl::mcImpl->prepareFile())
    return false;

  // and execute conversion
  convert_gen_impl::mcImpl->execute();

  return true;
}



/**********************   I M P L E M E N T A T I O N   **********************/
void convert_gen::startSave(const std::string& sLanguage,
                            const std::string& sFile)
{
  convert_gen_impl::mcImpl->startSave(sLanguage, sFile);
}
void convert_gen_impl::startSave(const std::string& sLanguage,
                                 const std::string& sFile)
{
  std::string x;

  x = sLanguage;
  x = sFile;
  throw l10nMem::showError("startSave called with non .po file");
}



/**********************   I M P L E M E N T A T I O N   **********************/
void convert_gen::save(const std::string& sFileName,
                       const std::string& sKey,
                       const std::string& sENUStext,
                       const std::string& sText,
                       bool               bFuzzy)
{
  convert_gen_impl::mcImpl->save(sFileName, sKey, sENUStext, sText, bFuzzy);
}
void convert_gen_impl::save(const std::string& sFileName,
                            const std::string& sKey,
                            const std::string& sENUStext,
                            const std::string& sText,
                            bool               bFuzzy)
{
  std::string x;

  if (bFuzzy)
      x = sFileName + sKey + sENUStext + sText;
  throw l10nMem::showError("save called with non .po file");
}



/**********************   I M P L E M E N T A T I O N   **********************/
void convert_gen::endSave()
{
  convert_gen_impl::mcImpl->endSave();
}
void convert_gen_impl::endSave()
{
  throw l10nMem::showError("endSave called with non .po file");
}



/**********************   I M P L E M E N T A T I O N   **********************/
bool convert_gen::checkAccess(std::string& sFile)
{
  return (OS_ACCESS(sFile.c_str(), 0) == 0);
}



/**********************   I M P L E M E N T A T I O N   **********************/
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;
}



/**********************   I M P L E M E N T A T I O N   **********************/
convert_gen_impl::convert_gen_impl(l10nMem& crMemory)
                                  : mbMergeMode(false),
                                    mbLoadMode(false),
                                    mcMemory(crMemory),
                                    miLineNo(1)
{
}



/**********************   I M P L E M E N T A T I O N   **********************/
convert_gen_impl::~convert_gen_impl()
{
  mcImpl = nullptr;
}



/**********************   I M P L E M E N T A T I O N   **********************/
bool convert_gen_impl::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;
}




/**********************   I M P L E M E N T A T I O N   **********************/
void convert_gen_impl::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);
    *nResult           = nMax_size;
    miSourceReadIndex += nMax_size;
  }
}



/**********************   I M P L E M E N T A T I O N   **********************/
void convert_gen_impl::writeSourceFile(const std::string& line)
{
  if (!line.size())
    return;

  if (mcOutputFile.is_open())
    mcOutputFile.write(line.c_str(), line.size());
}



/**********************   I M P L E M E N T A T I O N   **********************/
std::string& convert_gen_impl::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;
}