1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
25 #include <osl/process.h>
26 #include <rtl/strbuf.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/macros.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
47 /*****************************************************************************/
49 /*****************************************************************************/
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
;
60 // delete existing res. of type FilterList
61 for ( size_t i
= 0; i
< pFilterList
->size(); i
++ ) {
62 ExportListEntry
* test
= (*pFilterList
)[ i
];
68 // delete existing res. of type ItemList
69 for ( size_t i
= 0; i
< pItemList
->size(); i
++ ) {
70 ExportListEntry
* test
= (*pItemList
)[ i
];
76 // delete existing res. of type UIEntries
77 for ( size_t i
= 0; i
< pUIEntries
->size(); i
++ ) {
78 ExportListEntry
* test
= (*pUIEntries
)[ i
];
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();
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
;
133 return false; // no valid command line
137 o_aHandledArgs
.m_sInputFile
= OString( argv
[i
] );
142 o_aHandledArgs
.m_sOutputFile
= OString( argv
[i
] );
147 o_aHandledArgs
.m_sPrj
= OString( argv
[i
] );
152 o_aHandledArgs
.m_sPrjRoot
= OString( argv
[i
] );
157 o_aHandledArgs
.m_sMergeSrc
= OString( argv
[i
] );
158 o_aHandledArgs
.m_bMergeMode
= true;
161 case STATE_LANGUAGES
:
163 sLanguages
= OString( argv
[i
] );
169 if( !o_aHandledArgs
.m_sInputFile
.isEmpty() &&
170 !o_aHandledArgs
.m_sOutputFile
.isEmpty() )
176 o_aHandledArgs
= HandledArgs();
181 void Export::writeUsage(const OString
& rName
, const OString
& rFileType
)
184 << "Syntax: " << rName
.getStr()
185 << " [-p Prj] [-r PrjRoot] -i FileIn -o FileOut"
186 << " [-m DataBase] [-l l1,l2,...]\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 /*****************************************************************************/
200 isInitialized
= true;
202 /*****************************************************************************/
203 std::vector
<rtl::OString
> Export::GetLanguages(){
204 /*****************************************************************************/
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
]);
227 if ( rString
[i
] == '>' ) {
232 if ( i
< rString
.getLength()) {
233 switch ( rString
[i
]) {
235 sReturn
.append("<");
239 sReturn
.append(">");
243 sReturn
.append(""");
247 sReturn
.append("'");
251 if ((( i
+ 4 ) < rString
.getLength()) &&
252 ( rString
.copy( i
, 5 ) == "&" ))
253 sReturn
.append(rString
[i
]);
255 sReturn
.append("&");
259 sReturn
.append(rString
[i
]);
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("&", i
)) {
287 i
+= RTL_CONSTASCII_LENGTH("&");
288 } else if (rString
.match("<", i
)) {
290 i
+= RTL_CONSTASCII_LENGTH("<");
291 } else if (rString
.match(">", i
)) {
293 i
+= RTL_CONSTASCII_LENGTH(">");
294 } else if (rString
.match(""", i
)) {
296 i
+= RTL_CONSTASCII_LENGTH(""");
297 } else if (rString
.match("'", i
)) {
298 sReturn
.append('\'');
299 i
+= RTL_CONSTASCII_LENGTH("'");
301 sReturn
.append(rString
[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 /*****************************************************************************/
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 /*****************************************************************************/
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: */