Kaydet (Commit) 3179055d authored tarafından Ricardo Montania's avatar Ricardo Montania Kaydeden (comit) Markus Mohrhard

String Cleanup and news OUString methods/constructors

Change-Id: Ia6142020330d0e12650fdc519b66f00e607eac42
Reviewed-on: https://gerrit.libreoffice.org/1491Reviewed-by: 's avatarMarkus Mohrhard <markus.mohrhard@googlemail.com>
Tested-by: 's avatarMarkus Mohrhard <markus.mohrhard@googlemail.com>
üst c4f444dc
...@@ -41,7 +41,8 @@ public: ...@@ -41,7 +41,8 @@ public:
The extension string may be f.e. ".txt" or "", if no extension string is The extension string may be f.e. ".txt" or "", if no extension string is
given, ".tmp" is used. 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 /** TempFile will be removed from disk in dtor if EnableKillingTempFile was
called before. TempDirs will be removed recursively in that case. */ called before. TempDirs will be removed recursively in that case. */
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
using namespace osl; 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 struct TempFile_Impl
{ {
...@@ -48,9 +48,9 @@ String GetSystemTempDir_Impl() ...@@ -48,9 +48,9 @@ String GetSystemTempDir_Impl()
char sBuf[_MAX_PATH]; char sBuf[_MAX_PATH];
const char *pDir = TempDirImpl(sBuf); const char *pDir = TempDirImpl(sBuf);
::rtl::OString aTmpA( pDir ); OString aTmpA( pDir );
::rtl::OUString aTmp = ::rtl::OStringToOUString( aTmpA, osl_getThreadTextEncoding() ); OUString aTmp = ::rtl::OStringToOUString( aTmpA, osl_getThreadTextEncoding() );
rtl::OUString aRet; OUString aRet;
FileBase::getFileURLFromSystemPath( aTmp, aRet ); FileBase::getFileURLFromSystemPath( aTmp, aRet );
String aName = aRet; String aName = aRet;
if( aName.GetChar(aName.Len()-1) != '/' ) if( aName.GetChar(aName.Len()-1) != '/' )
...@@ -66,8 +66,8 @@ String ConstructTempDir_Impl( const String* pParent ) ...@@ -66,8 +66,8 @@ String ConstructTempDir_Impl( const String* pParent )
if ( pParent && pParent->Len() ) if ( pParent && pParent->Len() )
{ {
// if parent given try to use it // if parent given try to use it
rtl::OUString aTmp( *pParent ); OUString aTmp( *pParent );
rtl::OUString aRet; OUString aRet;
// test for valid filename // test for valid filename
{ {
...@@ -84,7 +84,7 @@ String ConstructTempDir_Impl( const String* pParent ) ...@@ -84,7 +84,7 @@ String ConstructTempDir_Impl( const String* pParent )
if ( !aName.Len() ) if ( !aName.Len() )
{ {
// if no parent or invalid parent : use system directory // 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() ) if ( rTempNameBase_Impl.isEmpty() )
rTempNameBase_Impl = GetSystemTempDir_Impl(); rTempNameBase_Impl = GetSystemTempDir_Impl();
aName = rTempNameBase_Impl; aName = rTempNameBase_Impl;
...@@ -98,6 +98,45 @@ String ConstructTempDir_Impl( const String* pParent ) ...@@ -98,6 +98,45 @@ String ConstructTempDir_Impl( const String* pParent )
return aName; 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 ) void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_True )
{ {
// add a suitable tempname // add a suitable tempname
...@@ -105,14 +144,14 @@ void CreateTempName_Impl( String& rName, sal_Bool bKeep, sal_Bool bDir = sal_Tru ...@@ -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] ?!? // ER 13.07.00 why not radix 36 [0-9A-Z] ?!?
const unsigned nRadix = 26; const unsigned nRadix = 26;
String aName( rName ); String aName( rName );
aName += rtl::OUString("sv"); aName += OUString("sv");
rName.Erase(); rName.Erase();
static unsigned long u = Time::GetSystemTicks(); static unsigned long u = Time::GetSystemTicks();
for ( unsigned long nOld = u; ++u != nOld; ) for ( unsigned long nOld = u; ++u != nOld; )
{ {
u %= (nRadix*nRadix*nRadix); u %= (nRadix*nRadix*nRadix);
rtl::OUString aTmp = rtl::OUStringBuffer(aName). OUString aTmp = OUStringBuffer(aName).
append((sal_Int32)(unsigned)u, nRadix). append((sal_Int32)(unsigned)u, nRadix).
append(".tmp"). append(".tmp").
makeStringAndClear(); makeStringAndClear();
...@@ -191,13 +230,64 @@ TempFile::TempFile( const String& rLeadingChars, const String* pExtension, ...@@ -191,13 +230,64 @@ TempFile::TempFile( const String& rLeadingChars, const String* pExtension,
aName += rLeadingChars; aName += rLeadingChars;
for ( sal_Int32 i=0;; i++ ) 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); aTmpBuffer.append(i);
if ( pExtension ) if ( pExtension )
aTmpBuffer.append(*pExtension); aTmpBuffer.append(*pExtension);
else else
aTmpBuffer.append(".tmp"); aTmpBuffer.append(".tmp");
rtl::OUString aTmp = aTmpBuffer.makeStringAndClear(); OUString aTmp = aTmpBuffer.makeStringAndClear();
if ( bDirectory ) if ( bDirectory )
{ {
...@@ -248,7 +338,7 @@ TempFile::~TempFile() ...@@ -248,7 +338,7 @@ TempFile::~TempFile()
String TempFile::GetName() const String TempFile::GetName() const
{ {
rtl::OUString aTmp; OUString aTmp;
aTmp = pImp->aName; aTmp = pImp->aName;
return aTmp; return aTmp;
} }
......
...@@ -69,6 +69,9 @@ public: ...@@ -69,6 +69,9 @@ public:
TempFile( const String& rLeadingChars, const String* pExtension=NULL, const String* pParent=NULL, TempFile( const String& rLeadingChars, const String* pExtension=NULL, const String* pParent=NULL,
sal_Bool bDirectory=sal_False); 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: 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 ). rLeadingChars="abc" means "abc0","abc1" and so on, depending on existing files in the folder ).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment