From a0c6e974a3393642bda80fed5bce464a6c6cf2ec Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 25 May 2010 02:29:05 +0000 Subject: We now have a portable tempfile function, cool, it compiles on windows. Fixed a bug in Socket, it wasn't closing the socket in all exception cases. Also fixed a few things in the unit test framework, going to add some more helpers soon. --- src/file.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src/file.cpp') diff --git a/src/file.cpp b/src/file.cpp index a0c3fd8..0566ee8 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -12,6 +12,7 @@ #include #include #include // for mkstemp +#include #include "bu/config.h" @@ -189,21 +190,41 @@ void Bu::File::setBlocking( bool bBlocking ) Bu::File Bu::File::tempFile( Bu::FString &sName ) { -#ifndef WIN32 - int afh_d = mkstemp( sName.getStr() ); + uint32_t iX; + iX = time( NULL ) + getpid(); + int iXes; + for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) + { + if( sName[iXes] != 'X' ) + break; + } + iXes++; + if( iXes == sName.getSize() ) + throw Bu::ExceptionBase("Invalid temporary filename template."); + for( int iter = 0; iter < 100; iter++ ) + { + for( int j = iXes; j < sName.getSize(); j++ ) + { + iX = (1103515245 * iX + 12345); + sName[j] = ('A'+(iX%26)) | ((iX&0x1000)?(0x20):(0)); + } - return Bu::File( afh_d ); -#else - return Bu::File( sName, Bu::File::Write|Bu::File::Create ); -#endif + try + { + return Bu::File( sName, Bu::File::Read|Bu::File::Write + |Bu::File::Create|Bu::File::Exclusive ); + } catch(...) { } + } + throw Bu::FileException("Failed to create unique temporary file after 100" + " iterations."); } void Bu::File::setSize( long iSize ) { -#ifndef WIN32 - ftruncate( fd, iSize ); +#ifdef WIN32 + chsize( fd, iSize ); #else -#warning Bu::File::setSize not implemented on this platform + ftruncate( fd, iSize ); #endif } -- cgit v1.2.3