This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsystem-file.cpp
More file actions
288 lines (241 loc) · 8.41 KB
/
system-file.cpp
File metadata and controls
288 lines (241 loc) · 8.41 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*-c++-*-
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "system-private.h"
#include <foundation-auto.h>
#define MCS_FILE_CONVERT_PATH(path, native_path) \
MCAutoStringRef native_path##__auto; \
MCStringRef native_path; \
do { if (!__MCSFilePathToNative (path, & native_path##__auto)) \
return false; \
native_path = * native_path##__auto; \
} while (0)
/* ================================================================
* Error handling
* ================================================================ */
bool
__MCSFileThrowIOErrorWithErrno (MCStringRef p_native_path,
MCStringRef p_message,
int p_errno)
{
MCAutoStringRef t_description;
MCAutoNumberRef t_error_code;
if (p_errno != 0)
{
/* UNCHECKED */ MCStringCreateWithCString (strerror (p_errno),
&t_description);
/* UNCHECKED */ MCNumberCreateWithInteger (p_errno,
&t_error_code);
}
else
{
t_description = MCSTR("Unknown error");
t_error_code = kMCZero;
}
MCAutoStringRef t_path;
/* UNCHECKED */ __MCSFilePathFromNative (p_native_path, &t_path);
if (p_message)
{
return MCErrorCreateAndThrowWithMessage (kMCSFileIOErrorTypeInfo,
p_message,
"path", *t_path,
"description", *t_description,
"error_code", *t_error_code,
NULL);
}
else
{
return MCErrorCreateAndThrow (kMCSFileIOErrorTypeInfo,
"path", *t_path,
"description", *t_description,
"error_code", *t_error_code,
NULL);
}
}
bool
__MCSFileThrowReadErrorWithErrno (MCStringRef p_native_path,
int p_errno)
{
return __MCSFileThrowIOErrorWithErrno (p_native_path, MCSTR("Failed to read from file '%{path}': %{description}"), p_errno);
}
bool
__MCSFileThrowWriteErrorWithErrno (MCStringRef p_native_path,
int p_errno)
{
return __MCSFileThrowIOErrorWithErrno (p_native_path, MCSTR("Failed to write to file '%{path}': %{description}"), p_errno);
}
bool
__MCSFileThrowOpenErrorWithErrno (MCStringRef p_native_path,
int p_errno)
{
return __MCSFileThrowIOErrorWithErrno (p_native_path, MCSTR("Failed to open file '%{path}': %{description}"), p_errno);
}
bool
__MCSFileThrowInvalidPathError (MCStringRef p_path)
{
return MCErrorCreateAndThrow (kMCSFileInvalidPathErrorTypeInfo,
"path", p_path,
NULL);
}
/* ================================================================
* Path manipulation
* ================================================================ */
MC_DLLEXPORT_DEF bool
MCSFileGetCurrentDirectory (MCStringRef & r_path)
{
MCAutoStringRef t_native_path;
if (!__MCSFileGetCurrentDirectory (&t_native_path))
return false;
if (!__MCSFilePathFromNative(*t_native_path, r_path))
return false;
return true;
}
MC_DLLEXPORT_DEF bool
MCSFilePathIsAbsolute (MCStringRef p_path)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFilePathIsAbsolute (t_native_path);
}
/* ================================================================
* Whole-file IO
* ================================================================ */
MC_DLLEXPORT_DEF bool
MCSFileGetContents (MCStringRef p_path,
MCDataRef & r_data)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileGetContents (t_native_path, r_data);
}
MC_DLLEXPORT_DEF bool
MCSFileSetContents (MCStringRef p_path,
MCDataRef p_data)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileSetContents (t_native_path, p_data);
}
/* ================================================================
* File streams
* ================================================================ */
MC_DLLEXPORT_DEF bool
MCSFileCreateStream (MCStringRef p_path,
intenum_t p_mode,
MCStreamRef & r_stream)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileCreateStream (t_native_path, p_mode, r_stream);
}
/* ================================================================
* File system operations
* ================================================================ */
MC_DLLEXPORT_DEF bool
MCSFileDelete (MCStringRef p_path)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileDelete (t_native_path);
}
MC_DLLEXPORT_DEF bool
MCSFileCreateDirectory (MCStringRef p_path)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileCreateDirectory (t_native_path);
}
MC_DLLEXPORT_DEF bool
MCSFileDeleteDirectory (MCStringRef p_path)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
return __MCSFileDeleteDirectory (t_native_path);
}
/* This callback function is used by MCSFileGetDirectoryEntries() to
* convert directory entries from system filename representation to
* LiveCode's internal representation. */
static bool
MCSFileGetDirectoryEntries_MapCallback (void *p_context,
MCValueRef p_native_path,
MCValueRef & r_path)
{
MCAssert (kMCStringTypeInfo == MCValueGetTypeInfo (p_native_path));
MCStringRef t_path;
if (!__MCSFilePathFromNative (static_cast<MCStringRef>(p_native_path),
t_path))
return false;
r_path = t_path;
return true;
}
MC_DLLEXPORT_DEF bool
MCSFileGetDirectoryEntries (MCStringRef p_path,
MCProperListRef & r_entries)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
MCAutoProperListRef t_native_entries;
if (!__MCSFileGetDirectoryEntries (t_native_path, &t_native_entries))
return false;
/* Convert the returned directory entries to LiveCode path
* representation. */
return MCProperListMap (*t_native_entries,
MCSFileGetDirectoryEntries_MapCallback,
r_entries,
NULL);
}
MC_DLLEXPORT_DEF bool
MCSFileGetType (MCStringRef p_path,
bool p_follow_links,
MCSFileType & r_type)
{
MCS_FILE_CONVERT_PATH(p_path, t_native_path);
bool t_success = true;
if (t_success)
{
t_success = __MCSFileGetType (t_native_path, p_follow_links, r_type);
}
if (t_success && r_type == kMCSFileTypeUnsupported)
{
MCLog ("%s: file '%@' has unrecognised type", __FUNCTION__, p_path);
}
return t_success;
}
/* ================================================================
* Initialization
* ================================================================ */
MC_DLLEXPORT_DEF MCTypeInfoRef kMCSFileIOErrorTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef kMCSFileEndOfFileErrorTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef kMCSFileInvalidPathErrorTypeInfo;
bool
__MCSFileInitialize (void)
{
/* Create error types */
if (!MCNamedErrorTypeInfoCreate(
MCNAME("livecode.lang.FileIOError"),
MCNAME("file"),
MCSTR("File input/output error for '%{path}': %{description}"),
kMCSFileIOErrorTypeInfo))
return false;
if (!MCNamedErrorTypeInfoCreate (
MCNAME("livecode.lang.EndOfFileError"),
MCNAME("file"),
MCSTR("End of file '%{path}'"),
kMCSFileEndOfFileErrorTypeInfo))
return false;
if (!MCNamedErrorTypeInfoCreate (
MCNAME("livecode.lang.InvalidFilenameError"),
MCNAME("file"),
MCSTR("No valid native path representation for path '%{path}'"),
kMCSFileInvalidPathErrorTypeInfo))
return false;
return true;
}
void
__MCSFileFinalize (void)
{
MCValueRelease (kMCSFileInvalidPathErrorTypeInfo);
MCValueRelease (kMCSFileEndOfFileErrorTypeInfo);
MCValueRelease (kMCSFileIOErrorTypeInfo);
}