| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 20 | */ |
| 21 | |
| 22 | #if !defined( INCLUDED_OS_FILE_H ) |
| 23 | #define INCLUDED_OS_FILE_H |
| 24 | |
| 25 | #include "globaldefs.h" |
| 26 | |
| 27 | /// \file |
| 28 | /// \brief OS file-system querying and manipulation. |
| 29 | |
| 30 | #if GDEF_OS_WINDOWS |
| 31 | #define S_ISDIR( mode ) ( mode & _S_IFDIR ) |
| 32 | #include <io.h> // _access() |
| 33 | |
| 34 | #ifndef F_OK |
| 35 | #define F_OK 0x00 |
| 36 | #endif |
| 37 | |
| 38 | #ifndef W_OK |
| 39 | #define W_OK 0x02 |
| 40 | #endif |
| 41 | |
| 42 | #ifndef R_OK |
| 43 | #define R_OK 0x04 |
| 44 | #endif |
| 45 | |
| 46 | #define access( path, mode ) _access( path, mode ) |
| 47 | #else |
| 48 | #include <unistd.h> // access() |
| 49 | #endif |
| 50 | |
| 51 | #include <stdio.h> // rename(), remove() |
| 52 | #include <sys/stat.h> // stat() |
| 53 | #include <sys/types.h> // this is included by stat.h on win32 |
| 54 | #include <cstddef> |
| 55 | #include <ctime> |
| 56 | |
| 57 | #include "debugging/debugging.h" |
| 58 | |
| 59 | /// \brief Attempts to move the file identified by \p from to \p to and returns true if the operation was successful. |
| 60 | /// |
| 61 | /// The operation will fail unless: |
| 62 | /// - The path \p from identifies an existing file which is accessible for writing. |
| 63 | /// - The directory component of \p from identifies an existing directory which is accessible for writing. |
| 64 | /// - The path \p to does not identify an existing file or directory. |
| 65 | /// - The directory component of \p to identifies an existing directory which is accessible for writing. |
| 66 | inline bool file_move( const char* from, const char* to ){ |
| 67 | ASSERT_MESSAGE( from != 0 && to != 0, "file_move: invalid path" ); |
| 68 | return rename( from, to ) == 0; |
| 69 | } |
| 70 | |
| 71 | /// \brief Attempts to remove the file identified by \p path and returns true if the operation was successful. |
| 72 | /// |
| 73 | /// The operation will fail unless: |
| 74 | /// - The \p path identifies an existing file. |
| 75 | /// - The parent-directory component of \p path identifies an existing directory which is accessible for writing. |
| 76 | inline bool file_remove( const char* path ){ |
| 77 | ASSERT_MESSAGE( path != 0, "file_remove: invalid path" ); |
| 78 | return remove( path ) == 0; |
| 79 | } |
| 80 | |
| 81 | namespace FileAccess |
| 82 | { |
| 83 | enum Mode |
| 84 | { |
| 85 | Read = R_OK, |
| 86 | Write = W_OK, |
| 87 | ReadWrite = Read | Write, |
| 88 | Exists = F_OK |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | /// \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. |
| 93 | inline bool file_accessible( const char* path, FileAccess::Mode mode ){ |
| 94 | ASSERT_MESSAGE( path != 0, "file_accessible: invalid path" ); |
| 95 | return access( path, static_cast<int>( mode ) ) == 0; |
| 96 | } |
| 97 | |
| 98 | /// \brief Returns true if the file or directory identified by \p path exists and may be opened for reading. |
| 99 | inline bool file_readable( const char* path ){ |
| 100 | return file_accessible( path, FileAccess::Read ); |
| 101 | } |
| 102 | |
| 103 | /// \brief Returns true if the file or directory identified by \p path exists and may be opened for writing. |
| 104 | inline bool file_writeable( const char* path ){ |
| 105 | return file_accessible( path, FileAccess::Write ); |
| 106 | } |
| 107 | |
| 108 | /// \brief Returns true if the file or directory identified by \p path exists. |
| 109 | inline bool file_exists( const char* path ){ |
| 110 | return file_accessible( path, FileAccess::Exists ); |
| 111 | } |
| 112 | |
| 113 | /// \brief Returns true if the file or directory identified by \p path exists and is a directory. |
| 114 | inline bool file_is_directory( const char* path ){ |
| 115 | ASSERT_MESSAGE( path != 0, "file_is_directory: invalid path" ); |
| 116 | struct stat st; |
| 117 | if ( stat( path, &st ) == -1 ) { |
| 118 | return false; |
| 119 | } |
| 120 | return S_ISDIR( st.st_mode ) != 0; |
| 121 | } |
| 122 | |
| 123 | typedef std::size_t FileSize; |
| 124 | |
| 125 | /// \brief Returns the size in bytes of the file identified by \p path, or 0 if the file was not found. |
| 126 | inline FileSize file_size( const char* path ){ |
| 127 | ASSERT_MESSAGE( path != 0, "file_size: invalid path" ); |
| 128 | struct stat st; |
| 129 | if ( stat( path, &st ) == -1 ) { |
| 130 | return 0; |
| 131 | } |
| 132 | return st.st_size; |
| 133 | } |
| 134 | |
| 135 | /// Seconds elapsed since Jan 1, 1970 |
| 136 | typedef std::time_t FileTime; |
| 137 | /// No file can have been modified earlier than this time. |
| 138 | const FileTime c_invalidFileTime = -1; |
| 139 | |
| 140 | /// \brief Returns the time that the file identified by \p path was last modified, or c_invalidFileTime if the file was not found. |
| 141 | inline FileTime file_modified( const char* path ){ |
| 142 | ASSERT_MESSAGE( path != 0, "file_modified: invalid path" ); |
| 143 | struct stat st; |
| 144 | if ( stat( path, &st ) == -1 ) { |
| 145 | return c_invalidFileTime; |
| 146 | } |
| 147 | return st.st_mtime; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | #endif |