00001 /*************************************************************************** 00002 * GFilename.hpp - Filename class * 00003 * ----------------------------------------------------------------------- * 00004 * copyright (C) 2015-2016 by Juergen Knoedlseder * 00005 * ----------------------------------------------------------------------- * 00006 * * 00007 * This program is free software: you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation, either version 3 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00019 * * 00020 ***************************************************************************/ 00021 /** 00022 * @file GFilename.hpp 00023 * @brief Filename class interface definition 00024 * @author Juergen Knoedlseder 00025 */ 00026 00027 #ifndef GFILENAME_HPP 00028 #define GFILENAME_HPP 00029 00030 /* __ Includes ___________________________________________________________ */ 00031 #include <string> 00032 #include "GBase.hpp" 00033 #include "GTools.hpp" 00034 00035 00036 /***********************************************************************//** 00037 * @class GFilename 00038 * 00039 * @brief Filename class 00040 * 00041 * This class handles filenames. A filename is a string composed of an 00042 * optional protocol (http:, ftp:, file:), an absolute or relative access 00043 * path, a file, and optionally a FITS extension. Examples of valid file 00044 * names are 00045 * 00046 * myfits.fits 00047 * myfile.fits[EVENTS] 00048 * ./data/myfile.fits 00049 * ~/data/myfile.fits 00050 * /home/myuser/data/myfile.fits 00051 * http://www.irap.omp.eu/data/myfile.fits 00052 * ftp://www.irap.omp.eu/data/myfile.fits 00053 * file:///home/myuser/data/myfile.fits 00054 * 00055 * A filename without the optional FITS extension is called a Uniform 00056 * Resource Locator (URL) an is accessed using the url() method. The URL 00057 * can be decomposed into the protocol, access path and the filename using 00058 * the protocol(), path(), and file(). 00059 * 00060 * The FITS extension is implemente using the GFitsExtension class. 00061 ***************************************************************************/ 00062 class GFilename : public GBase { 00063 00064 // Friend functions 00065 friend std::string operator+(const GFilename& filename, const std::string& string); 00066 friend std::string operator+(const std::string& string, const GFilename& filename); 00067 friend bool operator==(const GFilename &a, const GFilename &b); 00068 friend bool operator!=(const GFilename &a, const GFilename &b); 00069 00070 public: 00071 // Constructors and destructors 00072 GFilename(void); 00073 GFilename(const std::string& filename); 00074 GFilename(const char* filename); 00075 GFilename(const GFilename& filename); 00076 virtual ~GFilename(void); 00077 00078 // Operators 00079 GFilename& operator=(const GFilename& filename); 00080 operator std::string(void) const; 00081 00082 // Methods 00083 void clear(void); 00084 GFilename* clone(void) const; 00085 std::string classname(void) const; 00086 bool is_empty(void) const; 00087 int length(void) const; 00088 std::string url(void) const; 00089 std::string protocol(void) const; 00090 std::string path(void) const; 00091 std::string file(void) const; 00092 bool exists(void) const; 00093 bool is_fits(void) const; 00094 void remove(void) const; 00095 std::string extname(const std::string& defaultname = "") const; 00096 const std::string& expression(void) const; 00097 int extno(const int& defaultno = -1) const; 00098 int extver(const int& defaultver = 0) const; 00099 bool has_extname(void) const; 00100 bool has_extno(void) const; 00101 bool has_extver(void) const; 00102 bool has_expression(void) const; 00103 std::string print(const GChatter& chatter = NORMAL) const; 00104 00105 protected: 00106 // Protected methods 00107 void init_members(void); 00108 void copy_members(const GFilename& filename); 00109 void free_members(void); 00110 void set_filename(const std::string& filename); 00111 00112 // Protected members 00113 std::string m_filename; //!< Full file name 00114 std::string m_url; //!< File name (with stripped extension info) 00115 std::string m_protocol; //!< Access protocol 00116 std::string m_path; //!< Access path 00117 std::string m_file; //!< Name of file 00118 std::string m_extname; //!< Extension name ("": not set) 00119 int m_extno; //!< Extension number (-1: not set) 00120 int m_extver; //!< Extension version (0: not set) 00121 std::string m_expression; //!< Selection expression ("": not set) 00122 }; 00123 00124 00125 /***********************************************************************//** 00126 * @brief Implicit filename std::string convertor 00127 * 00128 * @return Full filename as std::string. 00129 * 00130 * Returns the full filename including any FITS extension as std::string. 00131 ***************************************************************************/ 00132 inline 00133 GFilename::operator std::string(void) const 00134 { 00135 return (gammalib::expand_env(m_filename)); 00136 } 00137 00138 00139 /***********************************************************************//** 00140 * @brief Return class name 00141 * 00142 * @return String containing the class name ("GFilename"). 00143 ***************************************************************************/ 00144 inline 00145 std::string GFilename::classname(void) const 00146 { 00147 return ("GFilename"); 00148 } 00149 00150 00151 /***********************************************************************//** 00152 * @brief Signal if filename is empty 00153 * 00154 * @return True if filename is empty, false otherwise. 00155 ***************************************************************************/ 00156 inline 00157 bool GFilename::is_empty(void) const 00158 { 00159 return (m_filename.empty()); 00160 } 00161 00162 00163 /***********************************************************************//** 00164 * @brief Return length of filename 00165 * 00166 * @return Length of filename. 00167 * 00168 * Returns the length of the filename, excluding any FITS extension. 00169 ***************************************************************************/ 00170 inline 00171 int GFilename::length(void) const 00172 { 00173 return (m_url.length()); 00174 } 00175 00176 00177 /***********************************************************************//** 00178 * @brief Return Uniform Resource Locator (URL) 00179 * 00180 * @return Uniform Resource Locator without FITS extension. 00181 * 00182 * Returns the Uniform Resource Locator without FITS extension. Any 00183 * environment variable in the URL string will be expanded. 00184 ***************************************************************************/ 00185 inline 00186 std::string GFilename::url(void) const 00187 { 00188 return (gammalib::expand_env(m_url)); 00189 } 00190 00191 00192 /***********************************************************************//** 00193 * @brief Return access protocol 00194 * 00195 * @return Access protocol. 00196 * 00197 * Returns the access protocol of the file. 00198 ***************************************************************************/ 00199 inline 00200 std::string GFilename::protocol(void) const 00201 { 00202 return (gammalib::expand_env(m_protocol)); 00203 } 00204 00205 00206 /***********************************************************************//** 00207 * @brief Return access path 00208 * 00209 * @return Access path. 00210 * 00211 * Returns the access path of the file. 00212 ***************************************************************************/ 00213 inline 00214 std::string GFilename::path(void) const 00215 { 00216 return (gammalib::expand_env(m_path)); 00217 } 00218 00219 00220 /***********************************************************************//** 00221 * @brief Return name of file 00222 * 00223 * @return File name. 00224 * 00225 * Returns the name of the file. 00226 ***************************************************************************/ 00227 inline 00228 std::string GFilename::file(void) const 00229 { 00230 return (gammalib::expand_env(m_file)); 00231 } 00232 00233 00234 /***********************************************************************//** 00235 * @brief Return expression name 00236 * 00237 * @return String containing file expression. 00238 ***************************************************************************/ 00239 inline 00240 const std::string& GFilename::expression(void) const 00241 { 00242 return (m_expression); 00243 } 00244 00245 00246 /***********************************************************************//** 00247 * @brief Signal if filename has an extension name 00248 * 00249 * @return True if filename has an extension name, false otherwise. 00250 ***************************************************************************/ 00251 inline 00252 bool GFilename::has_extname(void) const 00253 { 00254 return (!m_extname.empty()); 00255 } 00256 00257 00258 /***********************************************************************//** 00259 * @brief Signal if filename has an extension number 00260 * 00261 * @return True if filename has an extension number, false otherwise. 00262 ***************************************************************************/ 00263 inline 00264 bool GFilename::has_extno(void) const 00265 { 00266 return (m_extno >= 0); 00267 } 00268 00269 00270 /***********************************************************************//** 00271 * @brief Signal if filename has an extension version 00272 * 00273 * @return True if filename has an extension version, false otherwise. 00274 ***************************************************************************/ 00275 inline 00276 bool GFilename::has_extver(void) const 00277 { 00278 return (m_extver > 0); 00279 } 00280 00281 00282 /***********************************************************************//** 00283 * @brief Signal if filename has an expression 00284 * 00285 * @return True if filename has an expression, false otherwise. 00286 ***************************************************************************/ 00287 inline 00288 bool GFilename::has_expression(void) const 00289 { 00290 return (!m_expression.empty()); 00291 } 00292 00293 00294 /***********************************************************************//** 00295 * @brief String addition operator 00296 * 00297 * @param[in] filename Filename. 00298 * @param[in] string String. 00299 * @return String with filename + string. 00300 ***************************************************************************/ 00301 inline 00302 std::string operator+(const GFilename& filename, const std::string& string) 00303 { 00304 return (std::string(filename)+string); 00305 } 00306 00307 00308 /***********************************************************************//** 00309 * @brief String addition operator 00310 * 00311 * @param[in] string String. 00312 * @param[in] filename Filename. 00313 * @return String with string + filename. 00314 ***************************************************************************/ 00315 inline 00316 std::string operator+(const std::string& string, const GFilename& filename) 00317 { 00318 return (string+std::string(filename)); 00319 } 00320 00321 00322 /***********************************************************************//** 00323 * @brief Filename equality operator 00324 * 00325 * @param[in] a First filename. 00326 * @param[in] b Second filename. 00327 * @return True if filenames are equal. 00328 ***************************************************************************/ 00329 inline 00330 bool operator==(const GFilename &a, const GFilename &b) 00331 { 00332 return (std::string(a) == std::string(b)); 00333 } 00334 00335 00336 /***********************************************************************//** 00337 * @brief Filename inequality operator 00338 * 00339 * @param[in] a First filename. 00340 * @param[in] b Second filename. 00341 * @return True if filenames are not equal. 00342 ***************************************************************************/ 00343 inline 00344 bool operator!=(const GFilename &a, const GFilename &b) 00345 { 00346 return (std::string(a) != std::string(b)); 00347 } 00348 00349 #endif /* GFILENAME_HPP */