20 #if defined(ION_PLATFORM_IOS)
21 #include <Foundation/Foundation.h>
23 #if defined(ION_PLATFORM_WINDOWS)
34 #include "base/integral_types.h"
41 #if defined(ION_PLATFORM_WINDOWS)
42 std::string canonical_path = path;
45 const size_t length = canonical_path.length();
46 for (
size_t i = 0; i <
length; ++i)
47 if (canonical_path[i] ==
'\\')
48 canonical_path[i] =
'/';
49 return canonical_path;
58 #if defined(ION_PLATFORM_WINDOWS)
59 WCHAR pwd[MAX_PATH] = L
"";
60 const int result = ::GetCurrentDirectoryW(MAX_PATH, pwd);
61 assert(0 < result && result < ARRAYSIZE(pwd));
65 static const int kExpectedPathLength = 2048;
66 std::vector<char> path(kExpectedPathLength);
67 while (!getcwd(&path[0], static_cast<int>(path.size()))) {
68 path.resize(path.size() * 2);
75 std::chrono::system_clock::time_point* time) {
76 #if defined(ION_PLATFORM_WINDOWS)
77 static const int64 kEpochOffset = 116444736000000000ULL;
85 const std::wstring wide = Utf8ToWide(path);
86 HANDLE
handle = ::CreateFileW(wide.c_str(), GENERIC_READ,
87 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
88 OPEN_EXISTING, 0, NULL);
89 FILETIME create_time, access_time, write_time;
91 if (handle != INVALID_HANDLE_VALUE) {
92 if (GetFileTime(handle, &create_time, &access_time, &write_time)) {
93 const std::chrono::duration<int64, std::ratio<1, 10000000>> file_duration(
94 (static_cast<int64>(write_time.dwHighDateTime) << 32) +
95 write_time.dwLowDateTime - kEpochOffset);
96 *time = std::chrono::system_clock::time_point(file_duration);
105 # if defined(ION_PLATFORM_IOS) || defined(ION_PLATFORM_MAC)
106 # define ION_STAT_SECONDS static_cast<uint64>(info.st_mtimespec.tv_sec)
107 # define ION_STAT_NSECONDS static_cast<uint64>(info.st_mtimespec.tv_nsec)
108 # elif defined(ION_PLATFORM_ANDROID) || defined(ION_PLATFORM_GENERIC_ARM)
109 # define ION_STAT_SECONDS static_cast<uint64>(info.st_mtime)
110 # define ION_STAT_NSECONDS static_cast<uint64>(info.st_mtime_nsec)
111 # elif defined(ION_PLATFORM_ASMJS) || defined(ION_PLATFORM_QNX) || \
112 defined(ION_PLATFORM_NACL)
113 # define ION_STAT_SECONDS static_cast<uint64>(info.st_mtime)
114 # define ION_STAT_NSECONDS 0
115 # elif defined(ION_PLATFORM_LINUX)
116 # define ION_STAT_SECONDS static_cast<uint64>(info.st_mtim.tv_sec)
117 # define ION_STAT_NSECONDS static_cast<uint64>(info.st_mtim.tv_nsec)
119 # error No valid platform defined!
122 if (!stat(path.c_str(), &info)) {
123 *time = std::chrono::system_clock::time_point(
124 std::chrono::duration_cast<std::chrono::system_clock::duration>(
125 std::chrono::seconds(ION_STAT_SECONDS) +
126 std::chrono::nanoseconds(ION_STAT_NSECONDS)));
131 # undef ION_STAT_SECONDS
132 # undef ION_STAT_NSECONDS
137 #if defined(ION_PLATFORM_ANDROID)
138 return std::string(
"/data/local/tmp");
140 #elif defined(ION_PLATFORM_IOS)
141 return std::string([NSTemporaryDirectory() fileSystemRepresentation]);
142 #elif defined(ION_PLATFORM_WINDOWS)
143 WCHAR temp_path[MAX_PATH] = L
"";
144 int size = ::GetTempPathW(MAX_PATH, temp_path);
145 assert(0 < size && size < ARRAYSIZE(temp_path));
146 if (size > 0 && temp_path[size - 1] == L
'\\') {
147 temp_path[size - 1] = L
'\0';
151 return std::string(
"/tmp");
157 std::string return_path;
158 #if defined(ION_PLATFORM_WINDOWS)
159 WCHAR temp_path[MAX_PATH] = L
"";
160 int size = ::GetTempPathW(MAX_PATH, temp_path);
161 assert(0 < size && size < ARRAYSIZE(temp_path));
163 WCHAR wide_path[MAX_PATH] = L
"";
164 const UINT unique = ::GetTempFileNameW(temp_path, L
"ion", 0, wide_path);
168 #elif !defined(ION_PLATFORM_NACL)
171 const int fd = mkstemp(&path[0]);
180 FILE*
OpenFile(
const std::string& path,
const std::string& mode) {
182 #if defined(ION_PLATFORM_WINDOWS)
183 const std::wstring wide_path = Utf8ToWide(canonical_path);
184 const std::wstring wide_mode = Utf8ToWide(mode);
185 return _wfopen(wide_path.c_str(), wide_mode.c_str());
187 return fopen(path.c_str(), mode.c_str());
194 fseek(file, 0, SEEK_END);
195 const size_t length = ftell(file);
200 fseek(file, 0, SEEK_SET);
201 fread(&out->at(0),
sizeof(char), length, file);
210 #if defined(ION_PLATFORM_NACL)
212 #elif defined(ION_PLATFORM_WINDOWS)
213 return ::DeleteFileW(Utf8ToWide(path).c_str()) != 0;
215 return unlink(path.c_str()) == 0;
220 std::vector<std::string> files;
221 #if defined(ION_PLATFORM_WINDOWS)
222 std::wstring wild = ion::port::Utf8ToWide(path) + L
"/*";
223 WIN32_FIND_DATAW find_data;
224 HANDLE find_handle = ::FindFirstFileW(wild.c_str(), &find_data);
225 if (find_handle != INVALID_HANDLE_VALUE) {
227 if (!lstrcmpW(find_data.cFileName, L
".") ||
228 !lstrcmpW(find_data.cFileName, L
"..")) {
231 files.push_back(ion::port::WideToUtf8(find_data.cFileName));
232 }
while (::FindNextFileW(find_handle, &find_data));
233 ::FindClose(find_handle);
235 #elif !defined(ION_PLATFORM_NACL)
236 if (DIR* dir = opendir(path.c_str())) {
239 struct dirent dent_buf;
241 while (readdir_r(dir, &dent_buf, &dent) == 0 && dent) {
242 const std::string file = dent->d_name;
243 if (file !=
"." && file !=
"..")
244 files.push_back(file);
std::string GetTemporaryFilename()
Returns a platform-dependent string that names a valid filename which may be opened for reading or wr...
std::vector< std::string > ListDirectory(const std::string &path)
Returns the contents of path, non-recursively.
std::string GetCurrentWorkingDirectory()
Returns a platform-dependent string that is the current working directory.
bool RemoveFile(const std::string &path)
Attempts to remove the file at path and returns whether the file was successfully removed...
bool ReadDataFromFile(const std::string &path, std::string *out)
Opens the file at path and read the contents of the file into a string.
bool GetFileModificationTime(const std::string &path, std::chrono::system_clock::time_point *time)
Reads the last modification time of the passed file path into time and returns true, iff the file exists.
std::string GetCanonicalFilePath(const std::string &path)
Returns a canonical version of a file path string.
FILE * OpenFile(const std::string &path, const std::string &mode)
Opens the file at path and returns a FILE pointer suitable for passing to fread, fwrite, fclose, etc.
std::string GetTemporaryDirectory()
Returns a platform-dependent string that names the temporary directory.