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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
|
/* Copyright 2018, UCAR/Unidata.
See the COPYRIGHT file for more information.
*/
/*
TODO: make utf8 safe
*/
/*
WARNING:
If you modify this file,
then you need to got to
the include/ directory
and do the command:
make makenetcdfjson
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "ncjson.h"
#undef NCJDEBUG
#ifdef NCJDEBUG
static int ncjbreakpoint(int err) {return err;}
#define NCJTHROW(err) ((err)==NCJ_ERR?ncjbreakpoint(err):(err))
#else
#define NCJTHROW(err) (err)
#endif
/**************************************************/
#define NCJ_OK 0
#define NCJ_ERR (-1)
#define NCJ_EOF -2
#define NCJ_LBRACKET '['
#define NCJ_RBRACKET ']'
#define NCJ_LBRACE '{'
#define NCJ_RBRACE '}'
#define NCJ_COLON ':'
#define NCJ_COMMA ','
#define NCJ_QUOTE '"'
#define NCJ_ESCAPE '\\'
#define NCJ_TAG_TRUE "true"
#define NCJ_TAG_FALSE "false"
#define NCJ_TAG_NULL "null"
/* JSON_WORD Subsumes Number also */
#define JSON_WORD "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$+-."
/**************************************************/
typedef struct NCJparser {
char* text;
char* pos;
size_t yylen; /* |yytext| */
char* yytext; /* string or word */
long long num;
int tf;
int status; /* NCJ_ERR|NCJ_OK */
} NCJparser;
typedef struct NCJbuf {
int len; /* |text|; does not include nul terminator */
char* text; /* NULL || nul terminated */
} NCJbuf;
/**************************************************/
#if defined(_WIN32) && !defined(__MINGW32__)
#define strdup _strdup
#define strcasecmp _stricmp
#else
#include <strings.h>
#endif
#ifndef nullfree
#define nullfree(x) {if(x)free(x);}
#endif
#ifndef nulldup
#define nulldup(x) ((x)?strdup(x):(x))
#endif
#ifdef NCJDEBUG
static char* tokenname(int token);
#endif
/**************************************************/
/* Forward */
static int NCJparseR(NCJparser* parser, NCjson**);
static int NCJparseArray(NCJparser* parser, struct NCjlist* array);
static int NCJparseDict(NCJparser* parser, struct NCjlist* dict);
static int testbool(const char* word);
static int testint(const char* word);
static int testdouble(const char* word);
static int testnull(const char* word);
static int NCJlex(NCJparser* parser);
static int NCJyytext(NCJparser*, char* start, size_t pdlen);
static void NCJreclaimArray(struct NCjlist*);
static void NCJreclaimDict(struct NCjlist*);
static int NCJunescape(NCJparser* parser);
static int unescape1(int c);
static int listappend(struct NCjlist* list, NCjson* element);
static int NCJcloneArray(const NCjson* array, NCjson** clonep);
static int NCJcloneDict(const NCjson* dict, NCjson** clonep);
static int NCJunparseR(const NCjson* json, NCJbuf* buf, unsigned flags);
static int bytesappendquoted(NCJbuf* buf, const char* s);
static int bytesappend(NCJbuf* buf, const char* s);
static int bytesappendc(NCJbuf* bufp, const char c);
/* Hide everything for plugins */
#ifdef NETCDF_JSON_H
#define OPTSTATIC static
static int NCJparsen(size_t len, const char* text, unsigned flags, NCjson** jsonp);
static int NCJnew(int sort, NCjson** objectp);
static int NCJnewstring(int sort, const char* value, NCjson** jsonp);
static int NCJnewstringn(int sort, size_t len, const char* value, NCjson** jsonp);
static int NCJclone(const NCjson* json, NCjson** clonep);
static int NCJaddstring(NCjson* json, int sort, const char* s);
static int NCJinsert(NCjson* object, char* key, NCjson* jvalue);
static int NCJappend(NCjson* object, NCjson* value);
static int NCJunparse(const NCjson* json, unsigned flags, char** textp);
#else /*!NETCDF_JSON_H*/
#define OPTSTATIC
#endif /*NETCDF_JSON_H*/
/**************************************************/
OPTSTATIC int
NCJparse(const char* text, unsigned flags, NCjson** jsonp)
{
return NCJparsen(strlen(text),text,flags,jsonp);
}
OPTSTATIC int
NCJparsen(size_t len, const char* text, unsigned flags, NCjson** jsonp)
{
int stat = NCJ_OK;
NCJparser* parser = NULL;
NCjson* json = NULL;
parser = calloc(1,sizeof(NCJparser));
if(parser == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
parser->text = (char*)malloc(len+1+1);
if(parser->text == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
memcpy(parser->text,text,len);
/* trim trailing whitespace */
if(len > 0) {
char* p;
for(p=parser->text+(len-1);p >= parser->text;p--) {
if(*p > ' ') break;
}
len = (size_t)((p - parser->text) + 1);
}
if(len == 0)
{stat = NCJTHROW(NCJ_ERR); goto done;}
parser->text[len] = '\0';
parser->text[len+1] = '\0';
parser->pos = &parser->text[0];
parser->status = NCJ_OK;
#ifdef NCJDEBUG
fprintf(stderr,"json: |%s|\n",parser->text);
#endif
if((stat=NCJparseR(parser,&json))==NCJ_ERR) goto done;
/* Must consume all of the input */
if(parser->pos != (parser->text+len)) {stat = NCJ_ERR; goto done;}
*jsonp = json;
json = NULL;
done:
if(parser != NULL) {
nullfree(parser->text);
nullfree(parser->yytext);
free(parser);
}
(void)NCJreclaim(json);
return NCJTHROW(stat);
}
/*
Simple recursive descent
intertwined with dict and list parsers.
Invariants:
1. The json argument is provided by caller and filled in by NCJparseR.
2. Each call pushed back last unconsumed token
*/
static int
NCJparseR(NCJparser* parser, NCjson** jsonp)
{
int stat = NCJ_OK;
int token = NCJ_UNDEF;
NCjson* json = NULL;
if(jsonp == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if((token = NCJlex(parser)) == NCJ_UNDEF)
{stat = NCJTHROW(NCJ_ERR); goto done;}
switch (token) {
case NCJ_EOF:
break;
case NCJ_NULL:
if((stat = NCJnew(NCJ_NULL,&json))==NCJ_ERR) goto done;
break;
case NCJ_BOOLEAN:
if((stat = NCJnew(NCJ_BOOLEAN,&json))==NCJ_ERR) goto done;
json->string = strdup(parser->yytext);
break;
case NCJ_INT:
if((stat = NCJnew(NCJ_INT,&json))==NCJ_ERR) goto done;
json->string = strdup(parser->yytext);
break;
case NCJ_DOUBLE:
if((stat = NCJnew(NCJ_DOUBLE,&json))==NCJ_ERR) goto done;
json->string = strdup(parser->yytext);
break;
case NCJ_STRING:
if((stat = NCJnew(NCJ_STRING,&json))==NCJ_ERR) goto done;
json->string = strdup(parser->yytext);
break;
case NCJ_LBRACE:
if((stat = NCJnew(NCJ_DICT,&json))==NCJ_ERR) goto done;
if((stat = NCJparseDict(parser, &json->list))==NCJ_ERR) goto done;
break;
case NCJ_LBRACKET:
if((stat = NCJnew(NCJ_ARRAY,&json))==NCJ_ERR) goto done;
if((stat = NCJparseArray(parser, &json->list))==NCJ_ERR) goto done;
break;
case NCJ_RBRACE: /* We hit end of the dict we are parsing */
parser->pos--; /* pushback so NCJparseArray will catch */
json = NULL;
break;
case NCJ_RBRACKET:
parser->pos--; /* pushback so NCJparseDict will catch */
json = NULL;
break;
default:
stat = NCJTHROW(NCJ_ERR);
break;
}
if(jsonp && json) {*jsonp = json; json = NULL;}
done:
NCJreclaim(json);
return NCJTHROW(stat);
}
static int
NCJparseArray(NCJparser* parser, struct NCjlist* arrayp)
{
int stat = NCJ_OK;
int token = NCJ_UNDEF;
NCjson* element = NULL;
int stop = 0;
/* [ ^e1,e2, ...en] */
while(!stop) {
/* Recurse to get the value ei (might be null) */
if((stat = NCJparseR(parser,&element))==NCJ_ERR) goto done;
token = NCJlex(parser); /* Get next token */
/* Next token should be comma or rbracket */
switch(token) {
case NCJ_RBRACKET:
if(element != NULL) listappend(arrayp,element);
element = NULL;
stop = 1;
break;
case NCJ_COMMA:
/* Append the ei to the list */
if(element == NULL) {stat = NCJTHROW(NCJ_ERR); goto done;} /* error */
listappend(arrayp,element);
element = NULL;
break;
case NCJ_EOF:
case NCJ_UNDEF:
default:
stat = NCJTHROW(NCJ_ERR);
goto done;
}
}
done:
if(element != NULL)
NCJreclaim(element);
return NCJTHROW(stat);
}
static int
NCJparseDict(NCJparser* parser, struct NCjlist* dictp)
{
int stat = NCJ_OK;
int token = NCJ_UNDEF;
NCjson* value = NULL;
NCjson* key = NULL;
int stop = 0;
/* { ^k1:v1,k2:v2, ...kn:vn] */
while(!stop) {
/* Get the key, which must be a word of some sort */
token = NCJlex(parser);
switch(token) {
case NCJ_STRING:
case NCJ_BOOLEAN:
case NCJ_INT: case NCJ_DOUBLE: {
if((stat=NCJnewstring(token,parser->yytext,&key))==NCJ_ERR) goto done;
} break;
case NCJ_RBRACE: /* End of containing Dict */
stop = 1;
continue; /* leave loop */
case NCJ_EOF: case NCJ_UNDEF:
default:
stat = NCJTHROW(NCJ_ERR);
goto done;
}
/* Next token must be colon*/
switch((token = NCJlex(parser))) {
case NCJ_COLON: break;
case NCJ_UNDEF: case NCJ_EOF:
default: stat = NCJTHROW(NCJ_ERR); goto done;
}
/* Get the value */
if((stat = NCJparseR(parser,&value))==NCJ_ERR) goto done;
/* Next token must be comma or RBRACE */
switch((token = NCJlex(parser))) {
case NCJ_RBRACE:
stop = 1;
/* fall thru */
case NCJ_COMMA:
/* Insert key value into dict: key first, then value */
listappend(dictp,key);
key = NULL;
listappend(dictp,value);
value = NULL;
break;
case NCJ_EOF:
case NCJ_UNDEF:
default:
stat = NCJTHROW(NCJ_ERR);
goto done;
}
}
done:
if(key != NULL)
NCJreclaim(key);
if(value != NULL)
NCJreclaim(value);
return NCJTHROW(stat);
}
static int
NCJlex(NCJparser* parser)
{
int c;
int token = NCJ_UNDEF;
char* start;
size_t count;
while(token == 0) { /* avoid need to goto when retrying */
c = *parser->pos;
if(c == '\0') {
token = NCJ_EOF;
} else if(c <= ' ' || c == '\177') {/* ignore whitespace */
parser->pos++;
continue;
} else if(c == NCJ_ESCAPE) {
parser->pos++;
c = *parser->pos;
*parser->pos = unescape1(c);
continue;
} else if(strchr(JSON_WORD, c) != NULL) {
start = parser->pos;
for(;;) {
c = *parser->pos++;
if(c == '\0' || strchr(JSON_WORD,c) == NULL) break; /* end of word */
}
/* Pushback c */
parser->pos--;
count = ((parser->pos) - start);
if(NCJyytext(parser,start,count)) goto done;
/* Discriminate the word string to get the proper sort */
if(testbool(parser->yytext) == NCJ_OK)
token = NCJ_BOOLEAN;
/* do int test first since double subsumes int */
else if(testint(parser->yytext) == NCJ_OK)
token = NCJ_INT;
else if(testdouble(parser->yytext) == NCJ_OK)
token = NCJ_DOUBLE;
else if(testnull(parser->yytext) == NCJ_OK)
token = NCJ_NULL;
else
token = NCJ_STRING;
} else if(c == NCJ_QUOTE) {
parser->pos++;
start = parser->pos;
for(;;) {
c = *parser->pos++;
if(c == NCJ_ESCAPE) parser->pos++;
else if(c == NCJ_QUOTE || c == '\0') break;
}
if(c == '\0') {
parser->status = NCJ_ERR;
token = NCJ_UNDEF;
goto done;
}
count = ((parser->pos) - start) - 1; /* -1 for trailing quote */
if(NCJyytext(parser,start,count)==NCJ_ERR) goto done;
if(NCJunescape(parser)==NCJ_ERR) goto done;
token = NCJ_STRING;
} else { /* single char token */
if(NCJyytext(parser,parser->pos,1)==NCJ_ERR) goto done;
token = *parser->pos++;
}
#ifdef NCJDEBUG
fprintf(stderr,"%s(%d): |%s|\n",tokenname(token),token,parser->yytext);
#endif
} /*for(;;)*/
done:
if(parser->status == NCJ_ERR)
token = NCJ_UNDEF;
return token;
}
static int
testnull(const char* word)
{
if(strcasecmp(word,NCJ_TAG_NULL)==0)
return NCJTHROW(NCJ_OK);
return NCJTHROW(NCJ_ERR);
}
static int
testbool(const char* word)
{
if(strcasecmp(word,NCJ_TAG_TRUE)==0
|| strcasecmp(word,NCJ_TAG_FALSE)==0)
return NCJTHROW(NCJ_OK);
return NCJTHROW(NCJ_ERR);
}
static int
testint(const char* word)
{
int ncvt;
long long i;
int count = 0;
/* Try to convert to number */
ncvt = sscanf(word,"%lld%n",&i,&count);
return NCJTHROW((ncvt == 1 && strlen(word)==count ? NCJ_OK : NCJ_ERR));
}
static int
testdouble(const char* word)
{
int ncvt;
double d;
int count = 0;
/* Check for Nan and Infinity */
if(0==(int)strcasecmp("nan",word)) return NCJTHROW(NCJ_OK);
if(0==(int)strcasecmp("infinity",word)) return NCJTHROW(NCJ_OK);
if(0==(int)strcasecmp("-infinity",word)) return NCJTHROW(NCJ_OK);
/* Allow the XXXf versions as well */
if(0==(int)strcasecmp("nanf",word)) return NCJTHROW(NCJ_OK);
if(0==(int)strcasecmp("infinityf",word)) return NCJTHROW(NCJ_OK);
if(0==(int)strcasecmp("-infinityf",word)) return NCJTHROW(NCJ_OK);
/* Try to convert to number */
ncvt = sscanf(word,"%lg%n",&d,&count);
return NCJTHROW((ncvt == 1 && strlen(word)==count ? NCJ_OK : NCJ_ERR));
}
static int
NCJyytext(NCJparser* parser, char* start, size_t pdlen)
{
size_t len = (size_t)pdlen;
if(parser->yytext == NULL) {
parser->yytext = (char*)malloc(len+1);
parser->yylen = len;
} else if(parser->yylen <= len) {
parser->yytext = (char*) realloc(parser->yytext,len+1);
parser->yylen = len;
}
if(parser->yytext == NULL) return NCJTHROW(NCJ_ERR);
memcpy(parser->yytext,start,len);
parser->yytext[len] = '\0';
return NCJTHROW(NCJ_OK);
}
/**************************************************/
OPTSTATIC void
NCJreclaim(NCjson* json)
{
if(json == NULL) return;
switch(json->sort) {
case NCJ_INT:
case NCJ_DOUBLE:
case NCJ_BOOLEAN:
case NCJ_STRING:
nullfree(json->string);
break;
case NCJ_DICT:
NCJreclaimDict(&json->list);
break;
case NCJ_ARRAY:
NCJreclaimArray(&json->list);
break;
default: break; /* nothing to reclaim */
}
free(json);
}
static void
NCJreclaimArray(struct NCjlist* array)
{
int i;
for(i=0;i<array->len;i++) {
NCJreclaim(array->contents[i]);
}
nullfree(array->contents);
array->contents = NULL;
}
static void
NCJreclaimDict(struct NCjlist* dict)
{
NCJreclaimArray(dict);
}
/**************************************************/
/* Build Functions */
OPTSTATIC int
NCJnew(int sort, NCjson** objectp)
{
int stat = NCJ_OK;
NCjson* object = NULL;
if((object = (NCjson*)calloc(1,sizeof(NCjson))) == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
NCJsetsort(object,sort);
switch (sort) {
case NCJ_INT:
case NCJ_DOUBLE:
case NCJ_BOOLEAN:
case NCJ_STRING:
case NCJ_NULL:
break;
case NCJ_DICT:
case NCJ_ARRAY:
break;
default:
stat = NCJTHROW(NCJ_ERR);
goto done;
}
if(objectp) {*objectp = object; object = NULL;}
done:
if(stat) NCJreclaim(object);
return NCJTHROW(stat);
}
OPTSTATIC int
NCJnewstring(int sort, const char* value, NCjson** jsonp)
{
return NCJTHROW(NCJnewstringn(sort,strlen(value),value,jsonp));
}
OPTSTATIC int
NCJnewstringn(int sort, size_t len, const char* value, NCjson** jsonp)
{
int stat = NCJ_OK;
NCjson* json = NULL;
if(jsonp) *jsonp = NULL;
if(value == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if((stat = NCJnew(sort,&json))==NCJ_ERR)
goto done;
if((json->string = (char*)malloc(len+1))==NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
memcpy(json->string,value,len);
json->string[len] = '\0';
if(jsonp) *jsonp = json;
json = NULL; /* avoid memory errors */
done:
NCJreclaim(json);
return NCJTHROW(stat);
}
OPTSTATIC int
NCJdictget(const NCjson* dict, const char* key, NCjson** valuep)
{
int i,stat = NCJ_OK;
if(dict == NULL || dict->sort != NCJ_DICT)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if(valuep) {*valuep = NULL;}
for(i=0;i<NCJlength(dict);i+=2) {
NCjson* jkey = NCJith(dict,i);
if(jkey->string != NULL && strcmp(jkey->string,key)==0) {
if(valuep) {*valuep = NCJith(dict,i+1); break;}
}
}
done:
return NCJTHROW(stat);
}
/* Unescape the text in parser->yytext; can
do in place because unescaped string will
always be shorter */
static int
NCJunescape(NCJparser* parser)
{
char* p = parser->yytext;
char* q = p;
int c;
for(;(c=*p++);) {
if(c == NCJ_ESCAPE) {
c = *p++;
switch (c) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case NCJ_QUOTE: c = c; break;
case NCJ_ESCAPE: c = c; break;
default: c = c; break;/* technically not Json conformant */
}
}
*q++ = c;
}
*q = '\0';
return NCJTHROW(NCJ_OK);
}
/* Unescape a single character */
static int
unescape1(int c)
{
switch (c) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
default: c = c; break;/* technically not Json conformant */
}
return c;
}
#ifdef NCJDEBUG
static char*
tokenname(int token)
{
switch (token) {
case NCJ_STRING: return ("NCJ_STRING");
case NCJ_INT: return ("NCJ_INT");
case NCJ_DOUBLE: return ("NCJ_DOUBLE");
case NCJ_BOOLEAN: return ("NCJ_BOOLEAN");
case NCJ_DICT: return ("NCJ_DICT");
case NCJ_ARRAY: return ("NCJ_ARRAY");
case NCJ_NULL: return ("NCJ_NULL");
default:
if(token > ' ' && token <= 127) {
static char s[4];
s[0] = '\'';
s[1] = (char)token;
s[2] = '\'';
s[3] = '\0';
return (s);
} else
break;
}
return ("NCJ_UNDEF");
}
#endif
/* Convert a JSON value to an equivalent value of a specified sort */
OPTSTATIC int
NCJcvt(const NCjson* jvalue, int outsort, struct NCJconst* output)
{
int stat = NCJ_OK;
if(output == NULL) goto done;
#undef CASE
#define CASE(t1,t2) ((t1)<<4 | (t2)) /* the shift constant must be larger than log2(NCJ_NSORTS) */
switch (CASE(jvalue->sort,outsort)) {
case CASE(NCJ_BOOLEAN,NCJ_BOOLEAN):
if(strcasecmp(jvalue->string,NCJ_TAG_FALSE)==0) output->bval = 0; else output->bval = 1;
break;
case CASE(NCJ_BOOLEAN,NCJ_INT):
if(strcasecmp(jvalue->string,NCJ_TAG_FALSE)==0) output->ival = 0; else output->ival = 1;
break;
case CASE(NCJ_BOOLEAN,NCJ_DOUBLE):
if(strcasecmp(jvalue->string,NCJ_TAG_FALSE)==0) output->dval = 0.0; else output->dval = 1.0;
break;
case CASE(NCJ_BOOLEAN,NCJ_STRING):
output->sval = nulldup(jvalue->string);
break;
case CASE(NCJ_INT,NCJ_BOOLEAN):
sscanf(jvalue->string,"%lldd",&output->ival);
output->bval = (output->ival?1:0);
break;
case CASE(NCJ_INT,NCJ_INT):
sscanf(jvalue->string,"%lld",&output->ival);
break;
case CASE(NCJ_INT,NCJ_DOUBLE):
sscanf(jvalue->string,"%lld",&output->ival);
output->dval = (double)output->ival;
break;
case CASE(NCJ_INT,NCJ_STRING):
output->sval = nulldup(jvalue->string);
break;
case CASE(NCJ_DOUBLE,NCJ_BOOLEAN):
sscanf(jvalue->string,"%lf",&output->dval);
output->bval = (output->dval == 0?0:1);
break;
case CASE(NCJ_DOUBLE,NCJ_INT):
sscanf(jvalue->string,"%lf",&output->dval);
output->ival = (long long)output->dval;
break;
case CASE(NCJ_DOUBLE,NCJ_DOUBLE):
sscanf(jvalue->string,"%lf",&output->dval);
break;
case CASE(NCJ_DOUBLE,NCJ_STRING):
output->sval = nulldup(jvalue->string);
break;
case CASE(NCJ_STRING,NCJ_BOOLEAN):
if(strcasecmp(jvalue->string,NCJ_TAG_FALSE)==0) output->bval = 0; else output->bval = 1;
break;
case CASE(NCJ_STRING,NCJ_INT):
sscanf(jvalue->string,"%lld",&output->ival);
break;
case CASE(NCJ_STRING,NCJ_DOUBLE):
sscanf(jvalue->string,"%lf",&output->dval);
break;
case CASE(NCJ_STRING,NCJ_STRING):
output->sval = nulldup(jvalue->string);
break;
default:
stat = NCJTHROW(NCJ_ERR);
break;
}
done:
return NCJTHROW(stat);
}
static int
listappend(struct NCjlist* list, NCjson* json)
{
int stat = NCJ_OK;
NCjson** newcontents = NULL;
assert(list->len == 0 || list->contents != NULL);
if(json == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if(list->len == 0) {
nullfree(list->contents);
list->contents = (NCjson**)calloc(2,sizeof(NCjson*));
if(list->contents == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
list->contents[0] = json;
list->len++;
} else {
if((newcontents = (NCjson**)calloc((2*list->len)+1,sizeof(NCjson*)))==NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
memcpy(newcontents,list->contents,list->len*sizeof(NCjson*));
newcontents[list->len] = json;
list->len++;
free(list->contents);
list->contents = newcontents; newcontents = NULL;
}
done:
nullfree(newcontents);
return NCJTHROW(stat);
}
/**************************************************/
OPTSTATIC int
NCJclone(const NCjson* json, NCjson** clonep)
{
int stat = NCJ_OK;
NCjson* clone = NULL;
if(json == NULL) goto done;
switch(NCJsort(json)) {
case NCJ_INT:
case NCJ_DOUBLE:
case NCJ_BOOLEAN:
case NCJ_STRING:
if((stat=NCJnew(NCJsort(json),&clone))==NCJ_ERR) goto done;
if((NCJstring(clone) = strdup(NCJstring(json))) == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
break;
case NCJ_NULL:
if((stat=NCJnew(NCJsort(json),&clone))==NCJ_ERR) goto done;
break;
case NCJ_DICT:
if((stat=NCJcloneDict(json,&clone))==NCJ_ERR) goto done;
break;
case NCJ_ARRAY:
if((stat=NCJcloneArray(json,&clone))==NCJ_ERR) goto done;
break;
default: break; /* nothing to clone */
}
done:
if(stat == NCJ_OK && clonep) {*clonep = clone; clone = NULL;}
NCJreclaim(clone);
return NCJTHROW(stat);
}
static int
NCJcloneArray(const NCjson* array, NCjson** clonep)
{
int i, stat=NCJ_OK;
NCjson* clone = NULL;
if((stat=NCJnew(NCJ_ARRAY,&clone))==NCJ_ERR) goto done;
for(i=0;i<NCJlength(array);i++) {
NCjson* elem = NCJith(array,i);
NCjson* elemclone = NULL;
if((stat=NCJclone(elem,&elemclone))==NCJ_ERR) goto done;
NCJappend(clone,elemclone);
}
done:
if(stat == NCJ_OK && clonep) {*clonep = clone; clone = NULL;}
NCJreclaim(clone);
return stat;
}
static int
NCJcloneDict(const NCjson* dict, NCjson** clonep)
{
int i, stat=NCJ_OK;
NCjson* clone = NULL;
if((stat=NCJnew(NCJ_DICT,&clone))==NCJ_ERR) goto done;
for(i=0;i<NCJlength(dict);i++) {
NCjson* elem = NCJith(dict,i);
NCjson* elemclone = NULL;
if((stat=NCJclone(elem,&elemclone))==NCJ_ERR) goto done;
NCJappend(clone,elemclone);
}
done:
if(stat == NCJ_OK && clonep) {*clonep = clone; clone = NULL;}
NCJreclaim(clone);
return NCJTHROW(stat);
}
OPTSTATIC int
NCJaddstring(NCjson* json, int sort, const char* s)
{
int stat = NCJ_OK;
NCjson* jtmp = NULL;
if(NCJsort(json) != NCJ_DICT && NCJsort(json) != NCJ_ARRAY)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if((stat = NCJnewstring(sort, s, &jtmp))==NCJ_ERR) goto done;
if((stat = NCJappend(json,jtmp))==NCJ_ERR) goto done;
jtmp = NULL;
done:
NCJreclaim(jtmp);
return NCJTHROW(stat);
}
/* Insert key-value pair into a dict object. key will be strdup'd */
OPTSTATIC int
NCJinsert(NCjson* object, char* key, NCjson* jvalue)
{
int stat = NCJ_OK;
NCjson* jkey = NULL;
if(object == NULL || object->sort != NCJ_DICT || key == NULL || jvalue == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if((stat = NCJnewstring(NCJ_STRING,key,&jkey))==NCJ_ERR) goto done;
if((stat = NCJappend(object,jkey))==NCJ_ERR) goto done;
if((stat = NCJappend(object,jvalue))==NCJ_ERR) goto done;
done:
return NCJTHROW(stat);
}
/* Append value to an array or dict object. */
OPTSTATIC int
NCJappend(NCjson* object, NCjson* value)
{
if(object == NULL || value == NULL)
return NCJTHROW(NCJ_ERR);
switch (object->sort) {
case NCJ_ARRAY:
case NCJ_DICT:
listappend(&object->list,value);
break;
default:
return NCJTHROW(NCJ_ERR);
}
return NCJTHROW(NCJ_OK);
}
/**************************************************/
/* Unparser to convert NCjson object to text in buffer */
OPTSTATIC int
NCJunparse(const NCjson* json, unsigned flags, char** textp)
{
int stat = NCJ_OK;
NCJbuf buf = {0,NULL};
if((stat = NCJunparseR(json,&buf,flags))==NCJ_ERR)
goto done;
if(textp) {*textp = buf.text; buf.text = NULL; buf.len = 0;}
done:
nullfree(buf.text);
return NCJTHROW(stat);
}
static int
NCJunparseR(const NCjson* json, NCJbuf* buf, unsigned flags)
{
int stat = NCJ_OK;
int i;
switch (NCJsort(json)) {
case NCJ_STRING:
bytesappendquoted(buf,json->string);
break;
case NCJ_INT:
case NCJ_DOUBLE:
case NCJ_BOOLEAN:
bytesappend(buf,json->string);
break;
case NCJ_DICT:
bytesappendc(buf,NCJ_LBRACE);
if(json->list.len > 0 && json->list.contents != NULL) {
int shortlist = 0;
for(i=0;!shortlist && i < json->list.len;i+=2) {
if(i > 0) {bytesappendc(buf,NCJ_COMMA);bytesappendc(buf,' ');};
NCJunparseR(json->list.contents[i],buf,flags); /* key */
bytesappendc(buf,NCJ_COLON);
bytesappendc(buf,' ');
/* Allow for the possibility of a short dict entry */
if(json->list.contents[i+1] == NULL) { /* short */
bytesappendc(buf,'?');
shortlist = 1;
} else {
NCJunparseR(json->list.contents[i+1],buf,flags);
}
}
}
bytesappendc(buf,NCJ_RBRACE);
break;
case NCJ_ARRAY:
bytesappendc(buf,NCJ_LBRACKET);
if(json->list.len > 0 && json->list.contents != NULL) {
for(i=0;i < json->list.len;i++) {
if(i > 0) bytesappendc(buf,NCJ_COMMA);
NCJunparseR(json->list.contents[i],buf,flags);
}
}
bytesappendc(buf,NCJ_RBRACKET);
break;
case NCJ_NULL:
bytesappend(buf,"null");
break;
default:
stat = NCJTHROW(NCJ_ERR); goto done;
}
done:
return NCJTHROW(stat);
}
/* Escape a string and append to buf */
static int
escape(const char* text, NCJbuf* buf)
{
const char* p = text;
int c;
for(;(c=*p++);) {
char replace = 0;
switch (c) {
case '\b': replace = 'b'; break;
case '\f': replace = 'f'; break;
case '\n': replace = 'n'; break;
case '\r': replace = 'r'; break;
case '\t': replace = 't'; break;
case NCJ_QUOTE: replace = '\"'; break;
case NCJ_ESCAPE: replace = '\\'; break;
default: break;
}
if(replace) {
bytesappendc(buf,NCJ_ESCAPE);
bytesappendc(buf,replace);
} else
bytesappendc(buf,c);
}
return NCJTHROW(NCJ_OK);
}
static int
bytesappendquoted(NCJbuf* buf, const char* s)
{
bytesappend(buf,"\"");
escape(s,buf);
bytesappend(buf,"\"");
return NCJTHROW(NCJ_OK);
}
static int
bytesappend(NCJbuf* buf, const char* s)
{
int stat = NCJ_OK;
char* newtext = NULL;
if(buf == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
if(s == NULL) s = "";
if(buf->len == 0) {
assert(buf->text == NULL);
buf->text = strdup(s);
if(buf->text == NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
buf->len = strlen(s);
} else {
size_t slen = strlen(s);
size_t newlen = buf->len + slen + 1;
if((newtext = (char*)malloc(newlen))==NULL)
{stat = NCJTHROW(NCJ_ERR); goto done;}
strcpy(newtext,buf->text);
strcat(newtext,s);
free(buf->text); buf->text = NULL;
buf->text = newtext; newtext = NULL;
buf->len = newlen;
}
done:
nullfree(newtext);
return NCJTHROW(stat);
}
static int
bytesappendc(NCJbuf* bufp, const char c)
{
char s[2];
s[0] = c;
s[1] = '\0';
return bytesappend(bufp,s);
}
OPTSTATIC void
NCJdump(const NCjson* json, unsigned flags, FILE* out)
{
char* text = NULL;
(void)NCJunparse(json,0,&text);
if(out == NULL) out = stderr;
fprintf(out,"%s\n",text);
fflush(out);
nullfree(text);
}
OPTSTATIC const char*
NCJtotext(const NCjson* json)
{
static char outtext[4096];
char* text = NULL;
if(json == NULL) {strcpy(outtext,"<null>"); goto done;}
(void)NCJunparse(json,0,&text);
outtext[0] = '\0';
strlcat(outtext,text,sizeof(outtext));
nullfree(text);
done:
return outtext;
}
/* Hack to avoid static unused warning */
static void
netcdf_supresswarnings(void)
{
void* ignore;
ignore = (void*)netcdf_supresswarnings;
ignore = (void*)NCJinsert;
ignore = (void*)NCJaddstring;
ignore = (void*)NCJcvt;
ignore = (void*)NCJdictget;
ignore = (void*)NCJparse;
ignore = (void*)NCJdump;
ignore = (void*)NCJtotext;
ignore = ignore;
}
|