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