xref: /aoo41x/main/l10ntools/source/export2.cxx (revision ab4b560e)
13cd96b95SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
33cd96b95SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
43cd96b95SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
53cd96b95SAndrew Rist  * distributed with this work for additional information
63cd96b95SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
73cd96b95SAndrew Rist  * to you under the Apache License, Version 2.0 (the
83cd96b95SAndrew Rist  * "License"); you may not use this file except in compliance
93cd96b95SAndrew Rist  * with the License.  You may obtain a copy of the License at
103cd96b95SAndrew Rist  *
113cd96b95SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
123cd96b95SAndrew Rist  *
133cd96b95SAndrew Rist  * Unless required by applicable law or agreed to in writing,
143cd96b95SAndrew Rist  * software distributed under the License is distributed on an
153cd96b95SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163cd96b95SAndrew Rist  * KIND, either express or implied.  See the License for the
173cd96b95SAndrew Rist  * specific language governing permissions and limitations
183cd96b95SAndrew Rist  * under the License.
193cd96b95SAndrew Rist  *
203cd96b95SAndrew Rist  *************************************************************/
213cd96b95SAndrew Rist 
223cd96b95SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_l10ntools.hxx"
26cdf0e10cSrcweir #include "export.hxx"
27cdf0e10cSrcweir #include "utf8conv.hxx"
28cdf0e10cSrcweir #include <tools/datetime.hxx>
29cdf0e10cSrcweir #include <tools/isofallback.hxx>
30cdf0e10cSrcweir #include <stdio.h>
31cdf0e10cSrcweir #include <osl/time.h>
32cdf0e10cSrcweir #include <osl/process.h>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #include <iostream>
35cdf0e10cSrcweir #include <iomanip>
36cdf0e10cSrcweir #include <tools/urlobj.hxx>
37cdf0e10cSrcweir #include <time.h>
38cdf0e10cSrcweir #include <stdlib.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace std;
41cdf0e10cSrcweir //
42cdf0e10cSrcweir // class ResData();
43cdf0e10cSrcweir //
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /*****************************************************************************/
46cdf0e10cSrcweir ResData::~ResData()
47cdf0e10cSrcweir /*****************************************************************************/
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	if ( pStringList ) {
50cdf0e10cSrcweir 		// delete existing res. of type StringList
51cdf0e10cSrcweir 		for ( sal_uLong i = 0; i < pStringList->Count(); i++ ) {
52cdf0e10cSrcweir             ExportListEntry* test = pStringList->GetObject( i );
53cdf0e10cSrcweir             if( test != NULL ) delete test;
54cdf0e10cSrcweir 		}
55cdf0e10cSrcweir 		delete pStringList;
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 	if ( pFilterList ) {
58cdf0e10cSrcweir 		// delete existing res. of type FilterList
59cdf0e10cSrcweir 		for ( sal_uLong i = 0; i < pFilterList->Count(); i++ ) {
60cdf0e10cSrcweir 			ExportListEntry* test = pFilterList->GetObject( i );
61cdf0e10cSrcweir             delete test;
62cdf0e10cSrcweir 		}
63cdf0e10cSrcweir 		delete pFilterList;
64cdf0e10cSrcweir 	}
65cdf0e10cSrcweir 	if ( pItemList ) {
66cdf0e10cSrcweir 		// delete existing res. of type ItemList
67cdf0e10cSrcweir 		for ( sal_uLong i = 0; i < pItemList->Count(); i++ ) {
68cdf0e10cSrcweir 			ExportListEntry* test = pItemList->GetObject( i );
69cdf0e10cSrcweir             delete test;
70cdf0e10cSrcweir 		}
71cdf0e10cSrcweir 		delete pItemList;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 	if ( pUIEntries ) {
74cdf0e10cSrcweir 		// delete existing res. of type UIEntries
75cdf0e10cSrcweir 		for ( sal_uLong i = 0; i < pUIEntries->Count(); i++ ) {
76cdf0e10cSrcweir 			ExportListEntry* test = pUIEntries->GetObject( i );
77cdf0e10cSrcweir             delete test;
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 		delete pUIEntries;
80cdf0e10cSrcweir 	}
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //
84cdf0e10cSrcweir // class Export
85cdf0e10cSrcweir //
86cdf0e10cSrcweir 
87cdf0e10cSrcweir /*****************************************************************************/
88cdf0e10cSrcweir ByteString Export::sLanguages;
89cdf0e10cSrcweir ByteString Export::sForcedLanguages;
90cdf0e10cSrcweir //ByteString Export::sIsoCode99;
91cdf0e10cSrcweir /*****************************************************************************/
92cdf0e10cSrcweir 
93cdf0e10cSrcweir void Export::DumpExportList( ByteString& sListName , ExportList& aList ){
94cdf0e10cSrcweir 	printf( "%s\n", sListName.GetBuffer() );
95cdf0e10cSrcweir 	ByteString l("");
96cdf0e10cSrcweir 	ExportListEntry* aEntry;
97cdf0e10cSrcweir 	for( unsigned int x = 0; x < aList.Count() ; x++ ){
98cdf0e10cSrcweir 		aEntry = (ExportListEntry*) aList.GetObject( x );
99cdf0e10cSrcweir 		Export::DumpMap( l , *aEntry );
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir 	printf("\n");
102cdf0e10cSrcweir }
103cdf0e10cSrcweir ByteString Export::DumpMap( ByteString& sMapName , ByteStringHashMap& aMap ){
104cdf0e10cSrcweir 	ByteStringHashMap::const_iterator idbg;
105cdf0e10cSrcweir     ByteString sReturn;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	if( sMapName.Len() )
108cdf0e10cSrcweir 		printf("MapName %s\n", sMapName.GetBuffer());
109cdf0e10cSrcweir 	if( aMap.size() < 1 ) return ByteString();
110cdf0e10cSrcweir 	for( idbg = aMap.begin() ; idbg != aMap.end(); ++idbg ){
111cdf0e10cSrcweir 		ByteString a( idbg->first );
112cdf0e10cSrcweir 		ByteString b( idbg->second );
113cdf0e10cSrcweir 		printf("[%s]= %s",a.GetBuffer(),b.GetBuffer());
114cdf0e10cSrcweir 		printf("\n");
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 	printf("\n");
117cdf0e10cSrcweir 	return sReturn;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir /*****************************************************************************/
120cdf0e10cSrcweir void Export::SetLanguages( std::vector<ByteString> val ){
121cdf0e10cSrcweir /*****************************************************************************/
122cdf0e10cSrcweir     aLanguages = val;
123cdf0e10cSrcweir     isInitialized = true;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir /*****************************************************************************/
126cdf0e10cSrcweir std::vector<ByteString> Export::GetLanguages(){
127cdf0e10cSrcweir /*****************************************************************************/
128cdf0e10cSrcweir     return aLanguages;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir /*****************************************************************************/
131cdf0e10cSrcweir std::vector<ByteString> Export::GetForcedLanguages(){
132cdf0e10cSrcweir /*****************************************************************************/
133cdf0e10cSrcweir     return aForcedLanguages;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir std::vector<ByteString> Export::aLanguages       = std::vector<ByteString>();
136cdf0e10cSrcweir std::vector<ByteString> Export::aForcedLanguages = std::vector<ByteString>();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir /*****************************************************************************/
140cdf0e10cSrcweir void Export::QuotHTMLXRM( ByteString &rString )
141cdf0e10cSrcweir /*****************************************************************************/
142cdf0e10cSrcweir {
143cdf0e10cSrcweir 	ByteString sReturn;
144cdf0e10cSrcweir 	//sal_Bool bBreak = sal_False;
145cdf0e10cSrcweir 	for ( sal_uInt16 i = 0; i < rString.Len(); i++ ) {
146cdf0e10cSrcweir 		ByteString sTemp = rString.Copy( i );
147cdf0e10cSrcweir 		if ( sTemp.Search( "<Arg n=" ) == 0 ) {
148cdf0e10cSrcweir 			while ( i < rString.Len() && rString.GetChar( i ) != '>' ) {
149cdf0e10cSrcweir 		 		sReturn += rString.GetChar( i );
150cdf0e10cSrcweir 				i++;
151cdf0e10cSrcweir 			}
152cdf0e10cSrcweir 			if ( rString.GetChar( i ) == '>' ) {
153cdf0e10cSrcweir 				sReturn += ">";
154cdf0e10cSrcweir 				i++;
155cdf0e10cSrcweir 			}
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		if ( i < rString.Len()) {
159cdf0e10cSrcweir 			switch ( rString.GetChar( i )) {
160cdf0e10cSrcweir 				case '<':
161cdf0e10cSrcweir 					if( i+2 < rString.Len() &&
162cdf0e10cSrcweir                         (rString.GetChar( i+1 ) == 'b' || rString.GetChar( i+1 ) == 'B') &&
163cdf0e10cSrcweir                         rString.GetChar( i+2 ) == '>' )
164cdf0e10cSrcweir                     {
165cdf0e10cSrcweir                            sReturn +="<b>";
166cdf0e10cSrcweir                            i += 2;
167cdf0e10cSrcweir                     }
168cdf0e10cSrcweir                     else if( i+3 < rString.Len() &&
169cdf0e10cSrcweir                              rString.GetChar( i+1 ) == '/' &&
170cdf0e10cSrcweir                              (rString.GetChar( i+2 ) == 'b' || rString.GetChar( i+2 ) == 'B') &&
171cdf0e10cSrcweir                              rString.GetChar( i+3 ) == '>' )
172cdf0e10cSrcweir                     {
173cdf0e10cSrcweir                            sReturn +="</b>";
174cdf0e10cSrcweir                            i += 3;
175cdf0e10cSrcweir                     }
176cdf0e10cSrcweir                     else
177cdf0e10cSrcweir                         sReturn += "&lt;";
178cdf0e10cSrcweir 				break;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 				case '>':
181cdf0e10cSrcweir 					sReturn += "&gt;";
182cdf0e10cSrcweir 				break;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 				case '\"':
185cdf0e10cSrcweir 					sReturn += "&quot;";
186cdf0e10cSrcweir 				break;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 				case '\'':
189cdf0e10cSrcweir 					sReturn += "&apos;";
190cdf0e10cSrcweir 				break;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 				case '&':
193cdf0e10cSrcweir 					if ((( i + 4 ) < rString.Len()) &&
194cdf0e10cSrcweir 						( rString.Copy( i, 5 ) == "&amp;" ))
195cdf0e10cSrcweir 							sReturn += rString.GetChar( i );
196cdf0e10cSrcweir 					else
197cdf0e10cSrcweir 						sReturn += "&amp;";
198cdf0e10cSrcweir 				break;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 				default:
201cdf0e10cSrcweir 					sReturn += rString.GetChar( i );
202cdf0e10cSrcweir 				break;
203cdf0e10cSrcweir 			}
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 	rString = sReturn;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir /*****************************************************************************/
209cdf0e10cSrcweir void Export::QuotHTML( ByteString &rString )
210cdf0e10cSrcweir /*****************************************************************************/
211cdf0e10cSrcweir {
212cdf0e10cSrcweir 	ByteString sReturn;
213cdf0e10cSrcweir 	for ( sal_uInt16 i = 0; i < rString.Len(); i++ ) {
214cdf0e10cSrcweir 		ByteString sTemp = rString.Copy( i );
215cdf0e10cSrcweir 		if ( sTemp.Search( "<Arg n=" ) == 0 ) {
216cdf0e10cSrcweir 			while ( i < rString.Len() && rString.GetChar( i ) != '>' ) {
217cdf0e10cSrcweir 		 		sReturn += rString.GetChar( i );
218cdf0e10cSrcweir 				i++;
219cdf0e10cSrcweir 			}
220cdf0e10cSrcweir 			if ( rString.GetChar( i ) == '>' ) {
221cdf0e10cSrcweir 				sReturn += ">";
222cdf0e10cSrcweir 				i++;
223cdf0e10cSrcweir 			}
224cdf0e10cSrcweir 		}
225cdf0e10cSrcweir 		if ( i < rString.Len()) {
226cdf0e10cSrcweir 			switch ( rString.GetChar( i )) {
227cdf0e10cSrcweir 				case '<':
228cdf0e10cSrcweir 					sReturn += "&lt;";
229cdf0e10cSrcweir 				break;
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 				case '>':
232cdf0e10cSrcweir 					sReturn += "&gt;";
233cdf0e10cSrcweir 				break;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 				case '\"':
236cdf0e10cSrcweir 					sReturn += "&quot;";
237cdf0e10cSrcweir 				break;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 				case '\'':
240cdf0e10cSrcweir 					sReturn += "&apos;";
241cdf0e10cSrcweir 				break;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 				case '&':
244cdf0e10cSrcweir 					if ((( i + 4 ) < rString.Len()) &&
245cdf0e10cSrcweir 						( rString.Copy( i, 5 ) == "&amp;" ))
246cdf0e10cSrcweir 							sReturn += rString.GetChar( i );
247cdf0e10cSrcweir 					else
248cdf0e10cSrcweir 						sReturn += "&amp;";
249cdf0e10cSrcweir 				break;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 				default:
252cdf0e10cSrcweir 					sReturn += rString.GetChar( i );
253cdf0e10cSrcweir 				break;
254cdf0e10cSrcweir 			}
255cdf0e10cSrcweir 		}
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 	rString = sReturn;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir void Export::RemoveUTF8ByteOrderMarker( ByteString &rString ){
261cdf0e10cSrcweir     if( hasUTF8ByteOrderMarker( rString ) )
262cdf0e10cSrcweir         rString.Erase( 0 , 3 );
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir bool Export::hasUTF8ByteOrderMarker( const ByteString &rString ){
266*ab4b560eSHerbert Dürr     // UTF-8 BOM: Byte order marker signature
267*ab4b560eSHerbert Dürr     static const unsigned char bom[3] = { 0xEF, 0xBB, 0xBF };
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     return      rString.Len() >= 3 &&
270cdf0e10cSrcweir                 rString.GetChar( 0 ) == bom[ 0 ] &&
271cdf0e10cSrcweir                 rString.GetChar( 1 ) == bom[ 1 ] &&
272cdf0e10cSrcweir                 rString.GetChar( 2 ) == bom[ 2 ] ;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir bool Export::fileHasUTF8ByteOrderMarker( const ByteString &rString ){
275cdf0e10cSrcweir     SvFileStream aFileIn( String( rString , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ );
276cdf0e10cSrcweir     ByteString sLine;
277cdf0e10cSrcweir     if( !aFileIn.IsEof() ) {
278cdf0e10cSrcweir         aFileIn.ReadLine( sLine );
279cdf0e10cSrcweir         if( aFileIn.IsOpen() ) aFileIn.Close();
280cdf0e10cSrcweir         return hasUTF8ByteOrderMarker( sLine );
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir     if( aFileIn.IsOpen() ) aFileIn.Close();
283cdf0e10cSrcweir     return false;
284cdf0e10cSrcweir }
285cdf0e10cSrcweir void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){
286cdf0e10cSrcweir     SvFileStream aFileIn( String( rFilename , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ );
287cdf0e10cSrcweir     ByteString sLine;
288cdf0e10cSrcweir     if( !aFileIn.IsEof() ) {
289cdf0e10cSrcweir         aFileIn.ReadLine( sLine );
290cdf0e10cSrcweir         // Test header
291cdf0e10cSrcweir         if( hasUTF8ByteOrderMarker( sLine ) ){
292cdf0e10cSrcweir             //cout << "UTF8 Header found!\n";
293cdf0e10cSrcweir             DirEntry aTempFile = Export::GetTempFile();
294cdf0e10cSrcweir             ByteString sTempFile = ByteString( aTempFile.GetFull() , RTL_TEXTENCODING_ASCII_US );
295cdf0e10cSrcweir             SvFileStream aNewFile( String( sTempFile , RTL_TEXTENCODING_ASCII_US ) , STREAM_WRITE );
296cdf0e10cSrcweir             // Remove header
297cdf0e10cSrcweir             RemoveUTF8ByteOrderMarker( sLine );
298cdf0e10cSrcweir             //cout << "Copy stripped stuff to " << sTempFile.GetBuffer() << endl;
299cdf0e10cSrcweir             aNewFile.WriteLine( sLine );
300cdf0e10cSrcweir             // Copy the rest
301cdf0e10cSrcweir             while( !aFileIn.IsEof() ){
302cdf0e10cSrcweir                 aFileIn.ReadLine( sLine );
303cdf0e10cSrcweir                 aNewFile.WriteLine( sLine );
304cdf0e10cSrcweir             }
305cdf0e10cSrcweir             if( aFileIn.IsOpen() ) aFileIn.Close();
306cdf0e10cSrcweir             if( aNewFile.IsOpen() ) aNewFile.Close();
307cdf0e10cSrcweir             DirEntry aEntry( rFilename.GetBuffer() );
308cdf0e10cSrcweir             //cout << "Removing file " << rFilename.GetBuffer() << "\n";
309cdf0e10cSrcweir             aEntry.Kill();
310cdf0e10cSrcweir             //cout << "Renaming file " << sTempFile.GetBuffer() << " to " << rFilename.GetBuffer() << "\n";
311cdf0e10cSrcweir             DirEntry( sTempFile ).MoveTo( DirEntry( rFilename.GetBuffer() ) );
312cdf0e10cSrcweir         }
313cdf0e10cSrcweir     }
314cdf0e10cSrcweir     if( aFileIn.IsOpen() ) aFileIn.Close();
315cdf0e10cSrcweir }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir // Merge it into source code!
318cdf0e10cSrcweir //bool Export::isMergingGermanAllowed( const ByteString& rPrj ){
319cdf0e10cSrcweir //    (void) rPrj;
320cdf0e10cSrcweir //	return true;
321cdf0e10cSrcweir /*	static ByteStringBoolHashMap aHash;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir     if( aHash.find( rPrj ) != aHash.end() ){
324cdf0e10cSrcweir         return aHash[ rPrj ];
325cdf0e10cSrcweir     }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     ByteString sFile = Export::GetEnv( "SRC_ROOT" ) ;
328cdf0e10cSrcweir     sFile.Append("/");
329cdf0e10cSrcweir     sFile.Append( rPrj );
330cdf0e10cSrcweir     sFile.Append("/prj/l10n");
331cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
332cdf0e10cSrcweir     sFile.SearchAndReplaceAll('/','\\');
333cdf0e10cSrcweir #endif
334cdf0e10cSrcweir     DirEntry aFlagfile( sFile );
335cdf0e10cSrcweir 
336cdf0e10cSrcweir     aHash[ rPrj ] = !aFlagfile.Exists();
337cdf0e10cSrcweir     return aHash[ rPrj ];*/
338cdf0e10cSrcweir //}
339cdf0e10cSrcweir bool Export::CopyFile( const ByteString& source , const ByteString& dest )
340cdf0e10cSrcweir {
341cdf0e10cSrcweir //    cout << "CopyFile( " << source.GetBuffer() << " , " << dest.GetBuffer() << " )\n";
342cdf0e10cSrcweir     const int BUFFERSIZE    = 8192;
343cdf0e10cSrcweir     char buf[ BUFFERSIZE ];
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     FILE* IN_FILE = fopen( source.GetBuffer() , "r" );
346cdf0e10cSrcweir     if( IN_FILE == NULL )
347cdf0e10cSrcweir     {
348cdf0e10cSrcweir         cerr << "Export::CopyFile WARNING: Could not open " << source.GetBuffer() << "\n";
349cdf0e10cSrcweir         return false;
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     FILE* OUT_FILE = fopen( dest.GetBuffer() , "w" );
353cdf0e10cSrcweir     if( OUT_FILE == NULL )
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         cerr << "Export::CopyFile WARNING: Could not open/create " << dest.GetBuffer() << " for writing\n";
356cdf0e10cSrcweir         fclose( IN_FILE );
357cdf0e10cSrcweir         return false;
358cdf0e10cSrcweir     }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     while( fgets( buf , BUFFERSIZE , IN_FILE ) != NULL )
361cdf0e10cSrcweir     {
362cdf0e10cSrcweir         if( fputs( buf , OUT_FILE ) == EOF )
363cdf0e10cSrcweir         {
364cdf0e10cSrcweir             cerr << "Export::CopyFile WARNING: Write problems " << source.GetBuffer() << "\n";
365cdf0e10cSrcweir             fclose( IN_FILE );
366cdf0e10cSrcweir             fclose( OUT_FILE );
367cdf0e10cSrcweir             return false;
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir     }
370cdf0e10cSrcweir     if( ferror( IN_FILE ) )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         cerr << "Export::CopyFile WARNING: Read problems " << dest.GetBuffer() << "\n";
373cdf0e10cSrcweir         fclose( IN_FILE );
374cdf0e10cSrcweir         fclose( OUT_FILE );
375cdf0e10cSrcweir         return false;
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir     fclose ( IN_FILE );
378cdf0e10cSrcweir     fclose ( OUT_FILE );
379cdf0e10cSrcweir 
380cdf0e10cSrcweir     return true;
381cdf0e10cSrcweir }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir /*****************************************************************************/
384cdf0e10cSrcweir void Export::UnquotHTML( ByteString &rString )
385cdf0e10cSrcweir /*****************************************************************************/
386cdf0e10cSrcweir {
387cdf0e10cSrcweir 	ByteString sReturn;
388cdf0e10cSrcweir 	while ( rString.Len()) {
389cdf0e10cSrcweir 		if ( rString.Copy( 0, 5 ) == "&amp;" ) {
390cdf0e10cSrcweir 			sReturn += "&";
391cdf0e10cSrcweir 			rString.Erase( 0, 5 );
392cdf0e10cSrcweir 		}
393cdf0e10cSrcweir 		else if ( rString.Copy( 0, 4 ) == "&lt;" ) {
394cdf0e10cSrcweir 			sReturn += "<";
395cdf0e10cSrcweir 			rString.Erase( 0, 4 );
396cdf0e10cSrcweir 		}
397cdf0e10cSrcweir 		else if ( rString.Copy( 0, 4 ) == "&gt;" ) {
398cdf0e10cSrcweir 			sReturn += ">";
399cdf0e10cSrcweir 			rString.Erase( 0, 4 );
400cdf0e10cSrcweir 		}
401cdf0e10cSrcweir 		else if ( rString.Copy( 0, 6 ) == "&quot;" ) {
402cdf0e10cSrcweir 			sReturn += "\"";
403cdf0e10cSrcweir 			rString.Erase( 0, 6 );
404cdf0e10cSrcweir 		}
405cdf0e10cSrcweir 		else if ( rString.Copy( 0, 6 ) == "&apos;" ) {
406cdf0e10cSrcweir 			sReturn += "\'";
407cdf0e10cSrcweir 			rString.Erase( 0, 6 );
408cdf0e10cSrcweir 		}
409cdf0e10cSrcweir 		else {
410cdf0e10cSrcweir 			sReturn += rString.GetChar( 0 );
411cdf0e10cSrcweir 			rString.Erase( 0, 1 );
412cdf0e10cSrcweir 		}
413cdf0e10cSrcweir 	}
414cdf0e10cSrcweir 	rString = sReturn;
415cdf0e10cSrcweir }
416cdf0e10cSrcweir bool Export::isSourceLanguage( const ByteString &sLanguage )
417cdf0e10cSrcweir {
418cdf0e10cSrcweir 	return !isAllowed( sLanguage );
419cdf0e10cSrcweir }
420cdf0e10cSrcweir bool Export::isAllowed( const ByteString &sLanguage ){
421cdf0e10cSrcweir     return ! ( sLanguage.EqualsIgnoreCaseAscii("en-US") );
422cdf0e10cSrcweir }
423cdf0e10cSrcweir /*****************************************************************************/
424cdf0e10cSrcweir bool Export::LanguageAllowed( const ByteString &nLanguage )
425cdf0e10cSrcweir /*****************************************************************************/
426cdf0e10cSrcweir {
427cdf0e10cSrcweir     return std::find( aLanguages.begin() , aLanguages.end() , nLanguage ) != aLanguages.end();
428cdf0e10cSrcweir }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir bool Export::isInitialized = false;
431cdf0e10cSrcweir 
432cdf0e10cSrcweir /*****************************************************************************/
433cdf0e10cSrcweir void Export::InitLanguages( bool bMergeMode ){
434cdf0e10cSrcweir /*****************************************************************************/
435cdf0e10cSrcweir     if( !isInitialized ){
436cdf0e10cSrcweir         ByteString sTmp;
437cdf0e10cSrcweir         ByteStringBoolHashMap aEnvLangs;
438cdf0e10cSrcweir         for ( sal_uInt16 x = 0; x < sLanguages.GetTokenCount( ',' ); x++ ){
439cdf0e10cSrcweir             sTmp = sLanguages.GetToken( x, ',' ).GetToken( 0, '=' );
440cdf0e10cSrcweir             sTmp.EraseLeadingAndTrailingChars();
441cdf0e10cSrcweir             if( bMergeMode && !isAllowed( sTmp ) ){}
442cdf0e10cSrcweir             else if( !( (sTmp.GetChar(0)=='x' || sTmp.GetChar(0)=='X') && sTmp.GetChar(1)=='-' ) ){
443cdf0e10cSrcweir                 aLanguages.push_back( sTmp );
444cdf0e10cSrcweir             }
445cdf0e10cSrcweir         }
446cdf0e10cSrcweir         InitForcedLanguages( bMergeMode );
447cdf0e10cSrcweir         isInitialized = true;
448cdf0e10cSrcweir     }
449cdf0e10cSrcweir }
450cdf0e10cSrcweir /*****************************************************************************/
451cdf0e10cSrcweir void Export::InitForcedLanguages( bool bMergeMode ){
452cdf0e10cSrcweir /*****************************************************************************/
453cdf0e10cSrcweir     ByteString sTmp;
454cdf0e10cSrcweir     ByteStringBoolHashMap aEnvLangs;
455cdf0e10cSrcweir     for ( sal_uInt16 x = 0; x < sForcedLanguages.GetTokenCount( ',' ); x++ ){
456cdf0e10cSrcweir         sTmp = sForcedLanguages.GetToken( x, ',' ).GetToken( 0, '=' );
457cdf0e10cSrcweir         sTmp.EraseLeadingAndTrailingChars();
458cdf0e10cSrcweir         if( bMergeMode && isAllowed( sTmp ) ){}
459cdf0e10cSrcweir         else if( !( (sTmp.GetChar(0)=='x' || sTmp.GetChar(0)=='X') && sTmp.GetChar(1)=='-' ) )
460cdf0e10cSrcweir             aForcedLanguages.push_back( sTmp );
461cdf0e10cSrcweir     }
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir /*****************************************************************************/
465cdf0e10cSrcweir ByteString Export::GetFallbackLanguage( const ByteString nLanguage )
466cdf0e10cSrcweir /*****************************************************************************/
467cdf0e10cSrcweir {
468cdf0e10cSrcweir     ByteString sFallback=nLanguage;
469cdf0e10cSrcweir     GetIsoFallback( sFallback );
470cdf0e10cSrcweir     return sFallback;
471cdf0e10cSrcweir }
472cdf0e10cSrcweir 
473cdf0e10cSrcweir void Export::replaceEncoding( ByteString& rString )
474cdf0e10cSrcweir {
475cdf0e10cSrcweir // &#x2122; -> \u2122
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     for( xub_StrLen idx = 0; idx <= rString.Len()-8 ; idx++ )
478cdf0e10cSrcweir     {
479cdf0e10cSrcweir         if( rString.GetChar( idx )   == '&' &&
480cdf0e10cSrcweir             rString.GetChar( idx+1 ) == '#' &&
481cdf0e10cSrcweir             rString.GetChar( idx+2 ) == 'x' &&
482cdf0e10cSrcweir             rString.GetChar( idx+7 ) == ';' )
483cdf0e10cSrcweir         {
484cdf0e10cSrcweir             ByteString sTmp = rString.Copy( 0 , idx );
485cdf0e10cSrcweir             sTmp.Append( "\\u" );
486cdf0e10cSrcweir             sTmp.Append( rString.GetChar( idx+3 ) );
487cdf0e10cSrcweir             sTmp.Append( rString.GetChar( idx+4 ) );
488cdf0e10cSrcweir             sTmp.Append( rString.GetChar( idx+5 ) );
489cdf0e10cSrcweir             sTmp.Append( rString.GetChar( idx+6 ) );
490cdf0e10cSrcweir             sTmp.Append( rString.Copy( idx+8 , rString.Len() ) );
491cdf0e10cSrcweir             rString = sTmp;
492cdf0e10cSrcweir          }
493cdf0e10cSrcweir     }
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir /*****************************************************************************/
497cdf0e10cSrcweir void Export::FillInFallbacks( ResData *pResData )
498cdf0e10cSrcweir /*****************************************************************************/
499cdf0e10cSrcweir {
500cdf0e10cSrcweir     ByteString sCur;
501cdf0e10cSrcweir     for( unsigned int n = 0; n < aLanguages.size(); n++ ){
502cdf0e10cSrcweir         sCur = aLanguages[ n ];
503cdf0e10cSrcweir         if( isAllowed( sCur )  ){
504cdf0e10cSrcweir             ByteString nFallbackIndex = GetFallbackLanguage( sCur );
505cdf0e10cSrcweir             if( nFallbackIndex.Len() ){
506cdf0e10cSrcweir 				if ( !pResData->sText[ sCur ].Len())
507cdf0e10cSrcweir 					pResData->sText[ sCur ] =
508cdf0e10cSrcweir 						pResData->sText[ nFallbackIndex ];
509cdf0e10cSrcweir 
510cdf0e10cSrcweir                 if ( !pResData->sHelpText[ sCur ].Len())
511cdf0e10cSrcweir 					pResData->sHelpText[ sCur ] =
512cdf0e10cSrcweir 						pResData->sHelpText[ nFallbackIndex ];
513cdf0e10cSrcweir 
514cdf0e10cSrcweir                 if ( !pResData->sQuickHelpText[ sCur ].Len())
515cdf0e10cSrcweir 					pResData->sQuickHelpText[ sCur ] =
516cdf0e10cSrcweir 						pResData->sQuickHelpText[ nFallbackIndex ];
517cdf0e10cSrcweir 
518cdf0e10cSrcweir                 if ( !pResData->sTitle[ sCur ].Len())
519cdf0e10cSrcweir 					pResData->sTitle[ sCur ] =
520cdf0e10cSrcweir 						pResData->sTitle[ nFallbackIndex ];
521cdf0e10cSrcweir 
522cdf0e10cSrcweir 				if ( pResData->pStringList )
523cdf0e10cSrcweir 					FillInListFallbacks(
524cdf0e10cSrcweir 						pResData->pStringList, sCur, nFallbackIndex );
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 				if ( pResData->pPairedList )
527cdf0e10cSrcweir 					FillInListFallbacks(
528cdf0e10cSrcweir 						pResData->pPairedList, sCur, nFallbackIndex );
529cdf0e10cSrcweir 
530cdf0e10cSrcweir 				if ( pResData->pFilterList )
531cdf0e10cSrcweir 					FillInListFallbacks(
532cdf0e10cSrcweir 						pResData->pFilterList, sCur, nFallbackIndex );
533cdf0e10cSrcweir 
534cdf0e10cSrcweir                 if ( pResData->pItemList )
535cdf0e10cSrcweir 					FillInListFallbacks(
536cdf0e10cSrcweir 						pResData->pItemList, sCur, nFallbackIndex );
537cdf0e10cSrcweir 
538cdf0e10cSrcweir                 if ( pResData->pUIEntries )
539cdf0e10cSrcweir 					FillInListFallbacks(
540cdf0e10cSrcweir 						pResData->pUIEntries, sCur, nFallbackIndex );
541cdf0e10cSrcweir 			}
542cdf0e10cSrcweir         }
543cdf0e10cSrcweir     }
544cdf0e10cSrcweir }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir /*****************************************************************************/
547cdf0e10cSrcweir void Export::FillInListFallbacks(
548cdf0e10cSrcweir     ExportList *pList, const ByteString &nSource, const ByteString &nFallback )
549cdf0e10cSrcweir /*****************************************************************************/
550cdf0e10cSrcweir {
551cdf0e10cSrcweir 
552cdf0e10cSrcweir 	for ( sal_uLong i = 0; i < pList->Count(); i++ ) {
553cdf0e10cSrcweir 		ExportListEntry *pEntry = pList->GetObject( i );
554cdf0e10cSrcweir 		if ( !( *pEntry )[ nSource ].Len()){
555cdf0e10cSrcweir  			( *pEntry )[ nSource ] = ( *pEntry )[ nFallback ];
556cdf0e10cSrcweir 			ByteString x = ( *pEntry )[ nSource ];
557cdf0e10cSrcweir 			ByteString y = ( *pEntry )[ nFallback ];
558cdf0e10cSrcweir 		}
559cdf0e10cSrcweir     }
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir /*****************************************************************************/
563cdf0e10cSrcweir ByteString Export::GetTimeStamp()
564cdf0e10cSrcweir /*****************************************************************************/
565cdf0e10cSrcweir {
566cdf0e10cSrcweir //	return "xx.xx.xx";
567cdf0e10cSrcweir     char buf[20];
568cdf0e10cSrcweir 	Time aTime;
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 	snprintf(buf, sizeof(buf), "%8d %02d:%02d:%02d", int(Date().GetDate()),
571cdf0e10cSrcweir 		int(aTime.GetHour()), int(aTime.GetMin()), int(aTime.GetSec()));
572cdf0e10cSrcweir 	return ByteString(buf);
573cdf0e10cSrcweir }
574cdf0e10cSrcweir 
575cdf0e10cSrcweir /*****************************************************************************/
576cdf0e10cSrcweir sal_Bool Export::ConvertLineEnds(
577cdf0e10cSrcweir 	ByteString sSource, ByteString sDestination )
578cdf0e10cSrcweir /*****************************************************************************/
579cdf0e10cSrcweir {
580cdf0e10cSrcweir 	String sSourceFile( sSource, RTL_TEXTENCODING_ASCII_US );
581cdf0e10cSrcweir 	String sDestinationFile( sDestination, RTL_TEXTENCODING_ASCII_US );
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 	SvFileStream aSource( sSourceFile, STREAM_READ );
584cdf0e10cSrcweir 	if ( !aSource.IsOpen())
585cdf0e10cSrcweir 		return sal_False;
586cdf0e10cSrcweir 	SvFileStream aDestination( sDestinationFile, STREAM_STD_WRITE | STREAM_TRUNC );
587cdf0e10cSrcweir 	if ( !aDestination.IsOpen()) {
588cdf0e10cSrcweir 		aSource.Close();
589cdf0e10cSrcweir 		return sal_False;
590cdf0e10cSrcweir 	}
591cdf0e10cSrcweir 
592cdf0e10cSrcweir 	ByteString sLine;
593cdf0e10cSrcweir 
594cdf0e10cSrcweir 	while ( !aSource.IsEof()) {
595cdf0e10cSrcweir 		aSource.ReadLine( sLine );
596cdf0e10cSrcweir 		if ( !aSource.IsEof()) {
597cdf0e10cSrcweir 			sLine.EraseAllChars( '\r' );
598cdf0e10cSrcweir 			aDestination.WriteLine( sLine );
599cdf0e10cSrcweir 		}
600cdf0e10cSrcweir 		else
601cdf0e10cSrcweir 			aDestination.WriteByteString( sLine );
602cdf0e10cSrcweir 	}
603cdf0e10cSrcweir 
604cdf0e10cSrcweir 	aSource.Close();
605cdf0e10cSrcweir 	aDestination.Close();
606cdf0e10cSrcweir 
607cdf0e10cSrcweir 	return sal_True;
608cdf0e10cSrcweir }
609cdf0e10cSrcweir 
610cdf0e10cSrcweir /*****************************************************************************/
611cdf0e10cSrcweir ByteString Export::GetNativeFile( ByteString sSource )
612cdf0e10cSrcweir /*****************************************************************************/
613cdf0e10cSrcweir {
614cdf0e10cSrcweir 	DirEntry aTemp( GetTempFile());
615cdf0e10cSrcweir 	ByteString sReturn( aTemp.GetFull(), RTL_TEXTENCODING_ASCII_US );
616cdf0e10cSrcweir 
617cdf0e10cSrcweir 	for ( sal_uInt16 i = 0; i < 10; i++ )
618cdf0e10cSrcweir 		if ( ConvertLineEnds( sSource, sReturn ))
619cdf0e10cSrcweir 			return sReturn;
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 	return "";
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir const char* Export::GetEnv( const char *pVar )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir         char *pRet = getenv( pVar );
627cdf0e10cSrcweir         if ( !pRet )
628cdf0e10cSrcweir             pRet = 0;
629cdf0e10cSrcweir         return pRet;
630cdf0e10cSrcweir }
631cdf0e10cSrcweir 
632cdf0e10cSrcweir 
633cdf0e10cSrcweir int Export::getCurrentDirectory( rtl::OUString& base_fqurl_out, rtl::OUString& base_out )
634cdf0e10cSrcweir {
635cdf0e10cSrcweir 	DirEntry aDir(".");
636cdf0e10cSrcweir     aDir.ToAbs();
637cdf0e10cSrcweir 	base_out = rtl::OUString( aDir.GetFull() );
638cdf0e10cSrcweir     return osl::File::getFileURLFromSystemPath( base_out , base_fqurl_out );
639cdf0e10cSrcweir }
640cdf0e10cSrcweir 
641cdf0e10cSrcweir void Export::getCurrentDir( string& dir )
642cdf0e10cSrcweir {
643cdf0e10cSrcweir     char buffer[64000];
644cdf0e10cSrcweir     if( getcwd( buffer , sizeof( buffer ) ) == 0 ){
645cdf0e10cSrcweir         cerr << "Error: getcwd failed!\n";
646cdf0e10cSrcweir         exit( -1 );
647cdf0e10cSrcweir     }
648cdf0e10cSrcweir     dir = string( buffer );
649cdf0e10cSrcweir }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir 
652cdf0e10cSrcweir // Stolen from sal/osl/unx/tempfile.c
653cdf0e10cSrcweir 
654cdf0e10cSrcweir #define RAND_NAME_LENGTH 6
655cdf0e10cSrcweir 
656cdf0e10cSrcweir void Export::getRandomName( const ByteString& sPrefix , ByteString& sRandStr , const ByteString& sPostfix )
657cdf0e10cSrcweir {
658cdf0e10cSrcweir 	static const char LETTERS[]        = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
659cdf0e10cSrcweir 	static const int  COUNT_OF_LETTERS = sizeof(LETTERS)/sizeof(LETTERS[0]) - 1;
660cdf0e10cSrcweir 	sRandStr.Append( sPrefix );
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 	static sal_uInt64 value;
663cdf0e10cSrcweir     char     buffer[RAND_NAME_LENGTH];
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 	TimeValue			tv;
666cdf0e10cSrcweir 	sal_uInt64			v;
667cdf0e10cSrcweir     int					i;
668cdf0e10cSrcweir 
669cdf0e10cSrcweir 	osl_getSystemTime( &tv );
670cdf0e10cSrcweir 	oslProcessInfo	proInfo;
671cdf0e10cSrcweir 	osl_getProcessInfo( 0 , osl_Process_IDENTIFIER , &proInfo );
672cdf0e10cSrcweir 
673cdf0e10cSrcweir 	value += ((sal_uInt64) ( tv.Nanosec / 1000 ) << 16) ^ ( tv.Nanosec / 1000 ) ^ proInfo.Ident;
674cdf0e10cSrcweir 
675cdf0e10cSrcweir     v = value;
676cdf0e10cSrcweir 
677cdf0e10cSrcweir     for (i = 0; i < RAND_NAME_LENGTH; i++)
678cdf0e10cSrcweir     {
679cdf0e10cSrcweir         buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
680cdf0e10cSrcweir         v        /= COUNT_OF_LETTERS;
681cdf0e10cSrcweir     }
682cdf0e10cSrcweir 
683cdf0e10cSrcweir 	sRandStr.Append( buffer , RAND_NAME_LENGTH );
684cdf0e10cSrcweir 	sRandStr.Append( sPostfix );
685cdf0e10cSrcweir }
686cdf0e10cSrcweir 
687cdf0e10cSrcweir void Export::getRandomName( ByteString& sRandStr )
688cdf0e10cSrcweir {
689cdf0e10cSrcweir 	const ByteString sEmpty;
690cdf0e10cSrcweir 	getRandomName( sEmpty , sRandStr , sEmpty );
691cdf0e10cSrcweir }
692cdf0e10cSrcweir 
693cdf0e10cSrcweir /*****************************************************************************/
694cdf0e10cSrcweir DirEntry Export::GetTempFile()
695cdf0e10cSrcweir /*****************************************************************************/
696cdf0e10cSrcweir {
697cdf0e10cSrcweir     rtl::OUString* sTempFilename = new rtl::OUString();
698cdf0e10cSrcweir 
699cdf0e10cSrcweir     // Create a temp file
700cdf0e10cSrcweir     int nRC = osl::FileBase::createTempFile( 0 , 0 , sTempFilename );
701cdf0e10cSrcweir     if( nRC ) printf(" osl::FileBase::createTempFile RC = %d",nRC);
702cdf0e10cSrcweir 
703cdf0e10cSrcweir     String strTmp( *sTempFilename  );
704cdf0e10cSrcweir 
705cdf0e10cSrcweir     INetURLObject::DecodeMechanism eMechanism = INetURLObject::DECODE_TO_IURI;
706cdf0e10cSrcweir     String sDecodedStr = INetURLObject::decode( strTmp , '%' , eMechanism );
707cdf0e10cSrcweir     ByteString sTmp( sDecodedStr , RTL_TEXTENCODING_UTF8 );
708cdf0e10cSrcweir 
709cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
710cdf0e10cSrcweir     sTmp.SearchAndReplace("file:///","");
711cdf0e10cSrcweir     sTmp.SearchAndReplaceAll('/','\\');
712cdf0e10cSrcweir #else
713cdf0e10cSrcweir     // Set file permission to 644
714cdf0e10cSrcweir  	const sal_uInt64 nPerm = osl_File_Attribute_OwnRead | osl_File_Attribute_OwnWrite |
715cdf0e10cSrcweir                              osl_File_Attribute_GrpRead | osl_File_Attribute_OthRead ;
716cdf0e10cSrcweir 
717cdf0e10cSrcweir     nRC = osl::File::setAttributes( *sTempFilename , nPerm );
718cdf0e10cSrcweir     if( nRC ) printf(" osl::File::setAttributes RC = %d",nRC);
719cdf0e10cSrcweir 
720cdf0e10cSrcweir     sTmp.SearchAndReplace("file://","");
721cdf0e10cSrcweir #endif
722cdf0e10cSrcweir     DirEntry aDirEntry( sTmp );
723cdf0e10cSrcweir     delete sTempFilename;
724cdf0e10cSrcweir     return aDirEntry;
725cdf0e10cSrcweir }
726