diff options
author | Ricardo Montania <ricardo@linuxafundo.com.br> | 2012-12-27 11:28:45 -0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2012-12-28 18:16:04 +0000 |
commit | 3179055dc3e267c961a5618c810fbc0df1858d72 (patch) | |
tree | e3ee470480d1a1036f570372bcfa1b4fd61d3cae /unotools | |
parent | fix icu deps again (diff) | |
download | core-3179055dc3e267c961a5618c810fbc0df1858d72.tar.gz core-3179055dc3e267c961a5618c810fbc0df1858d72.zip |
String Cleanup and news OUString methods/constructors
Change-Id: Ia6142020330d0e12650fdc519b66f00e607eac42
Reviewed-on: https://gerrit.libreoffice.org/1491
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Tested-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Diffstat (limited to 'unotools')
-rw-r--r-- | unotools/inc/unotools/tempfile.hxx | 3 | ||||
-rw-r--r-- | unotools/source/ucbhelper/tempfile.cxx | 180 |
2 files changed, 159 insertions, 24 deletions
diff --git a/unotools/inc/unotools/tempfile.hxx b/unotools/inc/unotools/tempfile.hxx index 448e729a3998..971bad943e11 100644 --- a/unotools/inc/unotools/tempfile.hxx +++ b/unotools/inc/unotools/tempfile.hxx @@ -69,6 +69,9 @@ public: TempFile( const String& rLeadingChars, const String* pExtension=NULL, const String* pParent=NULL, sal_Bool bDirectory=sal_False); + TempFile( const OUString& rLeadingChars, const OUString* pExtension=NULL, const OUString* pParent=NULL, + sal_Bool bDirectory=sal_False); + /** Same as above; additionally the name starts with some given characters followed by a counter ( example: rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ). diff --git a/unotools/source/ucbhelper/tempfile.cxx b/unotools/source/ucbhelper/tempfile.cxx index 1782dcf6a011..754d9eb4f088 100644 --- a/unotools/source/ucbhelper/tempfile.cxx +++ b/unotools/source/ucbhelper/tempfile.cxx @@ -42,7 +42,7 @@ using namespace osl; namespace { struct TempNameBase_Impl - : public rtl::Static< ::rtl::OUString, TempNameBase_Impl > {}; + : public rtl::Static< OUString, TempNameBase_Impl > {}; } namespace utl @@ -60,23 +60,23 @@ struct TempFile_Impl {} }; -rtl::OUString getParentName( const rtl::OUString& aFileName ) +OUString getParentName( const OUString& aFileName ) { sal_Int32 lastIndex = aFileName.lastIndexOf( sal_Unicode('/') ); - rtl::OUString aParent = aFileName.copy( 0,lastIndex ); + OUString aParent = aFileName.copy( 0,lastIndex ); if( aParent[ aParent.getLength()-1] == sal_Unicode(':') && aParent.getLength() == 6 ) - aParent += rtl::OUString("/"); + aParent += "/"; - if( 0 == aParent.compareToAscii( "file://" ) ) - aParent = rtl::OUString("file:///"); + if( aParent == "file://" ) + aParent = "file:///"; return aParent; } -sal_Bool ensuredir( const rtl::OUString& rUnqPath ) +sal_Bool ensuredir( const OUString& rUnqPath ) { - rtl::OUString aPath; + OUString aPath; if ( rUnqPath.isEmpty() ) return sal_False; @@ -107,7 +107,7 @@ umask(old_mode); if( !bSuccess ) { // perhaps parent(s) don't exist - rtl::OUString aParentDir = getParentName( aPath ); + OUString aParentDir = getParentName( aPath ); if ( aParentDir != aPath ) { bSuccess = ensuredir( getParentName( aPath ) ); @@ -136,10 +136,10 @@ String ConstructTempDir_Impl( const String* pParent ) comphelper::getProcessComponentContext() ) ); // if parent given try to use it - rtl::OUString aTmp( *pParent ); + OUString aTmp( *pParent ); // test for valid filename - rtl::OUString aRet; + OUString aRet; ::osl::FileBase::getFileURLFromSystemPath( ::ucbhelper::getSystemPathFromFileURL( pBroker, aTmp ), aRet ); @@ -157,10 +157,10 @@ String ConstructTempDir_Impl( const String* pParent ) if ( !aName.Len() ) { - ::rtl::OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); + OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); if (rTempNameBase_Impl.isEmpty()) { - ::rtl::OUString ustrTempDirURL; + OUString ustrTempDirURL; ::osl::FileBase::RC rc = ::osl::File::getTempDirURL( ustrTempDirURL ); if (rc == ::osl::FileBase::E_None) @@ -180,6 +180,62 @@ String ConstructTempDir_Impl( const String* pParent ) return aName; } +OUString ConstructTempDir_Impl( const OUString* pParent ) +{ + OUString aName; + if ( pParent && pParent->getLength() ) + { + com::sun::star::uno::Reference< + com::sun::star::ucb::XUniversalContentBroker > pBroker( + com::sun::star::ucb::UniversalContentBroker::create( + comphelper::getProcessComponentContext() ) ); + + // if parent given try to use it + OUString aTmp( *pParent ); + + // test for valid filename + OUString aRet; + ::osl::FileBase::getFileURLFromSystemPath( + ::ucbhelper::getSystemPathFromFileURL( pBroker, aTmp ), + aRet ); + if ( !aRet.isEmpty() ) + { + ::osl::DirectoryItem aItem; + sal_Int32 i = aRet.getLength(); + if ( aRet[i-1] == '/' ) + i--; + + if ( DirectoryItem::get( aRet.copy(0, i), aItem ) == FileBase::E_None ) + aName = aRet; + } + } + + if ( !aName.isEmpty() ) + { + OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); + if (rTempNameBase_Impl.isEmpty()) + { + OUString ustrTempDirURL; + ::osl::FileBase::RC rc = ::osl::File::getTempDirURL( + ustrTempDirURL ); + if (rc == ::osl::FileBase::E_None) + rTempNameBase_Impl = ustrTempDirURL; + } + // if no parent or invalid parent : use default directory + DBG_ASSERT( !rTempNameBase_Impl.isEmpty(), "No TempDir!" ); + aName = rTempNameBase_Impl; + ensuredir( aName ); + } + + // Make sure that directory ends with a separator + sal_Int32 i = aName.getLength(); + if( i>0 && aName[i-1] != '/' ) + aName += "/"; + + return aName; +} + + void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_True ) { // add a suitable tempname @@ -187,7 +243,7 @@ void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_Tru unsigned const nRadix = 36; unsigned long const nMax = (nRadix*nRadix*nRadix*nRadix*nRadix*nRadix); String aName( rName ); - aName += rtl::OUString( "lu" ); + aName += OUString( "lu" ); rName.Erase(); static unsigned long u = Time::GetSystemTicks() % nMax; @@ -195,8 +251,8 @@ void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_Tru { u %= nMax; String aTmp( aName ); - aTmp += rtl::OUString::valueOf(static_cast<sal_Int64>(u), nRadix); - aTmp += rtl::OUString( ".tmp" ); + aTmp += OUString::valueOf(static_cast<sal_Int64>(u), nRadix); + aTmp += OUString( ".tmp" ); if ( bDir ) { @@ -272,7 +328,7 @@ void lcl_createName(TempFile_Impl& _rImpl,const String& rLeadingChars,sal_Bool _ if ( pExtension ) aTmp += *pExtension; else - aTmp += rtl::OUString( ".tmp" ); + aTmp += OUString( ".tmp" ); if ( bDirectory ) { FileBase::RC err = Directory::create( aTmp ); @@ -321,6 +377,74 @@ umask(old_mode); } +void lcl_createName(TempFile_Impl& _rImpl,const OUString& rLeadingChars,sal_Bool _bStartWithZero, const OUString* pExtension, const OUString* pParent, sal_Bool bDirectory) +{ + _rImpl.bIsDirectory = bDirectory; + + // get correct directory + OUString aName = ConstructTempDir_Impl( pParent ); + + sal_Bool bUseNumber = _bStartWithZero; + // now use special naming scheme ( name takes leading chars and an index counting up from zero + aName += rLeadingChars; + for ( sal_Int32 i=0;; i++ ) + { + OUString aTmp( aName ); + if ( bUseNumber ) + aTmp += OUString::valueOf( static_cast< sal_Int32>(i) ); + bUseNumber = sal_True; + if ( pExtension ) + aTmp += *pExtension; + else + aTmp += ".tmp"; + if ( bDirectory ) + { + FileBase::RC err = Directory::create( aTmp ); + if ( err == FileBase::E_None ) + { + _rImpl.aName = aTmp; + break; + } + else if ( err != FileBase::E_EXIST ) + // if f.e. name contains invalid chars stop trying to create dirs + break; + } + else + { + File aFile( aTmp ); +#ifdef UNX +/* RW permission for the user only! */ + mode_t old_mode = umask(077); +#endif + FileBase::RC err = aFile.open(osl_File_OpenFlag_Create); +#ifdef UNX +umask(old_mode); +#endif + if ( err == FileBase::E_None || err == FileBase::E_NOLCK ) + { + _rImpl.aName = aTmp; + aFile.close(); + break; + } + else if ( err != FileBase::E_EXIST ) + { + // if f.e. name contains invalid chars stop trying to create dirs + // but if there is a folder with such name proceed further + + DirectoryItem aTmpItem; + FileStatus aTmpStatus( osl_FileStatus_Mask_Type ); + if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None + || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None + || aTmpStatus.getFileType() != FileStatus::Directory ) + break; + } + } + if ( !_bStartWithZero ) + aTmp += OUString::valueOf( static_cast<sal_Int32>(i) ); + } +} + + String TempFile::CreateTempName( const String* pParent ) { // get correct directory @@ -330,7 +454,7 @@ String TempFile::CreateTempName( const String* pParent ) CreateTempName_Impl( aName, sal_False ); // convert to file URL - rtl::OUString aTmp; + OUString aTmp; if ( aName.Len() ) FileBase::getSystemPathFromFileURL( aName, aTmp ); return aTmp; @@ -355,6 +479,14 @@ TempFile::TempFile( const String& rLeadingChars, const String* pExtension, const { lcl_createName(*pImp,rLeadingChars,sal_True, pExtension, pParent, bDirectory); } + +TempFile::TempFile( const OUString& rLeadingChars, const OUString* pExtension, const OUString* pParent, sal_Bool bDirectory) + : pImp( new TempFile_Impl ) + , bKillingFileEnabled( sal_False ) +{ + lcl_createName(*pImp,rLeadingChars,sal_True, pExtension, pParent, bDirectory); +} + TempFile::TempFile( const String& rLeadingChars,sal_Bool _bStartWithZero, const String* pExtension, const String* pParent, sal_Bool bDirectory) : pImp( new TempFile_Impl ) , bKillingFileEnabled( sal_False ) @@ -388,7 +520,7 @@ sal_Bool TempFile::IsValid() const String TempFile::GetFileName() const { - rtl::OUString aTmp; + OUString aTmp; FileBase::getSystemPathFromFileURL( pImp->aName, aTmp ); return aTmp; } @@ -397,7 +529,7 @@ String TempFile::GetURL() const { if ( !pImp->aURL.Len() ) { - rtl::OUString aTmp; + OUString aTmp; LocalFileHelper::ConvertPhysicalNameToURL( GetFileName(), aTmp ); pImp->aURL = aTmp; } @@ -432,7 +564,7 @@ String TempFile::SetTempNameBaseDirectory( const String &rBaseName ) if( !rBaseName.Len() ) return String(); - rtl::OUString aUnqPath( rBaseName ); + OUString aUnqPath( rBaseName ); // remove trailing slash if ( rBaseName.GetChar( rBaseName.Len() - 1 ) == sal_Unicode( '/' ) ) @@ -448,14 +580,14 @@ String TempFile::SetTempNameBaseDirectory( const String &rBaseName ) bRet = sal_True; // failure to create base directory means returning an empty string - rtl::OUString aTmp; + OUString aTmp; if ( bRet ) { // append own internal directory bRet = sal_True; - ::rtl::OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); + OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); rTempNameBase_Impl = rBaseName; - rTempNameBase_Impl += rtl::OUString('/'); + rTempNameBase_Impl += "/"; TempFile aBase( NULL, sal_True ); if ( aBase.IsValid() ) |