qfileinfo_win32.cpp
Go to the documentation of this file.
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 
38 {
39  for ( int i=0; i<(int)n.length(); i++ )
40  {
41  if ( n[i] == '\\' )
42  n[i] = '/';
43  }
44 }
45 
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 
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 
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 
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 
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 
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  file = QDir::cleanDirPath(file);
270  reslashify(file);
271 #ifdef QT_LARGEFILE_SUPPORT
272  if ( _wstati64( (wchar_t*) file.ucs2(), b ) == -1 ) {
273 #else
274  if ( _wstat( (wchar_t*) file.ucs2(), b ) == -1 ) {
275 #endif
276  delete that->fic;
277  that->fic = 0;
278  }
279 #endif
280 }
281 
282 /*!
283  Returns the directory path of the file.
284 
285  If \e absPath is TRUE an absolute path is always returned.
286 
287  \sa dir(), filePath(), fileName(), isRelative()
288 */
289 #ifndef QT_NO_DIR
290 QString QFileInfo::dirPath( bool absPath ) const
291 {
292  QString s;
293  if ( absPath )
294  s = absFilePath();
295  else
296  s = fn;
297  int pos = s.findRev( '/' );
298  if ( pos == -1 ) {
299  return QString::fromLatin1(".");
300  } else {
301  if ( pos == 0 )
302  return QString::fromLatin1( "/" );
303  return s.left( pos );
304  }
305 }
306 #endif
307 /*!
308  Returns the name of the file, the file path is not included.
309 
310  Example:
311  \code
312  QFileInfo fi( "/tmp/abdomen.lower" );
313  QString name = fi.fileName(); // name = "abdomen.lower"
314  \endcode
315 
316  \sa isRelative(), filePath(), baseName(), extension()
317 */
318 
320 {
321  int p = fn.findRev( '/' );
322  if ( p == -1 ) {
323  return fn;
324  } else {
325  return fn.mid(p+1);
326  }
327 }
328 
329 /*!
330  Returns the absolute path name.
331 
332  The absolute path name is the file name including the absolute path. If
333  the QFileInfo is absolute (i.e. not relative) this function will return
334  the same string as filePath().
335 
336  Note that this function can be time-consuming under UNIX. (in the order
337  of milliseconds on a 486 DX2/66 running Linux).
338 
339  \sa isRelative(), filePath()
340 */
341 #ifndef QT_NO_DIR
343 {
344  if ( QDir::isRelativePath(fn) ) {
346  tmp += '/';
347  tmp += fn;
348  makeAbs( tmp );
349  return QDir::cleanDirPath( tmp );
350  } else {
351  QString tmp = fn;
352  makeAbs( tmp );
353  return QDir::cleanDirPath( tmp );
354  }
355 
356 }
357 #endif
static QString cleanDirPath(const QString &dirPath)
Definition: qdir.cpp:1077
void doStat() const
uint groupId() const
#define STATBUF
Definition: qfiledefs_p.h:199
#define STAT
Definition: qfiledefs_p.h:201
static void makeAbs(QString &)
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
uint ownerId() const
const bool FALSE
Definition: qglobal.h:370
static void slashify(QString &)
static bool isRelativePath(const QString &path)
Definition: qdir_unix.cpp:169
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
static QString fromLatin1(const char *, int len=-1)
Definition: qstring.cpp:14539
bool isFile() const
QString readLink() const
QString absFilePath() const
static const uint nobodyID
bool isSymLink() const
void setTime_t(uint secsSince1Jan1970UTC)
Definition: qdatetime.cpp:1142
QString fn
Definition: qfileinfo.h:126
QString dirPath(bool absPath=FALSE) const
QFileInfoCache * fic
Definition: qfileinfo.h:127
QString left(uint len) const
Definition: qstring.cpp:13199
uint length() const
Definition: qstring.h:679
std::void_t< T > n
static QString currentDirPath()
Definition: qdir_unix.cpp:141
const unsigned short * ucs2() const
Definition: qstring.cpp:12092
p
Definition: test.py:223
string tmp
Definition: languages.py:63
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
bool cache
Definition: qfileinfo.h:128
int findRev(QChar c, int index=-1, bool cs=TRUE) const
Definition: qstring.cpp:13021
The QDateTime class provides date and time functions.
Definition: qdatetime.h:161
uint size() const
QString group() const
#define STAT_DIR
Definition: qfiledefs_p.h:205
QDateTime lastModified() const
QString owner() const
QString fileName() const
#define STAT_REG
Definition: qfiledefs_p.h:204
static bool * b
Definition: config.cpp:1043
static void reslashify(QString &n)
bool qt_file_access(const QString &fn, int t)
Definition: qfile_unix.cpp:54
bool permission(int permissionSpec) const
static const Null null
Definition: qstring.h:376
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:51
#define STAT_MASK
Definition: qfiledefs_p.h:206
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
bool isDir() const
const bool TRUE
Definition: qglobal.h:371
QDateTime lastRead() const