Commit 460cd77dff1ba21727c92e2ff547a3f896ba4429
1 parent
bedebe80
Add some operators to the GFilename class.
Showing
4 changed files
with
95 additions
and
37 deletions
include/GFilename.hpp
| ... | ... | @@ -61,6 +61,12 @@ |
| 61 | 61 | ***************************************************************************/ |
| 62 | 62 | class GFilename : public GBase { |
| 63 | 63 | |
| 64 | + // Friend functions | |
| 65 | + friend std::string operator+(const GFilename& filename, const std::string& string); | |
| 66 | + friend std::string operator+(const std::string& string, const GFilename& filename); | |
| 67 | + friend bool operator==(const GFilename &a, const GFilename &b); | |
| 68 | + friend bool operator!=(const GFilename &a, const GFilename &b); | |
| 69 | + | |
| 64 | 70 | public: |
| 65 | 71 | // Constructors and destructors |
| 66 | 72 | GFilename(void); |
| ... | ... | @@ -71,7 +77,7 @@ public: |
| 71 | 77 | |
| 72 | 78 | // Operators |
| 73 | 79 | GFilename& operator=(const GFilename& filename); |
| 74 | - const std::string& operator()(void) const; | |
| 80 | + operator std::string(void) const; | |
| 75 | 81 | |
| 76 | 82 | // Methods |
| 77 | 83 | void clear(void); |
| ... | ... | @@ -83,6 +89,9 @@ public: |
| 83 | 89 | //std::string protocol(void) const; |
| 84 | 90 | //std::string& path(void) const; |
| 85 | 91 | //std::string& file(void) const; |
| 92 | + //bool exists(void) const; | |
| 93 | + //bool is_fits(void) const; | |
| 94 | + //void remove(void) const; | |
| 86 | 95 | std::string extname(const std::string& defaultname = "") const; |
| 87 | 96 | const std::string& expression(void) const; |
| 88 | 97 | int extno(const int& defaultno = -1) const; |
| ... | ... | @@ -114,16 +123,16 @@ protected: |
| 114 | 123 | |
| 115 | 124 | |
| 116 | 125 | /***********************************************************************//** |
| 117 | - * @brief Filename operator | |
| 126 | + * @brief Implicit filename std::string convertor | |
| 118 | 127 | * |
| 119 | - * @return Full filename. | |
| 128 | + * @return Full filename as std::string. | |
| 120 | 129 | * |
| 121 | - * Returns the full filename including any FITS extension. | |
| 130 | + * Returns the full filename including any FITS extension as std::string. | |
| 122 | 131 | ***************************************************************************/ |
| 123 | 132 | inline |
| 124 | -const std::string& GFilename::operator()(void) const | |
| 133 | +GFilename::operator std::string(void) const | |
| 125 | 134 | { |
| 126 | - return (m_fullname); | |
| 135 | + return (gammalib::expand_env(m_fullname)); | |
| 127 | 136 | } |
| 128 | 137 | |
| 129 | 138 | |
| ... | ... | @@ -237,4 +246,60 @@ bool GFilename::has_expression(void) const |
| 237 | 246 | return (!m_expression.empty()); |
| 238 | 247 | } |
| 239 | 248 | |
| 249 | + | |
| 250 | +/***********************************************************************//** | |
| 251 | + * @brief String addition operator | |
| 252 | + * | |
| 253 | + * @param[in] filename Filename. | |
| 254 | + * @param[in] string String. | |
| 255 | + * @return String with filename + string. | |
| 256 | + ***************************************************************************/ | |
| 257 | +inline | |
| 258 | +std::string operator+(const GFilename& filename, const std::string& string) | |
| 259 | +{ | |
| 260 | + return (std::string(filename)+string); | |
| 261 | +} | |
| 262 | + | |
| 263 | + | |
| 264 | +/***********************************************************************//** | |
| 265 | + * @brief String addition operator | |
| 266 | + * | |
| 267 | + * @param[in] string String. | |
| 268 | + * @param[in] filename Filename. | |
| 269 | + * @return String with string + filename. | |
| 270 | + ***************************************************************************/ | |
| 271 | +inline | |
| 272 | +std::string operator+(const std::string& string, const GFilename& filename) | |
| 273 | +{ | |
| 274 | + return (string+std::string(filename)); | |
| 275 | +} | |
| 276 | + | |
| 277 | + | |
| 278 | +/***********************************************************************//** | |
| 279 | + * @brief Filename equality operator | |
| 280 | + * | |
| 281 | + * @param[in] a First filename. | |
| 282 | + * @param[in] b Second filename. | |
| 283 | + * @return True if filenames are equal. | |
| 284 | + ***************************************************************************/ | |
| 285 | +inline | |
| 286 | +bool operator==(const GFilename &a, const GFilename &b) | |
| 287 | +{ | |
| 288 | + return (std::string(a) == std::string(b)); | |
| 289 | +} | |
| 290 | + | |
| 291 | + | |
| 292 | +/***********************************************************************//** | |
| 293 | + * @brief Filename inequality operator | |
| 294 | + * | |
| 295 | + * @param[in] a First filename. | |
| 296 | + * @param[in] b Second filename. | |
| 297 | + * @return True if filenames are not equal. | |
| 298 | + ***************************************************************************/ | |
| 299 | +inline | |
| 300 | +bool operator!=(const GFilename &a, const GFilename &b) | |
| 301 | +{ | |
| 302 | + return (std::string(a) != std::string(b)); | |
| 303 | +} | |
| 304 | + | |
| 240 | 305 | #endif /* GFILENAME_HPP */ | ... | ... |
pyext/GFilename.i
src/fits/GFits.cpp
| ... | ... | @@ -1017,19 +1017,19 @@ void GFits::open(const GFilename& filename, const bool& create) |
| 1017 | 1017 | m_hdu.clear(); |
| 1018 | 1018 | |
| 1019 | 1019 | // Expand environment variables |
| 1020 | - std::string fname(gammalib::expand_env(filename())); | |
| 1020 | + //std::string fname(gammalib::expand_env(filename())); | |
| 1021 | 1021 | |
| 1022 | 1022 | // Don't allow opening if a file is already open |
| 1023 | 1023 | if (m_fitsfile != NULL) { |
| 1024 | 1024 | std::string msg; |
| 1025 | - if (m_filename.url() == GFilename(fname).url()) { | |
| 1026 | - msg = "FITS file \""+m_filename.url()+"\" has already been " | |
| 1025 | + if (m_filename == filename) { | |
| 1026 | + msg = "FITS file \""+m_filename+"\" has already been " | |
| 1027 | 1027 | "opened, cannot open it again."; |
| 1028 | 1028 | } |
| 1029 | 1029 | else { |
| 1030 | - msg = "A FITS file \""+m_filename.url()+"\" has already " | |
| 1030 | + msg = "A FITS file \""+m_filename+"\" has already " | |
| 1031 | 1031 | "been opened, cannot open another FITS file \""+ |
| 1032 | - fname+"\" before closing the existing one."; | |
| 1032 | + filename+"\" before closing the existing one."; | |
| 1033 | 1033 | } |
| 1034 | 1034 | throw GException::invalid_argument(G_OPEN, msg); |
| 1035 | 1035 | } |
| ... | ... | @@ -1040,12 +1040,12 @@ void GFits::open(const GFilename& filename, const bool& create) |
| 1040 | 1040 | |
| 1041 | 1041 | // Try opening FITS file with readwrite access |
| 1042 | 1042 | int status = 0; |
| 1043 | - status = __ffopen(FHANDLE(m_fitsfile), fname.c_str(), 1, &status); | |
| 1043 | + status = __ffopen(FHANDLE(m_fitsfile), filename.c_str(), 1, &status); | |
| 1044 | 1044 | |
| 1045 | 1045 | // If failed then try opening as readonly |
| 1046 | 1046 | if (status == 104 || status == 112) { |
| 1047 | 1047 | status = 0; |
| 1048 | - status = __ffopen(FHANDLE(m_fitsfile), fname.c_str(), 0, &status); | |
| 1048 | + status = __ffopen(FHANDLE(m_fitsfile), filename.c_str(), 0, &status); | |
| 1049 | 1049 | m_readwrite = false; |
| 1050 | 1050 | } |
| 1051 | 1051 | |
| ... | ... | @@ -1053,7 +1053,7 @@ void GFits::open(const GFilename& filename, const bool& create) |
| 1053 | 1053 | // FITS file now |
| 1054 | 1054 | if (create && status == 104) { |
| 1055 | 1055 | status = 0; |
| 1056 | - status = __ffinit(FHANDLE(m_fitsfile), fname.c_str(), &status); | |
| 1056 | + status = __ffinit(FHANDLE(m_fitsfile), filename.c_str(), &status); | |
| 1057 | 1057 | m_readwrite = true; |
| 1058 | 1058 | m_created = true; |
| 1059 | 1059 | } |
| ... | ... | @@ -1061,17 +1061,17 @@ void GFits::open(const GFilename& filename, const bool& create) |
| 1061 | 1061 | // Throw special exception if status=202 (keyword not found). This error |
| 1062 | 1062 | // may occur if the file is opened with an expression |
| 1063 | 1063 | if (status == 202) { |
| 1064 | - throw GException::fits_open_error(G_OPEN, fname, status, | |
| 1064 | + throw GException::fits_open_error(G_OPEN, filename, status, | |
| 1065 | 1065 | "Keyword not found when opening file."); |
| 1066 | 1066 | } |
| 1067 | 1067 | |
| 1068 | 1068 | // Throw any other error |
| 1069 | 1069 | else if (status != 0) { |
| 1070 | - throw GException::fits_open_error(G_OPEN, fname, status); | |
| 1070 | + throw GException::fits_open_error(G_OPEN, filename, status); | |
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | // Store FITS file name as GFilename object |
| 1074 | - m_filename = GFilename(fname); | |
| 1074 | + m_filename = filename; | |
| 1075 | 1075 | |
| 1076 | 1076 | // Determine number of HDUs |
| 1077 | 1077 | int num_hdu = 0; |
| ... | ... | @@ -1227,11 +1227,8 @@ void GFits::save(const bool& clobber) |
| 1227 | 1227 | ***************************************************************************/ |
| 1228 | 1228 | void GFits::saveto(const GFilename& filename, const bool& clobber) |
| 1229 | 1229 | { |
| 1230 | - // Expand environment variables | |
| 1231 | - std::string fname = gammalib::expand_env(filename()); | |
| 1232 | - | |
| 1233 | 1230 | // Create gzipped file name version |
| 1234 | - std::string gzfname = fname + ".gz"; | |
| 1231 | + std::string gzfname = filename + ".gz"; | |
| 1235 | 1232 | |
| 1236 | 1233 | // Debug header |
| 1237 | 1234 | #if defined(G_DEBUG) |
| ... | ... | @@ -1243,8 +1240,8 @@ void GFits::saveto(const GFilename& filename, const bool& clobber) |
| 1243 | 1240 | // consider here also the possibility that the file is gzipped, but |
| 1244 | 1241 | // the file name does not contain the .gz extension. |
| 1245 | 1242 | if (clobber) { |
| 1246 | - if (gammalib::file_exists(fname)) { | |
| 1247 | - std::remove(fname.c_str()); | |
| 1243 | + if (gammalib::file_exists(filename)) { | |
| 1244 | + std::remove(filename.c_str()); | |
| 1248 | 1245 | } |
| 1249 | 1246 | else { |
| 1250 | 1247 | if (gammalib::file_exists(gzfname)) { |
| ... | ... | @@ -1254,25 +1251,24 @@ void GFits::saveto(const GFilename& filename, const bool& clobber) |
| 1254 | 1251 | } |
| 1255 | 1252 | |
| 1256 | 1253 | // Otherwise, if file exists then throw an exception |
| 1257 | - else if (gammalib::file_exists(fname) || | |
| 1254 | + else if (gammalib::file_exists(filename) || | |
| 1258 | 1255 | gammalib::file_exists(gzfname)) { |
| 1259 | - throw GException::fits_file_exist(G_SAVETO, fname); | |
| 1256 | + throw GException::fits_file_exist(G_SAVETO, filename); | |
| 1260 | 1257 | } |
| 1261 | 1258 | |
| 1262 | 1259 | // Create or open FITS file |
| 1263 | - GFits new_fits; | |
| 1264 | - new_fits.open(fname, true); | |
| 1260 | + GFits fits(filename, true);; | |
| 1265 | 1261 | |
| 1266 | 1262 | // Append all headers |
| 1267 | 1263 | for (int i = 0; i < size(); ++i) { |
| 1268 | - new_fits.append(*m_hdu[i]); | |
| 1264 | + fits.append(*m_hdu[i]); | |
| 1269 | 1265 | } |
| 1270 | 1266 | |
| 1271 | - // Save new FITS file | |
| 1272 | - new_fits.save(); | |
| 1267 | + // Save FITS file | |
| 1268 | + fits.save(); | |
| 1273 | 1269 | |
| 1274 | - // Close new FITS file | |
| 1275 | - new_fits.close(); | |
| 1270 | + // Close FITS file | |
| 1271 | + fits.close(); | |
| 1276 | 1272 | |
| 1277 | 1273 | // Debug trailer |
| 1278 | 1274 | #if defined(G_DEBUG) | ... | ... |
src/obs/GGti.cpp
| ... | ... | @@ -830,7 +830,7 @@ void GGti::write(GXmlElement& xml) const |
| 830 | 830 | if (!m_xml_filename.is_empty()) { |
| 831 | 831 | |
| 832 | 832 | // Write "file" attribute |
| 833 | - par->attribute("file", m_xml_filename()); | |
| 833 | + par->attribute("file", m_xml_filename); | |
| 834 | 834 | |
| 835 | 835 | // Write GTI file |
| 836 | 836 | save(m_xml_filename, true); |
| ... | ... | @@ -1059,7 +1059,7 @@ std::string GGti::print(const GChatter& chatter) const |
| 1059 | 1059 | // Optionally append XML filename |
| 1060 | 1060 | if (!m_xml_filename.is_empty()) { |
| 1061 | 1061 | result.append("\n"+gammalib::parformat("File name")); |
| 1062 | - result.append(m_xml_filename()); | |
| 1062 | + result.append(m_xml_filename); | |
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | } // endif: chatter was not silent | ... | ... |