]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/zipios++/filepath.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / zipios++ / filepath.h
1 #ifndef FILEPATH_H
2 #define FILEPATH_H
3
4 #include "zipios++/zipios-config.h"
5
6 #include <stdexcept>
7 #include <string>
8
9 namespace zipios {
10
11 using namespace std    ;
12
13 /** FilePath represents a path to a file or directory name. FilePath has
14     member functions to check if the file path is a valid file system entity,
15     and to check what kind of file system entity it is, e.g. is it a file, a 
16     directory, a pipe etc.
17 */
18 class FilePath {
19 public:
20   /** Constructor.
21       @param path A string representation of the path.
22       @param check_exists If true is specified the constructor will
23       check the existence and type of the path immediately, instead of
24       deferring that task until it is needed. */
25   FilePath( const string &path = "", bool check_exists = false ) ;
26
27   inline FilePath &operator= ( const string &rhs ) ;
28
29   inline operator string() const ;
30
31   inline std::string asString() const ;
32
33   /** Concatenates FilePath objects. A file separator is inserted
34       if appropriate. */
35   inline FilePath operator+ ( const FilePath &name ) const ;
36
37   /** Returns filename of the FilePath object by pruning the path
38       off. */
39   inline FilePath filename() const ;
40
41
42   /** @return true If the path is a valid file system entity. */
43   inline bool exists()         const ;
44
45   /** @return true if the path is a regular file. */
46   inline bool isRegular()      const ;
47
48   /** @return true if the path is a directory. */
49   inline bool isDirectory()    const ;
50
51   /** @return true if the path is character special (a character
52       device file).  */
53   inline bool isCharSpecial()  const ;
54
55   /** @return true if the path is block special (a block device
56       file). */
57   inline bool isBlockSpecial() const ;
58
59   /** @return true if the path is a socket. */
60   inline bool isSocket()       const ;
61
62   /** @return true if the path is a Fifo (a pipe). */
63   inline bool isFifo()         const ;
64
65 protected:
66
67   /** Prunes the trailing separator of a specified path. */
68   inline void pruneTrailingSeparator() ;
69
70   /** This function sets _checked to true, stats the path, to see if
71   it exists and to determine what type of file it is. All the query
72   functions check if _checked is true, and if it isn't they call
73   check(). This means stat'ing is deferred until it becomes
74   necessary. */
75   void check() const ;
76
77   static const char _separator;
78
79   // FIXME: Should be bitfield
80   mutable bool   _checked   ;
81   mutable bool   _exists    ;
82   mutable bool   _is_reg    ;
83   mutable bool   _is_dir    ;
84   mutable bool   _is_char   ;
85   mutable bool   _is_block  ;
86   mutable bool   _is_socket ;
87   mutable bool   _is_fifo   ;
88   string _path              ;
89 };
90
91
92 //
93 // Inline member functions
94 //
95
96 FilePath &FilePath::operator= ( const string &rhs ) {
97   _path = rhs ;
98   pruneTrailingSeparator() ;
99   return *this ;
100 }
101
102 void FilePath::pruneTrailingSeparator() {
103   if ( _path.size() > 0 )
104     if ( _path[ _path.size() -1 ] == _separator )
105       _path.erase( _path.size() - 1 ) ; 
106 }
107
108 FilePath::operator string() const { 
109   return _path ;
110
111
112 std::string FilePath::asString() const {
113   return _path ;
114 }
115
116
117 FilePath FilePath::operator+ ( const FilePath &name ) const { 
118   if ( _path.size() > 0 )
119     return _path + _separator + name._path ; 
120   else
121     return name._path ;
122 }
123
124
125 FilePath FilePath::filename() const {
126   string::size_type pos ;
127   pos = _path.find_last_of( _separator ) ;
128   if ( pos != string::npos )
129     return _path.substr( pos + 1);
130   else 
131     return _path ;
132 }
133
134
135 bool FilePath::exists() const {
136   if ( ! _checked )
137     check() ;
138   return _exists ;
139 }
140
141
142 bool FilePath::isRegular() const {
143   if ( ! _checked )
144     check() ;
145   return _is_reg ;
146 }
147
148
149 bool FilePath::isDirectory() const {
150   if ( ! _checked )
151     check() ;
152   return _is_dir ;
153 }
154
155
156 bool FilePath::isCharSpecial() const {
157   if ( ! _checked )
158     check() ;
159   return _is_char ;
160 }
161
162
163 bool FilePath::isBlockSpecial() const {
164   if ( ! _checked )
165     check() ;
166   return _is_block ;
167 }
168
169
170 bool FilePath::isSocket() const {
171   if ( ! _checked )
172     check() ;
173   return _is_socket ;
174 }
175
176
177 bool FilePath::isFifo() const {
178   if ( ! _checked )
179     check() ;
180   return _is_fifo ;
181 }
182
183
184 } // namespace
185 #endif
186
187 /** \file
188     Header file that defines FilePath.
189 */
190
191 /*
192   Zipios++ - a small C++ library that provides easy access to .zip files.
193   Copyright (C) 2000  Thomas Søndergaard
194   
195   This library is free software; you can redistribute it and/or
196   modify it under the terms of the GNU Lesser General Public
197   License as published by the Free Software Foundation; either
198   version 2 of the License, or (at your option) any later version.
199   
200   This library is distributed in the hope that it will be useful,
201   but WITHOUT ANY WARRANTY; without even the implied warranty of
202   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
203   Lesser General Public License for more details.
204   
205   You should have received a copy of the GNU Lesser General Public
206   License along with this library; if not, write to the Free Software
207   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
208 */