From 0e00f40fc6981bc83e27f8547f0882a4348f29a3 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 12 Feb 2016 11:28:25 +0100 Subject: [PATCH] Add GFilename::fullname() method that returns the full filename, including any extension. --- ChangeLog | 9 +++++---- NEWS | 5 +++-- include/GFilename.hpp | 32 +++++++++++++++++++++++++------- pyext/GFilename.i | 3 ++- src/support/GFilename.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++-------------------------------------- 5 files changed, 80 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index c916ea3..2b1b979 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,12 +1,13 @@ -2016-02-22 Juergen Knoedlseder +2016-02-12 Juergen Knoedlseder * Version 1.1.0 released ========================= - Add GCTAEventList::remove method + Add GFilename::fullname() method + Add GCTAEventList::remove() method Handle also additional columns in GCTAEventList::append method - Correct GFitsTableCol::insert method for empty columns - Correct GCTAEdisp2D::compute_ebounds methods (#1662) + Correct GFitsTableCol::insert() method for empty columns + Correct GCTAEdisp2D::compute_ebounds_xxx() methods (#1662) Use GFilename in GFits to store filename (#1649) Load CTA GTI extension from the HDU name provided by the DSS keyword (#1598) diff --git a/NEWS b/NEWS index be809b1..ba41e0a 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 Author(s) : Juergen Knoedlseder -11 February 2016 +12 February 2016 1. Introduction @@ -74,7 +74,8 @@ None 13. Support module ------------------ -None +Add GFilename::fullname() method that returns the full filename including +any FITS extension. 14. XML module diff --git a/include/GFilename.hpp b/include/GFilename.hpp index 0f1e7c7..7926563 100644 --- a/include/GFilename.hpp +++ b/include/GFilename.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GFilename.hpp - 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 * @@ -59,6 +59,7 @@ public: bool empty(void) const; int size(void) const; int length(void) const; + const std::string& fullname(void) const; const std::string& filename(void) const; std::string extname(const std::string& defaultname = "") const; const std::string& expression(void) const; @@ -78,10 +79,11 @@ protected: void set_filename(const std::string& filename); // Protected members - std::string m_filename; //!< Filename (with stripped extension info) - std::string m_extname; //!< Extension name ("": not set) - int m_extno; //!< Extension number (-1: not set) - int m_extver; //!< Extension version (0: not set) + std::string m_fullname; //!< Full file name + std::string m_filename; //!< File name (with stripped extension info) + std::string m_extname; //!< Extension name ("": not set) + int m_extno; //!< Extension number (-1: not set) + int m_extver; //!< Extension version (0: not set) std::string m_expression; //!< Selection expression ("": not set) }; @@ -137,9 +139,25 @@ int GFilename::length(void) const /***********************************************************************//** - * @brief Return file name + * @brief Return full filename * - * @return String containing file name. + * @return String containing filename with extension. + * + * Returns the full filename, including any extension. + ***************************************************************************/ +inline +const std::string& GFilename::fullname(void) const +{ + return (m_fullname); +} + + +/***********************************************************************//** + * @brief Return filename + * + * @return String containing filename without extension. + * + * Returns the file name without any extension. ***************************************************************************/ inline const std::string& GFilename::filename(void) const diff --git a/pyext/GFilename.i b/pyext/GFilename.i index 2461034..b7232f0 100644 --- a/pyext/GFilename.i +++ b/pyext/GFilename.i @@ -1,7 +1,7 @@ /*************************************************************************** * GFilename.i - 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 * @@ -51,6 +51,7 @@ public: bool empty(void) const; int size(void) const; int length(void) const; + const std::string& fullname(void) const; const std::string& filename(void) const; std::string extname(const std::string& defaultname = "") const; const std::string& expression(void) const; diff --git a/src/support/GFilename.cpp b/src/support/GFilename.cpp index b06a397..27c6b6f 100644 --- a/src/support/GFilename.cpp +++ b/src/support/GFilename.cpp @@ -350,6 +350,7 @@ std::string GFilename::print(const GChatter& chatter) const void GFilename::init_members(void) { // Initialise members + m_fullname.clear(); m_filename.clear(); m_extname.clear(); m_extno = -1; @@ -369,10 +370,11 @@ void GFilename::init_members(void) void GFilename::copy_members(const GFilename& filename) { // Copy members - m_filename = filename.m_filename; - m_extname = filename.m_extname; - m_extno = filename.m_extno; - m_extver = filename.m_extver; + m_fullname = filename.m_fullname; + m_filename = filename.m_filename; + m_extname = filename.m_extname; + m_extno = filename.m_extno; + m_extver = filename.m_extver; m_expression = filename.m_expression; // Return @@ -411,45 +413,48 @@ void GFilename::set_filename(const std::string& filename) // Clear file name clear(); - // Strip any whitespace - std::string fname = gammalib::strip_whitespace(filename); + // Strip any whitespace and store the full name + m_fullname = gammalib::strip_whitespace(filename); // Continue only if filename is not empty - if (!fname.empty()) { + if (!m_fullname.empty()) { // Check for [ symbol in filename - size_t start = fname.find_first_of("["); + size_t start = m_fullname.find_first_of("["); // If we have an extension then separate filename into filename // part and extension part and fill the information of th object if (start != std::string::npos) { // Check for ] symbol in filename - size_t stop = fname.find_first_of("]", start); + size_t stop = m_fullname.find_first_of("]", start); // If there is no ] symbol then throw an exception if (stop == std::string::npos) { - std::string msg = "Missing ] symbol in filename \""+ - fname+"\" extension. Please correct the " - "filename."; + std::string msg = "Missing ] symbol in file name \""+ + m_fullname+"\" extension. Please correct " + "the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } // If ] is not the last character then check if an opening bracket is next - if (stop < fname.length()-1) { + if (stop < m_fullname.length()-1) { - std::string character = gammalib::strip_whitespace(fname.substr(stop+1,1)); + // Get character + std::string character = gammalib::strip_whitespace(m_fullname.substr(stop+1,1)); // If no bracket is coming, throw an exception if (character != "[") { - std::string msg = "Non-bracket character \""+character+"\" beyond ] symbol in filename \""+ - fname+"\" expression. Please correct the filename."; + std::string msg = "Non-bracket character \""+character+ + "\" beyond ] symbol in file name \""+ + m_fullname+"\" expression. Please " + "correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } } // Extract extension name - std::string extname = gammalib::strip_whitespace(fname.substr(start+1, stop-start-1)); + std::string extname = gammalib::strip_whitespace(m_fullname.substr(start+1, stop-start-1)); // Check if there is a , symbol that separates extension name // and extension version @@ -464,8 +469,8 @@ void GFilename::set_filename(const std::string& filename) // If string is empty then throw an exception if (extver.empty()) { std::string msg = "No extension version found after , " - "symbol in filename \""+fname+ - "\". Please correct the filename."; + "symbol in file name \""+m_fullname+ + "\". Please correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } @@ -476,8 +481,9 @@ void GFilename::set_filename(const std::string& filename) // positive if (m_extver <= 0) { std::string msg = "Non-positive extension version " - "encountered in filename \""+fname+ - "\". Please correct the filename."; + "encountered in file name \""+ + m_fullname+ "\". Please correct the " + "file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } @@ -489,8 +495,8 @@ void GFilename::set_filename(const std::string& filename) // 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."; + "file name \""+m_fullname+"\". Please " + "correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } @@ -505,8 +511,8 @@ void GFilename::set_filename(const std::string& filename) // positive if (m_extno < 0) { std::string msg = "Negative extension number encountered " - "in filename \""+fname+"\". Please " - "correct the filename."; + "in file name \""+m_fullname+"\". " + "Please correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } @@ -521,48 +527,49 @@ void GFilename::set_filename(const std::string& filename) // Throw an exception if extension name is empty if (m_extname.empty()) { std::string msg = "Empty extension name encountered " - "in filename \""+fname+"\". Please " - "correct the filename."; + "in file name \""+m_fullname+"\". " + "Please correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } } // endelse: extension name provided // Store the file name - m_filename = gammalib::strip_whitespace(fname.substr(0,start)); + m_filename = gammalib::strip_whitespace(m_fullname.substr(0,start)); // Check if there is an expression behind the extension name - size_t expr_start = fname.find_first_of("[", stop+1); + size_t expr_start = m_fullname.find_first_of("[", stop+1); if (expr_start != std::string::npos) { // Check for ] symbol in expression - size_t expr_stop = fname.find_first_of("]", expr_start); + size_t expr_stop = m_fullname.find_first_of("]", expr_start); // If there is no ] symbol then throw an exception if (expr_stop == std::string::npos) { - std::string msg = "Missing ] symbol in filename \""+ - fname+"\" expression. Please correct the " - "filename."; + std::string msg = "Missing ] symbol in file name \""+ + m_fullname+"\" expression. Please " + "correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } // If ] is not the last character then throw an exception - if (expr_stop < fname.length()-1) { - std::string msg = "Characters beyond ] symbol in filename \""+ - fname+"\" expression. Please correct the filename."; + if (expr_stop < m_fullname.length()-1) { + std::string msg = "Characters beyond ] symbol in file " + "name \""+m_fullname+"\" expression. " + "Please correct the file name."; throw GException::invalid_argument(G_SET_FILENAME, msg); } // Store expression - m_expression = gammalib::strip_whitespace(fname.substr(expr_start+1, expr_stop - expr_start-1)); + m_expression = gammalib::strip_whitespace(m_fullname.substr(expr_start+1, expr_stop - expr_start-1)); } } // endif: had extension // ... otherwise simply set the filename else { - m_filename = fname; + m_filename = m_fullname; } } // endif: filename was not empty -- libgit2 0.21.4