2020-08-18 09:15:47 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Parse input to see if it looks like json.
|
|
|
|
Output 1 or 0.
|
|
|
|
*/
|
|
|
|
|
2022-04-07 05:54:42 +08:00
|
|
|
|
2023-11-30 21:46:58 +08:00
|
|
|
#include <stddef.h>
|
2022-04-07 05:54:42 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2020-08-18 09:15:47 +08:00
|
|
|
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "stdio.h"
|
2022-04-07 05:54:42 +08:00
|
|
|
#include "string.h"
|
2020-08-18 09:15:47 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-07 05:54:42 +08:00
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
#include "XGetopt.h"
|
|
|
|
#else
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2020-08-18 09:15:47 +08:00
|
|
|
#include "netcdf.h"
|
|
|
|
#include "nclist.h"
|
2021-07-18 06:55:30 +08:00
|
|
|
#include "ncjson.h"
|
2020-08-18 09:15:47 +08:00
|
|
|
|
|
|
|
#define MAXREAD 8192
|
|
|
|
|
2022-04-07 05:54:42 +08:00
|
|
|
/* Command line options */
|
|
|
|
struct Jsonpptions {
|
|
|
|
int trace;
|
|
|
|
} jsonoptions;
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
sortname(int thesort)
|
|
|
|
{
|
|
|
|
switch(thesort) {
|
|
|
|
default: break;
|
|
|
|
case NCJ_INT: return "NCJ_INT";
|
|
|
|
case NCJ_DOUBLE: return "NCJ_DOUBLE";
|
|
|
|
case NCJ_BOOLEAN: return "NCJ_BOOLEAN";
|
|
|
|
case NCJ_STRING: return "NCJ_STRING";
|
|
|
|
case NCJ_DICT: return "NCJ_DICT";
|
|
|
|
case NCJ_ARRAY: return "NCJ_ARRAY";
|
|
|
|
case NCJ_NULL: return "NCJ_NULL";
|
|
|
|
}
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
jsontrace(NCjson* json, int depth)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if(json == NULL) goto done;
|
|
|
|
printf("[%d] sort=%s",depth,sortname(NCJsort(json)));
|
|
|
|
switch(NCJsort(json)) {
|
|
|
|
case NCJ_INT:
|
|
|
|
case NCJ_DOUBLE:
|
|
|
|
case NCJ_BOOLEAN:
|
|
|
|
case NCJ_STRING:
|
|
|
|
printf(" string=|%s|\n",NCJstring(json));
|
|
|
|
break;
|
|
|
|
case NCJ_NULL:
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
case NCJ_ARRAY:
|
|
|
|
printf("\n");
|
|
|
|
for(i=0;i<NCJlength(json);i++)
|
|
|
|
jsontrace(NCJith(json,i),depth+1);
|
|
|
|
break;
|
|
|
|
case NCJ_DICT:
|
|
|
|
printf("\n");
|
|
|
|
for(i=0;i<NCJlength(json);i+=2) {
|
|
|
|
jsontrace(NCJith(json,i),depth+1);
|
|
|
|
jsontrace(NCJith(json,i+1),depth+1);
|
|
|
|
} break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:15:47 +08:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
char text[MAXREAD+1];
|
|
|
|
NCjson* json = NULL;
|
2023-11-30 21:46:58 +08:00
|
|
|
int i, c;
|
2021-01-29 11:11:01 +08:00
|
|
|
FILE* f = NULL;
|
|
|
|
|
2022-04-07 05:54:42 +08:00
|
|
|
nc_initialize();
|
|
|
|
memset((void*)&jsonoptions,0,sizeof(jsonoptions));
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "t")) != EOF) {
|
|
|
|
switch(c) {
|
|
|
|
case 't': jsonoptions.trace = 1; break;
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr,"unknown option\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get file argument */
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
fprintf(stderr, "zisjson: only one input file argument permitted\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (argc == 0)
|
|
|
|
f = stdin;
|
|
|
|
else {
|
|
|
|
/* use argv[0] as input */
|
|
|
|
f = fopen(argv[0],"r");
|
|
|
|
if(f == NULL) {fprintf(stderr,"No such file: %s\n",argv[1]); exit(1);}
|
|
|
|
}
|
2020-08-18 09:15:47 +08:00
|
|
|
|
|
|
|
/* Read json from stdin */
|
|
|
|
for(i=0;;i++) {
|
|
|
|
unsigned char c;
|
2023-11-30 21:46:58 +08:00
|
|
|
size_t red = fread(&c, 1, 1, f);
|
2020-08-18 09:15:47 +08:00
|
|
|
if(red != 1) break;
|
|
|
|
if(i < MAXREAD) text[i] = (char)c;
|
|
|
|
}
|
|
|
|
if(i >= MAXREAD) {
|
|
|
|
fprintf(stderr,"Input too long\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
text[i] = '\0';
|
|
|
|
if(i == 0) {
|
|
|
|
stat = NC_EEMPTY;
|
|
|
|
} else {
|
|
|
|
stat = NCJparse(text,0,&json);
|
2022-04-07 05:54:42 +08:00
|
|
|
if(!stat) {
|
|
|
|
if(jsonoptions.trace) jsontrace(json,0);
|
|
|
|
NCJreclaim(json);
|
|
|
|
}
|
2020-08-18 09:15:47 +08:00
|
|
|
}
|
2022-04-07 05:54:42 +08:00
|
|
|
printf("%d",(stat?0:1)); /* parse success|failure */
|
2021-01-29 11:11:01 +08:00
|
|
|
if(f != stdin) fclose(f);
|
2020-08-18 09:15:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|