Files
libreoffice/l10ntools/source/export2.cxx

557 lines
17 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 16:07:07 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 16:07:07 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 16:07:07 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 16:07:07 +00:00
*
************************************************************************/
2000-09-18 16:07:07 +00:00
#include "export.hxx"
#include <stdio.h>
#include <osl/time.h>
#include <osl/process.h>
#include <rtl/ustring.hxx>
#include <sal/macros.h>
#include <iostream>
#include <iomanip>
#include <tools/urlobj.hxx>
2011-08-29 23:03:08 +01:00
#include <comphelper/string.hxx>
#include <time.h>
#include <stdlib.h>
using namespace std;
2011-08-29 23:03:08 +01:00
using comphelper::string::getToken;
2000-09-18 16:07:07 +00:00
//
// class ResData();
//
/*****************************************************************************/
ResData::~ResData()
/*****************************************************************************/
{
if ( pStringList ) {
// delete existing res. of type StringList
for ( size_t i = 0; i < pStringList->size(); i++ ) {
ExportListEntry* test = (*pStringList)[ i ];
if( test != NULL ) delete test;
2000-09-18 16:07:07 +00:00
}
delete pStringList;
}
if ( pFilterList ) {
// delete existing res. of type FilterList
for ( size_t i = 0; i < pFilterList->size(); i++ ) {
ExportListEntry* test = (*pFilterList)[ i ];
delete test;
2000-09-18 16:07:07 +00:00
}
delete pFilterList;
}
if ( pItemList ) {
// delete existing res. of type ItemList
for ( size_t i = 0; i < pItemList->size(); i++ ) {
ExportListEntry* test = (*pItemList)[ i ];
delete test;
2000-09-18 16:07:07 +00:00
}
delete pItemList;
}
if ( pUIEntries ) {
// delete existing res. of type UIEntries
for ( size_t i = 0; i < pUIEntries->size(); i++ ) {
ExportListEntry* test = (*pUIEntries)[ i ];
delete test;
2000-09-18 16:07:07 +00:00
}
delete pUIEntries;
}
}
//
// class Export
//
/*****************************************************************************/
2011-12-07 10:49:55 +00:00
rtl::OString Export::sLanguages;
rtl::OString Export::sForcedLanguages;
/*****************************************************************************/
2011-12-07 10:49:55 +00:00
void Export::DumpExportList(const rtl::OString& rListName, ExportList& aList)
{
printf( "%s\n", rListName.getStr() );
2012-01-30 20:44:22 +00:00
rtl::OString l;
ExportListEntry* aEntry;
2012-01-30 20:44:22 +00:00
for( unsigned int x = 0; x < aList.size() ; ++x )
{
aEntry = (ExportListEntry*) aList[ x ];
Export::DumpMap( l , *aEntry );
}
printf("\n");
}
2011-12-07 10:49:55 +00:00
2012-01-29 22:00:26 +00:00
void Export::DumpMap(const rtl::OString& rMapName,
2011-12-07 10:49:55 +00:00
ByteStringHashMap& aMap)
{
if( rMapName.getLength() )
printf("MapName %s\n", rMapName.getStr());
2012-01-29 22:00:26 +00:00
if( aMap.size() < 1 )
return;
for(ByteStringHashMap::const_iterator idbg = aMap.begin(); idbg != aMap.end(); ++idbg)
{
ByteString a( idbg->first );
ByteString b( idbg->second );
printf("[%s]= %s",a.GetBuffer(),b.GetBuffer());
printf("\n");
}
printf("\n");
2012-01-29 22:00:26 +00:00
return;
}
2011-12-07 10:49:55 +00:00
/*****************************************************************************/
2012-01-25 20:34:11 +00:00
void Export::SetLanguages( std::vector<rtl::OString> val ){
/*****************************************************************************/
aLanguages = val;
isInitialized = true;
}
/*****************************************************************************/
2012-01-25 20:34:11 +00:00
std::vector<rtl::OString> Export::GetLanguages(){
/*****************************************************************************/
return aLanguages;
}
/*****************************************************************************/
2012-01-25 20:34:11 +00:00
std::vector<rtl::OString> Export::GetForcedLanguages(){
/*****************************************************************************/
return aForcedLanguages;
}
2012-01-25 20:34:11 +00:00
std::vector<rtl::OString> Export::aLanguages = std::vector<rtl::OString>();
std::vector<rtl::OString> Export::aForcedLanguages = std::vector<rtl::OString>();
/*****************************************************************************/
void Export::QuotHTML( ByteString &rString )
/*****************************************************************************/
{
2011-09-20 15:25:11 +01:00
rtl::OStringBuffer sReturn;
for ( sal_uInt16 i = 0; i < rString.Len(); i++ ) {
2000-12-08 11:49:25 +00:00
ByteString sTemp = rString.Copy( i );
if ( sTemp.Search( "<Arg n=" ) == 0 ) {
while ( i < rString.Len() && rString.GetChar( i ) != '>' ) {
2011-09-20 15:25:11 +01:00
sReturn.append(rString.GetChar(i));
2000-12-08 11:49:25 +00:00
i++;
}
if ( rString.GetChar( i ) == '>' ) {
2011-09-20 15:25:11 +01:00
sReturn.append('>');
i++;
}
2000-12-08 11:49:25 +00:00
}
if ( i < rString.Len()) {
switch ( rString.GetChar( i )) {
case '<':
2011-09-20 15:25:11 +01:00
sReturn.append("&lt;");
2000-12-08 11:49:25 +00:00
break;
2000-12-08 11:49:25 +00:00
case '>':
2011-09-20 15:25:11 +01:00
sReturn.append("&gt;");
2000-12-08 11:49:25 +00:00
break;
2000-12-08 11:49:25 +00:00
case '\"':
2011-09-20 15:25:11 +01:00
sReturn.append("&quot;");
2000-12-08 11:49:25 +00:00
break;
2000-12-08 11:49:25 +00:00
case '\'':
2011-09-20 15:25:11 +01:00
sReturn.append("&apos;");
2000-12-08 11:49:25 +00:00
break;
2000-12-08 11:49:25 +00:00
case '&':
if ((( i + 4 ) < rString.Len()) &&
( rString.Copy( i, 5 ) == "&amp;" ))
2011-09-20 15:25:11 +01:00
sReturn.append(rString.GetChar(i));
2000-12-08 11:49:25 +00:00
else
2011-09-20 15:25:11 +01:00
sReturn.append("&amp;");
2000-12-08 11:49:25 +00:00
break;
2000-12-08 11:49:25 +00:00
default:
2011-09-20 15:25:11 +01:00
sReturn.append(rString.GetChar(i));
2000-12-08 11:49:25 +00:00
break;
}
}
}
2011-09-20 15:25:11 +01:00
rString = sReturn.makeStringAndClear();
}
void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString )
{
if( hasUTF8ByteOrderMarker( rString ) )
rString = rString.copy(3);
}
bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString )
{
return rString.getLength() >= 3 && rString[0] == '\xEF' &&
rString[1] == '\xBB' && rString[2] == '\xBF' ;
}
2012-01-30 20:44:22 +00:00
bool Export::fileHasUTF8ByteOrderMarker(const rtl::OString &rString)
{
2012-01-30 20:44:22 +00:00
SvFileStream aFileIn(rtl::OStringToOUString(rString, RTL_TEXTENCODING_ASCII_US), STREAM_READ);
rtl::OString sLine;
if( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine );
if( aFileIn.IsOpen() )
aFileIn.Close();
return hasUTF8ByteOrderMarker( sLine );
}
if( aFileIn.IsOpen() ) aFileIn.Close();
return false;
}
2012-01-30 20:44:22 +00:00
void Export::RemoveUTF8ByteOrderMarkerFromFile(const rtl::OString &rFilename)
{
2012-01-30 20:44:22 +00:00
SvFileStream aFileIn(rtl::OStringToOUString(rFilename , RTL_TEXTENCODING_ASCII_US) , STREAM_READ );
rtl::OString sLine;
if( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine );
// Test header
if( hasUTF8ByteOrderMarker( sLine ) )
{
DirEntry aTempFile = Export::GetTempFile();
2011-11-27 20:37:42 +00:00
rtl::OString sTempFile = rtl::OUStringToOString(aTempFile.GetFull() , RTL_TEXTENCODING_ASCII_US);
SvFileStream aNewFile(rtl::OStringToOUString(sTempFile , RTL_TEXTENCODING_ASCII_US) , STREAM_WRITE);
// Remove header
RemoveUTF8ByteOrderMarker( sLine );
aNewFile.WriteLine( sLine );
// Copy the rest
while( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine );
aNewFile.WriteLine( sLine );
}
if( aFileIn.IsOpen() ) aFileIn.Close();
if( aNewFile.IsOpen() ) aNewFile.Close();
2012-01-30 20:44:22 +00:00
DirEntry aEntry( rFilename.getStr() );
aEntry.Kill();
2012-01-30 20:44:22 +00:00
DirEntry( sTempFile ).MoveTo( DirEntry( rFilename.getStr() ) );
}
}
if( aFileIn.IsOpen() )
aFileIn.Close();
}
2012-01-30 20:44:22 +00:00
bool Export::CopyFile(const rtl::OString& rSource, const rtl::OString& rDest)
{
const int BUFFERSIZE = 8192;
char buf[ BUFFERSIZE ];
2012-01-30 20:44:22 +00:00
FILE* IN_FILE = fopen( rSource.getStr() , "r" );
if( IN_FILE == NULL )
{
2012-01-30 20:44:22 +00:00
cerr << "Export::CopyFile WARNING: Could not open " << rSource.getStr() << "\n";
return false;
}
2012-01-30 20:44:22 +00:00
FILE* OUT_FILE = fopen( rDest.getStr() , "w" );
if( OUT_FILE == NULL )
{
2012-01-30 20:44:22 +00:00
cerr << "Export::CopyFile WARNING: Could not open/create " << rDest.getStr() << " for writing\n";
fclose( IN_FILE );
return false;
}
while( fgets( buf , BUFFERSIZE , IN_FILE ) != NULL )
{
if( fputs( buf , OUT_FILE ) == EOF )
{
2012-01-30 20:44:22 +00:00
cerr << "Export::CopyFile WARNING: Write problems " << rSource.getStr() << "\n";
fclose( IN_FILE );
fclose( OUT_FILE );
return false;
}
}
if( ferror( IN_FILE ) )
{
2012-01-30 20:44:22 +00:00
cerr << "Export::CopyFile WARNING: Read problems " << rDest.getStr() << "\n";
fclose( IN_FILE );
fclose( OUT_FILE );
return false;
}
fclose ( IN_FILE );
fclose ( OUT_FILE );
return true;
}
/*****************************************************************************/
void Export::UnquotHTML( ByteString &rString )
/*****************************************************************************/
{
2011-09-20 15:25:11 +01:00
rtl::OStringBuffer sReturn;
while ( rString.Len())
{
if ( rString.Copy( 0, 5 ) == "&amp;" )
{
sReturn.append('&');
rString.Erase( 0, 5 );
}
2011-09-20 15:25:11 +01:00
else if ( rString.Copy( 0, 4 ) == "&lt;" )
{
sReturn.append('<');
rString.Erase( 0, 4 );
}
2011-09-20 15:25:11 +01:00
else if ( rString.Copy( 0, 4 ) == "&gt;" )
{
sReturn.append('>');
rString.Erase( 0, 4 );
}
2011-09-20 15:25:11 +01:00
else if ( rString.Copy( 0, 6 ) == "&quot;" )
{
sReturn.append('\"');;
rString.Erase( 0, 6 );
}
2011-09-20 15:25:11 +01:00
else if ( rString.Copy( 0, 6 ) == "&apos;" )
{
sReturn.append('\'');
rString.Erase( 0, 6 );
}
2011-09-20 15:25:11 +01:00
else
{
sReturn.append(rString.GetChar(0));
rString.Erase( 0, 1 );
}
}
2011-09-20 15:25:11 +01:00
rString = sReturn.makeStringAndClear();
}
2012-01-30 20:44:22 +00:00
bool Export::isSourceLanguage(const rtl::OString &rLanguage)
{
2012-01-30 20:44:22 +00:00
return !isAllowed(rLanguage);
}
2012-01-30 20:44:22 +00:00
bool Export::isAllowed(const rtl::OString &rLanguage)
{
return !rLanguage.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US"));
}
bool Export::isInitialized = false;
/*****************************************************************************/
void Export::InitLanguages( bool bMergeMode ){
/*****************************************************************************/
2011-12-07 10:49:55 +00:00
if( !isInitialized )
{
ByteString sTmp;
ByteStringBoolHashMap aEnvLangs;
2011-12-07 10:49:55 +00:00
sal_Int32 nIndex = 0;
do
{
rtl::OString aToken = sLanguages.getToken(0, ',', nIndex);
sTmp = getToken(aToken, 0, '=');
sTmp = comphelper::string::strip(sTmp, ' ');
if( bMergeMode && !isAllowed( sTmp ) ){}
else if( !( (sTmp.GetChar(0)=='x' || sTmp.GetChar(0)=='X') && sTmp.GetChar(1)=='-' ) ){
aLanguages.push_back( sTmp );
}
}
2011-12-07 10:49:55 +00:00
while ( nIndex >= 0 );
InitForcedLanguages( bMergeMode );
isInitialized = true;
}
}
/*****************************************************************************/
void Export::InitForcedLanguages( bool bMergeMode ){
/*****************************************************************************/
ByteString sTmp;
ByteStringBoolHashMap aEnvLangs;
2011-12-07 10:49:55 +00:00
sal_Int32 nIndex = 0;
do
{
rtl::OString aToken = sForcedLanguages.getToken(0, ',', nIndex);
sTmp = getToken(aToken, 0, '=');
sTmp = comphelper::string::strip(sTmp, ' ');
if( bMergeMode && isAllowed( sTmp ) ){}
else if( !( (sTmp.GetChar(0)=='x' || sTmp.GetChar(0)=='X') && sTmp.GetChar(1)=='-' ) )
aForcedLanguages.push_back( sTmp );
}
2011-12-07 10:49:55 +00:00
while ( nIndex >= 0 );
}
2012-01-30 20:44:22 +00:00
rtl::OString Export::GetTimeStamp()
2001-06-07 12:33:31 +00:00
{
// return "xx.xx.xx";
char buf[20];
Time aTime( Time::SYSTEM );
2001-06-07 12:33:31 +00:00
snprintf(buf, sizeof(buf), "%8d %02d:%02d:%02d", int(Date( Date::SYSTEM).GetDate()),
int(aTime.GetHour()), int(aTime.GetMin()), int(aTime.GetSec()));
2012-01-30 20:44:22 +00:00
return rtl::OString(buf);
2001-06-07 12:33:31 +00:00
}
/*****************************************************************************/
sal_Bool Export::ConvertLineEnds(
2001-06-07 12:33:31 +00:00
ByteString sSource, ByteString sDestination )
/*****************************************************************************/
{
String sSourceFile( sSource, RTL_TEXTENCODING_ASCII_US );
String sDestinationFile( sDestination, RTL_TEXTENCODING_ASCII_US );
SvFileStream aSource( sSourceFile, STREAM_READ );
if ( !aSource.IsOpen())
return sal_False;
2001-06-07 12:33:31 +00:00
SvFileStream aDestination( sDestinationFile, STREAM_STD_WRITE | STREAM_TRUNC );
if ( !aDestination.IsOpen()) {
aSource.Close();
return sal_False;
2001-06-07 12:33:31 +00:00
}
rtl::OString sLine;
2001-06-07 12:33:31 +00:00
while ( !aSource.IsEof())
{
2001-06-07 12:33:31 +00:00
aSource.ReadLine( sLine );
if ( !aSource.IsEof()) //a complete line
{
sLine = comphelper::string::remove(sLine, '\r');
aDestination.WriteLine( sLine );
}
else //a final incomplete line, just copy it as-is
aDestination.Write( sLine.getStr(), sLine.getLength() );
2001-06-07 12:33:31 +00:00
}
aSource.Close();
aDestination.Close();
return sal_True;
2001-06-07 12:33:31 +00:00
}
/*****************************************************************************/
ByteString Export::GetNativeFile( ByteString sSource )
/*****************************************************************************/
{
DirEntry aTemp( GetTempFile());
2011-11-27 20:37:42 +00:00
rtl::OString sReturn(rtl::OUStringToOString(aTemp.GetFull(), RTL_TEXTENCODING_ASCII_US));
2001-06-07 12:33:31 +00:00
for ( sal_uInt16 i = 0; i < 10; i++ )
if ( ConvertLineEnds( sSource, sReturn ))
return sReturn;
2001-06-07 12:33:31 +00:00
return "";
}
const char* Export::GetEnv( const char *pVar )
{
char *pRet = getenv( pVar );
if ( !pRet )
pRet = 0;
return pRet;
}
void Export::getCurrentDir( string& dir )
{
char buffer[64000];
if( getcwd( buffer , sizeof( buffer ) ) == 0 ){
cerr << "Error: getcwd failed!\n";
exit( -1 );
}
dir = string( buffer );
}
// Stolen from sal/osl/unx/tempfile.c
#define RAND_NAME_LENGTH 6
2012-01-30 20:44:22 +00:00
rtl::OString Export::getRandomName(const rtl::OString& rPrefix, const rtl::OString& rPostfix)
{
static const char LETTERS[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
static const int COUNT_OF_LETTERS = SAL_N_ELEMENTS(LETTERS) - 1;
2012-01-30 20:44:22 +00:00
rtl::OStringBuffer sRandStr(rPrefix);
static sal_uInt64 value;
char buffer[RAND_NAME_LENGTH];
TimeValue tv;
sal_uInt64 v;
int i;
osl_getSystemTime( &tv );
oslProcessInfo proInfo;
proInfo.Size = sizeof(oslProcessInfo);
osl_getProcessInfo( 0 , osl_Process_IDENTIFIER , &proInfo );
value += ((sal_uInt64) ( tv.Nanosec / 1000 ) << 16) ^ ( tv.Nanosec / 1000 ) ^ proInfo.Ident;
v = value;
2012-01-30 20:44:22 +00:00
for (i = 0; i < RAND_NAME_LENGTH; ++i)
{
buffer[i] = LETTERS[v % COUNT_OF_LETTERS];
v /= COUNT_OF_LETTERS;
}
2012-01-30 20:44:22 +00:00
sRandStr.append(buffer , RAND_NAME_LENGTH);
sRandStr.append(rPostfix);
return sRandStr.makeStringAndClear();
}
2001-06-07 12:33:31 +00:00
/*****************************************************************************/
DirEntry Export::GetTempFile()
/*****************************************************************************/
{
rtl::OUString* sTempFilename = new rtl::OUString();
// Create a temp file
int nRC = osl::FileBase::createTempFile( 0 , 0 , sTempFilename );
if( nRC ) printf(" osl::FileBase::createTempFile RC = %d",nRC);
String strTmp( *sTempFilename );
INetURLObject::DecodeMechanism eMechanism = INetURLObject::DECODE_TO_IURI;
String sDecodedStr = INetURLObject::decode( strTmp , '%' , eMechanism );
2011-11-27 20:37:42 +00:00
rtl::OString sTmp(rtl::OUStringToOString(sDecodedStr , RTL_TEXTENCODING_UTF8));
2011-04-28 08:48:54 +02:00
#if defined(WNT)
2011-11-27 20:37:42 +00:00
sTmp = comphelper::string::replace(sTmp,
rtl::OString(RTL_CONSTASCII_STRINGPARAM("file:///")),
rtl::OString());
sTmp = sTmp.replace('/', '\\');
#else
// Set file permission to 644
2011-11-27 20:37:42 +00:00
const sal_uInt64 nPerm = osl_File_Attribute_OwnRead | osl_File_Attribute_OwnWrite |
osl_File_Attribute_GrpRead | osl_File_Attribute_OthRead ;
nRC = osl::File::setAttributes( *sTempFilename , nPerm );
if( nRC ) printf(" osl::File::setAttributes RC = %d",nRC);
2011-11-27 20:37:42 +00:00
sTmp = comphelper::string::replace(sTmp,
rtl::OString(RTL_CONSTASCII_STRINGPARAM("file://")),
rtl::OString());
#endif
DirEntry aDirEntry( sTmp );
delete sTempFilename;
return aDirEntry;
2001-06-07 12:33:31 +00:00
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */