00001 #ifndef FILEPATH_H 00002 #define FILEPATH_H 00003 00004 #include "zipios-config.h" 00005 00006 #include <stdexcept> 00007 #include <string> 00008 00009 namespace zipios { 00010 00011 using namespace std ; 00012 00018 class FilePath { 00019 public: 00025 FilePath( const string &path = "", bool check_exists = false ) ; 00026 00027 inline FilePath &operator= ( const string &rhs ) ; 00028 00029 inline operator string() const ; 00030 00033 inline FilePath operator+ ( const FilePath &name ) const ; 00034 00037 inline FilePath filename() const ; 00038 00039 00041 inline bool exists() const ; 00042 00044 inline bool isRegular() const ; 00045 00047 inline bool isDirectory() const ; 00048 00051 inline bool isCharSpecial() const ; 00052 00055 inline bool isBlockSpecial() const ; 00056 00058 inline bool isSocket() const ; 00059 00061 inline bool isFifo() const ; 00062 00063 protected: 00064 00066 inline void pruneTrailingSeparator() ; 00067 00073 void check() const ; 00074 00075 static const char _separator; 00076 00077 // FIXME: Should be bitfield 00078 mutable bool _checked ; 00079 mutable bool _exists ; 00080 mutable bool _is_reg ; 00081 mutable bool _is_dir ; 00082 mutable bool _is_char ; 00083 mutable bool _is_block ; 00084 mutable bool _is_socket ; 00085 mutable bool _is_fifo ; 00086 string _path ; 00087 }; 00088 00089 00090 // 00091 // Inline member functions 00092 // 00093 00094 FilePath &FilePath::operator= ( const string &rhs ) { 00095 _path = rhs ; 00096 pruneTrailingSeparator() ; 00097 return *this ; 00098 } 00099 00100 void FilePath::pruneTrailingSeparator() { 00101 if ( _path.size() > 0 ) 00102 if ( _path[ _path.size() -1 ] == _separator ) 00103 _path.erase( _path.size() - 1 ) ; 00104 } 00105 00106 FilePath::operator string() const { 00107 return _path ; 00108 } 00109 00110 00111 FilePath FilePath::operator+ ( const FilePath &name ) const { 00112 if ( _path.size() > 0 ) 00113 return _path + _separator + name._path ; 00114 else 00115 return name._path ; 00116 } 00117 00118 00119 FilePath FilePath::filename() const { 00120 string::size_type pos ; 00121 pos = _path.find_last_of( _separator ) ; 00122 if ( pos != string::npos ) 00123 return _path.substr( pos + 1); 00124 else 00125 return _path ; 00126 } 00127 00128 00129 bool FilePath::exists() const { 00130 if ( ! _checked ) 00131 check() ; 00132 return _exists ; 00133 } 00134 00135 00136 bool FilePath::isRegular() const { 00137 if ( ! _checked ) 00138 check() ; 00139 return _is_reg ; 00140 } 00141 00142 00143 bool FilePath::isDirectory() const { 00144 if ( ! _checked ) 00145 check() ; 00146 return _is_dir ; 00147 } 00148 00149 00150 bool FilePath::isCharSpecial() const { 00151 if ( ! _checked ) 00152 check() ; 00153 return _is_char ; 00154 } 00155 00156 00157 bool FilePath::isBlockSpecial() const { 00158 if ( ! _checked ) 00159 check() ; 00160 return _is_block ; 00161 } 00162 00163 00164 bool FilePath::isSocket() const { 00165 if ( ! _checked ) 00166 check() ; 00167 return _is_socket ; 00168 } 00169 00170 00171 bool FilePath::isFifo() const { 00172 if ( ! _checked ) 00173 check() ; 00174 return _is_fifo ; 00175 } 00176 00177 00178 } // namespace 00179 #endif 00180 00185 /* 00186 Zipios++ - a small C++ library that provides easy access to .zip files. 00187 Copyright (C) 2000 Thomas Søndergaard 00188 00189 This library is free software; you can redistribute it and/or 00190 modify it under the terms of the GNU Lesser General Public 00191 License as published by the Free Software Foundation; either 00192 version 2 of the License, or (at your option) any later version. 00193 00194 This library is distributed in the hope that it will be useful, 00195 but WITHOUT ANY WARRANTY; without even the implied warranty of 00196 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00197 Lesser General Public License for more details. 00198 00199 You should have received a copy of the GNU Lesser General Public 00200 License along with this library; if not, write to the Free Software 00201 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00202 */