Bump for 4.0-15
[LibreOffice.git] / l10ntools / source / export2.cxx
blob5ef2231f6d6ee8bff264bcc0a0db9ef35d94e13b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include "export.hxx"
23 #include <stdio.h>
24 #include <osl/time.h>
25 #include <osl/process.h>
26 #include <rtl/strbuf.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/macros.h>
29 #include <iostream>
30 #include <iomanip>
31 #include <time.h>
32 #include <stdlib.h>
34 //flags for handleArguments()
35 #define STATE_NON 0x0001
36 #define STATE_INPUT 0x0002
37 #define STATE_OUTPUT 0x0003
38 #define STATE_PRJ 0x0004
39 #define STATE_ROOT 0x0005
40 #define STATE_MERGESRC 0x0006
41 #define STATE_LANGUAGES 0x0007
44 // class ResData();
47 /*****************************************************************************/
48 ResData::~ResData()
49 /*****************************************************************************/
51 if ( pStringList ) {
52 // delete existing res. of type StringList
53 for ( size_t i = 0; i < pStringList->size(); i++ ) {
54 ExportListEntry* test = (*pStringList)[ i ];
55 if( test != NULL ) delete test;
57 delete pStringList;
59 if ( pFilterList ) {
60 // delete existing res. of type FilterList
61 for ( size_t i = 0; i < pFilterList->size(); i++ ) {
62 ExportListEntry* test = (*pFilterList)[ i ];
63 delete test;
65 delete pFilterList;
67 if ( pItemList ) {
68 // delete existing res. of type ItemList
69 for ( size_t i = 0; i < pItemList->size(); i++ ) {
70 ExportListEntry* test = (*pItemList)[ i ];
71 delete test;
73 delete pItemList;
75 if ( pUIEntries ) {
76 // delete existing res. of type UIEntries
77 for ( size_t i = 0; i < pUIEntries->size(); i++ ) {
78 ExportListEntry* test = (*pUIEntries)[ i ];
79 delete test;
81 delete pUIEntries;
86 // class Export
89 /*****************************************************************************/
90 rtl::OString Export::sLanguages;
91 rtl::OString Export::sForcedLanguages;
92 /*****************************************************************************/
94 bool Export::handleArguments(
95 int argc, char * argv[], HandledArgs& o_aHandledArgs)
97 o_aHandledArgs = HandledArgs();
98 sLanguages = "";
99 sal_uInt16 nState = STATE_NON;
101 for( int i = 1; i < argc; i++ )
103 if ( OString( argv[ i ] ).toAsciiUpperCase() == "-I" )
105 nState = STATE_INPUT; // next token specifies source file
107 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-O" )
109 nState = STATE_OUTPUT; // next token specifies the dest file
111 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-P" )
113 nState = STATE_PRJ; // next token specifies the cur. project
115 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-R" )
117 nState = STATE_ROOT; // next token specifies path to project root
119 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-M" )
121 nState = STATE_MERGESRC; // next token specifies the merge database
123 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-L" )
125 nState = STATE_LANGUAGES;
127 else
129 switch ( nState )
131 case STATE_NON:
133 return false; // no valid command line
135 case STATE_INPUT:
137 o_aHandledArgs.m_sInputFile = OString( argv[i] );
139 break;
140 case STATE_OUTPUT:
142 o_aHandledArgs.m_sOutputFile = OString( argv[i] );
144 break;
145 case STATE_PRJ:
147 o_aHandledArgs.m_sPrj = OString( argv[i] );
149 break;
150 case STATE_ROOT:
152 o_aHandledArgs.m_sPrjRoot = OString( argv[i] );
154 break;
155 case STATE_MERGESRC:
157 o_aHandledArgs.m_sMergeSrc = OString( argv[i] );
158 o_aHandledArgs.m_bMergeMode = true;
160 break;
161 case STATE_LANGUAGES:
163 sLanguages = OString( argv[i] );
165 break;
169 if( !o_aHandledArgs.m_sInputFile.isEmpty() &&
170 !o_aHandledArgs.m_sOutputFile.isEmpty() )
172 return true;
174 else
176 o_aHandledArgs = HandledArgs();
177 return false;
181 void Export::writeUsage(const OString& rName, const OString& rFileType)
183 std::cout
184 << "Syntax: " << rName.getStr()
185 << " [-p Prj] [-r PrjRoot] -i FileIn -o FileOut"
186 << " [-m DataBase] [-l l1,l2,...]\n"
187 << " Prj: Project\n"
188 << " PrjRoot: Path to project root (../.. etc.)\n"
189 << " FileIn: Source files (*." << rFileType.getStr() << ")\n"
190 << " FileOut: Destination file (*.*)\n"
191 << " DataBase: Mergedata (*.po)\n"
192 << " -l: Restrict the handled languages; l1, l2, ... are elements of"
193 << " (de, en-US, ...)\n";
196 /*****************************************************************************/
197 void Export::SetLanguages( std::vector<rtl::OString> val ){
198 /*****************************************************************************/
199 aLanguages = val;
200 isInitialized = true;
202 /*****************************************************************************/
203 std::vector<rtl::OString> Export::GetLanguages(){
204 /*****************************************************************************/
205 return aLanguages;
207 /*****************************************************************************/
208 std::vector<rtl::OString> Export::GetForcedLanguages(){
209 /*****************************************************************************/
210 return aForcedLanguages;
212 std::vector<rtl::OString> Export::aLanguages = std::vector<rtl::OString>();
213 std::vector<rtl::OString> Export::aForcedLanguages = std::vector<rtl::OString>();
215 /*****************************************************************************/
216 rtl::OString Export::QuoteHTML( rtl::OString const &rString )
217 /*****************************************************************************/
219 rtl::OStringBuffer sReturn;
220 for ( sal_Int32 i = 0; i < rString.getLength(); i++ ) {
221 rtl::OString sTemp = rString.copy( i );
222 if ( sTemp.match( "<Arg n=" ) ) {
223 while ( i < rString.getLength() && rString[i] != '>' ) {
224 sReturn.append(rString[i]);
225 i++;
227 if ( rString[i] == '>' ) {
228 sReturn.append('>');
229 i++;
232 if ( i < rString.getLength()) {
233 switch ( rString[i]) {
234 case '<':
235 sReturn.append("&lt;");
236 break;
238 case '>':
239 sReturn.append("&gt;");
240 break;
242 case '\"':
243 sReturn.append("&quot;");
244 break;
246 case '\'':
247 sReturn.append("&apos;");
248 break;
250 case '&':
251 if ((( i + 4 ) < rString.getLength()) &&
252 ( rString.copy( i, 5 ) == "&amp;" ))
253 sReturn.append(rString[i]);
254 else
255 sReturn.append("&amp;");
256 break;
258 default:
259 sReturn.append(rString[i]);
260 break;
264 return sReturn.makeStringAndClear();
267 void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString )
269 if( hasUTF8ByteOrderMarker( rString ) )
270 rString = rString.copy(3);
273 bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString )
275 return rString.getLength() >= 3 && rString[0] == '\xEF' &&
276 rString[1] == '\xBB' && rString[2] == '\xBF' ;
279 /*****************************************************************************/
280 rtl::OString Export::UnquoteHTML( rtl::OString const &rString )
281 /*****************************************************************************/
283 rtl::OStringBuffer sReturn;
284 for (sal_Int32 i = 0; i != rString.getLength();) {
285 if (rString.match("&amp;", i)) {
286 sReturn.append('&');
287 i += RTL_CONSTASCII_LENGTH("&amp;");
288 } else if (rString.match("&lt;", i)) {
289 sReturn.append('<');
290 i += RTL_CONSTASCII_LENGTH("&lt;");
291 } else if (rString.match("&gt;", i)) {
292 sReturn.append('>');
293 i += RTL_CONSTASCII_LENGTH("&gt;");
294 } else if (rString.match("&quot;", i)) {
295 sReturn.append('"');
296 i += RTL_CONSTASCII_LENGTH("&quot;");
297 } else if (rString.match("&apos;", i)) {
298 sReturn.append('\'');
299 i += RTL_CONSTASCII_LENGTH("&apos;");
300 } else {
301 sReturn.append(rString[i]);
302 ++i;
305 return sReturn.makeStringAndClear();
308 bool Export::isSourceLanguage(const rtl::OString &rLanguage)
310 return !isAllowed(rLanguage);
313 bool Export::isAllowed(const rtl::OString &rLanguage)
315 return !rLanguage.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US"));
318 bool Export::isInitialized = false;
320 /*****************************************************************************/
321 void Export::InitLanguages( bool bMergeMode ){
322 /*****************************************************************************/
323 if( !isInitialized )
325 rtl::OString sTmp;
326 OStringBoolHashMap aEnvLangs;
328 sal_Int32 nIndex = 0;
331 rtl::OString aToken = sLanguages.getToken(0, ',', nIndex);
332 sTmp = aToken.getToken(0, '=').trim();
333 if( bMergeMode && !isAllowed( sTmp ) ){}
334 else if( !( (sTmp[0]=='x' || sTmp[0]=='X') && sTmp[1]=='-' ) ){
335 aLanguages.push_back( sTmp );
338 while ( nIndex >= 0 );
340 InitForcedLanguages( bMergeMode );
341 isInitialized = true;
344 /*****************************************************************************/
345 void Export::InitForcedLanguages( bool bMergeMode ){
346 /*****************************************************************************/
347 rtl::OString sTmp;
348 OStringBoolHashMap aEnvLangs;
350 sal_Int32 nIndex = 0;
353 rtl::OString aToken = sForcedLanguages.getToken(0, ',', nIndex);
355 sTmp = aToken.getToken(0, '=').trim();
356 if( bMergeMode && isAllowed( sTmp ) ){}
357 else if( !( (sTmp[0]=='x' || sTmp[0]=='X') && sTmp[1]=='-' ) )
358 aForcedLanguages.push_back( sTmp );
360 while ( nIndex >= 0 );
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */