Fix for UBSan build
[platform/upstream/doxygen.git] / qtools / qfileinfo_win32.cpp
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 static void reslashify( 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::slashify( QString& n )
38 {
39   for ( int i=0; i<(int)n.length(); i++ ) 
40   {
41      if ( n[i] == '\\' )
42           n[i] = '/';
43   }
44 }
45
46 void QFileInfo::makeAbs( QString & )
47 {
48   // TODO: what to do here?
49   return;
50 }
51
52 extern bool qt_file_access( const QString& fn, int t );
53
54 /*!
55   Returns TRUE if we are pointing to a real file.
56   \sa isDir(), isSymLink()
57 */
58 bool QFileInfo::isFile() const
59 {
60     if ( !fic || !cache )
61         doStat();
62     return fic ? (fic->st.st_mode & STAT_MASK) == STAT_REG : FALSE;
63 }
64
65 /*!
66   Returns TRUE if we are pointing to a directory or a symbolic link to
67   a directory.
68   \sa isFile(), isSymLink()
69 */
70
71 bool QFileInfo::isDir() const
72 {
73     if ( !fic || !cache )
74         doStat();
75     return fic ? (fic->st.st_mode & STAT_MASK) == STAT_DIR : FALSE;
76 }
77
78 /*!
79   Returns TRUE if we are pointing to a symbolic link.
80   \sa isFile(), isDir(), readLink()
81 */
82
83 bool QFileInfo::isSymLink() const
84 {
85     if ( !fic || !cache )
86         doStat();
87     return fic ? fic->isSymLink : FALSE;
88 }
89
90
91 /*!
92   Returns the name a symlink points to, or a null QString if the
93   object does not refer to a symbolic link.
94
95   This name may not represent an existing file; it is only a string.
96   QFileInfo::exists() returns TRUE if the symlink points to an
97   existing file.
98
99   \sa exists(), isSymLink(), isDir(), isFile()
100 */
101
102 QString QFileInfo::readLink() const
103 {
104     QString r;
105     return r;
106 }
107
108 static const uint nobodyID = (uint) -2;
109
110 /*!
111   Returns the owner of the file.
112
113   On systems where files do not have owners this function returns 
114   a null string.
115
116   Note that this function can be time-consuming under UNIX. (in the order
117   of milliseconds on a 486 DX2/66 running Linux).
118
119   \sa ownerId(), group(), groupId()
120 */
121
122 QString QFileInfo::owner() const
123 {
124     return QString::null;
125 }
126
127 /*!
128   Returns the id of the owner of the file.
129
130   On systems where files do not have owners this function returns ((uint) -2).
131
132   \sa owner(), group(), groupId()
133 */
134
135 uint QFileInfo::ownerId() const
136 {
137     return (uint)-2;
138 }
139
140 /*!
141   Returns the group the file belongs to.
142
143   On systems where files do not have groups this function always
144   returns 0.
145
146   Note that this function can be time-consuming under UNIX (in the order of
147   milliseconds on a 486 DX2/66 running Linux).
148
149   \sa groupId(), owner(), ownerId()
150 */
151
152 QString QFileInfo::group() const
153 {
154     return QString::null;
155 }
156
157 /*!
158   Returns the id of the group the file belongs to.
159
160   On systems where files do not have groups this function always
161   returns ((uind) -2).
162
163   \sa group(), owner(), ownerId()
164 */
165
166 uint QFileInfo::groupId() const
167 {
168     return (uint)-2;
169 }
170
171
172 /*!
173   \fn bool QFileInfo::permission( int permissionSpec ) const
174
175   Tests for file permissions.  The \e permissionSpec argument can be several
176   flags of type PermissionSpec or'ed together to check for permission
177   combinations.
178
179   On systems where files do not have permissions this function always
180   returns TRUE.
181
182   Example:
183   \code
184     QFileInfo fi( "/tmp/tonsils" );
185     if ( fi.permission( QFileInfo::WriteUser | QFileInfo::ReadGroup ) )
186         qWarning( "Tonsils can be changed by me, and the group can read them.");
187     if ( fi.permission( QFileInfo::WriteGroup | QFileInfo::WriteOther ) )
188         qWarning( "Danger! Tonsils can be changed by the group or others!" );
189   \endcode
190
191   \sa isReadable(), isWritable(), isExecutable()
192 */
193
194 bool QFileInfo::permission( int permissionSpec ) const
195 {
196   return TRUE;
197 }
198
199 /*!
200   Returns the file size in bytes, or 0 if the file does not exist if the size
201   cannot be fetched.
202 */
203
204 uint QFileInfo::size() const
205 {
206     if ( !fic || !cache )
207         doStat();
208     if ( fic )
209         return (uint)fic->st.st_size;
210     else
211         return 0;
212 }
213
214
215 /*!
216   Returns the date and time when the file was last modified.
217   \sa lastRead()
218 */
219
220 QDateTime QFileInfo::lastModified() const
221 {
222     QDateTime dt;
223     if ( !fic || !cache )
224         doStat();
225     if ( fic )
226         dt.setTime_t( fic->st.st_mtime );
227     return dt;
228 }
229
230 /*!
231   Returns the date and time when the file was last read (accessed).
232
233   On systems that do not support last read times, the modification time is
234   returned.
235
236   \sa lastModified()
237 */
238
239 QDateTime QFileInfo::lastRead() const
240 {
241     QDateTime dt;
242     if ( !fic || !cache )
243         doStat();
244     if ( fic )
245         dt.setTime_t( fic->st.st_atime );
246     return dt;
247 }
248
249
250 void QFileInfo::doStat() const
251 {
252     QFileInfo *that = ((QFileInfo*)this);       // mutable function
253     if ( !that->fic )
254         that->fic = new QFileInfoCache;
255     STATBUF *b = &that->fic->st;
256     that->fic->isSymLink = FALSE;
257
258 #if defined(__CYGWIN32_)
259     int r;
260
261     r = STAT( QFile::encodeName(fn), b );
262
263     if ( r != 0 ) {
264         delete that->fic;
265         that->fic = 0;
266     }
267 #else
268     QString file = fn;
269     reslashify(file);
270 #ifdef QT_LARGEFILE_SUPPORT
271     if ( _wstati64( (wchar_t*) file.ucs2(), b ) == -1 ) {
272 #else
273     if ( _wstat( (wchar_t*) file.ucs2(), b ) == -1 ) {
274 #endif
275       delete that->fic;
276       that->fic = 0;
277     }
278 #endif
279 }
280
281 /*!
282   Returns the directory path of the file.
283
284   If \e absPath is TRUE an absolute path is always returned.
285
286   \sa dir(), filePath(), fileName(), isRelative()
287 */
288 #ifndef QT_NO_DIR
289 QString QFileInfo::dirPath( bool absPath ) const
290 {
291     QString s;
292     if ( absPath )
293         s = absFilePath();
294     else
295         s = fn;
296     int pos = s.findRev( '/' );
297     if ( pos == -1 ) {
298         return QString::fromLatin1(".");
299     } else {
300         if ( pos == 0 )
301             return QString::fromLatin1( "/" );
302         return s.left( pos );
303     }
304 }
305 #endif
306 /*!
307   Returns the name of the file, the file path is not included.
308
309   Example:
310   \code
311      QFileInfo fi( "/tmp/abdomen.lower" );
312      QString name = fi.fileName();              // name = "abdomen.lower"
313   \endcode
314
315   \sa isRelative(), filePath(), baseName(), extension()
316 */
317
318 QString QFileInfo::fileName() const
319 {
320     int p = fn.findRev( '/' );
321     if ( p == -1 ) {
322         return fn;
323     } else {
324         return fn.mid(p+1);
325     }
326 }
327
328 /*!
329   Returns the absolute path name.
330
331   The absolute path name is the file name including the absolute path. If
332   the QFileInfo is absolute (i.e. not relative) this function will return
333   the same string as filePath().
334
335   Note that this function can be time-consuming under UNIX. (in the order
336   of milliseconds on a 486 DX2/66 running Linux).
337
338   \sa isRelative(), filePath()
339 */
340 #ifndef QT_NO_DIR
341 QString QFileInfo::absFilePath() const
342 {
343     if ( QDir::isRelativePath(fn) ) {
344         QString tmp = QDir::currentDirPath();
345         tmp += '/';
346         tmp += fn;
347         makeAbs( tmp );
348         return QDir::cleanDirPath( tmp );
349     } else {
350         QString tmp = fn;
351         makeAbs( tmp );
352         return QDir::cleanDirPath( tmp );
353     }
354
355 }
356 #endif