/
rasqal_literal.c
4851 lines (4150 loc) · 129 KB
/
rasqal_literal.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
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* rasqal_literal.c - Rasqal literals
*
* Copyright (C) 2003-2010, David Beckett http://www.dajobe.org/
* Copyright (C) 2003-2005, University of Bristol, UK http://www.bristol.ac.uk/
*
* This package is Free Software and part of Redland http://librdf.org/
*
* It is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <rasqal_config.h>
#endif
#ifdef WIN32
#include <win32_rasqal_config.h>
#endif
/* for strtof() and round() prototypes */
#define _ISOC99_SOURCE 1
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
/* for ptrdiff_t */
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#include <stdarg.h>
/* for isnan() */
#ifdef HAVE_MATH_H
#include <math.h>
#endif
/* for INT_MIN and INT_MAX */
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#include "rasqal.h"
#include "rasqal_internal.h"
#define DEBUG_FH stderr
#ifndef STANDALONE
/* prototypes */
static rasqal_literal_type rasqal_literal_promote_numerics(rasqal_literal* l1, rasqal_literal* l2, int flags);
static int rasqal_literal_set_typed_value(rasqal_literal* l, rasqal_literal_type type, const unsigned char* string, int canonicalize);
/**
* rasqal_new_integer_literal:
* @world: rasqal world object
* @type: Type of literal such as RASQAL_LITERAL_INTEGER or RASQAL_LITERAL_BOOLEAN
* @integer: int value
*
* Constructor - Create a new Rasqal integer literal.
*
* The integer decimal number is turned into a rasqal integer literal
* and given a datatype of xsd:integer
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_integer_literal(rasqal_world* world, rasqal_literal_type type,
int integer)
{
raptor_uri* dt_uri;
rasqal_literal* l;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(l) {
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = type;
l->value.integer = integer;
if(type == RASQAL_LITERAL_BOOLEAN) {
/* static l->string for boolean, does not need freeing */
l->string = integer ? RASQAL_XSD_BOOLEAN_TRUE : RASQAL_XSD_BOOLEAN_FALSE;
l->string_len = integer ? RASQAL_XSD_BOOLEAN_TRUE_LEN : RASQAL_XSD_BOOLEAN_FALSE_LEN;
} else {
size_t slen = 0;
l->string = rasqal_xsd_format_integer(integer, &slen);
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string) {
rasqal_free_literal(l);
return NULL;
}
}
dt_uri = rasqal_xsd_datatype_type_to_uri(world, l->type);
if(!dt_uri) {
rasqal_free_literal(l);
return NULL;
}
l->datatype = raptor_uri_copy(dt_uri);
l->parent_type = rasqal_xsd_datatype_parent_type(type);
}
return l;
}
/**
* rasqal_new_numeric_literal_from_long:
* @world: rasqal world object
* @type: Type of literal such as RASQAL_LITERAL_INTEGER or RASQAL_LITERAL_BOOLEAN
* @value: long value
*
* Constructor - Create a new Rasqal numeric literal from a long.
*
* The value is turned into a rasqal integer or decimal literal and
* given a datatype of xsd:integer
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_numeric_literal_from_long(rasqal_world* world,
rasqal_literal_type type,
long value)
{
rasqal_xsd_decimal* d;
unsigned char *string;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
/* boolean values should always be in range */
if(type == RASQAL_LITERAL_BOOLEAN) {
int ivalue = value ? 1 : 0;
return rasqal_new_integer_literal(world, type, ivalue);
}
/* For other types, if in int range, make an integer literal */
if(value >= INT_MIN && value <= INT_MAX) {
return rasqal_new_integer_literal(world, type, RASQAL_GOOD_CAST(int, value));
}
/* Otherwise turn it into a decimal */
d = rasqal_new_xsd_decimal(world);
rasqal_xsd_decimal_set_long(d, value);
string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_decimal_as_counted_string(d, NULL));
return rasqal_new_decimal_literal_from_decimal(world, string, d);
}
/**
* rasqal_new_typed_literal:
* @world: rasqal world object
* @type: Type of literal such as RASQAL_LITERAL_INTEGER or RASQAL_LITERAL_BOOLEAN
* @string: lexical form - ownership not taken
*
* Constructor - Create a new Rasqal integer literal from a string
*
* The integer decimal number is turned into a rasqal integer literal
* and given a datatype of xsd:integer
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_typed_literal(rasqal_world* world, rasqal_literal_type type,
const unsigned char* string)
{
rasqal_literal* l;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(!l)
return NULL;
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = type;
if(!rasqal_xsd_datatype_check(type, string, 0)) {
rasqal_free_literal(l);
return NULL;
}
if(rasqal_literal_set_typed_value(l, type, string, 0)) {
rasqal_free_literal(l);
l = NULL;
}
return l;
}
/**
* rasqal_new_floating_literal:
* @world: rasqal world object
* @type: type - #RASQAL_LITERAL_FLOAT or #RASQAL_LITERAL_DOUBLE
* @d: floating literal (double)
*
* Constructor - Create a new Rasqal float literal from a double.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_floating_literal(rasqal_world *world,
rasqal_literal_type type, double d)
{
raptor_uri* dt_uri;
rasqal_literal* l;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
if(type != RASQAL_LITERAL_FLOAT && type != RASQAL_LITERAL_DOUBLE)
return NULL;
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(l) {
size_t slen = 0;
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = type;
l->value.floating = d;
l->string = rasqal_xsd_format_double(d, &slen);
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string) {
rasqal_free_literal(l);
return NULL;
}
dt_uri = rasqal_xsd_datatype_type_to_uri(world, l->type);
if(!dt_uri) {
rasqal_free_literal(l);
return NULL;
}
l->datatype = raptor_uri_copy(dt_uri);
}
return l;
}
/**
* rasqal_new_double_literal:
* @world: rasqal world object
* @d: double literal
*
* Constructor - Create a new Rasqal double literal.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_double_literal(rasqal_world* world, double d)
{
return rasqal_new_floating_literal(world, RASQAL_LITERAL_DOUBLE, d);
}
#ifndef RASQAL_DISABLE_DEPRECATED
/**
* rasqal_new_float_literal:
* @world: rasqal world object
* @f: float literal
*
* Constructor - Create a new Rasqal float literal.
*
* @Deprecated: Use rasqal_new_floating_literal() with type
* #RASQAL_LITERAL_FLOAT and double value.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_float_literal(rasqal_world *world, float f)
{
return rasqal_new_floating_literal(world, RASQAL_LITERAL_FLOAT, (double)f);
}
#endif
/**
* rasqal_new_uri_literal:
* @world: rasqal world object
* @uri: #raptor_uri uri
*
* Constructor - Create a new Rasqal URI literal from a raptor URI.
*
* The uri is an input parameter and is stored in the literal, not copied.
* The uri is freed also on failure.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_uri_literal(rasqal_world* world, raptor_uri *uri)
{
rasqal_literal* l;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(l) {
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = RASQAL_LITERAL_URI;
l->value.uri = uri;
} else {
raptor_free_uri(uri);
}
return l;
}
/**
* rasqal_new_pattern_literal:
* @world: rasqal world object
* @pattern: regex pattern
* @flags: regex flags
*
* Constructor - Create a new Rasqal pattern literal.
*
* The pattern and flags are input parameters and are stored in the
* literal, not copied. They are freed also on failure.
* The set of flags recognised depends on the regex library and the query
* language.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_pattern_literal(rasqal_world* world,
const unsigned char *pattern,
const char *flags)
{
rasqal_literal* l;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(pattern, char*, NULL);
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(l) {
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = RASQAL_LITERAL_PATTERN;
l->string = pattern;
l->string_len = RASQAL_BAD_CAST(unsigned int, strlen(RASQAL_GOOD_CAST(const char*, pattern)));
l->flags = RASQAL_GOOD_CAST(const unsigned char*, flags);
} else {
if(flags)
RASQAL_FREE(char*, flags);
RASQAL_FREE(char*, pattern);
}
return l;
}
/**
* rasqal_new_decimal_literal:
* @world: rasqal world object
* @string: decimal literal
*
* Constructor - Create a new Rasqal decimal literal.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_decimal_literal(rasqal_world* world, const unsigned char *string)
{
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(string, char*, NULL);
return rasqal_new_decimal_literal_from_decimal(world, string, NULL);
}
/**
* rasqal_new_decimal_literal_from_decimal:
* @world: rasqal world object
* @string: decimal literal string
* @decimal: rasqal XSD Decimal
*
* Constructor - Create a new Rasqal decimal literal.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_decimal_literal_from_decimal(rasqal_world* world,
const unsigned char *string,
rasqal_xsd_decimal* decimal)
{
rasqal_literal* l;
raptor_uri *dt_uri;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
/* string and decimal NULLness are checked below */
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(!l)
return NULL;
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = RASQAL_LITERAL_DECIMAL;
if(string) {
if(!rasqal_xsd_datatype_check(l->type, string, 0)) {
rasqal_free_literal(l);
return NULL;
}
if(rasqal_literal_set_typed_value(l, l->type, string, 0)) {
rasqal_free_literal(l);
l = NULL;
}
} else if(decimal) {
dt_uri = rasqal_xsd_datatype_type_to_uri(world, l->type);
if(!dt_uri) {
rasqal_free_literal(l);
l = NULL;
} else {
size_t slen = 0;
l->datatype = raptor_uri_copy(dt_uri);
l->value.decimal = decimal;
/* string is owned by l->value.decimal */
l->string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_decimal_as_counted_string(l->value.decimal, &slen));
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string) {
rasqal_free_literal(l);
l = NULL;
}
}
} else {
/* no string or decimal was given */
rasqal_free_literal(l);
l = NULL;
}
return l;
}
/*
* rasqal_new_numeric_literal:
* @world: rasqal world object
* @type: datatype
* @double: double
*
* INTERNAL - Make a numeric datatype from a double
*
* Return value: new literal or NULL on failure
**/
rasqal_literal*
rasqal_new_numeric_literal(rasqal_world* world, rasqal_literal_type type,
double d)
{
char buffer[30];
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
switch(type) {
case RASQAL_LITERAL_DOUBLE:
case RASQAL_LITERAL_FLOAT:
return rasqal_new_floating_literal(world, type, d);
break;
case RASQAL_LITERAL_INTEGER:
case RASQAL_LITERAL_INTEGER_SUBTYPE:
if(d >= (double)INT_MIN && d <= (double)INT_MAX)
return rasqal_new_integer_literal(world, type, RASQAL_GOOD_CAST(int, d));
/* otherwise FALLTHROUGH and make it a decimal */
case RASQAL_LITERAL_DECIMAL:
sprintf(buffer, "%g", d);
return rasqal_new_decimal_literal(world, RASQAL_GOOD_CAST(unsigned char*, buffer));
break;
case RASQAL_LITERAL_XSD_STRING:
case RASQAL_LITERAL_BOOLEAN:
case RASQAL_LITERAL_DATE:
case RASQAL_LITERAL_DATETIME:
case RASQAL_LITERAL_UNKNOWN:
case RASQAL_LITERAL_BLANK:
case RASQAL_LITERAL_URI:
case RASQAL_LITERAL_STRING:
case RASQAL_LITERAL_PATTERN:
case RASQAL_LITERAL_QNAME:
case RASQAL_LITERAL_VARIABLE:
case RASQAL_LITERAL_UDT:
RASQAL_FATAL2("Unexpected numeric type %d\n", type);
}
return NULL;
}
/**
* rasqal_new_datetime_literal_from_datetime:
* @world: rasqal world object
* @dt: rasqal XSD Datetime
*
* Constructor - Create a new Rasqal datetime literal from an existing datetime.
*
* Takes ownership of @dt
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_datetime_literal_from_datetime(rasqal_world* world,
rasqal_xsd_datetime* dt)
{
rasqal_literal* l;
raptor_uri *dt_uri;
size_t slen = 0;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(dt, rasqal_xsd_datetime, NULL);
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(!l)
goto failed;
l->valid = 1;
l->usage = 1;
l->world = world;
l->type = RASQAL_LITERAL_DATETIME;
dt_uri = rasqal_xsd_datatype_type_to_uri(world, l->type);
if(!dt_uri)
goto failed;
l->datatype = raptor_uri_copy(dt_uri);
l->value.datetime = dt;
l->string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_datetime_to_counted_string(l->value.datetime, &slen));
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string)
goto failed;
return l;
failed:
if(l)
rasqal_free_literal(l);
if(dt)
rasqal_free_xsd_datetime(dt);
return NULL;
}
/*
* rasqal_literal_set_typed_value:
* @l: literal
* @type: type
* @string: string or NULL to use existing literal string
* @canonicalize: non-0 to canonicalize the existing string
*
* INTERNAL - Set a literal typed value
*
* Return value: non-0 on failure
**/
static int
rasqal_literal_set_typed_value(rasqal_literal* l, rasqal_literal_type type,
const unsigned char* string,
int canonicalize)
{
char *eptr;
raptor_uri* dt_uri;
int i;
double d;
rasqal_literal_type original_type = l->type;
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(l, rasqal_literal, 1);
retype:
l->valid = rasqal_xsd_datatype_check(type, string ? string : l->string,
0 /* no flags set */);
if(!l->valid) {
RASQAL_DEBUG3("Invalid type %s string '%s' - setting to type UDT\n",
rasqal_xsd_datatype_label(type), string ? string : l->string);
type = RASQAL_LITERAL_UDT;
}
if(l->language) {
RASQAL_FREE(char*, l->language);
l->language = NULL;
}
l->type = type;
if(string && l->type != RASQAL_LITERAL_DECIMAL) {
if(l->string)
RASQAL_FREE(char*, l->string);
l->string_len = RASQAL_BAD_CAST(unsigned int, strlen(RASQAL_GOOD_CAST(const char*, string)));
l->string = RASQAL_MALLOC(unsigned char*, l->string_len + 1);
if(!l->string)
return 1;
memcpy((void*)l->string, string, l->string_len + 1);
}
if(l->type <= RASQAL_LITERAL_LAST_XSD) {
dt_uri = rasqal_xsd_datatype_type_to_uri(l->world, l->type);
if(!dt_uri)
return 1;
if(l->datatype)
raptor_free_uri(l->datatype);
l->datatype = raptor_uri_copy(dt_uri);
l->parent_type = rasqal_xsd_datatype_parent_type(type);
}
switch(type) {
case RASQAL_LITERAL_INTEGER:
case RASQAL_LITERAL_INTEGER_SUBTYPE:
if(1) {
long long_i;
eptr = NULL;
errno = 0;
long_i = strtol(RASQAL_GOOD_CAST(const char*, l->string), &eptr, 10);
if(*eptr)
return 1;
if(errno == ERANGE) {
/* under or overflow of strtol() so fallthrough to DECIMAL */
} else if(long_i >= INT_MIN && long_i <= INT_MAX) {
l->value.integer = RASQAL_GOOD_CAST(int, long_i);
break;
}
}
/* Will not fit in an int so turn it into a decimal */
type = RASQAL_LITERAL_DECIMAL;
goto retype;
break;
case RASQAL_LITERAL_DOUBLE:
case RASQAL_LITERAL_FLOAT:
if(1) {
size_t slen = 0;
d = 0.0;
(void)sscanf(RASQAL_GOOD_CAST(char*, l->string), "%lf", &d);
l->value.floating = d;
if(canonicalize) {
if(l->string)
RASQAL_FREE(char*, l->string);
l->string = rasqal_xsd_format_double(d, &slen);
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
}
}
break;
case RASQAL_LITERAL_DECIMAL:
if(1) {
size_t slen = 0;
rasqal_xsd_decimal* new_d;
new_d = rasqal_new_xsd_decimal(l->world);
if(!new_d)
return 1;
if(!string)
/* use existing literal decimal object (SHARED) string */
string = l->string;
if(rasqal_xsd_decimal_set_string(new_d,
RASQAL_GOOD_CAST(const char*, string))) {
rasqal_free_xsd_decimal(new_d);
return 1;
}
if(l->value.decimal) {
rasqal_free_xsd_decimal(l->value.decimal);
}
l->value.decimal = new_d;
/* old l->string is now invalid and MAY need to be freed */
if(l->string && original_type != RASQAL_LITERAL_DECIMAL)
RASQAL_FREE(char*, l->string);
/* new l->string is owned by l->value.decimal and will be
* freed on literal destruction
*/
l->string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_decimal_as_counted_string(l->value.decimal, &slen));
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string)
return 1;
}
break;
case RASQAL_LITERAL_XSD_STRING:
case RASQAL_LITERAL_UDT:
/* No change - kept as same type - xsd:string or user defined */
break;
case RASQAL_LITERAL_BOOLEAN:
i = rasqal_xsd_boolean_value_from_string(l->string);
/* Free passed in string */
if(l->string != RASQAL_XSD_BOOLEAN_TRUE &&
l->string != RASQAL_XSD_BOOLEAN_FALSE)
RASQAL_FREE(char*, l->string);
/* and replace with a static string */
l->string = i ? RASQAL_XSD_BOOLEAN_TRUE : RASQAL_XSD_BOOLEAN_FALSE;
l->string_len = i ? RASQAL_XSD_BOOLEAN_TRUE_LEN : RASQAL_XSD_BOOLEAN_FALSE_LEN;
l->value.integer = i;
break;
case RASQAL_LITERAL_STRING:
/* No change - kept as a string */
break;
case RASQAL_LITERAL_DATE:
if(1) {
size_t slen = 0;
if(l->value.date)
rasqal_free_xsd_date(l->value.date);
l->value.date = rasqal_new_xsd_date(l->world, RASQAL_GOOD_CAST(const char*, l->string));
if(!l->value.date) {
RASQAL_FREE(char*, l->string);
return 1;
}
RASQAL_FREE(char*, l->string);
l->string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_date_to_counted_string(l->value.date, &slen));
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string)
return 1;
}
break;
case RASQAL_LITERAL_DATETIME:
if(1) {
size_t slen = 0;
if(l->value.datetime)
rasqal_free_xsd_datetime(l->value.datetime);
l->value.datetime = rasqal_new_xsd_datetime(l->world,
RASQAL_GOOD_CAST(const char*, l->string));
if(!l->value.datetime) {
RASQAL_FREE(char*, l->string);
return 1;
}
RASQAL_FREE(char*, l->string);
l->string = RASQAL_GOOD_CAST(unsigned char*, rasqal_xsd_datetime_to_counted_string(l->value.datetime, &slen));
l->string_len = RASQAL_BAD_CAST(unsigned int, slen);
if(!l->string)
return 1;
}
break;
case RASQAL_LITERAL_UNKNOWN:
case RASQAL_LITERAL_BLANK:
case RASQAL_LITERAL_URI:
case RASQAL_LITERAL_PATTERN:
case RASQAL_LITERAL_QNAME:
case RASQAL_LITERAL_VARIABLE:
RASQAL_FATAL2("Unexpected native type %d\n", type);
break;
default:
RASQAL_FATAL2("Unknown native type %d\n", type);
}
return 0;
}
/*
* rasqal_literal_string_to_native:
* @l: #rasqal_literal to operate on inline
* @flags: flags for literal checking. 1 to canonicalize string
*
* INTERNAL - Upgrade a datatyped literal string to an internal typed literal
*
* At present this promotes datatyped literals
* xsd:integer to RASQAL_LITERAL_INTEGER
* xsd:double to RASQAL_LITERAL_DOUBLE
* xsd:float to RASQAL_LITERAL_FLOAT
* xsd:boolean to RASQAL_LITERAL_BOOLEAN
* xsd:decimal to RASQAL_LITERAL_DECIMAL
* xsd:dateTime to RASQAL_LITERAL_DATETIME
* xsd:date to RASQAL_LITERAL_DATE
*
* Return value: non-0 on failure
**/
int
rasqal_literal_string_to_native(rasqal_literal *l, int flags)
{
rasqal_literal_type native_type = RASQAL_LITERAL_UNKNOWN;
int rc = 0;
int canonicalize = (flags & 1 );
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(l, rasqal_literal, 1);
/* RDF literal with no datatype (plain literal) */
if(!l->datatype)
return 0;
native_type = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype);
/* plain literal - nothing to do */
if(native_type == RASQAL_LITERAL_STRING)
return 0;
/* xsd:string - mark and return */
if(native_type == RASQAL_LITERAL_XSD_STRING) {
l->type = native_type;
return 0;
}
/* If a user defined type - update the literal */
if(native_type == RASQAL_LITERAL_UNKNOWN) {
l->type = RASQAL_LITERAL_UDT;
return 0;
}
rc = rasqal_literal_set_typed_value(l, native_type,
NULL /* existing string */,
canonicalize);
if(!rasqal_xsd_datatype_check(native_type, l->string, 1))
return 0;
return rc;
}
/*
* rasqal_new_string_literal_common:
* @world: rasqal world object
* @string: UTF-8 string lexical form
* @language: RDF language (xml:lang) (or NULL)
* @datatype: datatype URI (or NULL for plain literal)
* @datatype_qname: datatype qname string (or NULL for plain literal)
* @flags: bitflags - 1 to do native type promotion; 2 to canonicalize string
*
* INTERNAL Constructor - Create a new Rasqal string literal.
*
* All parameters are input parameters and if present are stored in
* the literal, not copied. They are freed also on failure.
*
* The datatype and datatype_qname parameters are alternatives; the
* qname is a datatype that cannot be resolved till later since the
* prefixes have not yet been declared or checked.
*
* If the string literal is datatyped and of certain types recognised
* it may be converted to a different literal type by
* rasqal_literal_string_to_native() only if @flags has bit 1 set.
* If bit 2 is set AS WELL, literals will have their string converted
* to the canonical format.
*
* Return value: New #rasqal_literal or NULL on failure
**/
static rasqal_literal*
rasqal_new_string_literal_common(rasqal_world* world,
const unsigned char *string,
const char *language,
raptor_uri *datatype,
const unsigned char *datatype_qname,
int flags)
{
rasqal_literal* l;
int native_type_promotion = (flags & 1);
int canonicalize = (flags & 2) >> 1;
l = RASQAL_CALLOC(rasqal_literal*, 1, sizeof(*l));
if(l) {
rasqal_literal_type datatype_type = RASQAL_LITERAL_STRING;
l->valid = 1;
l->usage = 1;
l->world = world;
if(datatype && language) {
/* RDF typed literal but this is not allowed so delete language */
RASQAL_FREE(char*, language);
language = NULL;
}
l->type = RASQAL_LITERAL_STRING;
l->string = string;
l->string_len = RASQAL_BAD_CAST(unsigned int, strlen(RASQAL_GOOD_CAST(const char*, string)));
l->language = language;
l->datatype = datatype;
l->flags = datatype_qname;
if(datatype)
datatype_type = rasqal_xsd_datatype_uri_to_type(world, datatype);
l->parent_type = rasqal_xsd_datatype_parent_type(datatype_type);
if(native_type_promotion &&
rasqal_literal_string_to_native(l, canonicalize)) {
rasqal_free_literal(l);
l = NULL;
}
} else {
if(language)
RASQAL_FREE(char*, language);
if(datatype)
raptor_free_uri(datatype);
if(datatype_qname)
RASQAL_FREE(char*, datatype_qname);
RASQAL_FREE(char*, string);
}
return l;
}
/**
* rasqal_new_string_literal:
* @world: rasqal world object
* @string: UTF-8 string lexical form
* @language: RDF language (xml:lang) (or NULL)
* @datatype: datatype URI (or NULL for plain literal)
* @datatype_qname: datatype qname string (or NULL for plain literal)
*
* Constructor - Create a new Rasqal string literal.
*
* All parameters are input parameters and if present are stored in
* the literal, not copied. They are freed also on failure.
*
* The datatype and datatype_qname parameters are alternatives; the
* qname is a datatype that cannot be resolved till later since the
* prefixes have not yet been declared or checked.
*
* If the string literal is datatyped and of certain types recognised
* it may be converted to a different literal type.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_string_literal(rasqal_world* world,
const unsigned char *string,
const char *language,
raptor_uri *datatype,
const unsigned char *datatype_qname)
{
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(string, char*, NULL);
return rasqal_new_string_literal_common(world, string, language, datatype,
datatype_qname, 1);
}
/*
* rasqal_new_string_literal_node:
* @world: rasqal world object
* @string: UTF-8 string lexical form
* @language: RDF language (xml:lang) (or NULL)
* @datatype: datatype URI (or NULL for plain literal)
*
* INTERNAL Constructor - Create a new Rasqal literal with promotion and canonicalization
*
* All parameters are input parameters and if present are stored in
* the literal, not copied. They are freed also on failure.
*
* Return value: New #rasqal_literal or NULL on failure
**/
rasqal_literal*
rasqal_new_string_literal_node(rasqal_world* world, const unsigned char *string,
const char *language, raptor_uri *datatype)
{
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(string, char*, NULL);
return rasqal_new_string_literal_common(world, string, language, datatype,
NULL, 1 | 2);
}
/**