-
Notifications
You must be signed in to change notification settings - Fork 6
/
yaml4pl.c
1211 lines (998 loc) · 27.2 KB
/
yaml4pl.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) 2018-2023, VU University Amsterdam
SWI-Prolog Solutions b.v.
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.
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <yaml.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#ifdef _MSC_VER
#define strdup(s) _strdup(s)
#endif
atom_t ATOM_null;
atom_t ATOM_true;
atom_t ATOM_false;
atom_t ATOM_canonical;
atom_t ATOM_unicode;
atom_t ATOM_stream_start;
atom_t ATOM_stream_end;
atom_t ATOM_document_start;
atom_t ATOM_document_end;
atom_t ATOM_mapping_start;
atom_t ATOM_mapping_end;
atom_t ATOM_sequence_start;
atom_t ATOM_sequence_end;
atom_t ATOM_scalar;
atom_t ATOM_alias;
atom_t ATOM_plain;
atom_t ATOM_single_quoted;
atom_t ATOM_double_quoted;
atom_t ATOM_literal;
atom_t ATOM_folded;
functor_t FUNCTOR_mapping3;
functor_t FUNCTOR_sequence3;
functor_t FUNCTOR_eq2;
functor_t FUNCTOR_tag2;
functor_t FUNCTOR_error2;
functor_t FUNCTOR_yaml_error2;
#define N_TOP 0
#define N_MAPPING 1
#define N_SEQUENCE 2
typedef struct node
{ struct node *parent;
int type; /* N_* */
term_t key; /* Created key */
term_t doc; /* saved document tail */
} node;
static int yaml_read_handler(void *closure,
unsigned char *buffer, size_t size,
size_t *size_read);
static node *
push(node **stack, node *old)
{ node *n;
if ( (n=malloc(sizeof(*n))) )
{ *n = *old;
n->parent = *stack;
*stack = n;
return n;
}
PL_resource_error("memory");
return NULL;
}
static int
pop(node **stack, node *old)
{ node *n;
if ( (n=*stack) )
{ *old = *n;
*stack = n->parent;
free(n);
return TRUE;
}
assert(0);
return FALSE;
}
static int
extend(node *state, term_t tmp, term_t v)
{ switch(state->type)
{ case N_TOP:
return PL_unify(state->doc, v);
case N_SEQUENCE:
return PL_unify_list(state->doc, tmp, state->doc) &&
PL_unify(tmp, v);
case N_MAPPING:
if ( state->key )
{ term_t key = state->key;
state->key = 0;
return PL_unify_list(state->doc, tmp, state->doc) &&
PL_unify_term(tmp, PL_FUNCTOR, FUNCTOR_eq2,
PL_TERM, key, PL_TERM, v);
} else
{ state->key = v;
return TRUE;
}
default:
assert(0);
return FALSE;
}
}
static inline int
is_digit(int c)
{ return ( c >= '0' && c <= '9' );
}
static inline int
is_nonzero_digit(int c)
{ return ( c >= '1' && c <= '9' );
}
static inline int
is_odigit(int c)
{ return ( c >= '0' && c <= '7' );
}
static inline int
is_xdigit(int c)
{ return ( (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F') );
}
enum ntype
{ N_NONE = 0,
N_INT,
N_FLOAT,
};
/* See https://yaml.org/spec/1.2.2/#10214-floating-point */
static enum ntype
is_number(const char *s, size_t len)
{ if ( len > 0 )
{ const char *e = s+len;
int isfloat = FALSE;
if ( e>s && *s == '-' ) /* -? */
s++;
if ( e-s >= 3 && *s == '0' ) /* 0oN+$ or 0xN+$ */
{ s++;
if ( *s == 'o' )
{ s++;
if ( s < e )
{ while(is_odigit(*s))
s++;
}
return s == e ? N_INT : N_NONE;
} else if ( *s == 'x' )
{ s++;
if ( s < e )
{ while(is_xdigit(*s))
s++;
}
return s == e ? N_INT : N_NONE;
}
s--;
}
if ( e == s )
return N_NONE;
if ( *s == '0' ) /* ( 0 | ([1-9]+ [0-9]*) ) */
{ s++;
} else if ( is_nonzero_digit(*s) )
{ s++;
while(is_digit(*s))
s++;
} else
return N_NONE;
if ( e > s && *s == '.' ) /* ( \. [0-9]* ) */
{ s++;
if ( e > s && is_digit(*s) ) /* officical YAML says "0." __is__ allowed */
{ while(e > s && is_digit(*s)) /* we do not allow it. */
s++;
isfloat = TRUE;
} else
return N_NONE;
}
if ( e > s && (*s == 'e' || *s == 'E') ) /* ( \. [0-9]* )? ( [eE] [-+]? [0-9]+ )? */
{ s++;
if ( e > s && (*s == '+' || *s == '-') )
s++;
if ( e > s && is_digit(*s) )
{ s++;
while(e > s && is_digit(*s))
s++;
} else
return N_NONE;
isfloat = TRUE;
}
if ( s == e )
return isfloat ? N_FLOAT : N_INT;
}
return N_NONE;
}
static int
is_null(const char *s, size_t len)
{ if ( len == 4 &&
( strcmp(s, "null") == 0 ||
strcmp(s, "Null") == 0 ||
strcmp(s, "NULL") == 0 ) )
return TRUE;
if ( len == 1 && *s == '~' )
return TRUE;
return FALSE;
}
static int
is_true(const char *s, size_t len)
{ if ( len == 4 &&
( strcmp(s, "true") == 0 ||
strcmp(s, "True") == 0 ||
strcmp(s, "TRUE") == 0 ) )
return TRUE;
return FALSE;
}
static int
is_false(const char *s, size_t len)
{ if ( len == 5 &&
( strcmp(s, "false") == 0 ||
strcmp(s, "False") == 0 ||
strcmp(s, "FALSE") == 0 ) )
return TRUE;
return FALSE;
}
static int
is_special_float(const char *s, size_t len, double *d)
{ int neg = FALSE;
if ( len == 4 &&
( strcmp(s, ".nan") == 0 ||
strcmp(s, ".NaN") == 0 ||
strcmp(s, ".NAN") == 0 ) )
{ *d = NAN;
return TRUE;
}
if ( *s == '-' || *s == '+' )
{ neg = (*s == '-');
s++;
len--;
}
if ( len == 4 &&
( strcmp(s, ".inf") == 0 ||
strcmp(s, ".Inf") == 0 ||
strcmp(s, ".INF") == 0 ) )
{ if ( neg )
*d = -HUGE_VAL;
else
*d = HUGE_VAL;
return TRUE;
}
return FALSE;
}
static const char *
implicit_tag(const char *s)
{ enum ntype rc;
double d;
size_t len = strlen(s);
if ( (rc=is_number(s, len)) != N_NONE )
return rc == N_FLOAT ? "float" : "int";
else if ( is_null(s, len) )
return "null";
else if ( is_true(s, len) || is_false(s, len) )
return "bool";
else if ( is_special_float(s, len, &d) )
return "float";
else
return NULL;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Out a scalar. If it is tagged we put a term tag(Tag, String) and leave
the rest to the Prolog library. If it is not tagged we need to implement
"10.3.2. Tag Resolution" from http://yaml.org/spec/1.2/spec.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
put_scalar(term_t t, int iskey, const yaml_event_t *event)
{ char *s = (char*)event->data.scalar.value;
size_t len = event->data.scalar.length;
int flags = iskey ? PL_ATOM|REP_UTF8 : PL_STRING|REP_UTF8;
double d;
if ( event->data.scalar.tag )
{ term_t tmp = PL_new_term_ref();
return ( PL_put_functor(t, FUNCTOR_tag2) &&
PL_get_arg(1, t, tmp) &&
PL_unify_atom_chars(tmp, (char*)event->data.scalar.tag) &&
PL_get_arg(2, t, tmp) &&
PL_unify_chars(tmp, flags, len, s) );
}
if ( is_number(s, len) != N_NONE )
{ return PL_chars_to_term(s, t);
} else if ( is_null(s, len) )
{ return PL_unify_atom(t, ATOM_null);
} else if ( is_true(s, len) )
{ return PL_unify_atom(t, ATOM_true);
} else if ( is_false(s, len) )
{ return PL_unify_atom(t, ATOM_false);
} else if ( is_special_float(s, len, &d) )
{ return PL_unify_float(t, d);
} else
{ return PL_unify_chars(t, flags, len, s);
}
}
#define ANCHOR_TABLE_SIZE 16
typedef struct hcell
{ struct hcell *next;
char *name;
term_t value;
} hcell;
typedef struct htable
{ size_t count;
size_t size;
hcell **entries;
} htable;
static htable *
new_anchor_table(void)
{ htable *t;
size_t ebytes = sizeof(*t->entries) * ANCHOR_TABLE_SIZE;
if ( (t = malloc(sizeof(*t))) &&
(t->entries = malloc(ebytes)) )
{ t->count = 0;
t->size = ANCHOR_TABLE_SIZE;
memset(t->entries, 0, ebytes);
return t;
}
if ( t )
free(t);
PL_resource_error("memory");
return NULL;
}
static void
free_anchor_table(htable *t)
{ if ( t )
{ size_t i;
for(i=0; i<t->size; i++)
{ hcell *c = t->entries[i];
hcell *n;
for(; c; c=n)
{ n = c->next;
free(c->name);
free(c);
}
}
free(t->entries);
free(t);
}
}
#define SEED 0x6263533a
#define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
static unsigned int
MurmurHashAligned2(const void * key, size_t len, unsigned int seed)
{ const unsigned int m = 0x5bd1e995;
const int r = 24;
unsigned int h = seed ^ (unsigned int)len;
const unsigned char * data = (const unsigned char *)key;
while( len >= 4 )
{ unsigned int k;
k = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;
MIX(h,k,m);
data += 4;
len -= 4;
}
switch( len )
{ case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
static int
rehash(htable *t)
{ size_t newsize = t->size*2;
size_t ebytes = sizeof(*t->entries) * newsize;
hcell **new;
if ( (new=malloc(ebytes)) )
{ size_t i;
memset(new, 0, ebytes);
for(i=0; i<t->size; i++)
{ hcell *c = t->entries[i];
hcell *n;
for(; c; c=n)
{ int k = MurmurHashAligned2(c->name, strlen(c->name), SEED) % newsize;
n = c->next;
c->next = new[k];
new[k] = c;
}
}
free(t->entries);
t->size = newsize;
t->entries = new;
return FALSE;
}
return PL_resource_error("memory");
}
static int
add_anchor(htable *t, const char *name, term_t value)
{ const char *s = name;
unsigned int k;
hcell *c;
if ( t->count > t->size )
{ if ( !rehash(t) )
return FALSE;
}
k = MurmurHashAligned2(s, strlen(s), SEED) % t->size;
if ( (c=malloc(sizeof(*c))) )
{ c->name = strdup(s);
c->value = PL_copy_term_ref(value);
c->next = t->entries[k];
t->entries[k] = c;
t->count++;
return TRUE;
}
return PL_resource_error("memory");
}
term_t
find_anchor(htable *t, const char *name)
{ unsigned int k = MurmurHashAligned2(name, strlen(name), SEED) % t->size;
hcell *c;
for(c=t->entries[k]; c; c=c->next)
{ if ( strcmp(c->name, name) == 0 )
return c->value;
}
return 0;
}
static int
store_anchor(htable **anchors, term_t node, const yaml_event_t *event)
{ if ( event->data.alias.anchor )
{ if ( !*anchors )
{ if ( !(*anchors = new_anchor_table()) )
return FALSE;
}
return add_anchor(*anchors, (const char*)event->data.alias.anchor, node);
}
return TRUE;
}
static int
create_mapping(term_t t, const yaml_event_t *event)
{ if ( !PL_put_functor(t, FUNCTOR_mapping3) )
return FALSE;
if ( event->data.mapping_start.tag )
{ term_t tag = PL_new_term_ref();
if ( !PL_put_atom_chars(tag, (char*)event->data.mapping_start.tag) ||
!PL_cons_functor(t, FUNCTOR_tag2, tag, t) )
return FALSE;
}
return TRUE;
}
static int
get_mapping_mapping(term_t t)
{ if ( PL_is_functor(t, FUNCTOR_tag2) )
_PL_get_arg(2, t, t);
_PL_get_arg(1, t, t); /* mapping */
return TRUE;
}
static int
create_sequence(term_t t, const yaml_event_t *event)
{ if ( !PL_put_functor(t, FUNCTOR_sequence3) )
return FALSE;
if ( event->data.sequence_start.tag )
{ term_t tag = PL_new_term_ref();
if ( !PL_put_atom_chars(tag, (char*)event->data.sequence_start.tag) ||
!PL_cons_functor(t, FUNCTOR_tag2, tag, t) )
return FALSE;
}
return TRUE;
}
static int
get_sequence_list(term_t t)
{ if ( PL_is_functor(t, FUNCTOR_tag2) )
_PL_get_arg(2, t, t);
_PL_get_arg(1, t, t);
return TRUE;
}
static int
parse_document(yaml_parser_t *parser, term_t doc)
{ yaml_event_t event;
node *stack = NULL;
node state;
term_t tmp = PL_new_term_ref();
htable *anchor_table = NULL;
state.type = N_TOP;
state.key = 0;
state.doc = doc;
do
{ if ( !yaml_parser_parse(parser, &event) )
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex, PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_yaml_error2,
PL_INT, (int)parser->error,
PL_STRING, parser->problem,
PL_VARIABLE) )
PL_raise_exception(ex);
goto error;
}
switch(event.type)
{ case YAML_NO_EVENT:
break;
case YAML_STREAM_START_EVENT:
break;
case YAML_STREAM_END_EVENT:
break;
case YAML_DOCUMENT_START_EVENT:
break;
case YAML_DOCUMENT_END_EVENT:
break;
case YAML_SEQUENCE_START_EVENT:
{ term_t list;
if ( !(list = PL_new_term_ref()) ||
!create_sequence(list, &event) ||
!store_anchor(&anchor_table, list, &event) ||
!extend(&state, tmp, list) ||
!push(&stack, &state) ||
!get_sequence_list(list) )
goto error;
state.doc = list;
state.type = N_SEQUENCE;
break;
}
case YAML_SEQUENCE_END_EVENT:
if ( !PL_unify_nil(state.doc) ||
!pop(&stack, &state) )
goto error;
break;
case YAML_MAPPING_START_EVENT:
{ term_t mapping;
if ( !(mapping = PL_new_term_ref()) ||
!create_mapping(mapping, &event) ||
!store_anchor(&anchor_table, mapping, &event) ||
!extend(&state, tmp, mapping) ||
!push(&stack, &state) ||
!get_mapping_mapping(mapping) )
goto error;
state.doc = mapping;
state.type = N_MAPPING;
state.key = 0;
break;
}
case YAML_MAPPING_END_EVENT:
if ( !PL_unify_nil(state.doc) ||
!pop(&stack, &state) )
goto error;
break;
case YAML_ALIAS_EVENT:
{ term_t node;
const char *anchor = (const char*)event.data.alias.anchor;
if ( anchor_table &&
(node = find_anchor(anchor_table, anchor)) )
{ if ( !extend(&state, tmp, node) )
goto error;
} else
{ term_t ex;
return ( (ex=PL_new_term_ref()) &&
PL_put_atom_chars(ex, anchor) &&
PL_existence_error("anchor", ex) );
}
break;
}
case YAML_SCALAR_EVENT:
{ term_t val;
int iskey = (state.type == N_MAPPING && !state.key);
if ( !(val = PL_new_term_ref()) ||
!put_scalar(val, iskey, &event) ||
!extend(&state, tmp, val) )
goto error;
break;
}
}
if ( event.type != YAML_STREAM_END_EVENT )
yaml_event_delete(&event);
} while(event.type != YAML_STREAM_END_EVENT);
yaml_event_delete(&event);
free_anchor_table(anchor_table);
return TRUE;
error:
free_anchor_table(anchor_table);
while(stack)
pop(&stack, &state);
return FALSE;
}
static foreign_t
yaml_parse_stream(term_t stream, term_t doc)
{ IOSTREAM *fd;
yaml_parser_t parser;
int rc;
if ( !PL_get_stream(stream, &fd, SIO_INPUT) )
return FALSE;
if ( !yaml_parser_initialize(&parser) )
{ PL_release_stream(fd);
return PL_resource_error("memory");
}
yaml_parser_set_input(&parser, yaml_read_handler, fd);
rc = parse_document(&parser, PL_copy_term_ref(doc));
yaml_parser_delete(&parser);
rc = rc && PL_release_stream(fd);
return rc;
}
/*******************************
* INPUT *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
UTF-8 Decoding, based on http://www.cl.cam.ac.uk/~mgk25/unicode.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define ISUTF8_CB(c) (((c)&0xc0) == 0x80) /* Is continuation byte */
#define CONT(i) ISUTF8_CB(in[i])
#define VAL(i, s) ((in[i]&0x3f) << s)
#define IS_UTF8_2BYTE(in) \
((in[0]&0xe0) == 0xc0 && CONT(1))
#define IS_UTF8_3BYTE(in) \
((in[0]&0xf0) == 0xe0 && CONT(1)&&CONT(2))
#define IS_UTF8_4BYTE(in) \
((in[0]&0xf8) == 0xf0 && CONT(1)&&CONT(2)&&CONT(3))
#define IS_UTF8_5BYTE(in) \
((in[0]&0xfc) == 0xf8 && CONT(1)&&CONT(2)&&CONT(3)&&CONT(4))
#define IS_UTF8_6BYTE(in) \
((in[0]&0xfe) == 0xfc && CONT(1)&&CONT(2)&&CONT(3)&&CONT(4)&&CONT(5))
#define utf8_get_char(in, chr) \
(*(in) & 0x80 ? __utf8_get_char(in, chr) \
: (*(chr) = *(in), (char *)(in)+1))
static char *
__utf8_get_char(const char *in, int *chr)
{ if ( IS_UTF8_2BYTE(in) ) /* 2-byte, 0x80-0x7ff */
{ *chr = ((in[0]&0x1f) << 6)|VAL(1,0);
return (char *)in+2;
}
if ( IS_UTF8_3BYTE(in) ) /* 3-byte, 0x800-0xffff */
{ *chr = ((in[0]&0xf) << 12)|VAL(1,6)|VAL(2,0);
return (char *)in+3;
}
if ( IS_UTF8_4BYTE(in) ) /* 4-byte, 0x10000-0x1FFFFF */
{ *chr = ((in[0]&0x7) << 18)|VAL(1,12)|VAL(2,6)|VAL(3,0);
return (char *)in+4;
}
if ( IS_UTF8_5BYTE(in) ) /* 5-byte, 0x200000-0x3FFFFFF */
{ *chr = ((in[0]&0x3) << 24)|VAL(1,18)|VAL(2,12)|VAL(3,6)|VAL(4,0);
return (char *)in+5;
}
if ( IS_UTF8_6BYTE(in) ) /* 6-byte, 0x400000-0x7FFFFFF */
{ *chr = ((in[0]&0x1) << 30)|VAL(1,24)|VAL(2,18)|VAL(3,12)|VAL(4,6)|VAL(5,0);
return (char *)in+6;
}
*chr = (*in)&0xff; /* Error */
return (char *)in+1;
}
static int
yaml_read_handler(void *closure,
unsigned char *buffer, size_t size, size_t *size_read)
{ IOSTREAM *in = closure;
*size_read = Sfread(buffer, 1, size, in);
return !Sferror(in);
}
static int
yaml_write_handler(void *closure,
unsigned char *buffer, size_t size)
{ IOSTREAM *out = closure;
if ( out->encoding == ENC_OCTET )
{ Sfwrite(buffer, 1, size, out);
} else
{ int c;
const char *s = (const char *)buffer;
const char *end = &s[size];
while ( s < end )
{ s = utf8_get_char(s, &c);
if ( Sputcode(c, out) < 0 )
break;
}
}
return !Sferror(out);
}
/*******************************
* EMITTER *
*******************************/
static int
write_emitter(IOSTREAM *s, atom_t aref, int flags)
{ yaml_emitter_t *ref = PL_blob_data(aref, NULL, NULL);
Sfprintf(s, "<yaml_emitter>(%p)", ref);
return TRUE;
}
static void
acquire_emitter(atom_t aref)
{ yaml_emitter_t *ref = PL_blob_data(aref, NULL, NULL);
(void)ref;
}
static int
release_emitter(atom_t aref)
{ yaml_emitter_t *ref = PL_blob_data(aref, NULL, NULL);
yaml_emitter_delete(ref);
free(ref);
return TRUE;
}
static int
save_emitter(atom_t aref, IOSTREAM *fd)
{ yaml_emitter_t *ref = PL_blob_data(aref, NULL, NULL);
(void)fd;
return PL_warning("Cannot save reference to <yaml_emitter>(%p)",
ref);
}
static atom_t
load_emitter(IOSTREAM *fd)
{ (void)fd;
return PL_new_atom("<yaml_emitter>");
}
static PL_blob_t emitter_blob =
{ PL_BLOB_MAGIC,
PL_BLOB_NOCOPY,
"yaml_emitter",
release_emitter,
NULL,
write_emitter,
acquire_emitter,
save_emitter,
load_emitter
};
static int
unify_emitter(term_t t, yaml_emitter_t *emitter)
{ return PL_unify_blob(t, emitter, sizeof(*emitter), &emitter_blob);
}
static int
get_emitter(term_t t, yaml_emitter_t **emitter)
{ void *p;
size_t len;
PL_blob_t *type;
if ( PL_get_blob(t, &p, &len, &type) && type == &emitter_blob )
{ *emitter = p;
return TRUE;
}
PL_type_error("yaml_emitter", t);
return FALSE;
}
static foreign_t
yaml_emitter_create(term_t t, term_t stream, term_t options)
{ yaml_emitter_t *emitter = NULL;
IOSTREAM *fd;
int rc = FALSE;
if ( !PL_get_stream(stream, &fd, SIO_OUTPUT) )
return FALSE;
if ( (emitter=malloc(sizeof(*emitter))) )
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
term_t arg = PL_new_term_ref();
int uset = FALSE;
if ( !yaml_emitter_initialize(emitter) )
{ rc = PL_resource_error("memory");
goto out;
}
yaml_emitter_set_output(emitter, yaml_write_handler, fd);
while(PL_get_list_ex(tail, head, tail))
{ atom_t name;
size_t arity;
if ( PL_get_name_arity(head, &name, &arity) && arity == 1 )
{ _PL_get_arg(1, head, arg);
if ( name == ATOM_canonical )
{ int v;
if ( PL_get_bool_ex(arg, &v) )
yaml_emitter_set_canonical(emitter, v);
else
goto out;
} else if ( name == ATOM_unicode )
{ int v;
if ( PL_get_bool_ex(arg, &v) )
yaml_emitter_set_unicode(emitter, v);
else
goto out;
uset = TRUE;
}
} else
{ rc = PL_type_error("option", head);
goto out;
}
}
if ( !PL_get_nil_ex(tail) )
goto out;
if ( !uset )
yaml_emitter_set_unicode(emitter, TRUE);
return unify_emitter(t, emitter);
} else
{ rc = PL_resource_error("memory");
goto out;
}
out:
PL_release_stream(fd);
if ( emitter )
free(emitter);
return rc;
}
static int
get_string_arg(int i, term_t ev, term_t tmp, yaml_char_t **s)
{ char *pls;
if ( PL_get_arg(i, ev, tmp) )
{ int flags = CVT_ATOMIC|BUF_RING|REP_UTF8|CVT_EXCEPTION;
if ( PL_is_variable(tmp) )
{ *s = NULL;
return TRUE;
} else if ( PL_get_chars(tmp, &pls, flags) )
{ *s = (yaml_char_t*)pls;
return TRUE;
}
}
return FALSE;
}