-
Notifications
You must be signed in to change notification settings - Fork 5
/
table.c
2242 lines (1831 loc) · 49.9 KB
/
table.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 1999-2018, University of Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <config.h>
#define O_ORDER /* include order.c package */
#ifndef BUILD_DATE
#define BUILD_DATE __DATE__
#endif
#include <SWI-Stream.h>
#include "table.h"
#include "error.h"
#include <stdlib.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_FLOATINGPOINT_H
#include <floatingpoint.h> /* strtod() prototype */
#endif
#ifdef O_DEBUG
#define DEBUG(g) g
#else
#define DEBUG(g)
#endif
#ifdef O_ORDER
#include "order.h"
#endif
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#undef offsetof
#define offsetof(t, f) ((intptr_t)&(((t*)0)->f))
#define sizeofquery(t) offsetof(query, field[t->nfields])
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SWI-Prolog management of external tables.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
open_table(+File, +Fields, +Options, -Handle)
Open a file as a table. Fields is a list of field-types occuring
in the file. A field type is one of:
atom read field as atom
string read field as SWI-Prolog string
code_list read field as list of ASCII codes
integer read field as integer
hexadecimal read field as hex integer
float read field as floating point number
Options is a list of options. Each option is of the form
<Name>(<Value>>). Defined options are:
record_separator(Code)
Defines the separator between two
records. Default is 10 (newline)
field_separator(Code)
Defines the separator between two
fields in a record. Default is
32 (space), implying all blank
space.
sorted(Field)
Defines the file is sorted in the
specified field name. In this
case lookup will exploit binary
search if possible.
unique(Field)
Defines the field to be unique.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*******************************
* PROTOTYPES *
*******************************/
static foreign_t unify_field_info(term_t t, Field field);
/*******************************
* CONSTANTS *
*******************************/
#ifndef EOS
#define EOS (0)
#endif
#define isBlank(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' )
static atom_t ATOM_atom;
static atom_t ATOM_code_list;
static atom_t ATOM_downcase;
static atom_t ATOM_eq;
static atom_t ATOM_escape;
static atom_t ATOM_exact;
static atom_t ATOM_field;
static atom_t ATOM_field_count;
static atom_t ATOM_field_separator;
static atom_t ATOM_file;
static atom_t ATOM_float;
static atom_t ATOM_functor;
static atom_t ATOM_integer;
static atom_t ATOM_hexadecimal;
static atom_t ATOM_key_field;
static atom_t ATOM_map_space_to_underscore;
static atom_t ATOM_prefix;
static atom_t ATOM_record;
static atom_t ATOM_record_separator;
static atom_t ATOM_size;
static atom_t ATOM_skip;
static atom_t ATOM_sorted;
static atom_t ATOM_string;
static atom_t ATOM_substring;
static atom_t ATOM_syntax;
static atom_t ATOM_unique;
static atom_t ATOM_width;
static atom_t ATOM_window;
static atom_t ATOM_arg;
static atom_t ATOM_encoding;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_utf8;
static atom_t ATOM_native;
static functor_t FUNCTOR_minus2;
static void
init_constants()
{
ATOM_arg = PL_new_atom("arg");
ATOM_atom = PL_new_atom("atom");
ATOM_code_list = PL_new_atom("code_list");
ATOM_downcase = PL_new_atom("downcase");
ATOM_eq = PL_new_atom("=");
ATOM_escape = PL_new_atom("escape");
ATOM_exact = PL_new_atom("exact");
ATOM_field = PL_new_atom("field");
ATOM_field_count = PL_new_atom("field_count");
ATOM_field_separator = PL_new_atom("field_separator");
ATOM_file = PL_new_atom("file");
ATOM_float = PL_new_atom("float");
ATOM_functor = PL_new_atom("functor");
ATOM_integer = PL_new_atom("integer");
ATOM_hexadecimal = PL_new_atom("hexadecimal");
ATOM_key_field = PL_new_atom("key_field");
ATOM_map_space_to_underscore = PL_new_atom("map_space_to_underscore");
ATOM_prefix = PL_new_atom("prefix");
ATOM_record = PL_new_atom("record");
ATOM_record_separator = PL_new_atom("record_separator");
ATOM_size = PL_new_atom("size");
ATOM_skip = PL_new_atom("skip");
ATOM_sorted = PL_new_atom("sorted");
ATOM_string = PL_new_atom("string");
ATOM_substring = PL_new_atom("substring");
ATOM_syntax = PL_new_atom("syntax");
ATOM_unique = PL_new_atom("unique");
ATOM_width = PL_new_atom("width");
ATOM_window = PL_new_atom("window");
ATOM_encoding = PL_new_atom("encoding");
ATOM_iso_latin_1 = PL_new_atom("iso_latin_1");
ATOM_utf8 = PL_new_atom("utf8");
ATOM_native = PL_new_atom("native");
FUNCTOR_minus2 = PL_new_functor(PL_new_atom("-"), 2);
}
/*******************************
* ERRORS *
*******************************/
static int
domain_error(term_t actual, const char *expected)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, "domain_error", 2,
PL_CHARS, expected,
PL_TERM, actual,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
type_error(term_t actual, const char *expected)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, "type_error", 2,
PL_CHARS, expected,
PL_TERM, actual,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
existence_error(term_t actual, const char *expected)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR_CHARS, "error", 2,
PL_FUNCTOR_CHARS, "existence_error", 2,
PL_CHARS, expected,
PL_TERM, actual,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
format_error(const char *pred, size_t pos, Field f)
{ char buf[1024];
sprintf(buf, "%s: bad record, field %d (%s), char-index %ld",
pred, f->index, PL_atom_chars(f->name), (long)pos);
return PL_representation_error(buf);
}
/*******************************
* HIGH-LEVEL GET-* *
*******************************/
static int
get_size_ex(term_t t, size_t *v)
{ int64_t i;
if ( PL_get_int64(t, &i) )
{ if ( i < 0 )
return domain_error(t, "nonneg"),FALSE;
*v = (size_t)i; /* TBD: Check on 32-bit systems */
return TRUE;
}
return type_error(t, "integer"),FALSE;
}
static int
get_offset_ex(term_t t, table_offset_t *v)
{ int64_t i;
if ( PL_get_int64(t, &i) )
{ if ( i < 0 )
return domain_error(t, "nonneg"),FALSE;
*v = (table_offset_t)i; /* TBD: Check on 32-bit systems */
return TRUE;
}
return type_error(t, "integer"),FALSE;
}
static int
get_table_ex(term_t handle, Table *table)
{ int64_t l;
if ( PL_get_int64(handle, &l) )
{ Table t = (Table)(intptr_t)l;
if ( t->magic == TABLE_MAGIC )
{ *table = t;
return TRUE;
}
return existence_error(handle, "table");
}
return type_error(handle, "table");
}
static int
get_type(atom_t typename, int *type)
{ if ( typename == ATOM_atom )
*type = FIELD_ATOM;
else if ( typename == ATOM_string )
*type = FIELD_STRING;
else if ( typename == ATOM_code_list )
*type = FIELD_CODELIST;
else if ( typename == ATOM_integer )
*type = FIELD_INTEGER;
else if ( typename == ATOM_hexadecimal )
*type = FIELD_HEX;
else if ( typename == ATOM_float )
*type = FIELD_FLOAT;
else
return FALSE;
return TRUE;
}
static int
get_field_flag(atom_t name, term_t arg, Field field)
{ if ( name == ATOM_sorted )
{ field->flags |= FIELD_SORTED;
if ( arg )
{ atom_t tab;
if ( !PL_get_atom(arg, &tab) ||
!(field->ord = findOrdTable(tab)) )
return FALSE;
}
} else if ( name == ATOM_unique && arg == 0 )
field->flags |= FIELD_UNIQUE;
else if ( name == ATOM_downcase && arg == 0 )
field->flags |= FIELD_DOWNCASE;
else if ( name == ATOM_syntax && arg == 0 )
field->flags |= FIELD_ALLOWBADNUM;
else if ( name == ATOM_map_space_to_underscore && arg == 0 )
field->flags |= FIELD_MAPSPACETOUNDERSCORE;
else if ( name == ATOM_width && arg )
return PL_get_integer(arg, &field->width);
else if ( name == ATOM_arg && arg )
return PL_get_integer(arg, &field->arg);
else if ( name == ATOM_skip && !arg )
field->arg = 0;
else
return FALSE;
return TRUE;
}
static int
default_escape_table(Table t)
{ int i;
if ( !(t->escape_table = malloc(256)) )
return PL_resource_error("memory");
for(i=0; i<256; i++)
t->escape_table[i] = i;
if ( t->escape == '\\' )
{ t->escape_table['b'] = '\b';
t->escape_table['e'] = 27;
t->escape_table['n'] = '\n';
t->escape_table['r'] = '\r';
t->escape_table['t'] = '\t';
}
return TRUE;
}
static int
get_char(term_t t, int *chr)
{ int i;
if ( !PL_get_integer(t, &i) || i < 0 || i > 255 )
return FALSE;
*chr = i;
return TRUE;
}
static foreign_t
pl_new_table(term_t file, term_t columns, term_t options, term_t handle)
{ Table table = malloc(sizeof(*table)); /* table to create */
field fields[MAXFIELDS]; /* scratch field structures */
int nfields=0; /* # collected fields */
int defarg=1; /* default argument */
term_t tail = PL_copy_term_ref(columns); /* for enumerating lists */
term_t head = PL_new_term_ref(); /* scratch list-head */
term_t arg = PL_new_term_ref(); /* scratch argument */
memset(table, 0, sizeof(*table));
table->record_functor = 0; /* not filled */
table->keyfield = -1;
table->escape = -1;
table->escape_table = NULL;
table->encoding = REP_ISO_LATIN_1;
if ( !PL_get_atom(file, &table->file) )
{ free(table);
return error(ERR_INSTANTIATION, "open_table/4", 1, file);
}
while(PL_get_list(tail, head, tail))
{ size_t arity;
atom_t typename;
if ( !PL_get_name_arity(head, &fields[nfields].name, &arity) ||
arity < 1 || arity > 2 ||
!PL_get_arg(1, head, arg) ||
!PL_get_atom(arg, &typename) ||
!get_type(typename, &fields[nfields].type) )
{ free(table);
return error(ERR_INSTANTIATION, "new_table/4", 2, columns);
}
fields[nfields].index = nfields; /* index number (0..) */
fields[nfields].width = 0; /* variable-width field */
fields[nfields].flags = 0; /* default */
fields[nfields].arg = defarg; /* default field identifier */
fields[nfields].ord = NULL; /* ordering table */
if ( arity == 2 )
{ fid_t fid = PL_open_foreign_frame();
term_t tail2 = PL_new_term_ref();
term_t head2 = PL_new_term_ref();
term_t arg2 = PL_new_term_ref();
_PL_get_arg(2, head, tail2);
while(PL_get_list(tail2, head2, tail2))
{ atom_t a;
size_t optarity;
if ( PL_get_name_arity(head2, &a, &optarity) )
{ if ( optarity == 1 )
{ _PL_get_arg(1, head2, arg2);
if ( !get_field_flag(a, arg2, &fields[nfields]) )
goto colerr;
} else
{ if ( !get_field_flag(a, 0, &fields[nfields]) )
goto colerr;
}
} else
{ colerr:
free(table);
return error(ERR_INSTANTIATION, "new_table/4", 2, columns);
}
}
if ( !PL_get_nil(tail2) )
{ free(table);
return error(ERR_INSTANTIATION, "new_table/4", 2, columns);
}
if ( fields[nfields].flags & FIELD_SORTED )
table->keyfield = nfields;
PL_close_foreign_frame(fid);
}
if ( fields[nfields].arg > 0 )
defarg = fields[nfields].arg + 1;
nfields++;
}
if ( !PL_get_nil(tail) )
{ free(table);
return error(ERR_INSTANTIATION, "new_table/4", 2, columns);
}
table->record_sep = '\n';
table->field_sep = ' ';
if ( !PL_put_term(tail, options) )
return FALSE;
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
if ( !PL_get_name_arity(head, &name, &arity) )
goto err3;
if ( name == ATOM_escape && arity == 2 )
{ term_t head2 = PL_new_term_ref();
term_t tail2 = PL_new_term_ref();
_PL_get_arg(1, head, arg);
if ( !PL_get_integer(arg, &table->escape) )
goto err3;
if ( !default_escape_table(table) )
{ free(table);
return FALSE;
}
_PL_get_arg(2, head, tail2);
while(PL_get_list(tail2, head2, tail2))
{ atom_t name;
size_t arity;
int f, t;
if ( !PL_get_name_arity(head2, &name, &arity) ||
name != ATOM_eq || arity != 2 )
goto err3;
if ( !PL_get_arg(1, head2, arg) ||
!get_char(arg, &f) ||
!PL_get_arg(2, head2, arg) ||
!get_char(arg, &t) )
goto err3;
table->escape_table[f] = t;
}
if ( PL_get_nil(tail2) )
continue;
goto err3;
}
if ( arity != 1 )
goto err3;
_PL_get_arg(1, head, arg);
if ( name == ATOM_record_separator )
{ if ( !PL_get_integer(arg, &table->record_sep) )
goto err3;
} else if ( name == ATOM_field_separator )
{ if ( !PL_get_integer(arg, &table->field_sep) )
goto err3;
} else if ( name == ATOM_functor )
{ if ( !PL_get_functor(arg, &table->record_functor) )
goto err3;
} else if ( name == ATOM_encoding )
{ atom_t enc;
if ( !PL_get_atom_ex(arg, &enc) )
goto err3;
if ( enc == ATOM_iso_latin_1 )
table->encoding = REP_ISO_LATIN_1;
else if ( enc == ATOM_utf8 )
table->encoding = REP_UTF8;
else if ( enc == ATOM_native )
table->encoding = REP_MB;
else
{ PL_domain_error("encoding", arg);
goto err3;
}
} else
goto err3;
}
if ( !PL_get_nil(tail) )
{ err3:
free(table);
return error(ERR_INSTANTIATION, "new_table/4", 3, options);
}
table->nfields = nfields;
table->fields = malloc(sizeof(struct fieldtag)*nfields);
memcpy(&table->fields[0], fields, sizeof(struct fieldtag)*nfields);
if ( !table->record_functor )
{ int maxarg=0;
int i;
for(i=0; i<nfields; i++)
maxarg = max(maxarg, fields[i].arg);
table->record_functor = PL_new_functor(ATOM_record, maxarg);
}
table->magic = TABLE_MAGIC;
table->size = -1;
table->buffer = NULL;
table->window = NULL;
#ifdef __WINDOWS__
table->hfile = NULL;
table->hmap = NULL;
#endif
#ifdef HAVE_MMAP
table->fd = -1;
#endif
return PL_unify_integer(handle, (intptr_t)table);
}
static int
open_table(Table table)
{ if ( !table->opened )
{
#ifdef __WINDOWS__
BY_HANDLE_FILE_INFORMATION info;
table->hfile = CreateFile(PL_atom_chars(table->file),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if ( !table->hfile )
goto errio;
if ( !GetFileInformationByHandle(table->hfile, &info) )
goto errio;
table->size = info.nFileSizeLow;
if ( table->size > 0 )
{ table->hmap = CreateFileMapping(table->hfile,
NULL,
PAGE_READONLY,
0L,
(DWORD)table->size, /* Truncated? */
NULL);
if ( !table->hmap )
goto errio;
table->buffer = MapViewOfFile(table->hmap,
FILE_MAP_READ,
0L, 0L, /* offset */
0L); /* size (0=all) */
if ( !table->buffer )
goto errio;
}
table->window = table->buffer;
table->window_size = table->size;
table->opened = TRUE;
return TRUE;
/* error happened */
errio:
{ int id = GetLastError();
if ( table->hmap )
CloseHandle(table->hmap);
if ( table->hfile )
CloseHandle(table->hfile);
table->buffer = NULL;
table->window = NULL;
table->hfile = NULL;
table->hmap = NULL;
return error(ERR_IO, "open_table/1", id, NULL);
}
#elif defined(HAVE_MMAP)
struct stat buf;
#ifndef MAP_NORESERVE
#define MAP_NORESERVE 0
#endif
if ( (table->fd = open(PL_atom_chars(table->file), O_RDONLY)) < 0 )
goto errio;
if ( fstat(table->fd, &buf) < 0 )
goto errio;
table->size = buf.st_size;
if ( table->size > 0 )
{ if ( (table->buffer = mmap(NULL, table->size,
PROT_READ, MAP_SHARED|MAP_NORESERVE,
table->fd, 0)) == (char *) -1 )
goto errio;
}
close(table->fd);
table->fd = -1;
table->window = table->buffer;
table->window_size = table->size;
table->opened = TRUE;
return TRUE;
/* error happened */
errio:
if ( table->buffer )
munmap(table->buffer, table->size);
if ( table->fd >= 0 )
close(table->fd);
table->buffer = NULL;
table->window = NULL;
table->fd = -1;
return error(ERR_IO, "open_table/1", errno, NULL);
#endif
}
return TRUE;
}
static foreign_t
pl_table_window(term_t handle, term_t start, term_t size)
{ Table table;
size_t from;
size_t wsize;
if ( !get_table_ex(handle, &table) )
return FALSE;
if ( !get_size_ex(start, &from) )
return FALSE;
if ( !get_size_ex(size, &wsize) )
return FALSE;
if ( from > table->size )
from = table->size;
table->window = table->buffer + from;
if ( table->window + wsize > table->buffer + table->size )
wsize = (table->buffer + table->size) - table->window;
table->window_size = wsize;
PL_succeed;
}
static foreign_t
pl_get_table_attribute(term_t handle, term_t name, term_t value)
{ Table table;
atom_t n;
size_t arity;
if ( !get_table_ex(handle, &table) )
return FALSE;
if ( PL_get_name_arity(name, &n, &arity) )
{ if ( n == ATOM_file && arity == 0 )
return PL_unify_atom(value, table->file);
if ( n == ATOM_field && arity == 1 )
{ term_t a = PL_new_term_ref();
int i;
_PL_get_arg(1, name, a);
if ( PL_get_integer(a, &i) )
{ if ( i >= 1 && i <= table->nfields )
return unify_field_info(value, &table->fields[i-1]);
return FALSE;
}
goto ierr2;
}
if ( n == ATOM_field_separator && arity == 0 )
return PL_unify_integer(value, table->field_sep);
if ( n == ATOM_record_separator && arity == 0 )
return PL_unify_integer(value, table->record_sep);
if ( n == ATOM_field_count && arity == 0 )
return PL_unify_integer(value, table->nfields);
if ( n == ATOM_key_field && arity == 0 )
{ if ( table->keyfield >= 0 )
return PL_unify_integer(value, table->keyfield+1);
else
return FALSE;
}
if ( !open_table(table) )
return FALSE;
if ( n == ATOM_size && arity == 0 )
return PL_unify_integer(value, table->size);
if ( n == ATOM_window && arity == 0 )
return PL_unify_term(value,
PL_FUNCTOR, FUNCTOR_minus2,
PL_LONG, table->window - table->buffer,
PL_LONG, table->window_size);
}
ierr2:
return error(ERR_INSTANTIATION, "get_table_attribute/3", 2, name);
}
static foreign_t
unify_field_info(term_t t, Field field) /* name(Type, Flags) */
{ term_t flags = PL_new_term_ref();
term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(flags);
int options = 0;
atom_t type;
switch(field->type)
{ case FIELD_ATOM:
type = ATOM_atom;
break;
case FIELD_STRING:
type = ATOM_string;
break;
case FIELD_CODELIST:
type = ATOM_code_list;
break;
case FIELD_INTEGER:
type = ATOM_integer;
break;
case FIELD_HEX:
type = ATOM_hexadecimal;
break;
case FIELD_FLOAT:
type = ATOM_float;
break;
default:
type = 0;
assert(0);
}
if ( field->flags & FIELD_UNIQUE )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_atom(head, ATOM_unique) )
return FALSE;
options++;
}
if ( field->flags & FIELD_DOWNCASE )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_atom(head, ATOM_downcase) )
return FALSE;
options++;
}
if ( field->flags & FIELD_ALLOWBADNUM )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_atom(head, ATOM_syntax) )
return FALSE;
options++;
}
if ( field->flags & FIELD_MAPSPACETOUNDERSCORE )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_atom(head, ATOM_map_space_to_underscore) )
return FALSE;
options++;
}
if ( field->flags & FIELD_SORTED )
{ if ( !PL_unify_list(tail, head, tail) )
return FALSE;
if ( field->ord )
{ if ( !PL_unify_term(head, PL_FUNCTOR, PL_new_functor(ATOM_sorted, 1),
PL_ATOM, field->ord->name) )
return FALSE;
} else
{ if ( !PL_unify_atom(head, ATOM_sorted) )
return FALSE;
}
options++;
}
if ( field->width > 0 )
{ if ( !PL_unify_term(head, PL_FUNCTOR, PL_new_functor(ATOM_width, 1),
PL_INT, field->width) )
return FALSE;
options++;
}
if ( field->arg > 0 )
{ if ( !PL_unify_term(head, PL_FUNCTOR, PL_new_functor(ATOM_arg, 1),
PL_INT, field->arg) )
return FALSE;
options++;
}
if ( options )
{ return (PL_unify_nil(tail) &&
PL_unify_term(t, PL_FUNCTOR, PL_new_functor(field->name, 2),
PL_ATOM, type,
PL_TERM, flags));
} else
{ return PL_unify_term(t, PL_FUNCTOR, PL_new_functor(field->name, 1),
PL_ATOM, type);
}
}
static foreign_t
pl_open_table(term_t handle)
{ Table table;
if ( !get_table_ex(handle, &table) )
return FALSE;
return open_table(table);
}
static foreign_t
pl_close_table(term_t handle)
{ Table table;
if ( !get_table_ex(handle, &table) )
return FALSE;
if ( table->buffer )
{
#ifdef __WINDOWS__
if ( table->buffer )
UnmapViewOfFile(table->buffer);
if ( table->hmap )
CloseHandle(table->hmap);
if ( table->hfile )
CloseHandle(table->hfile);
table->hfile = NULL;
table->hmap = NULL;
#endif
#ifdef HAVE_MMAP
if ( table->buffer )
munmap(table->buffer, table->size);
if ( table->fd >= 0 )
close(table->fd);
table->fd = -1;
#endif
table->size = -1;
table->buffer = NULL;
table->window = NULL;
table->opened = FALSE;
}
PL_succeed;
}
static foreign_t
pl_free_table(term_t handle)
{ Table table;
if ( !pl_close_table(handle) )
PL_fail;
if ( !get_table_ex(handle, &table) )
return FALSE;
table->magic = 0; /* so it won't be recognised */
if ( table->escape_table )
free(table->escape_table);
free(table->fields);
free(table);
PL_succeed;
}
/*******************************
* POSITIONING *