forked from SWI-Prolog/packages-clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfile.c
554 lines (444 loc) · 13.5 KB
/
memfile.c
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2012, University of Amsterdam
VU University Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "error.h"
#define streq(s,q) (strcmp((s), (q)) == 0)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Memory-files
make_memory_file(-Handle)
free_memory_file(+Handle)
open_memory_file(+Handle, +Mode, -Stream)
size_memory_file(+Handle, -Size)
memory_file_to_codes(+Handle, -Codes)
memory_file_to_atom(+Handle, -Atom)
atom_to_memory_file(+Atom, -Handle)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static functor_t FUNCTOR_memory_file1;
static atom_t ATOM_encoding;
static atom_t ATOM_unknown;
static atom_t ATOM_octet;
static atom_t ATOM_ascii;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_text;
static atom_t ATOM_utf8;
static atom_t ATOM_unicode_be;
static atom_t ATOM_unicode_le;
static atom_t ATOM_wchar_t;
static atom_t ATOM_read;
static atom_t ATOM_write;
static atom_t ATOM_free_on_close;
#define MEMFILE_MAGIC 0x5624a6b3L
#define NOSIZE ((size_t)-1)
typedef struct
{ long magic; /* MEMFILE_MAGIC */
IOENC encoding; /* encoding of the data */
int free_on_close; /* free if it is closed */
char *data; /* data of the file */
size_t data_size; /* byte-size of data */
size_t size; /* size in characters */
IOSTREAM *stream; /* Stream hanging onto it */
atom_t atom; /* Created from atom */
} memfile;
static int
unify_memfile(term_t handle, memfile *f)
{ if ( PL_unify_term(handle,
PL_FUNCTOR, FUNCTOR_memory_file1,
PL_POINTER, f) )
return TRUE;
if ( !PL_is_variable(handle) )
return PL_uninstantiation_error(handle);
return FALSE; /* (resource) error */
}
static int
get_memfile(term_t handle, memfile **f)
{ if ( PL_is_functor(handle, FUNCTOR_memory_file1) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, handle, a);
if ( PL_get_pointer(a, &ptr) )
{ memfile *m = ptr;
if ( m->magic == MEMFILE_MAGIC )
{ *f = ptr;
return TRUE;
}
return pl_error(NULL, 0, NULL, ERR_EXISTENCE,
"memory_file", handle);
}
}
return pl_error(NULL, 0, NULL, ERR_ARGTYPE, 1,
handle, "memory_file");
}
static foreign_t
new_memory_file(term_t handle)
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return pl_error(NULL, 0, NULL, ERR_ERRNO, errno,
"create", "memory_file", handle);
m->magic = MEMFILE_MAGIC;
m->encoding = ENC_UTF8;
m->data = 0;
m->size = 0;
if ( unify_memfile(handle, m) )
return TRUE;
m->magic = 0;
free(m);
return FALSE;
}
static int
destroy_memory_file(memfile *m)
{ if ( m->stream )
Sclose(m->stream);
if ( m->atom )
PL_unregister_atom(m->atom);
else if ( m->data )
Sfree(m->data); /* MS-Windows: malloc by other DLL! */
m->magic = 0;
free(m);
return TRUE;
}
static foreign_t
free_memory_file(term_t handle)
{ memfile *m;
if ( get_memfile(handle, &m) )
return destroy_memory_file(m);
return FALSE;
}
static void
closehook(void *closure)
{ memfile *m = closure;
m->stream = NULL;
if ( m->free_on_close )
destroy_memory_file(m);
}
static foreign_t
alreadyOpen(term_t handle, const char *op)
{ return pl_error(NULL, 0, "already open",
ERR_PERMISSION, handle, op, "memory_file");
}
static struct encname
{ IOENC code;
atom_t *name;
} encoding_names[] =
{ { ENC_UNKNOWN, &ATOM_unknown },
{ ENC_OCTET, &ATOM_octet },
{ ENC_ASCII, &ATOM_ascii },
{ ENC_ISO_LATIN_1, &ATOM_iso_latin_1 },
{ ENC_ANSI, &ATOM_text },
{ ENC_UTF8, &ATOM_utf8 },
{ ENC_UNICODE_BE, &ATOM_unicode_be },
{ ENC_UNICODE_LE, &ATOM_unicode_le },
{ ENC_WCHAR, &ATOM_wchar_t },
{ ENC_UNKNOWN, NULL },
};
static IOENC
atom_to_encoding(atom_t a)
{ struct encname *en;
for(en=encoding_names; en->name; en++)
{ if ( *en->name == a )
return en->code;
}
return ENC_UNKNOWN;
}
static int
get_encoding(term_t t, IOENC *enc)
{ atom_t en;
if ( PL_get_atom(t, &en) )
{ IOENC encoding;
if ( (encoding = atom_to_encoding(en)) == ENC_UNKNOWN )
return pl_error(NULL, 0, NULL, ERR_DOMAIN, t, "encoding");
*enc = encoding;
return TRUE;
}
return pl_error(NULL, 0, NULL, ERR_TYPE, t, "encoding");
}
static foreign_t
open_memory_file4(term_t handle, term_t mode, term_t stream, term_t options)
{ memfile *m;
char *x;
atom_t iom;
IOSTREAM *fd;
IOENC encoding;
int free_on_close = FALSE;
if ( !get_memfile(handle, &m) )
return FALSE;
if ( m->stream )
return alreadyOpen(handle, "open");
if ( !PL_get_atom(mode, &iom) )
return pl_error("open_memory_file", 3, NULL, ERR_ARGTYPE, 2,
mode, "io_mode");
encoding = m->encoding;
if ( options )
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
while(PL_get_list(tail, head, tail))
{ int arity;
atom_t name;
if ( PL_get_name_arity(head, &name, &arity) && arity == 1 )
{ term_t arg = PL_new_term_ref();
_PL_get_arg(1, head, arg);
if ( name == ATOM_encoding )
{ if ( !get_encoding(arg, &encoding) )
return FALSE;
} else if ( name == ATOM_free_on_close )
{ if ( !PL_get_bool(arg, &free_on_close) )
return pl_error("open_memory_file", 4, NULL, ERR_TYPE,
arg, "boolean");
}
} else
return pl_error("open_memory_file", 4, NULL, ERR_TYPE, head, "option");
}
if ( !PL_get_nil(tail) )
return pl_error("open_memory_file", 4, NULL, ERR_TYPE, tail, "list");
}
if ( iom == ATOM_write )
{ x = "w";
if ( m->atom )
return pl_error("open_memory_file", 3, NULL, ERR_PERMISSION,
handle, "write", "memory_file");
if ( m->data )
{ Sfree(m->data);
m->data = NULL;
}
m->data_size = 0;
m->size = NOSIZE; /* don't know */
m->encoding = encoding;
} else if ( iom == ATOM_read )
{ x = "r";
m->free_on_close = free_on_close;
} else
{ return pl_error("open_memory_file", 3, NULL, ERR_DOMAIN,
mode, "io_mode");
}
if ( !(fd = Sopenmem(&m->data, &m->data_size, x)) )
return pl_error("open_memory_file", 3, NULL, ERR_ERRNO, errno,
"create", "memory_file", handle);
fd->close_hook = closehook;
fd->closure = m;
fd->encoding = encoding;
m->stream = fd;
if ( PL_unify_stream(stream, fd) )
{ return TRUE;
} else
{ Sclose(fd);
return FALSE;
}
}
static foreign_t
open_memory_file(term_t handle, term_t mode, term_t stream)
{ return open_memory_file4(handle, mode, stream, 0);
}
static foreign_t
size_memory_file(term_t handle, term_t sizeh, term_t encoding)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ if ( m->stream && !m->atom )
return alreadyOpen(handle, "size");
if ( m->data )
{ IOENC size_enc = m->encoding;
size_t size;
if ( encoding )
{ if ( !get_encoding(encoding, &size_enc) )
return FALSE;
} else
size_enc = m->encoding;
if ( m->size != NOSIZE && size_enc == m->encoding )
{ size = m->size;
} else
{ switch( size_enc )
{ case ENC_ISO_LATIN_1:
case ENC_OCTET:
size = m->data_size;
break;
case ENC_WCHAR:
size = m->data_size / sizeof(wchar_t);
break;
case ENC_UTF8:
size = PL_utf8_strlen(m->data, m->data_size);
break;
default:
assert(0);
return FALSE;
}
if ( size_enc == m->encoding )
m->size = size;
}
return PL_unify_int64(sizeh, size);
} else
{ return PL_unify_integer(sizeh, 0);
}
}
return FALSE;
}
static foreign_t
size_memory_file2(term_t handle, term_t size)
{ return size_memory_file(handle, size, 0);
}
static foreign_t
size_memory_file3(term_t handle, term_t size, term_t encoding)
{ return size_memory_file(handle, size, encoding);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
utf8_position_memory_file(+MF, -Here, -Size)
Given MF is a UTF-8 encoded memory file, unify here with the
byte-position of the read-pointer and Size with the total size of the
memory file in bytes. This is a bit hacky predicate, but the information
is easily available at low cost, while it is very valuable for producing
answers in content-length computation of the HTTP server. See
http_wrapper.pl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static foreign_t
utf8_position(term_t handle, term_t here, term_t size)
{ memfile *m;
if ( !get_memfile(handle, &m) )
return FALSE;
if ( m->encoding != ENC_UTF8 )
return pl_error(NULL, 0, "no UTF-8 encoding",
ERR_PERMISSION, handle, "utf8_position", "memory_file");
if ( !PL_unify_integer(size, m->data_size) )
return FALSE;
if ( m->stream )
{ IOPOS *op = m->stream->position;
long p;
m->stream->position = NULL;
p = Stell(m->stream);
m->stream->position = op;
return PL_unify_integer(here, p);
} else
return PL_unify_integer(here, 0);
}
static foreign_t
atom_to_memory_file(term_t atom, term_t handle)
{ atom_t a;
if ( PL_get_atom(atom, &a) )
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return pl_error(NULL, 0, NULL, ERR_ERRNO, errno, "create", "memory_file", handle);
m->atom = a;
PL_register_atom(m->atom);
m->magic = MEMFILE_MAGIC;
if ( (m->data = (char *)PL_atom_nchars(a, &m->size)) )
{ m->encoding = ENC_ISO_LATIN_1;
m->data_size = m->size;
} else if ( (m->data = (char *)PL_atom_wchars(a, &m->size)) )
{ m->encoding = ENC_WCHAR;
m->data_size = m->size * sizeof(wchar_t);
} else if ( PL_blob_data(a, &m->size, NULL) )
{ m->data = PL_blob_data(a, &m->data_size, NULL);
m->encoding = ENC_OCTET;
m->size = m->data_size;
}
if ( unify_memfile(handle, m) )
return TRUE;
else
{ PL_unregister_atom(m->atom);
m->magic = 0;
free(m);
return FALSE;
}
} else
{ return pl_error(NULL, 0, NULL, ERR_ARGTYPE, 1,
atom, "atom");
}
}
static foreign_t
memory_file_to_text(term_t handle, term_t atom, term_t encoding, int flags)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ IOENC enc;
if ( encoding )
{ if ( !get_encoding(encoding, &enc) )
return FALSE;
} else
enc = m->encoding;
if ( m->stream && (m->stream->flags & SIO_OUTPUT))
return alreadyOpen(handle, "to_atom");
if ( m->data )
{ switch(enc)
{ case ENC_ISO_LATIN_1:
case ENC_OCTET:
return PL_unify_chars(atom, flags, m->data_size, m->data);
case ENC_WCHAR:
return PL_unify_wchars(atom, flags,
m->data_size/sizeof(wchar_t),
(pl_wchar_t*)m->data);
case ENC_UTF8:
return PL_unify_chars(atom, flags|REP_UTF8, m->data_size, m->data);
default:
assert(0);
}
} else
return PL_unify_chars(atom, flags, 0, "");
}
return FALSE;
}
static foreign_t
memory_file_to_atom2(term_t handle, term_t atom)
{ return memory_file_to_text(handle, atom, 0, PL_ATOM);
}
static foreign_t
memory_file_to_atom3(term_t handle, term_t atom, term_t encoding)
{ return memory_file_to_text(handle, atom, encoding, PL_ATOM);
}
static foreign_t
memory_file_to_codes2(term_t handle, term_t atom)
{ return memory_file_to_text(handle, atom, 0, PL_CODE_LIST);
}
static foreign_t
memory_file_to_codes3(term_t handle, term_t atom, term_t encoding)
{ return memory_file_to_text(handle, atom, encoding, PL_CODE_LIST);
}
#define MKATOM(n) ATOM_ ## n = PL_new_atom(#n);
install_t
install_memfile()
{ if ( PL_query(PL_QUERY_VERSION) <= 50505 )
{ PL_warning("Requires SWI-Prolog version 5.5.6 or later");
return;
}
FUNCTOR_memory_file1 = PL_new_functor(PL_new_atom("$memory_file"), 1);
MKATOM(encoding);
MKATOM(unknown);
MKATOM(octet);
MKATOM(ascii);
MKATOM(iso_latin_1);
MKATOM(text);
MKATOM(utf8);
MKATOM(unicode_be);
MKATOM(unicode_le);
MKATOM(wchar_t);
MKATOM(read);
MKATOM(write);
MKATOM(free_on_close);
PL_register_foreign("new_memory_file", 1, new_memory_file, 0);
PL_register_foreign("free_memory_file", 1, free_memory_file, 0);
PL_register_foreign("size_memory_file", 2, size_memory_file2, 0);
PL_register_foreign("size_memory_file", 3, size_memory_file3, 0);
PL_register_foreign("open_memory_file", 3, open_memory_file, 0);
PL_register_foreign("open_memory_file", 4, open_memory_file4, 0);
PL_register_foreign("atom_to_memory_file", 2, atom_to_memory_file, 0);
PL_register_foreign("memory_file_to_atom", 2, memory_file_to_atom2, 0);
PL_register_foreign("memory_file_to_codes", 2, memory_file_to_codes2,0);
PL_register_foreign("memory_file_to_atom", 3, memory_file_to_atom3, 0);
PL_register_foreign("memory_file_to_codes", 3, memory_file_to_codes3,0);
PL_register_foreign("utf8_position_memory_file", 3, utf8_position, 0);
}