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