forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
462 lines (407 loc) · 10.1 KB
/
File.cpp
File metadata and controls
462 lines (407 loc) · 10.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <hxcpp.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <hx/OS.h>
#ifdef NEKO_WINDOWS
# include <windows.h>
# include <io.h>
#endif
/**
<doc>
<h1>File</h1>
<p>
The file api can be used for different kind of file I/O.
</p>
</doc>
**/
namespace
{
struct fio : public hx::Object
{
HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdFio };
String name;
FILE *io;
bool closeIo;
void create(FILE *inFile, String inName, bool inClose)
{
name = inName;
HX_OBJ_WB_GET(this,name.raw_ref());
io = inFile;
closeIo = inClose;
_hx_set_finalizer(this, finalize);
}
void destroy(bool inForceClose = false)
{
if (io && (inForceClose || closeIo))
fclose(io);
io = 0;
name = String();
}
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(name); }
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext *__inCtx) { HX_VISIT_MEMBER(name); }
#endif
static void finalize(Dynamic inObj)
{
((fio *)(inObj.mPtr))->destroy();
}
String toString() { return HX_CSTRING("fio:") + name; }
};
fio *getFio(Dynamic handle, bool inRequireFile=true)
{
fio *result = dynamic_cast<fio *>(handle.mPtr);
if (!result || (!result->io && inRequireFile))
hx::Throw( HX_CSTRING("Bad file handle") );
return result;
}
static void file_error(const char *msg, String inName)
{
hx::ExitGCFreeZone();
Array<String> err = Array_obj<String>::__new(2,2);
err[0] = String(msg);
err[1] = inName;
hx::Throw(err);
}
}
/**
file_open : f:string -> r:string -> 'file
<doc>
Call the C function [fopen] with the file path and access rights.
Return the opened file or throw an exception if the file couldn't be open.
</doc>
**/
Dynamic _hx_std_file_open( String fname, String r )
{
FILE *file = 0;
hx::strbuf buf0;
hx::strbuf buf1;
#ifdef NEKO_WINDOWS
hx::EnterGCFreeZone();
file = _wfopen(fname.wchar_str(&buf0),r.wchar_str(&buf1));
#else
hx::EnterGCFreeZone();
file = fopen(fname.utf8_str(&buf0),r.utf8_str(&buf1));
#endif
if (!file)
file_error("file_open",fname);
hx::ExitGCFreeZone();
fio *f = new fio;
f->create(file,fname,true);
return f;
}
/**
file_close : 'file -> void
<doc>Close an file. Any other operations on this file will fail</doc>
**/
void _hx_std_file_close( Dynamic handle )
{
fio *fio = getFio(handle);
fio->destroy(true);
}
/**
file_name : 'file -> string
<doc>Return the name of the file which was opened</doc>
**/
String hx_std_file_name( Dynamic handle )
{
fio *fio = getFio(handle);
return fio->name;
}
/**
file_write : 'file -> s:string -> p:int -> l:int -> int
<doc>
Write up to [l] chars of string [s] starting at position [p].
Returns the number of chars written which is >= 0.
</doc>
**/
int _hx_std_file_write( Dynamic handle, Array<unsigned char> s, int p, int n )
{
fio *f = getFio(handle);
int buflen = s->length;
int len = n;
if( p < 0 || len < 0 || p > buflen || p + len > buflen )
return 0;
hx::AutoGCFreeZone zone;
#ifdef HX_WINDOWS
if (_isatty(_fileno(f->io))) {
fflush(f->io);
HANDLE win_handle = (HANDLE)_get_osfhandle(_fileno(f->io));
static const int MAX_BUFFER_SIZE = 8192;
wchar_t buf[MAX_BUFFER_SIZE / 2];
int result = MultiByteToWideChar(CP_UTF8, 0, (char *)&s[p], len, buf, MAX_BUFFER_SIZE / 2);
DWORD written = 0;
if(!WriteConsoleW(win_handle, buf, result, &written, NULL)) {
file_error("file_write", f->name);
}
if (written == result) {
return len;
} else {
auto first_code_unit_remaining = buf[written];
if (first_code_unit_remaining > 0xDCEE && first_code_unit_remaining <= 0xDFF) {
DWORD tmp;
WriteConsoleW(win_handle, &buf[written], 1, &tmp, NULL);
written += 1;
}
int count = 0;
for (int i = 0; i < written; i++) {
wchar_t ch = buf[i];
if (ch >= 0 && ch <= 0x7F) {
count += 1;
} else if (ch >= 0x0080 && ch <= 0x07FF) {
count += 2;
} else if (ch >= 0xDCEE && ch <= 0xDFF) {
count += 1;
} else {
count += 3;
}
}
return count;
}
}
#endif
while( len > 0 )
{
POSIX_LABEL(file_write_again);
int d = (int)fwrite(&s[p],1,len,f->io);
if( d <= 0 )
{
HANDLE_FINTR(f->io,file_write_again);
file_error("file_write",f->name);
}
p += d;
len -= d;
}
return n;
}
/**
file_read : 'file -> s:string -> p:int -> l:int -> int
<doc>
Read up to [l] chars into the string [s] starting at position [p].
Returns the number of chars readed which is > 0 (or 0 if l == 0).
</doc>
**/
int _hx_std_file_read( Dynamic handle, Array<unsigned char> buf, int p, int n )
{
fio *f = getFio(handle);
int buf_len = buf->length;
int len = n;
if( p < 0 || len < 0 || p > buf_len || p + len > buf_len )
return 0;
hx::EnterGCFreeZone();
// Attempt to increase the chances of pinning on the stack...
unsigned char *bufPtr = &buf[0];
while( len > 0 )
{
POSIX_LABEL(file_read_again);
int d = (int)fread(bufPtr + p,1,len,f->io);
if( d <= 0 )
{
int size = n - len;
HANDLE_FINTR(f->io,file_read_again);
if( size == 0 )
file_error("file_read",f->name);
hx::ExitGCFreeZone();
return size;
}
p += d;
len -= d;
}
hx::ExitGCFreeZone();
return n;
}
/**
file_write_char : 'file -> c:int -> void
<doc>Write the char [c]. Error if [c] outside of the range 0..255</doc>
**/
void _hx_std_file_write_char( Dynamic handle, int c )
{
fio *f = getFio(handle);
if( c < 0 || c > 255 )
return;
char cc = (char)c;
hx::EnterGCFreeZone();
POSIX_LABEL(write_char_again);
if( fwrite(&cc,1,1,f->io) != 1 )
{
HANDLE_FINTR(f->io,write_char_again);
file_error("file_write_char",f->name);
}
hx::ExitGCFreeZone();
}
/**
file_read_char : 'file -> int
<doc>Read a char from the file. Exception on error</doc>
**/
int _hx_std_file_read_char( Dynamic handle )
{
fio *f = getFio(handle);
unsigned char cc = 0;
hx::EnterGCFreeZone();
POSIX_LABEL(read_char_again);
if( fread(&cc,1,1,f->io) != 1 )
{
HANDLE_FINTR(f->io,read_char_again);
file_error("file_read_char",f->name);
}
hx::ExitGCFreeZone();
return cc;
}
/**
file_seek : 'file -> pos:int -> mode:int -> void
<doc>Use [fseek] to move the file pointer.</doc>
**/
void _hx_std_file_seek( Dynamic handle, int pos, int kind )
{
fio *f = getFio(handle);
hx::EnterGCFreeZone();
if( fseek(f->io,pos,kind) != 0 )
file_error("file_seek",f->name);
hx::ExitGCFreeZone();
}
/**
file_tell : 'file -> int
<doc>Return the current position in the file</doc>
**/
int _hx_std_file_tell( Dynamic handle )
{
fio *f = getFio(handle);
hx::EnterGCFreeZone();
int p = ftell(f->io);
if( p == -1 )
file_error("file_tell",f->name);
hx::ExitGCFreeZone();
return p;
}
/**
file_eof : 'file -> bool
<doc>Tell if we have reached the end of the file</doc>
**/
bool _hx_std_file_eof( Dynamic handle )
{
fio *f = getFio(handle);
return feof(f->io);
}
/**
file_flush : 'file -> void
<doc>Flush the file buffer</doc>
**/
void _hx_std_file_flush( Dynamic handle )
{
fio *f = getFio(handle);
hx::EnterGCFreeZone();
if( fflush( f->io ) != 0 )
file_error("file_flush",f->name);
hx::ExitGCFreeZone();
}
/**
file_contents : f:string -> string
<doc>Read the content of the file [f] and return it.</doc>
**/
String _hx_std_file_contents_string( String name )
{
std::vector<char> buffer;
hx::strbuf buf;
#ifdef NEKO_WINDOWS
hx::EnterGCFreeZone();
FILE *file = _wfopen(name.wchar_str(&buf), L"rb");
#else
hx::EnterGCFreeZone();
FILE *file = fopen(name.utf8_str(&buf), "rb");
#endif
if(!file)
file_error("file_contents",name);
fseek(file,0,SEEK_END);
int len = ftell(file);
if (len<0)
file_error("file_ftell",name);
if (len==0)
{
fclose(file);
hx::ExitGCFreeZone();
return String::emptyString;
}
fseek(file,0,SEEK_SET);
buffer.resize(len);
int p = 0;
while( len > 0 )
{
POSIX_LABEL(file_contents);
int d = (int)fread(&buffer[p],1,len,file);
if( d <= 0 )
{
HANDLE_FINTR(file,file_contents);
fclose(file);
file_error("file_contents",name);
}
p += d;
len -= d;
}
fclose(file);
hx::ExitGCFreeZone();
return String::create(&buffer[0], buffer.size());
}
/**
file_contents : f:string -> string
<doc>Read the content of the file [f] and return it.</doc>
**/
Array<unsigned char> _hx_std_file_contents_bytes( String name )
{
hx::strbuf buf;
#ifdef NEKO_WINDOWS
hx::EnterGCFreeZone();
FILE *file = _wfopen(name.wchar_str(&buf), L"rb");
#else
hx::EnterGCFreeZone();
FILE *file = fopen(name.utf8_str(&buf), "rb");
#endif
if(!file)
file_error("file_contents",name);
fseek(file,0,SEEK_END);
int len = ftell(file);
if (len<0)
file_error("file_ftell",name);
fseek(file,0,SEEK_SET);
hx::ExitGCFreeZone();
Array<unsigned char> buffer = Array_obj<unsigned char>::__new(len,len);
hx::EnterGCFreeZone();
if (len)
{
char *dest = (char *)&buffer[0];
int p = 0;
while( len > 0 )
{
POSIX_LABEL(file_contents1);
int d = (int)fread(dest + p,1,len,file);
if( d <= 0 )
{
HANDLE_FINTR(file,file_contents1);
fclose(file);
file_error("file_contents",name);
}
p += d;
len -= d;
}
}
fclose(file);
hx::ExitGCFreeZone();
return buffer;
}
Dynamic _hx_std_file_stdin()
{
fio *f = new fio();
f->create(stdin, HX_CSTRING("stdin"), false);
return f;
}
Dynamic _hx_std_file_stdout()
{
fio *f = new fio();
f->create(stdout, HX_CSTRING("stdout"), false);
return f;
}
Dynamic _hx_std_file_stderr()
{
fio *f = new fio();
f->create(stderr, HX_CSTRING("stderr"), false);
return f;
}