forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.h
166 lines (143 loc) · 3.87 KB
/
platform.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EP_PLATFORM_H
#define EP_PLATFORM_H
// Headers
#include "system.h"
#include <cstdint>
#include <string>
#ifdef _WIN32
# include <windows.h>
#else
# ifdef __vita__
# include <psp2/io/dirent.h>
# include <psp2/io/stat.h>
# else
# include <dirent.h>
# include <sys/stat.h>
# endif
# include <unistd.h>
# include <sys/types.h>
#endif
/**
* Provides abstractions for accessing operating system APIs.
*
* Don't use them directly, use FileFinder instead
*/
namespace Platform {
enum class FileType {
File,
Directory,
Other,
Unknown
};
/** Wrapper around file access */
class File {
public:
explicit File() = delete;
File& operator=(const File&) = delete;
File(const File&) = delete;
/**
* Accesses a file.
*
* @param name File to access
*/
explicit File(std::string name);
~File() = default;
/** @return True when the file exists */
bool Exists() const;
/**
* @param follow_symlinks Whether to follow symlinks (if supported on this platform)
* @return true if it is a regular file
*/
bool IsFile(bool follow_symlinks) const;
/**
* @param follow_symlinks Whether to follow symlinks (if supported on this platform)
* @return true if it is a directory
*/
bool IsDirectory(bool follow_symlinks) const;
/**
* @param follow_symlinks Whether to follow symlinks (if supported on this platform)
* @return type of the file or FileType::Unknown on error
*/
FileType GetType(bool follow_symlinks) const;
/** @return Filesize or -1 on error */
int64_t GetSize() const;
/**
* Creates a directory recursively at the filename path.
* @param follow_symlinks Whether to follow symlinks (if supported on this platform)
* @return true when the directory was created.
*/
bool MakeDirectory(bool follow_symlinks) const;
private:
#ifdef _WIN32
const std::wstring filename;
#else
const std::string filename;
#endif
};
/** Wrapper around directory reading */
class Directory {
public:
explicit Directory() = delete;
Directory& operator=(const Directory&) = delete;
Directory(const Directory&) = delete;
/**
* Opens a directory for reading.
*
* @param name Directory to open
*/
explicit Directory(const std::string& name);
~Directory();
/**
* Reads one directory entry.
* Use GetEntryName/GetEntryType afterwards to retrieve information.
*
* @return true on success, false on failure or when end is reached.
*/
bool Read();
/** @return Name of the last read entry */
std::string GetEntryName() const;
/** @return Type of the last read entry */
FileType GetEntryType() const;
/** Closes the directory */
void Close();
/** @return true if opening the directory was successful */
explicit operator bool() const noexcept;
private:
#if defined(_WIN32)
HANDLE dir_handle = nullptr;
WIN32_FIND_DATAW entry = {};
bool first_entry = true;
#elif defined(__vita__)
int dir_handle = -1;
struct SceIoDirent entry = {};
#else
DIR* dir_handle = nullptr;
struct dirent* entry = nullptr;
#endif
bool valid_entry = false;
};
inline Directory::operator bool() const noexcept {
#ifdef __vita__
return dir_handle >= 0;
#else
return dir_handle != nullptr;
#endif
}
}
#endif