Commit 6d1334750107a0172368e5cba0bc4067bd494485

Authored by Jürgen Knödlseder
1 parent 8ca6fbd6

Correctly interpret numbers in FITS extension names (fixes #1611).

src/support/GFilename.cpp
1 1 /***************************************************************************
2 2 * GFilename.cpp - Filename class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2015-2016 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -469,9 +469,17 @@ void GFilename::set_filename(const std::string& filename)
469 469  
470 470 } // endif: extension version provided
471 471  
  472 + // If we have an empty extension name then throw an exception
  473 + if (extname.empty()) {
  474 + std::string msg = "An empty extension has been specified in "
  475 + "filename \""+fname+"\". Please correct the"
  476 + " filename.";
  477 + throw GException::invalid_argument(G_SET_FILENAME, msg);
  478 + }
  479 +
472 480 // If we have a purely numerical extension then convert the
473 481 // extension name into an extension number
474   - if (extname.find_first_of("0123456789") != std::string::npos) {
  482 + if (extname.find_first_not_of("+-0123456789") == std::string::npos) {
475 483  
476 484 // Extract extension number
477 485 m_extno = gammalib::toint(extname);
... ...
test/test_GSupport.cpp
1 1 /***************************************************************************
2 2 * test_GSupport.cpp - test support module *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2016 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -680,6 +680,12 @@ void TestGSupport::test_filename(void)
680 680 "Expected \"EVENTS\" extension name, "
681 681 "found \""+filename.extname()+"\"");
682 682  
  683 + // Set filename with number in extension name
  684 + filename = "myfile.fits[EVENTS_2D]";
  685 + test_assert(filename.extname() == "EVENTS_2D",
  686 + "Expected \"EVENTS_2D\" extension name, "
  687 + "found \""+filename.extname()+"\"");
  688 +
683 689 // Set filename with extension number and version
684 690 filename = "myfile.fits[1,2]";
685 691 test_assert(filename.filename() == "myfile.fits",
... ...