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 /tools | |
parent | c4f444dc40a1e8429250996dc2b39200c91ea1ce (diff) |
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 'tools')
-rw-r--r-- | tools/inc/tools/tempfile.hxx | 3 | ||||
-rw-r--r-- | tools/source/fsys/tempfile.cxx | 114 |
2 files changed, 104 insertions, 13 deletions
diff --git a/tools/inc/tools/tempfile.hxx b/tools/inc/tools/tempfile.hxx index 9e84adc96a51..315ba24b32e1 100644 --- a/tools/inc/tools/tempfile.hxx +++ b/tools/inc/tools/tempfile.hxx @@ -41,7 +41,8 @@ public: The extension string may be f.e. ".txt" or "", if no extension string is given, ".tmp" is used. */ - TempFile( const String& rLeadingChars, const String* pExtension=NULL, const String* pParent=NULL, sal_Bool bDirectory=sal_False ); + 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 ); /** TempFile will be removed from disk in dtor if EnableKillingTempFile was called before. TempDirs will be removed recursively in that case. */ diff --git a/tools/source/fsys/tempfile.cxx b/tools/source/fsys/tempfile.cxx index 7a3f9dfc2f0a..685960956c31 100644 --- a/tools/source/fsys/tempfile.cxx +++ b/tools/source/fsys/tempfile.cxx @@ -35,7 +35,7 @@ using namespace osl; -namespace { struct TempNameBase_Impl : public rtl::Static< ::rtl::OUString, TempNameBase_Impl > {}; } +namespace { struct TempNameBase_Impl : public rtl::Static< OUString, TempNameBase_Impl > {}; } struct TempFile_Impl { @@ -48,9 +48,9 @@ String GetSystemTempDir_Impl() char sBuf[_MAX_PATH]; const char *pDir = TempDirImpl(sBuf); - ::rtl::OString aTmpA( pDir ); - ::rtl::OUString aTmp = ::rtl::OStringToOUString( aTmpA, osl_getThreadTextEncoding() ); - rtl::OUString aRet; + OString aTmpA( pDir ); + OUString aTmp = ::rtl::OStringToOUString( aTmpA, osl_getThreadTextEncoding() ); + OUString aRet; FileBase::getFileURLFromSystemPath( aTmp, aRet ); String aName = aRet; if( aName.GetChar(aName.Len()-1) != '/' ) @@ -66,8 +66,8 @@ String ConstructTempDir_Impl( const String* pParent ) if ( pParent && pParent->Len() ) { // if parent given try to use it - rtl::OUString aTmp( *pParent ); - rtl::OUString aRet; + OUString aTmp( *pParent ); + OUString aRet; // test for valid filename { @@ -84,7 +84,7 @@ String ConstructTempDir_Impl( const String* pParent ) if ( !aName.Len() ) { // if no parent or invalid parent : use system directory - ::rtl::OUString& rTempNameBase_Impl = TempNameBase_Impl::get(); + OUString& rTempNameBase_Impl = TempNameBase_Impl::get(); if ( rTempNameBase_Impl.isEmpty() ) rTempNameBase_Impl = GetSystemTempDir_Impl(); aName = rTempNameBase_Impl; @@ -98,6 +98,45 @@ String ConstructTempDir_Impl( const String* pParent ) return aName; } +OUString ConstructTempDir_Impl( const OUString* pParent ) +{ + OUString aName; + if ( pParent && pParent->getLength() ) + { + // if parent given try to use it + OUString aTmp( *pParent ); + OUString aRet; + + // test for valid filename + { + ::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() ) + { + // if no parent or invalid parent : use system directory + OUString& rTempNameBase_Impl = TempNameBase_Impl::get(); + if ( rTempNameBase_Impl.isEmpty() ) + rTempNameBase_Impl = GetSystemTempDir_Impl(); + aName = rTempNameBase_Impl; + } + + // Make sure that directory ends with a separator + xub_StrLen 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 @@ -105,14 +144,14 @@ void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_Tru // ER 13.07.00 why not radix 36 [0-9A-Z] ?!? const unsigned nRadix = 26; String aName( rName ); - aName += rtl::OUString("sv"); + aName += OUString("sv"); rName.Erase(); static unsigned long u = Time::GetSystemTicks(); for ( unsigned long nOld = u; ++u != nOld; ) { u %= (nRadix*nRadix*nRadix); - rtl::OUString aTmp = rtl::OUStringBuffer(aName). + OUString aTmp = OUStringBuffer(aName). append((sal_Int32)(unsigned)u, nRadix). append(".tmp"). makeStringAndClear(); @@ -191,13 +230,64 @@ TempFile::TempFile( const String& rLeadingChars, const String* pExtension, aName += rLeadingChars; for ( sal_Int32 i=0;; i++ ) { - rtl::OUStringBuffer aTmpBuffer(aName); + OUStringBuffer aTmpBuffer(aName); + aTmpBuffer.append(i); + if ( pExtension ) + aTmpBuffer.append(*pExtension); + else + aTmpBuffer.append(".tmp"); + OUString aTmp = aTmpBuffer.makeStringAndClear(); + + if ( bDirectory ) + { + FileBase::RC err = Directory::create( aTmp ); + if ( err == FileBase::E_None ) + { + pImp->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 ); + FileBase::RC err = aFile.open(osl_File_OpenFlag_Create); + if ( err == FileBase::E_None ) + { + pImp->aName = aTmp; + aFile.close(); + break; + } + else if ( err != FileBase::E_EXIST ) + // if f.e. name contains invalid chars stop trying to create dirs + break; + } + } +} + +TempFile::TempFile( const OUString& rLeadingChars, const OUString* pExtension, + const OUString* pParent, sal_Bool bDirectory ) + : pImp( new TempFile_Impl ) + , bKillingFileEnabled( sal_False ) +{ + pImp-> bIsDirectory = bDirectory; + + // get correct directory + OUString aName = ConstructTempDir_Impl( pParent ); + + // now use special naming scheme (name takes leading chars and an index couting up from zero + aName += rLeadingChars; + for ( sal_Int32 i=0;; i++ ) + { + OUStringBuffer aTmpBuffer(aName); aTmpBuffer.append(i); if ( pExtension ) aTmpBuffer.append(*pExtension); else aTmpBuffer.append(".tmp"); - rtl::OUString aTmp = aTmpBuffer.makeStringAndClear(); + OUString aTmp = aTmpBuffer.makeStringAndClear(); if ( bDirectory ) { @@ -248,7 +338,7 @@ TempFile::~TempFile() String TempFile::GetName() const { - rtl::OUString aTmp; + OUString aTmp; aTmp = pImp->aName; return aTmp; } |