From 6d1334750107a0172368e5cba0bc4067bd494485 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Mon, 4 Jan 2016 14:20:06 +0100 Subject: [PATCH] Correctly interpret numbers in FITS extension names (fixes #1611). --- src/support/GFilename.cpp | 12 ++++++++++-- test/test_GSupport.cpp | 8 +++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/support/GFilename.cpp b/src/support/GFilename.cpp index 97ed953..f410b1e 100644 --- a/src/support/GFilename.cpp +++ b/src/support/GFilename.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GFilename.cpp - Filename class * * ----------------------------------------------------------------------- * - * copyright (C) 2015 by Juergen Knoedlseder * + * copyright (C) 2015-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -469,9 +469,17 @@ void GFilename::set_filename(const std::string& filename) } // endif: extension version provided + // If we have an empty extension name then throw an exception + if (extname.empty()) { + std::string msg = "An empty extension has been specified in " + "filename \""+fname+"\". Please correct the" + " filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + // If we have a purely numerical extension then convert the // extension name into an extension number - if (extname.find_first_of("0123456789") != std::string::npos) { + if (extname.find_first_not_of("+-0123456789") == std::string::npos) { // Extract extension number m_extno = gammalib::toint(extname); diff --git a/test/test_GSupport.cpp b/test/test_GSupport.cpp index d58fc6b..608d943 100644 --- a/test/test_GSupport.cpp +++ b/test/test_GSupport.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * test_GSupport.cpp - test support module * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2015 by Juergen Knoedlseder * + * copyright (C) 2010-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -680,6 +680,12 @@ void TestGSupport::test_filename(void) "Expected \"EVENTS\" extension name, " "found \""+filename.extname()+"\""); + // Set filename with number in extension name + filename = "myfile.fits[EVENTS_2D]"; + test_assert(filename.extname() == "EVENTS_2D", + "Expected \"EVENTS_2D\" extension name, " + "found \""+filename.extname()+"\""); + // Set filename with extension number and version filename = "myfile.fits[1,2]"; test_assert(filename.filename() == "myfile.fits", -- libgit2 0.21.4