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