From 321955070fdf8581a1cf13cf36e97f752ca85b30 Mon Sep 17 00:00:00 2001 From: fgerlits Date: Wed, 10 Nov 2004 10:09:04 +0000 Subject: [PATCH] got rid of tempnam() and fopen() --- .../modules/storage/src/TestStorageClient.cxx | 50 +++++++++++-------- .../storage/src/TestStorageClientTest.cxx | 21 ++++---- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/livesupport/modules/storage/src/TestStorageClient.cxx b/livesupport/modules/storage/src/TestStorageClient.cxx index 7973d69e8..c499de8dc 100644 --- a/livesupport/modules/storage/src/TestStorageClient.cxx +++ b/livesupport/modules/storage/src/TestStorageClient.cxx @@ -21,8 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Author : $Author: maroy $ - Version : $Revision: 1.11 $ + Author : $Author: fgerlits $ + Version : $Revision: 1.12 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $ ------------------------------------------------------------------------------*/ @@ -39,6 +39,7 @@ #error "Need unistd.h" #endif +#include #include #include "TestStorageClient.h" @@ -253,11 +254,24 @@ TestStorageClient :: acquirePlaylist(Ptr::Ref id) const ++it; } - std::string smilFileName = "file://"; - smilFileName += tempnam(0, "smil"); + char fileName[] = "/tmp/tempPlaylistXXXXXX"; + if (!mkstemp(fileName)) { + throw std::logic_error("could not create temp file"); + } + std::string sysCommand = "mv "; + sysCommand += fileName; + sysCommand += " "; + sysCommand += fileName; + sysCommand += ".smil > /dev/null 2>&1"; + if (system(sysCommand.c_str()) != 0) { + throw std::logic_error("could not rename temp file"); + } + + std::string smilFileName("file://"); + smilFileName += fileName; smilFileName += ".smil"; smilDocument->write_to_file(smilFileName, "UTF-8"); - + Ptr::Ref playlistUri(new std::string(smilFileName)); newPlaylist->setUri(playlistUri); return newPlaylist; @@ -275,15 +289,12 @@ TestStorageClient :: releasePlaylist(Ptr::Ref playlist) const throw std::logic_error("playlist URI not found"); } - try { - std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r"); - if (!f) { - throw std::logic_error("playlist temp file not found"); - } - std::fclose(f); + std::ifstream ifs(playlist->getUri()->substr(7).c_str()); + if (ifs) { + ifs.close(); } - catch (std::exception &e) { - throw std::logic_error("playlist temp file I/O error"); + else { + throw std::logic_error("playlist temp file not found"); } std::remove(playlist->getUri()->substr(7).c_str()); @@ -415,16 +426,11 @@ TestStorageClient :: acquireAudioClip(Ptr::Ref id) const // cut the "file:" off std::string audioClipFileName = storedAudioClip->getUri()->substr(5); - try { - std::FILE * f = fopen(audioClipFileName.c_str(), "r"); - if (f) { - std::fclose(f); - } - else { - throw std::logic_error("could not read audio clip"); - } + std::ifstream ifs(audioClipFileName.c_str()); + if (ifs) { + ifs.close(); } - catch (std::exception &e) { + else { throw std::logic_error("could not read audio clip"); } diff --git a/livesupport/modules/storage/src/TestStorageClientTest.cxx b/livesupport/modules/storage/src/TestStorageClientTest.cxx index 5cb4b68e5..97cd279b8 100644 --- a/livesupport/modules/storage/src/TestStorageClientTest.cxx +++ b/livesupport/modules/storage/src/TestStorageClientTest.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.10 $ + Version : $Revision: 1.11 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClientTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -41,6 +41,7 @@ #include +#include #include #include "TestStorageClient.h" @@ -270,12 +271,11 @@ TestStorageClientTest :: acquirePlaylistTest(void) CPPUNIT_ASSERT(playlist->getUri()); CPPUNIT_ASSERT(playlist->getUri()->substr(0,7) == "file://"); - try { - std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r"); - CPPUNIT_ASSERT(f); - std::fclose(f); + std::ifstream ifs1(playlist->getUri()->substr(7).c_str()); + if (ifs1) { + ifs1.close(); } - catch (std::exception &e) { + else { CPPUNIT_FAIL("temp file not created correctly"); } @@ -288,11 +288,10 @@ TestStorageClientTest :: acquirePlaylistTest(void) eMsg += e.what(); CPPUNIT_FAIL(eMsg); } - try { - std::FILE * f = fopen(savedTempFilePath.c_str(), "r"); - CPPUNIT_ASSERT(!f); - } - catch (std::exception &e) { + + std::ifstream ifs2(playlist->getUri()->substr(7).c_str()); + if (ifs2) { + ifs2.close(); CPPUNIT_FAIL("temp file not destroyed correctly"); }