GammaLib 2.0.0
Loading...
Searching...
No Matches
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 ***************************************************************************/
62class 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
72public:
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
108protected:
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 ***************************************************************************/
135inline
136GFilename::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 ***************************************************************************/
147inline
148std::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 ***************************************************************************/
159inline
160bool 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 ***************************************************************************/
173inline
174int 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 ***************************************************************************/
188inline
189std::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 ***************************************************************************/
202inline
203std::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 ***************************************************************************/
216inline
217std::string GFilename::path(void) const
218{
220}
221
222
223/***********************************************************************//**
224 * @brief Return name of file
225 *
226 * @return File name.
227 *
228 * Returns the name of the file.
229 ***************************************************************************/
230inline
231std::string GFilename::file(void) const
232{
234}
235
236
237/***********************************************************************//**
238 * @brief Return expression name
239 *
240 * @return String containing file expression.
241 ***************************************************************************/
242inline
243const 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 ***************************************************************************/
254inline
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 ***************************************************************************/
266inline
267bool 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 ***************************************************************************/
278inline
279bool 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 ***************************************************************************/
290inline
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 ***************************************************************************/
304inline
305std::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 ***************************************************************************/
318inline
319std::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 ***************************************************************************/
332inline
333bool 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 ***************************************************************************/
346inline
347bool operator!=(const GFilename &a, const GFilename &b)
348{
349 return (std::string(a) != std::string(b));
350}
351
352#endif /* GFILENAME_HPP */
Definition of interface for all GammaLib classes.
std::string operator+(const GFilename &filename, const std::string &string)
String addition operator.
bool operator==(const GFilename &a, const GFilename &b)
Filename equality operator.
bool operator!=(const GFilename &a, const GFilename &b)
Filename inequality operator.
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Filename class.
Definition GFilename.hpp:62
std::string print(const GChatter &chatter=NORMAL) const
Print file name information.
GFilename * clone(void) const
Clone file name.
GFilename & operator=(const GFilename &filename)
Assignment operator.
void copy_members(const GFilename &filename)
Copy class members.
int m_extver
Extension version (0: not set)
std::string path(void) const
Return access path.
std::string m_path
Access path.
void free_members(void)
Delete class members.
std::string classname(void) const
Return class name.
std::string file(void) const
Return name of file.
int length(void) const
Return length of filename.
std::string m_file
Name of file.
bool has_expression(void) const
Signal if filename has an expression.
friend std::string operator+(const GFilename &filename, const std::string &string)
String addition operator.
std::string type(void) const
Return file type.
bool has_extname(void) const
Signal if filename has an extension name.
bool is_fits(void) const
Checks whether file is a FITS file.
GFilename(void)
Void constructor.
Definition GFilename.cpp:65
void set_filename(const std::string &filename)
Set file name.
bool has_extno(void) const
Signal if filename has an extension number.
std::string m_url
File name (with stripped extension info)
std::string url(void) const
Return Uniform Resource Locator (URL)
void remove(void) const
Remove file from disk.
std::string extname(const std::string &defaultname="") const
Return extension name.
friend bool operator==(const GFilename &a, const GFilename &b)
Filename equality operator.
bool exists(void) const
Checks whether file exists.
std::string m_filename
Full file name.
int extno(const int &defaultno=-1) const
Return extension number.
int m_extno
Extension number (-1: not set)
const std::string & expression(void) const
Return expression name.
std::string protocol(void) const
Return access protocol.
std::string m_extname
Extension name ("": not set)
int extver(const int &defaultver=0) const
Return extension version number.
void init_members(void)
Initialise class members.
bool has_extver(void) const
Signal if filename has an extension version.
std::string m_protocol
Access protocol.
bool is_empty(void) const
Signal if filename is empty.
std::string m_expression
Selection expression ("": not set)
virtual ~GFilename(void)
Destructor.
friend bool operator!=(const GFilename &a, const GFilename &b)
Filename inequality operator.
void clear(void)
Clear file name.
std::string expand_env(const std::string &arg)
Expand environment variables in string.
Definition GTools.cpp:214