2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_OS_FILE_H )
23 #define INCLUDED_OS_FILE_H
25 #include "globaldefs.h"
28 /// \brief OS file-system querying and manipulation.
31 #define S_ISDIR( mode ) ( mode & _S_IFDIR )
32 #include <io.h> // _access()
33 #define access( path, mode ) _access( path, mode )
35 #include <unistd.h> // access()
38 #include <stdio.h> // rename(), remove()
39 #include <sys/stat.h> // stat()
40 #include <sys/types.h> // this is included by stat.h on win32
44 #include "debugging/debugging.h"
46 /// \brief Attempts to move the file identified by \p from to \p to and returns true if the operation was successful.
48 /// The operation will fail unless:
49 /// - The path \p from identifies an existing file which is accessible for writing.
50 /// - The directory component of \p from identifies an existing directory which is accessible for writing.
51 /// - The path \p to does not identify an existing file or directory.
52 /// - The directory component of \p to identifies an existing directory which is accessible for writing.
53 inline bool file_move( const char* from, const char* to ){
54 ASSERT_MESSAGE( from != 0 && to != 0, "file_move: invalid path" );
55 return rename( from, to ) == 0;
58 /// \brief Attempts to remove the file identified by \p path and returns true if the operation was successful.
60 /// The operation will fail unless:
61 /// - The \p path identifies an existing file.
62 /// - The parent-directory component of \p path identifies an existing directory which is accessible for writing.
63 inline bool file_remove( const char* path ){
64 ASSERT_MESSAGE( path != 0, "file_remove: invalid path" );
65 return remove( path ) == 0;
74 ReadWrite = Read | Write,
79 /// \brief Returns true if the file or directory identified by \p path exists and/or may be accessed for reading, writing or both, depending on the value of \p mode.
80 inline bool file_accessible( const char* path, FileAccess::Mode mode ){
81 ASSERT_MESSAGE( path != 0, "file_accessible: invalid path" );
82 return access( path, static_cast<int>( mode ) ) == 0;
85 /// \brief Returns true if the file or directory identified by \p path exists and may be opened for reading.
86 inline bool file_readable( const char* path ){
87 return file_accessible( path, FileAccess::Read );
90 /// \brief Returns true if the file or directory identified by \p path exists and may be opened for writing.
91 inline bool file_writeable( const char* path ){
92 return file_accessible( path, FileAccess::Write );
95 /// \brief Returns true if the file or directory identified by \p path exists.
96 inline bool file_exists( const char* path ){
97 return file_accessible( path, FileAccess::Exists );
100 /// \brief Returns true if the file or directory identified by \p path exists and is a directory.
101 inline bool file_is_directory( const char* path ){
102 ASSERT_MESSAGE( path != 0, "file_is_directory: invalid path" );
104 if ( stat( path, &st ) == -1 ) {
107 return S_ISDIR( st.st_mode ) != 0;
110 typedef std::size_t FileSize;
112 /// \brief Returns the size in bytes of the file identified by \p path, or 0 if the file was not found.
113 inline FileSize file_size( const char* path ){
114 ASSERT_MESSAGE( path != 0, "file_size: invalid path" );
116 if ( stat( path, &st ) == -1 ) {
122 /// Seconds elapsed since Jan 1, 1970
123 typedef std::time_t FileTime;
124 /// No file can have been modified earlier than this time.
125 const FileTime c_invalidFileTime = -1;
127 /// \brief Returns the time that the file identified by \p path was last modified, or c_invalidFileTime if the file was not found.
128 inline FileTime file_modified( const char* path ){
129 ASSERT_MESSAGE( path != 0, "file_modified: invalid path" );
131 if ( stat( path, &st ) == -1 ) {
132 return c_invalidFileTime;