Zipios++
filepath.h
Go to the documentation of this file.
1#ifndef FILEPATH_H
2#define FILEPATH_H
3
4#include "zipios++/zipios-config.h"
5
6#include <stdexcept>
7#include <string>
8
9namespace zipios {
10
11using namespace std ;
12
18class FilePath {
19public:
25 FilePath( const string &path = "", bool check_exists = false ) ;
26
27 inline FilePath &operator= ( const string &rhs ) ;
28
29 inline operator string() const ;
30
33 inline FilePath operator+ ( const FilePath &name ) const ;
34
37 inline FilePath filename() const ;
38
39
41 inline bool exists() const ;
42
44 inline bool isRegular() const ;
45
47 inline bool isDirectory() const ;
48
51 inline bool isCharSpecial() const ;
52
55 inline bool isBlockSpecial() const ;
56
58 inline bool isSocket() const ;
59
61 inline bool isFifo() const ;
62
63protected:
64
66 inline void pruneTrailingSeparator() ;
67
73 void check() const ;
74
75 static const char _separator;
76
77 // FIXME: Should be bitfield
78 mutable bool _checked ;
79 mutable bool _exists ;
80 mutable bool _is_reg ;
81 mutable bool _is_dir ;
82 mutable bool _is_char ;
83 mutable bool _is_block ;
84 mutable bool _is_socket ;
85 mutable bool _is_fifo ;
86 string _path ;
87};
88
89
90//
91// Inline member functions
92//
93
94FilePath &FilePath::operator= ( const string &rhs ) {
95 _path = rhs ;
97 return *this ;
98}
99
101 if ( _path.size() > 0 )
102 if ( _path[ _path.size() -1 ] == _separator )
103 _path.erase( _path.size() - 1 ) ;
104}
105
106FilePath::operator string() const {
107 return _path ;
108}
109
110
112 if ( _path.size() > 0 )
113 return _path + _separator + name._path ;
114 else
115 return name._path ;
116}
117
118
120 string::size_type pos ;
121 pos = _path.find_last_of( _separator ) ;
122 if ( pos != string::npos )
123 return _path.substr( pos + 1);
124 else
125 return _path ;
126}
127
128
129bool FilePath::exists() const {
130 if ( ! _checked )
131 check() ;
132 return _exists ;
133}
134
135
137 if ( ! _checked )
138 check() ;
139 return _is_reg ;
140}
141
142
144 if ( ! _checked )
145 check() ;
146 return _is_dir ;
147}
148
149
151 if ( ! _checked )
152 check() ;
153 return _is_char ;
154}
155
156
158 if ( ! _checked )
159 check() ;
160 return _is_block ;
161}
162
163
164bool FilePath::isSocket() const {
165 if ( ! _checked )
166 check() ;
167 return _is_socket ;
168}
169
170
171bool FilePath::isFifo() const {
172 if ( ! _checked )
173 check() ;
174 return _is_fifo ;
175}
176
177
178} // namespace
179#endif
180
185/*
186 Zipios++ - a small C++ library that provides easy access to .zip files.
187 Copyright (C) 2000 Thomas Søndergaard
188
189 This library is free software; you can redistribute it and/or
190 modify it under the terms of the GNU Lesser General Public
191 License as published by the Free Software Foundation; either
192 version 2 of the License, or (at your option) any later version.
193
194 This library is distributed in the hope that it will be useful,
195 but WITHOUT ANY WARRANTY; without even the implied warranty of
196 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
197 Lesser General Public License for more details.
198
199 You should have received a copy of the GNU Lesser General Public
200 License along with this library; if not, write to the Free Software
201 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
202*/
FilePath represents a path to a file or directory name.
Definition filepath.h:18
bool isFifo() const
Definition filepath.h:171
void check() const
This function sets _checked to true, stats the path, to see if it exists and to determine what type o...
Definition filepath.cpp:27
bool isCharSpecial() const
Definition filepath.h:150
bool isBlockSpecial() const
Definition filepath.h:157
void pruneTrailingSeparator()
Prunes the trailing separator of a specified path.
Definition filepath.h:100
bool isDirectory() const
Definition filepath.h:143
bool exists() const
Definition filepath.h:129
FilePath filename() const
Returns filename of the FilePath object by pruning the path off.
Definition filepath.h:119
FilePath operator+(const FilePath &name) const
Concatenates FilePath objects.
Definition filepath.h:111
bool isRegular() const
Definition filepath.h:136
bool isSocket() const
Definition filepath.h:164