-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinypds.h
1825 lines (1766 loc) · 58.1 KB
/
tinypds.h
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
/*
* Tiny PDS3 parser
*
* This file provides both the interface and the implementation.
* To instantiate the implementation add the following in *ONE* source file before including this file
* #define TINY_PDS_IMPL
*
* Licensed under the MIT License
* (c) 2016-2020 Vincent Cruz
*/
#ifndef TINY_PDS_H
#define TINY_PDS_H
#include <stdint.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Status values.
*/
enum PDS_STATUS {
PDS_END = -1,
PDS_OK = 0,
PDS_INVALID_VALUE,
PDS_INVALID_RANGE,
PDS_INVALID_ARGUMENT,
PDS_INCOMPLETE,
PDS_FAILURE
};
/**
* Value types.
*/
enum PDS_VALUE_TYPE {
/** Unknown value. **/
PDS_UNKNOWN_VALUE = 0,
/** Integer value stored as a signed 64 bits integer. **/
PDS_INTEGER_VALUE,
/** Real value stored as a double precision floating point. **/
PDS_REAL_VALUE,
/** Date/time value @see PDS_datetime **/
PDS_DATE_TIME_VALUE,
/** Quoted string. **/
PDS_TEXT_STRING_VALUE,
/** Symbolic literal. **/
PDS_SYMBOLIC_VALUE,
/** Identifier. **/
PDS_IDENTIFIER_VALUE
};
/**
* String.
*/
typedef struct {
/** Value type. Must be equal to PDS_TEXT_STRING_VALUE. **/
int type;
/** Pointer to the first character of the string. **/
const char *first;
/** Pointer to the last character of the string. **/
const char *last;
} PDS_string;
/**
* Integer value.
*/
typedef struct {
/** Value type. Must be equal to PDS_INTEGER_VALUE. **/
int type;
/** Measurement unit. **/
struct { const char *first, *last; } unit;
/** Integer value. **/
int64_t value;
} PDS_integer;
/**
* Real value.
*/
typedef struct {
/** Value type. Must be equal to PDS_REAL_VALUE. **/
int type;
/** Measurement unit. **/
struct { const char *first, *last; } unit;
/** Real (floating-point) value. **/
double value;
} PDS_real;
/**
* Time types.
*/
enum PDS_TIME_TYPE {
/** Local timezone. **/
PDS_LOCAL_TIME = 0,
/** UTC time. **/
PDS_UTC_TIME,
/** Zoned time (UTC+offset). **/
PDS_ZONED_TIME
};
/**
* Date and time structure.
*/
typedef struct {
/** Value type. Must be equal to PDS_DATE_TIME_VALUE. **/
int type;
/** Year in 4 digits form. **/
uint16_t year;
/** Day of month (number between 1 and 31). **/
uint16_t day;
/** Month (number between 1 and 12). **/
uint8_t month;
/** Hour (number between 0 and 23). **/
uint8_t hour;
/** Minutes (number between 0 and 59). **/
uint8_t minute;
/** Seconds (number between 0 and 59). **/
uint8_t second;
/** Microseconds (number between 0 and 999999). **/
uint32_t microsecond;
/** Hour time offset (number between -12 and 12). **/
int8_t hour_offset;
/** Minute time offset (number between 0 and 59). **/
int8_t minute_offset;
/** Time type @see PDS_TIME_TYPE **/
uint8_t time_type;
} PDS_datetime;
/**
* Scalar value.
*/
typedef union {
/** Value type @see PDS_VALUE_TYPE **/
int type;
/** Integer value. **/
PDS_integer integer;
/** Real value. **/
PDS_real real;
/** Date time. **/
PDS_datetime date_time;
/** Text string. **/
PDS_string text;
/** Symbolic literal. **/
PDS_string symbolic;
/** Identifier. **/
PDS_string identifier;
} PDS_scalar;
/** New element callback. **/
typedef int (*PDS_begin_callback) (const char *first, const char *last, void *user_data);
/** End of current element callback. **/
typedef int (*PDS_end_callback) (const char *first, const char *last, void *user_data);
/** Token callbacks **/
typedef struct {
PDS_begin_callback begin;
PDS_end_callback end;
} PDS_token_callbacks;
/** New collection callback. **/
typedef int (*PDS_collection_begin_callback) (void *user_data);
/** End of collection callback. **/
typedef int (*PDS_collection_end_callback) (void *user_data);
/** Collection callbacks **/
typedef struct {
/** Collection start callback. **/
PDS_collection_begin_callback begin;
/** Collection end callback. **/
PDS_collection_end_callback end;
} PDS_collection_callbacks;
/** New scalar callback. **/
typedef int (*PDS_scalar_callback) (const PDS_scalar *scalar, void *user_data);
/** Error description. **/
typedef struct {
/** Pointer to the beginning of the line being parsed. **/
const char *line_first;
/** Pointer to the last of the line being parsed. **/
const char *line_last;
/** Line number. **/
int number;
/** Position of the erroneous character character. **/
int position;
/** Current status @see PDS_STATUS **/
int status;
/** Error message. **/
const char *msg;
} PDS_error_description;
/** Error callback. **/
typedef void (*PDS_error_callback)(const PDS_error_description *description, void *user_data);
/** Read a char from input stream. **/
typedef char (*PDS_get_char) (void *user_data);
/**
* PSD parser callbacks.
*/
typedef struct {
/** Get a new char from input stream. **/
PDS_get_char get_char;
/** Attribute callbacks. **/
PDS_token_callbacks attribute;
/** Pointer callbacks. **/
PDS_token_callbacks pointer;
/** Group callbacks. **/
PDS_token_callbacks group;
/** Object callbacks. **/
PDS_token_callbacks object;
/** Set callbacks. **/
PDS_collection_callbacks set;
/** Sequence callbacks. **/
PDS_collection_callbacks sequence;
/** Scalar callback. **/
PDS_scalar_callback scalar;
/** Display error message. **/
PDS_error_callback error;
} PDS_callbacks;
/**
* Remove leading and trailing white spaces in a string.
* A white space is either a space (' '), form-feed ('\\f'), newline ('\\n'),
* carriage return ('\\r'), horizontal tab ('\\t') or vertical tab ('\\v').
* @param [in] first Pointer to the first character of the input string.
* @param [in] last Pointer to the last character of the input string.
* @param [out] begin Stores the pointer to the first non-space character.
* @param [out] end Stores the pointer to the last non-space character.
*/
void PDS_trim(const char *first, const char *last, const char **begin, const char **end);
/**
* Find the first occurence of a character in a string.
* @param [in] first Pointer to the first character of the input string.
* @param [in] last Pointer to the last character of the input string.
* @param [in] c Character to search.
* @return A pointer to the matched character in the input string or 0 if the
* character is not found.
*/
const char* PDS_find_first(const char *first, const char *last, char c);
/**
* Compare two strings.
* @param [in] f0 Pointer to the first characer of the 1st string.
* @param [in] l0 Pointer to the last characer of the 1st string.
* @param [in] f1 Pointer to the first characer of the 2nd string.
* @param [in] l1 Pointer to the last characer of the 2nd string.
* @return 1 if the strings are equal, 0 otherwise.
*/
int PDS_string_compare(const char *f0, const char *l0, const char *f1, const char *l1);
/**
* Compare two strings ignoring case.
* @param [in] f0 Pointer to the first characer of the 1st string.
* @param [in] l0 Pointer to the last characer of the 1st string.
* @param [in] f1 Pointer to the first characer of the 2nd string.
* @param [in] l1 Pointer to the last characer of the 2nd string.
* @return 1 if the strings are equal, 0 otherwise.
*/
int PDS_string_case_compare(const char *f0, const char *l0, const char *f1, const char *l1);
/**
* Set attribute callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Attribute start callback.
* @param [in] end Attribute end callback.
*/
void PDS_set_attribute_callbacks(PDS_callbacks *callbacks, PDS_begin_callback begin,
PDS_end_callback end);
/**
* Set pointer callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Pointer start callback.
* @param [in] end Pointer end callback.
*/
void PDS_set_pointer_callbacks(PDS_callbacks *callbacks, PDS_begin_callback begin,
PDS_end_callback end);
/**
* Set group callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Group start callback.
* @param [in] end Group end callback.
*/
void PDS_set_group_callbacks(PDS_callbacks *callbacks, PDS_begin_callback begin,
PDS_end_callback end);
/**
* Set object callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Object start callback.
* @param [in] end Object end callback.
*/
void PDS_set_object_callbacks(PDS_callbacks *callbacks, PDS_begin_callback begin,
PDS_end_callback end);
/**
* Set 'set' callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Set start callback.
* @param [in] end Set end callback.
*/
void PDS_set_set_callbacks(PDS_callbacks *callbacks, PDS_collection_begin_callback begin,
PDS_collection_end_callback end);
/**
* Set sequence callbacks.
* @param [in,out] callbacks Parser callbacks.
* @param [in] begin Sequence start callback.
* @param [in] end Sequence end callback.
*/
void PDS_set_sequence_callbacks(PDS_callbacks *callbacks, PDS_collection_begin_callback begin,
PDS_collection_end_callback end);
/**
* Set scalar callback.
* @param [in,out] callbacks Parser callbacks.
* @param [in] scalar Scalar callback.
*/
void PDS_set_scalar_callback(PDS_callbacks *callbacks, PDS_scalar_callback scalar);
/**
* Set error callback.
* @param [in,out] callbacks Parser callbacks.
* @param [in] error Error callback.
*/
void PDS_set_error_callback(PDS_callbacks *callbacks, PDS_error_callback error);
/**
* Parse the PDS data contained in the input buffer.
* @param [in] callbacks Parser callbacks.
* @param [in] buffer Input buffer.
* @param [in] len Length of the input buffer.
* @param [in] user_data User data.
* @return 1 if the PDS data contained in the input buffer was successfully parsed, or 0 if an error occured.
*/
int PDS_parse(PDS_callbacks *callbacks, const char *buffer, int len, void *user_data);
#ifdef __cplusplus
}
#endif
#endif /* TINY_PDS_H */
/*****************************************************************************/
#ifdef TINY_PDS_IMPL
#define PDS_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static inline int PDS_isspace(char c) {
return ( (' ' == (c)) || ('\t' == (c))
|| ('\r' == (c)) || ('\n' == (c))
|| ('\f' == (c)) || ('\v' == (c)) );
}
static inline int PDS_isdigit(char c) {
return ((c) >= '0') && ((c) <= '9');
}
static inline int PDS_islower(char c) {
return ((c) >= 'a') && ((c) <= 'z');
}
static inline int PDS_isupper(char c) {
return ((c) >= 'A') && ((c) <= 'Z');
}
static inline int PDS_isalpha(char c) {
return PDS_islower(c) || PDS_isupper(c);
}
static inline int PDS_isalnum(char c) {
return PDS_isalpha(c) || PDS_isdigit(c);
}
static inline int PDS_toupper(char c) {
return PDS_islower(c) ? ('A' + ((c)-'a')) : (c);
}
static inline int PDS_extascii(char c) {
return ((uint8_t)c >= 0x20) && ((uint8_t)c <= 0xfe);
}
#if 0
static int PDS_tolower(char c) {
return PDS_isupper(c) ? ('a' + ((c)-'A')) : (c);
}
static const char *PDS_special_chars = "={}()+-<>.\"\'_,/*:#&^";
static const char *PDS_spacing_chars = " \t";
static const char *PDS_format_effectors = "\r\n\f\v";
static const char *PDS_other_chars = "!$%;?@[]`|~";
#endif
/**
* PDS token type.
* These types correspond to the type of assignment they belong to.
*/
enum PDS_TOKEN_TYPE {
PDS_TOKEN_UNKNOWN = 0,
PDS_TOKEN_ATTRIBUTE,
PDS_TOKEN_POINTER,
PDS_TOKEN_GROUP,
PDS_TOKEN_OBJECT,
PDS_TOKEN_END,
};
/**
* PDS token flag.
*/
enum PDS_TOKEN_FLAG {
PDS_TOKEN_FLAG_NONE = 0,
PDS_TOKEN_FLAG_BEGIN,
PDS_TOKEN_FLAG_END
};
/**
* PDS token.
*/
typedef struct {
/** Pointer to the first character of the token. **/
const char *first;
/** Pointer to the last character of the token. **/
const char *last;
/** Token type @see PDS_TOKEN_TYPE **/
int type;
/** Flag (begin/end, ...). **/
int flag;
} PDS_token;
/** PDS parser. **/
typedef struct {
/** Current status @see PDS_STATUS **/
int status;
/** Pointer to the first character of the input text. **/
const char *first;
/** Pointer to the last character of the input text. **/
const char *last;
/** Pointer to the current character. **/
const char *current;
/** Pointer to the beginning of the current line. **/
const char *line;
/** Number of the line being parsed.**/
int line_num;
/** User data. **/
void *user_data;
/** Current token. **/
PDS_token token;
/** Current scalar value. **/
PDS_scalar scalar;
/** Callbacks. **/
PDS_callbacks callbacks;
} PDS_parser;
/**
* Set error and run error display callback.
* @param [in,out] parser Parser.
* @param [in] current Current string pointer.
* @param [in] error Error status @see PDS_STATUS
* @param [in] message Error message.
*/
static void PDS_error(PDS_parser *parser, const char *current, int error, const char *message) {
parser->status = error;
if(0 != parser->callbacks.error) {
PDS_error_description description;
description.line_first = description.line_last = parser->line;
if(description.line_last) {
for(; description.line_last != parser->last; description.line_last++) {
if('\n' == *description.line_last) {
break;
}
}
}
description.number = parser->line_num;
description.position = current - parser->line + 1;
description.status = error;
description.msg = message;
parser->callbacks.error(&description, parser->user_data);
}
}
/**
* Skip comment.
* Comments are C-like comments, but unlike them they must fit on a single line.
* Nested comments are forbidden.
* @param [in,out] parser PDS parser context.
* @return 0 if an error occured, 1 upon success.
*/
static int PDS_skip_comment(PDS_parser *parser) {
const char* end = parser->last;
const char* current = parser->current;
char previous;
/* The first 2 characters must be '/' and '*'. */
if(current > end) { goto no_input; }
if('/' != current[0]) {
PDS_error(parser, current, PDS_INVALID_VALUE, "invalid input");
goto err;
}
current++;
if(current > end) { goto no_input; }
if('*' != current[0]) {
PDS_error(parser, current, PDS_INVALID_VALUE, "invalid input");
goto err;
}
current++;
for(previous='\0'; current<=end; current++) {
char c = current[0];
if(('/' == c) && ('*' == previous)) {
/* we reached the end of a comment */
parser->current = current;
return 1;
}
else if(('\r' == c) || ('\n' == c)) {
PDS_error(parser, current, PDS_INVALID_VALUE, "unterminated comment");
goto err;
}
else if(('*' == c) && ('/' == previous)) {
PDS_error(parser, current, PDS_INVALID_VALUE, "nested comments");
goto err;
}
previous = c;
}
no_input:
/* No data left. */
PDS_error(parser, current, PDS_INCOMPLETE, "unterminated comments");
err:
parser->current = current;
return 0;
}
/**
* Skip whitespaces and comments from string.
* A white space is either a space (' '), form-feed ('\\f'), newline ('\\n'),
* carriage return ('\\r'), horizontal tab ('\\t') or vertical tab ('\\v').
* Comments are C-like comments, but unlike them they must fit on a single line.
* Nested comments are forbidden.
* @param [in,out] parser PDS parser context.
* @return 0 if an error occured, 1 upon success.
*/
static int PDS_skip_whitespaces(PDS_parser *parser) {
for(; parser->current<=parser->last; parser->current++) {
char c = parser->current[0];
if(!PDS_isspace(c)) {
/* skip comments if the current symbol is not a space */
if('/' != c) {
return 1;
}
else if(0 == PDS_skip_comment(parser)) {
return 0;
}
}
else if('\n' == c) {
/* keep track of line number */
++parser->line_num;
parser->line = parser->current+1;
}
}
return 1;
}
/*
* Remove leading and trailing white spaces in a string.
* A white space is either a space (' '), form-feed ('\f'), newline ('\n'),
* carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v').
*/
void PDS_trim(const char *first, const char *last, const char **begin, const char **end) {
for(; (first <= last) && PDS_isspace(*first); ++first) {
}
for(; (last>=first) && PDS_isspace(*last); --last) {
}
*begin = (first<=last) ? first : 0;
*end = (first<=last) ? last : 0;
}
/*
* Find the first occurence of a character in a string.
*/
const char* PDS_find_first(const char *first, const char *last, char c) {
while(first<=last) {
if(c == *first++) {
return first-1;
}
}
return 0;
}
/*
* Compare two strings.
*/
int PDS_string_compare(const char *f0, const char *l0, const char *f1, const char *l1) {
for(; (f0<=l0) && (f1<=l1) && (*f0==*f1); ++f0, ++f1) {
}
return ((f0>l0)&&(f1>l1));
}
/*
* Compare two strings ignoring case.
*/
int PDS_string_case_compare(const char *f0, const char *l0, const char *f1, const char *l1) {
for(; (f0<=l0) && (f1<=l1) && (PDS_toupper(*f0) == PDS_toupper(*f1)); ++f0, ++f1) {
}
return ((f0>l0)&&(f1>l1));
}
/*
* Parse an integer in the specified base.
*/
static int64_t PDS_parse_int_base(const char *first, const char *last, const char **end, int base, int *status) {
const char *ptr;
int64_t current;
int64_t accumulator;
int64_t cutoff;
int64_t cutlimit;
int any;
int neg = 0;
if(end) {
*end = first;
}
/* Sanity check. */
if(PDS_OK != *status) {
return 0;
}
if((base<2) || (base>16)) {
*status = PDS_INVALID_VALUE;
return INT64_MAX;
}
/* Trim spaces. */
for(ptr=first; (ptr<=last) && PDS_isspace(*ptr); ++ptr) {
}
/* Check for possible sign. */
if('-' == *ptr) {
neg = 1;
++ptr;
}
else {
neg = 0;
if('+' == *ptr) {
++ptr;
}
}
/*
* Compute the cutoff value between legal and illegal number of the
* accumulator prior to adding the last read digit. If the accumulator is
* egal to this cutoff value, the last read digit must not produce an
* invalid result.
* In other words, the cuttoff value is MAX_INT/base and the limit is
* MAX_INT%base.
*/
cutoff = INT64_MAX;
cutlimit = cutoff % base;
cutoff /= base;
/* Parse integer. */
for(accumulator=0, any=0; ptr<=last; ++ptr) {
if(PDS_isdigit(*ptr)) {
current = *ptr - '0';
}
else if(PDS_isalpha(*ptr)) {
current = *ptr - (PDS_islower(*ptr) ? ('a'-10) : ('A'-10));
}
else {
break;
}
if(current >= base) {
break;
}
if(any >= 0) {
if( (accumulator > cutoff)
|| ((accumulator == cutoff) && (current > cutlimit)) ) {
any = -1;
}
else {
any = 1;
accumulator *= base;
accumulator += current;
}
}
}
if(0 != end) {
*end = any ? ptr : first;
}
if(any < 0) {
*status = PDS_INVALID_RANGE;
return neg ? INT64_MIN : INT64_MAX;
}
else {
*status = any ? PDS_OK : PDS_INVALID_VALUE;
return (int64_t)(neg ? -accumulator : accumulator);
}
}
/**
* Parse an integer number.
* The number can be expressed in decimal or based notation.
* A based integer is represented by a decimal number representing the base
* followed by a number expressed in the specified base enclosed between '#'
* characters.
* @param [in,out] parser Parser.
* @return 1 if the string contains a valid integer, 0 otherwise.
*/
static int PDS_parse_int(PDS_parser *parser) {
const char *ptr = 0;
int64_t value;
/* Sanity check. */
if(PDS_OK != parser->status) {
return 0;
}
/* Parse value. */
value = PDS_parse_int_base(parser->current, parser->last, &ptr, 10, &parser->status);
if(PDS_OK != parser->status) {
PDS_error(parser, ptr, parser->status, "invalid integer value");
return 0;
}
/*
* If the character following the current number is a '#', this may mean
* that the number is in fact expressed in based notation.
*/
if((ptr < parser->last) && (*ptr == '#')) {
if((value < 2) || (value > 16)) {
PDS_error(parser, ptr, PDS_INVALID_RANGE, "invalid integer base");
return 0;
}
const char *current = ptr+1;
value = PDS_parse_int_base(current, parser->last, &ptr, (int)value, &parser->status);
if(PDS_OK == parser->status) {
/* Check that the number is followed by a closing '#'. */
if('#' != *ptr) {
PDS_error(parser, ptr, PDS_INVALID_VALUE, "invalid of missing delimiter");
return 0;
}
else {
++ptr;
}
}
else {
PDS_error(parser, ptr, parser->status, "invalid integer value");
return 0;
}
}
parser->current = ptr;
parser->scalar.type = PDS_INTEGER_VALUE;
parser->scalar.integer.value = value;
parser->scalar.integer.unit.first = parser->scalar.integer.unit.last = 0;
return 1;
}
/**
* Parse a floating point value.
* @param [in,out] parser Parser.
* @return 1 if a floating point value was successfully parser, or 0 if an error occured.
*/
static int PDS_parse_real(PDS_parser *parser) {
double value;
int neg = 0;
int div;
int64_t integer;
const char *ptr = 0;
/* Integer part (can be negative). */
integer = PDS_parse_int_base(parser->current, parser->last, &ptr, 10, &parser->status);
/* The integer part can be empty (ex: .03). */
if((0 == integer) || (ptr == parser->current)) {
integer = 0;
parser->status = PDS_OK;
if('-' == *parser->current) {
neg = 1;
if(ptr == parser->current) {
++ptr;
}
}
else if(('+' == *parser->current) && (ptr == parser->current)) {
++ptr;
}
}
else if(integer < 0) {
neg = 1;
}
if(PDS_OK != parser->status) {
PDS_error(parser, ptr, parser->status, "invalid value");
return 0;
}
value = (double)integer;
/* Check for decimal part. */
if('.' == *ptr) {
double decimal = 0.0;
++ptr;
for(div=10; (ptr<=parser->last) && PDS_isdigit(*ptr); ptr++, div*=10) {
integer = *ptr - '0';
if(integer) {
decimal += integer/(double)div;
}
}
value += neg ? -decimal : decimal;
}
/* Check for exponent. */
if(('e' == *ptr) || ('E' == *ptr)) {
int64_t i;
int64_t n;
n = PDS_parse_int_base(ptr+1, parser->last, &ptr, 10, &parser->status);
if(PDS_OK != parser->status) {
PDS_error(parser, ptr, PDS_INVALID_VALUE, "invalid exponent value");
return 0;
}
if(n < 0) {
for(i=0, div=1; i>n; --i, div*=10) {
}
value /= (double)div;
}
else {
int exponent;
for(i=0, exponent=1; i<n; ++i, exponent*=10) {
}
value *= (double)exponent;
}
}
parser->current = ptr;
parser->scalar.type = PDS_REAL_VALUE;
parser->scalar.real.value = value;
parser->scalar.real.unit.first = parser->scalar.real.unit.last = 0;
return 1;
}
/**
* Parse identifier.
* @param [in] first First character of the input string.
* @param [in] last Last character of the input string.
* @param [out] end If not null stores the pointer to the first invalid
* character of the input string.
* @param [in,out] status Status variable set to PDS_OK if the string contains
* a valid identifier or PDS_INVALID_VALUE.
* @return Pointer to the beginning of the identifier or 0 if the string is invalid.
*/
static const char* PDS_parse_identifier(const char *first, const char *last, const char **end, int *status) {
const char *ptr;
char c;
if(PDS_OK != *status) {
*end = first;
return 0;
}
ptr = first;
/* First character must be an alphabetic character. */
c = *ptr++;
if(PDS_isalpha(c)) {
/* Then follows alphanumeric or '_'. */
for(; ptr<=last; ptr++) {
c = *ptr;
if(!PDS_isalnum(c)) {
/* '.' was added in order to parse AMIE SMART-1 pds files... */
if(('_' == c) || ('.' == c)) {
++ptr;
c = *ptr;
if(!PDS_isalnum(c)) {
*status = PDS_INVALID_VALUE;
break;
}
}
else {
break;
}
}
}
/* The last valid character must be alphanumeric. */
if(PDS_OK == *status) {
*end = ptr;
return first;
}
}
*end = first;
*status = PDS_INVALID_VALUE;
return 0;
}
/**
* Parse measurement unit.
* If the parsing was succesfull, the content of the measurement string can be retreived from first+1 to
* end-1(excluded).
* @param [in,out] parser Parser.
* @return 1 if a valid unit was parsed, 0 if the string is invalid.
*/
static int PDS_parse_unit(PDS_parser *parser) {
const char *first = parser->current;
if(PDS_OK != parser->status) {
return 0;
}
if((PDS_INTEGER_VALUE != parser->scalar.type) && (PDS_REAL_VALUE != parser->scalar.type)) {
PDS_error(parser, first, PDS_INVALID_ARGUMENT, "invalid argument");
return 0;
}
if('<' != *first) {
PDS_error(parser, first, PDS_INVALID_VALUE, "invalid unit delimiter");
return 0;
}
++first;
if(first >= parser->last) {
parser->current = first;
return 0;
}
parser->scalar.integer.unit.first = first;
while(first<parser->last) {
/* unit factor */
for(; (first<parser->last) && (PDS_isalpha(*first) || ('_'==*first) || (' '==*first)); first++) { /* DAWN hack, units can be a literal string... */
}
/* Check for multiplier, exponent or divisor. */
if(first < parser->last) {
if('*' == *first) {
++first;
if('*' == *first) {
const char *next = 0;
(void)PDS_parse_int_base(first+1, parser->last, &next, 10, &parser->status);
if(PDS_OK != parser->status) {
PDS_error(parser, next, parser->status, "invalid unit exponent");
return 0;
}
first = next;
}
else if(!PDS_isalpha(*first)) {
PDS_error(parser, first, PDS_INVALID_VALUE, "invalid unit");
return 0;
}
}
else if('/' == *first) {
++first;
if(!PDS_isalpha(*first)) {
PDS_error(parser, first, PDS_INVALID_VALUE, "invalid unit divisor");
return 0;
}
}
else if('>' == *first) {
break;
}
else {
PDS_error(parser, first, PDS_INVALID_VALUE, "unauthorized unit character");
return 0;
}
}
}
if((first>parser->last) || ('>' != *first)) {
PDS_error(parser, first, PDS_INVALID_VALUE, "missing or invalid unit delimiter");
return 0;
}
parser->scalar.integer.unit.last = first-1;
parser->current = first+1;
return 1;
}
/**
* Parse a literal symbol.
* If the parsing was succesfull, the content of the literal value can be retreived from first+1 to
* end-1(excluded).
* @param [in,out] parser Parser.
* @return 1 if the string contains a valid literal symbol, 0 if the string is invalid.
*/
static int PDS_parse_symbol(PDS_parser *parser) {
const char *first = parser->current;
if(PDS_OK != parser->status) {
return 0;
}
/* The string must start with an apostrophe. */
if('\'' != *first) {
PDS_error(parser, first, PDS_INVALID_VALUE, "invalid delimiter");
return 0;
}
++first;
parser->scalar.symbolic.first = first;
/* The string must contain at least one valid character. */
if((first>=parser->last) || ('\'' == *first)) {
PDS_error(parser, first, PDS_INVALID_VALUE, "empty literal symbol");
return 0;
}
/* The string may contain spacing, alpha-numeric, other and special characters except the apostrophe. */
for(; (first<parser->last) && ('\''!=*first) && (('\t'==*first) || ((*first>=0x20) && (*first<=0x7e))); first++) {
}
if((first>parser->last) || ('\'' != *first)) {
PDS_error(parser, first, PDS_INVALID_VALUE, "missing literal symbol delimiter");
return 0;
}
parser->scalar.symbolic.last = first-1;
parser->scalar.type = PDS_SYMBOLIC_VALUE;
parser->current = first+1;
return 1;
}
/**
* Parse a quoted string.
* If the parsing was succesfull, the content of the quoted text can be retreived from first+1 to
* end-1(excluded).
* @param [in,out] parser Parser.
* @return 1 if the string contains a valid quoted text, 0 if the string is invalid.
*/
static int PDS_parse_string(PDS_parser *parser) {
const char *first = parser->current;
const char *last = parser->last;
if(PDS_OK != parser->status) {
return 0;
}
if('"' != *first++) {
PDS_error(parser,first, PDS_INVALID_VALUE, "invalid or missing string delimiter");
return 0;
}
parser->scalar.text.first = first;
for(; (first<last) && ('"' != *first) && (PDS_isspace(*first) || PDS_extascii(*first)); first++) {
if('\n' == *first) {
++parser->line_num;
parser->line = first+1;
}
}
if(first > last) {
PDS_error(parser, first, PDS_INCOMPLETE, "nested comments");
return 0;
}
if('"' != *first) {
PDS_error(parser, first, PDS_INVALID_VALUE, "missing string delimiter");
return 0;
}
parser->scalar.type = PDS_TEXT_STRING_VALUE;
parser->current = first+1;
parser->scalar.text.last = first-1;
parser->current = first+1;
return 1;
}
/**
* Parse date.
* @param [in] first First character of the input string.
* @param [in] last Last character of the input string.
* @param [out] end If not null stores the pointer to the first invalid
* character of the input string.
* @param [out] date Date.
* @param [in,out] status Status variable set to PDS_OK if the string contains
* a valid date or PDS_INVALID_VALUE.
* @return 1 if the string contains a valid date, 0 if the string is invalid.
*/
static int PDS_parse_date(const char *first, const char *last, const char **end, PDS_datetime *date, int *status) {
int64_t value;
const char *next = 0;
*end = first;