|
1 /****************************************************************************** |
|
2 * |
|
3 * |
|
4 * |
|
5 * Copyright (C) 1997-2001 by Dimitri van Heesch. |
|
6 * |
|
7 * Permission to use, copy, modify, and distribute this software and its |
|
8 * documentation under the terms of the GNU General Public License is hereby |
|
9 * granted. No representations are made about the suitability of this software |
|
10 * for any purpose. It is provided "as is" without express or implied warranty. |
|
11 * See the GNU General Public License for more details. |
|
12 * |
|
13 * Documents produced by Doxygen are derivative works derived from the |
|
14 * input used in their production; they are not affected by this license. |
|
15 * |
|
16 * Based on qfileinfo_unix.cpp |
|
17 * |
|
18 * Copyright (C) 1992-2000 Trolltech AS. |
|
19 */ |
|
20 |
|
21 #include "qglobal.h" |
|
22 |
|
23 #include "qfileinfo.h" |
|
24 #include "qfiledefs_p.h" |
|
25 #include "qdatetime.h" |
|
26 #include "qdir.h" |
|
27 |
|
28 void QFileInfo::slashify( QString& n ) |
|
29 { |
|
30 for ( int i=0; i<(int)n.length(); i++ ) |
|
31 { |
|
32 if ( n[i] == '\\' ) |
|
33 n[i] = '/'; |
|
34 } |
|
35 } |
|
36 |
|
37 void QFileInfo::makeAbs( QString & ) |
|
38 { |
|
39 // TODO: what to do here? |
|
40 return; |
|
41 } |
|
42 |
|
43 extern bool qt_file_access( const QString& fn, int t ); |
|
44 |
|
45 /*! |
|
46 Returns TRUE if we are pointing to a real file. |
|
47 \sa isDir(), isSymLink() |
|
48 */ |
|
49 bool QFileInfo::isFile() const |
|
50 { |
|
51 if ( !fic || !cache ) |
|
52 doStat(); |
|
53 return fic ? (fic->st.st_mode & STAT_MASK) == STAT_REG : FALSE; |
|
54 } |
|
55 |
|
56 /*! |
|
57 Returns TRUE if we are pointing to a directory or a symbolic link to |
|
58 a directory. |
|
59 \sa isFile(), isSymLink() |
|
60 */ |
|
61 |
|
62 bool QFileInfo::isDir() const |
|
63 { |
|
64 if ( !fic || !cache ) |
|
65 doStat(); |
|
66 return fic ? (fic->st.st_mode & STAT_MASK) == STAT_DIR : FALSE; |
|
67 } |
|
68 |
|
69 /*! |
|
70 Returns TRUE if we are pointing to a symbolic link. |
|
71 \sa isFile(), isDir(), readLink() |
|
72 */ |
|
73 |
|
74 bool QFileInfo::isSymLink() const |
|
75 { |
|
76 if ( !fic || !cache ) |
|
77 doStat(); |
|
78 return fic ? fic->isSymLink : FALSE; |
|
79 } |
|
80 |
|
81 |
|
82 /*! |
|
83 Returns the name a symlink points to, or a null QString if the |
|
84 object does not refer to a symbolic link. |
|
85 |
|
86 This name may not represent an existing file; it is only a string. |
|
87 QFileInfo::exists() returns TRUE if the symlink points to an |
|
88 existing file. |
|
89 |
|
90 \sa exists(), isSymLink(), isDir(), isFile() |
|
91 */ |
|
92 |
|
93 QString QFileInfo::readLink() const |
|
94 { |
|
95 QString r; |
|
96 return r; |
|
97 } |
|
98 |
|
99 static const uint nobodyID = (uint) -2; |
|
100 |
|
101 /*! |
|
102 Returns the owner of the file. |
|
103 |
|
104 On systems where files do not have owners this function returns |
|
105 a null string. |
|
106 |
|
107 Note that this function can be time-consuming under UNIX. (in the order |
|
108 of milliseconds on a 486 DX2/66 running Linux). |
|
109 |
|
110 \sa ownerId(), group(), groupId() |
|
111 */ |
|
112 |
|
113 QString QFileInfo::owner() const |
|
114 { |
|
115 return QString::null; |
|
116 } |
|
117 |
|
118 /*! |
|
119 Returns the id of the owner of the file. |
|
120 |
|
121 On systems where files do not have owners this function returns ((uint) -2). |
|
122 |
|
123 \sa owner(), group(), groupId() |
|
124 */ |
|
125 |
|
126 uint QFileInfo::ownerId() const |
|
127 { |
|
128 return (uint)-2; |
|
129 } |
|
130 |
|
131 /*! |
|
132 Returns the group the file belongs to. |
|
133 |
|
134 On systems where files do not have groups this function always |
|
135 returns 0. |
|
136 |
|
137 Note that this function can be time-consuming under UNIX (in the order of |
|
138 milliseconds on a 486 DX2/66 running Linux). |
|
139 |
|
140 \sa groupId(), owner(), ownerId() |
|
141 */ |
|
142 |
|
143 QString QFileInfo::group() const |
|
144 { |
|
145 return QString::null; |
|
146 } |
|
147 |
|
148 /*! |
|
149 Returns the id of the group the file belongs to. |
|
150 |
|
151 On systems where files do not have groups this function always |
|
152 returns ((uind) -2). |
|
153 |
|
154 \sa group(), owner(), ownerId() |
|
155 */ |
|
156 |
|
157 uint QFileInfo::groupId() const |
|
158 { |
|
159 return (uint)-2; |
|
160 } |
|
161 |
|
162 |
|
163 /*! |
|
164 \fn bool QFileInfo::permission( int permissionSpec ) const |
|
165 |
|
166 Tests for file permissions. The \e permissionSpec argument can be several |
|
167 flags of type PermissionSpec or'ed together to check for permission |
|
168 combinations. |
|
169 |
|
170 On systems where files do not have permissions this function always |
|
171 returns TRUE. |
|
172 |
|
173 Example: |
|
174 \code |
|
175 QFileInfo fi( "/tmp/tonsils" ); |
|
176 if ( fi.permission( QFileInfo::WriteUser | QFileInfo::ReadGroup ) ) |
|
177 qWarning( "Tonsils can be changed by me, and the group can read them."); |
|
178 if ( fi.permission( QFileInfo::WriteGroup | QFileInfo::WriteOther ) ) |
|
179 qWarning( "Danger! Tonsils can be changed by the group or others!" ); |
|
180 \endcode |
|
181 |
|
182 \sa isReadable(), isWritable(), isExecutable() |
|
183 */ |
|
184 |
|
185 bool QFileInfo::permission( int permissionSpec ) const |
|
186 { |
|
187 return TRUE; |
|
188 } |
|
189 |
|
190 /*! |
|
191 Returns the file size in bytes, or 0 if the file does not exist if the size |
|
192 cannot be fetched. |
|
193 */ |
|
194 |
|
195 uint QFileInfo::size() const |
|
196 { |
|
197 if ( !fic || !cache ) |
|
198 doStat(); |
|
199 if ( fic ) |
|
200 return (uint)fic->st.st_size; |
|
201 else |
|
202 return 0; |
|
203 } |
|
204 |
|
205 |
|
206 /*! |
|
207 Returns the date and time when the file was last modified. |
|
208 \sa lastRead() |
|
209 */ |
|
210 |
|
211 QDateTime QFileInfo::lastModified() const |
|
212 { |
|
213 QDateTime dt; |
|
214 if ( !fic || !cache ) |
|
215 doStat(); |
|
216 if ( fic ) |
|
217 dt.setTime_t( fic->st.st_mtime ); |
|
218 return dt; |
|
219 } |
|
220 |
|
221 /*! |
|
222 Returns the date and time when the file was last read (accessed). |
|
223 |
|
224 On systems that do not support last read times, the modification time is |
|
225 returned. |
|
226 |
|
227 \sa lastModified() |
|
228 */ |
|
229 |
|
230 QDateTime QFileInfo::lastRead() const |
|
231 { |
|
232 QDateTime dt; |
|
233 if ( !fic || !cache ) |
|
234 doStat(); |
|
235 if ( fic ) |
|
236 dt.setTime_t( fic->st.st_atime ); |
|
237 return dt; |
|
238 } |
|
239 |
|
240 |
|
241 void QFileInfo::doStat() const |
|
242 { |
|
243 QFileInfo *that = ((QFileInfo*)this); // mutable function |
|
244 if ( !that->fic ) |
|
245 that->fic = new QFileInfoCache; |
|
246 STATBUF *b = &that->fic->st; |
|
247 that->fic->isSymLink = FALSE; |
|
248 |
|
249 int r; |
|
250 |
|
251 r = STAT( QFile::encodeName(fn), b ); |
|
252 |
|
253 if ( r != 0 ) { |
|
254 delete that->fic; |
|
255 that->fic = 0; |
|
256 } |
|
257 } |
|
258 |
|
259 /*! |
|
260 Returns the directory path of the file. |
|
261 |
|
262 If \e absPath is TRUE an absolute path is always returned. |
|
263 |
|
264 \sa dir(), filePath(), fileName(), isRelative() |
|
265 */ |
|
266 #ifndef QT_NO_DIR |
|
267 QString QFileInfo::dirPath( bool absPath ) const |
|
268 { |
|
269 QString s; |
|
270 if ( absPath ) |
|
271 s = absFilePath(); |
|
272 else |
|
273 s = fn; |
|
274 int pos = s.findRev( '/' ); |
|
275 if ( pos == -1 ) { |
|
276 return QString::fromLatin1("."); |
|
277 } else { |
|
278 if ( pos == 0 ) |
|
279 return QString::fromLatin1( "/" ); |
|
280 return s.left( pos ); |
|
281 } |
|
282 } |
|
283 #endif |
|
284 /*! |
|
285 Returns the name of the file, the file path is not included. |
|
286 |
|
287 Example: |
|
288 \code |
|
289 QFileInfo fi( "/tmp/abdomen.lower" ); |
|
290 QString name = fi.fileName(); // name = "abdomen.lower" |
|
291 \endcode |
|
292 |
|
293 \sa isRelative(), filePath(), baseName(), extension() |
|
294 */ |
|
295 |
|
296 QString QFileInfo::fileName() const |
|
297 { |
|
298 int p = fn.findRev( '/' ); |
|
299 if ( p == -1 ) { |
|
300 return fn; |
|
301 } else { |
|
302 return fn.mid(p+1); |
|
303 } |
|
304 } |
|
305 |
|
306 /*! |
|
307 Returns the absolute path name. |
|
308 |
|
309 The absolute path name is the file name including the absolute path. If |
|
310 the QFileInfo is absolute (i.e. not relative) this function will return |
|
311 the same string as filePath(). |
|
312 |
|
313 Note that this function can be time-consuming under UNIX. (in the order |
|
314 of milliseconds on a 486 DX2/66 running Linux). |
|
315 |
|
316 \sa isRelative(), filePath() |
|
317 */ |
|
318 #ifndef QT_NO_DIR |
|
319 QString QFileInfo::absFilePath() const |
|
320 { |
|
321 if ( QDir::isRelativePath(fn) ) { |
|
322 QString tmp = QDir::currentDirPath(); |
|
323 tmp += '/'; |
|
324 tmp += fn; |
|
325 makeAbs( tmp ); |
|
326 return QDir::cleanDirPath( tmp ); |
|
327 } else { |
|
328 QString tmp = fn; |
|
329 makeAbs( tmp ); |
|
330 return QDir::cleanDirPath( tmp ); |
|
331 } |
|
332 |
|
333 } |
|
334 #endif |