GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GFilename.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GFilename.hpp - Filename class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2015-2018 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GFilename.hpp
23  * @brief Filename class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GFILENAME_HPP
28 #define GFILENAME_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 #include "GTools.hpp"
34 
35 
36 /***********************************************************************//**
37  * @class GFilename
38  *
39  * @brief Filename class
40  *
41  * This class handles filenames. A filename is a string composed of an
42  * optional protocol (http:, ftp:, file:), an absolute or relative access
43  * path, a file, and optionally a FITS extension. Examples of valid file
44  * names are
45  *
46  * myfits.fits
47  * myfile.fits[EVENTS]
48  * ./data/myfile.fits
49  * ~/data/myfile.fits
50  * /home/myuser/data/myfile.fits
51  * http://www.irap.omp.eu/data/myfile.fits
52  * ftp://www.irap.omp.eu/data/myfile.fits
53  * file:///home/myuser/data/myfile.fits
54  *
55  * A filename without the optional FITS extension is called a Uniform
56  * Resource Locator (URL) an is accessed using the url() method. The URL
57  * can be decomposed into the protocol, access path and the filename using
58  * the protocol(), path(), and file().
59  *
60  * The FITS extension is implemente using the GFitsExtension class.
61  ***************************************************************************/
62 class GFilename : public GBase {
63 
64  // Friend functions
65  friend std::string operator+(const GFilename& filename,
66  const std::string& string);
67  friend std::string operator+(const std::string& string,
68  const GFilename& filename);
69  friend bool operator==(const GFilename &a, const GFilename &b);
70  friend bool operator!=(const GFilename &a, const GFilename &b);
71 
72 public:
73  // Constructors and destructors
74  GFilename(void);
75  GFilename(const std::string& filename);
76  GFilename(const char* filename);
77  GFilename(const GFilename& filename);
78  virtual ~GFilename(void);
79 
80  // Operators
81  GFilename& operator=(const GFilename& filename);
82  operator std::string(void) const;
83 
84  // Methods
85  void clear(void);
86  GFilename* clone(void) const;
87  std::string classname(void) const;
88  bool is_empty(void) const;
89  int length(void) const;
90  std::string url(void) const;
91  std::string protocol(void) const;
92  std::string path(void) const;
93  std::string file(void) const;
94  std::string type(void) const;
95  bool exists(void) const;
96  bool is_fits(void) const;
97  void remove(void) const;
98  std::string extname(const std::string& defaultname = "") const;
99  const std::string& expression(void) const;
100  int extno(const int& defaultno = -1) const;
101  int extver(const int& defaultver = 0) const;
102  bool has_extname(void) const;
103  bool has_extno(void) const;
104  bool has_extver(void) const;
105  bool has_expression(void) const;
106  std::string print(const GChatter& chatter = NORMAL) const;
107 
108 protected:
109  // Protected methods
110  void init_members(void);
111  void copy_members(const GFilename& filename);
112  void free_members(void);
113  void set_filename(const std::string& filename);
114 
115  // Protected members
116  std::string m_filename; //!< Full file name
117  std::string m_url; //!< File name (with stripped extension info)
118  std::string m_protocol; //!< Access protocol
119  std::string m_path; //!< Access path
120  std::string m_file; //!< Name of file
121  std::string m_extname; //!< Extension name ("": not set)
122  int m_extno; //!< Extension number (-1: not set)
123  int m_extver; //!< Extension version (0: not set)
124  std::string m_expression; //!< Selection expression ("": not set)
125 };
126 
127 
128 /***********************************************************************//**
129  * @brief Implicit filename std::string convertor
130  *
131  * @return Full filename as std::string.
132  *
133  * Returns the full filename including any FITS extension as std::string.
134  ***************************************************************************/
135 inline
136 GFilename::operator std::string(void) const
137 {
138  return (gammalib::expand_env(m_filename));
139 }
140 
141 
142 /***********************************************************************//**
143  * @brief Return class name
144  *
145  * @return String containing the class name ("GFilename").
146  ***************************************************************************/
147 inline
148 std::string GFilename::classname(void) const
149 {
150  return ("GFilename");
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Signal if filename is empty
156  *
157  * @return True if filename is empty, false otherwise.
158  ***************************************************************************/
159 inline
160 bool GFilename::is_empty(void) const
161 {
162  return (m_filename.empty());
163 }
164 
165 
166 /***********************************************************************//**
167  * @brief Return length of filename
168  *
169  * @return Length of filename.
170  *
171  * Returns the length of the filename, excluding any FITS extension.
172  ***************************************************************************/
173 inline
174 int GFilename::length(void) const
175 {
176  return ((int)m_url.length());
177 }
178 
179 
180 /***********************************************************************//**
181  * @brief Return Uniform Resource Locator (URL)
182  *
183  * @return Uniform Resource Locator without FITS extension.
184  *
185  * Returns the Uniform Resource Locator without FITS extension. Any
186  * environment variable in the URL string will be expanded.
187  ***************************************************************************/
188 inline
189 std::string GFilename::url(void) const
190 {
191  return (gammalib::expand_env(m_url));
192 }
193 
194 
195 /***********************************************************************//**
196  * @brief Return access protocol
197  *
198  * @return Access protocol.
199  *
200  * Returns the access protocol of the file.
201  ***************************************************************************/
202 inline
203 std::string GFilename::protocol(void) const
204 {
206 }
207 
208 
209 /***********************************************************************//**
210  * @brief Return access path
211  *
212  * @return Access path.
213  *
214  * Returns the access path of the file.
215  ***************************************************************************/
216 inline
217 std::string GFilename::path(void) const
218 {
219  return (gammalib::expand_env(m_path));
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Return name of file
225  *
226  * @return File name.
227  *
228  * Returns the name of the file.
229  ***************************************************************************/
230 inline
231 std::string GFilename::file(void) const
232 {
233  return (gammalib::expand_env(m_file));
234 }
235 
236 
237 /***********************************************************************//**
238  * @brief Return expression name
239  *
240  * @return String containing file expression.
241  ***************************************************************************/
242 inline
243 const std::string& GFilename::expression(void) const
244 {
245  return (m_expression);
246 }
247 
248 
249 /***********************************************************************//**
250  * @brief Signal if filename has an extension name
251  *
252  * @return True if filename has an extension name, false otherwise.
253  ***************************************************************************/
254 inline
255 bool GFilename::has_extname(void) const
256 {
257  return (!m_extname.empty());
258 }
259 
260 
261 /***********************************************************************//**
262  * @brief Signal if filename has an extension number
263  *
264  * @return True if filename has an extension number, false otherwise.
265  ***************************************************************************/
266 inline
267 bool GFilename::has_extno(void) const
268 {
269  return (m_extno >= 0);
270 }
271 
272 
273 /***********************************************************************//**
274  * @brief Signal if filename has an extension version
275  *
276  * @return True if filename has an extension version, false otherwise.
277  ***************************************************************************/
278 inline
279 bool GFilename::has_extver(void) const
280 {
281  return (m_extver > 0);
282 }
283 
284 
285 /***********************************************************************//**
286  * @brief Signal if filename has an expression
287  *
288  * @return True if filename has an expression, false otherwise.
289  ***************************************************************************/
290 inline
292 {
293  return (!m_expression.empty());
294 }
295 
296 
297 /***********************************************************************//**
298  * @brief String addition operator
299  *
300  * @param[in] filename Filename.
301  * @param[in] string String.
302  * @return String with filename + string.
303  ***************************************************************************/
304 inline
305 std::string operator+(const GFilename& filename, const std::string& string)
306 {
307  return (std::string(filename)+string);
308 }
309 
310 
311 /***********************************************************************//**
312  * @brief String addition operator
313  *
314  * @param[in] string String.
315  * @param[in] filename Filename.
316  * @return String with string + filename.
317  ***************************************************************************/
318 inline
319 std::string operator+(const std::string& string, const GFilename& filename)
320 {
321  return (string+std::string(filename));
322 }
323 
324 
325 /***********************************************************************//**
326  * @brief Filename equality operator
327  *
328  * @param[in] a First filename.
329  * @param[in] b Second filename.
330  * @return True if filenames are equal.
331  ***************************************************************************/
332 inline
333 bool operator==(const GFilename &a, const GFilename &b)
334 {
335  return (std::string(a) == std::string(b));
336 }
337 
338 
339 /***********************************************************************//**
340  * @brief Filename inequality operator
341  *
342  * @param[in] a First filename.
343  * @param[in] b Second filename.
344  * @return True if filenames are not equal.
345  ***************************************************************************/
346 inline
347 bool operator!=(const GFilename &a, const GFilename &b)
348 {
349  return (std::string(a) != std::string(b));
350 }
351 
352 #endif /* GFILENAME_HPP */
std::string file(void) const
Return name of file.
Definition: GFilename.hpp:231
std::string protocol(void) const
Return access protocol.
Definition: GFilename.hpp:203
void free_members(void)
Delete class members.
Definition: GFilename.cpp:583
bool has_extno(void) const
Signal if filename has an extension number.
Definition: GFilename.hpp:267
bool has_expression(void) const
Signal if filename has an expression.
Definition: GFilename.hpp:291
bool has_extver(void) const
Signal if filename has an extension version.
Definition: GFilename.hpp:279
void copy_members(const GFilename &filename)
Copy class members.
Definition: GFilename.cpp:562
std::string m_protocol
Access protocol.
Definition: GFilename.hpp:118
std::string m_expression
Selection expression (&quot;&quot;: not set)
Definition: GFilename.hpp:124
int m_extver
Extension version (0: not set)
Definition: GFilename.hpp:123
const std::string & expression(void) const
Return expression name.
Definition: GFilename.hpp:243
std::string m_path
Access path.
Definition: GFilename.hpp:119
bool is_empty(void) const
Signal if filename is empty.
Definition: GFilename.hpp:160
int extver(const int &defaultver=0) const
Return extension version number.
Definition: GFilename.cpp:435
Definition of interface for all GammaLib classes.
bool has_extname(void) const
Signal if filename has an extension name.
Definition: GFilename.hpp:255
int length(void) const
Return length of filename.
Definition: GFilename.hpp:174
std::string extname(const std::string &defaultname="") const
Return extension name.
Definition: GFilename.cpp:385
void set_filename(const std::string &filename)
Set file name.
Definition: GFilename.cpp:606
Gammalib tools definition.
void init_members(void)
Initialise class members.
Definition: GFilename.cpp:539
GFilename * clone(void) const
Clone file name.
Definition: GFilename.cpp:206
std::string m_filename
Full file name.
Definition: GFilename.hpp:116
GArf operator+(const GArf &a, const GArf &b)
Auxiliary Response File addition operator friend.
Definition: GArf.hpp:293
std::string m_url
File name (with stripped extension info)
Definition: GFilename.hpp:117
GFilename & operator=(const GFilename &filename)
Assignment operator.
Definition: GFilename.cpp:158
virtual ~GFilename(void)
Destructor.
Definition: GFilename.cpp:136
std::string path(void) const
Return access path.
Definition: GFilename.hpp:217
int m_extno
Extension number (-1: not set)
Definition: GFilename.hpp:122
GFilename(void)
Void constructor.
Definition: GFilename.cpp:65
friend std::string operator+(const GFilename &filename, const std::string &string)
String addition operator.
Definition: GFilename.hpp:305
friend bool operator!=(const GFilename &a, const GFilename &b)
Filename inequality operator.
Definition: GFilename.hpp:347
Filename class.
Definition: GFilename.hpp:62
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
std::string m_extname
Extension name (&quot;&quot;: not set)
Definition: GFilename.hpp:121
bool exists(void) const
Checks whether file exists.
Definition: GFilename.cpp:223
GChatter
Definition: GTypemaps.hpp:33
bool is_fits(void) const
Checks whether file is a FITS file.
Definition: GFilename.cpp:313
std::string type(void) const
Return file type.
Definition: GFilename.cpp:264
std::string url(void) const
Return Uniform Resource Locator (URL)
Definition: GFilename.hpp:189
void clear(void)
Clear file name.
Definition: GFilename.cpp:188
std::string classname(void) const
Return class name.
Definition: GFilename.hpp:148
int extno(const int &defaultno=-1) const
Return extension number.
Definition: GFilename.cpp:410
std::string expand_env(const std::string &arg)
Expand environment variables in string.
Definition: GTools.cpp:214
friend bool operator==(const GFilename &a, const GFilename &b)
Filename equality operator.
Definition: GFilename.hpp:333
bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition: GEnergy.hpp:297
bool operator!=(const GEbounds &a, const GEbounds &b)
Energy boundaries inequality operator friend.
Definition: GEbounds.hpp:213
std::string m_file
Name of file.
Definition: GFilename.hpp:120
std::string print(const GChatter &chatter=NORMAL) const
Print file name information.
Definition: GFilename.cpp:456