From d4cf5f7d53f1f202fa767c1f9ec57c85c2b7d72f Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 17 Feb 2016 23:26:45 +0100 Subject: [PATCH] Handle non-existing extension name in GFits::open(). --- src/fits/GFits.cpp | 22 +++++++++++++++++----- test/test_GObservation.cpp | 6 ------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/fits/GFits.cpp b/src/fits/GFits.cpp index bb04b66..2112ff7 100644 --- a/src/fits/GFits.cpp +++ b/src/fits/GFits.cpp @@ -1039,18 +1039,30 @@ void GFits::open(const GFilename& filename, const bool& create) int status = 0; status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 1, &status); - // If failed then try opening as readonly - if (status == 104 || status == 112) { + // If failed then try opening as readonly. + // + // Possible error codes are: + // 104: FILE_NOT_OPENED (could not open the named file) + // 107: END_OF_FILE (tried to move past end of file, occurs if extname is invalid) + // 112: READONLY_FILE (cannot write to readonly file) + if (status == 104 || status == 107 || status == 112) { status = 0; status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 0, &status); m_readwrite = false; } // If failed and if we are allowed to create a new FITS file then create - // FITS file now - if (create && status == 104) { + // FITS file now. We pass the URL as filename since we want to create a + // fresh file without any extension. We also remove any file before in + // case that file exists but is not a FITS file. + // + // Possible error codes are: + // 104: FILE_NOT_OPENED (could not open the named file) + // 107: END_OF_FILE (tried to move past end of file, occurs if extname is invalid) + if (create && (status == 104 || status == 107)) { + filename.remove(); status = 0; - status = __ffinit(FHANDLE(m_fitsfile), std::string(filename).c_str(), &status); + status = __ffinit(FHANDLE(m_fitsfile), filename.url().c_str(), &status); m_readwrite = true; m_created = true; } diff --git a/test/test_GObservation.cpp b/test/test_GObservation.cpp index 236bb8c..930064c 100644 --- a/test/test_GObservation.cpp +++ b/test/test_GObservation.cpp @@ -262,12 +262,6 @@ void TestGObservation::test_ebounds(void) ***************************************************************************/ void TestGObservation::test_gti(void) { - // Remove test files - GFilename file1("test_gti1.fits"); - GFilename file2("test_gti2.fits"); - file1.remove(); - file2.remove(); - // Test void constructor test_try("Void constructor"); try { -- libgit2 0.21.4