1 /*********************************************************************
2 * Copyright 2016, UCAR/Unidata
3 * See netcdf/COPYRIGHT file for copying and redistribution conditions.
4 *********************************************************************/
5
6 /* Define various things common to all the t_dmr*.c testers */
7 #undef DEBUG
8 #undef DUMP
9
10 #include "d4includes.h"
11
12 #ifdef DEBUG
13 #include "ezxml.h"
14 #endif
15
16 typedef int TDMR;
17 #define TDMR_PARSE 1
18 #define TDMR_META 2
19 #define TDMR_DATA 4
20
21 static NCbytes* input = NULL;
22 static NCbytes* output = NULL;
23 static NCD4meta* metadata = NULL;
24 static char* infile = NULL;
25 static char* outfile = NULL;
26 static int ncid = 0;
27 static int translatenc4 = 0;
28
29 static int
readfile(const char * filename,NCbytes * content)30 readfile(const char* filename, NCbytes* content)
31 {
32 FILE* stream;
33 char part[1024];
34
35 stream = fopen(filename,"r");
36 if(stream == NULL) return errno;
37 for(;;) {
38 size_t count = fread(part, 1, sizeof(part), stream);
39 if(count <= 0) break;
40 ncbytesappendn(content,part,count);
41 if(ferror(stream)) {fclose(stream); return NC_EIO;}
42 if(feof(stream)) break;
43 }
44 ncbytesnull(content);
45 fclose(stream);
46 return NC_NOERR;
47 }
48
49 static void
fail(int code)50 fail(int code)
51 {
52 if(code != NC_NOERR)
53 fprintf(stderr,"***Fail: %s\n",nc_strerror(code));
54 exit((code==NC_NOERR?EXIT_SUCCESS:EXIT_FAILURE));
55 }
56
57 static void
setup(int tdmr,int argc,char ** argv)58 setup(int tdmr, int argc, char** argv)
59 {
60 int ret = NC_NOERR;
61 argc--; argv++;
62 int expected = 0;
63 NCD4mode mode = 0;
64
65 switch(tdmr) {
66 case TDMR_PARSE:
67 expected = 1;
68 mode = NCD4_DMR;
69 break;
70 case TDMR_META:
71 expected = 2;
72 mode = NCD4_DMR;
73 break;
74 case TDMR_DATA:
75 fprintf(stderr,"setup is not used for t_dmrdata\n");
76 mode = NCD4_DAP;
77 exit(1);
78 }
79
80 if(argc < expected) {
81 fprintf(stderr, "too few arguments\n");
82 exit(1);
83 }
84 infile = argv[0];
85 outfile = NULL;
86 input = ncbytesnew();
87 output = ncbytesnew();
88 if((ret = readfile(infile,input))) fail(ret);
89 {
90 const char* trans = getenv("translatenc4");
91 if(trans != NULL)
92 translatenc4 = 1;
93 }
94
95 #ifdef DUMP
96 NCD4_dumpbytes(ncbyteslength(input),ncbytescontents(input),0);
97 #endif
98
99 if((metadata=NCD4_newmeta(ncbyteslength(input),ncbytescontents(input)))==NULL)
100 fail(NC_ENOMEM);
101 metadata->mode = mode;
102
103 /* Create a fake NCD4INFO */
104 {
105 NCD4INFO* controller = (NCD4INFO*)calloc(1,sizeof(NCD4INFO));
106 if(controller == NULL)
107 fail(NC_ENOMEM);
108 metadata->controller = controller;
109 controller->controls.translation = NCD4_TRANSNC4;
110 if(translatenc4)
111 controller->controls.translation = NCD4_TRANSNC4;
112 NCD4_applyclientparamcontrols(controller);
113 }
114 if((ret=NCD4_dechunk(metadata))) /* ok for mode == DMR or mode == DAP */
115 fail(ret);
116 #ifdef DEBUG
117 {
118 int swap = (metadata->serial.hostbigendian != metadata->serial.remotebigendian);
119 void* d = metadata->serial.dap;
120 size_t sz = metadata->serial.dapsize;
121 fprintf(stderr,"====================\n");
122 fprintf(stderr,"%s\n",metadata->serial.dmr);
123 fprintf(stderr,"----------\n");
124 NCD4_dumpbytes(sz,d,swap);
125 fprintf(stderr,"====================\n");
126 fflush(stderr);
127 }
128 #endif
129 if(expected > 1) {
130 outfile = argv[1];
131 if((ret = nc_create(outfile,NC_CLOBBER|NC_NETCDF4,&ncid))) fail(ret);
132 }
133
134 #ifdef DEBUG
135 {
136 char* tree;
137 ezxml_t dom = ezxml_parse_str(ncbytescontents(input),ncbyteslength(input));
138 if(dom == NULL) exit(1);
139 tree = ezxml_toxml(dom);
140 fprintf(stderr,"////////////////////\n");
141 fprintf(stderr,"%s\n",tree);
142 fprintf(stderr,"////////////////////\n");
143 }
144 #endif
145 {
146 const char* slevel = getenv("d4loglevel");
147 int level;
148 if(slevel != NULL && sscanf(slevel,"%d",&level) == 1) {
149 nc_set_log_level(level);
150 }
151 }
152 }
153
154 int
cleanup(int ret)155 cleanup(int ret)
156 {
157 if(outfile != NULL) {
158 if(ret != NC_NOERR)
159 ret = nc_abort(ncid);
160 else
161 ret = nc_close(ncid);
162 }
163 if(metadata->controller != NULL)
164 free(metadata->controller);
165 NCD4_reclaimMeta(metadata);
166 ncbytesfree(input);
167 ncbytesfree(output);
168 if(ret)
169 fail(ret);
170 else
171 exit(EXIT_SUCCESS);
172 return 0;
173 }
174
175 #if 0
176 static void
177 printxml(const char* input)
178 {
179 char* tree;
180 ezxml_t dom = ezxml_parse_str(input,strlen(input));
181 if(dom == NULL) exit(1);
182 tree = ezxml_toxml(dom);
183 fprintf(stderr,"////////////////////\n");
184 fprintf(stderr,"%s\n",tree);
185 fprintf(stderr,"////////////////////\n");
186 }
187 #endif
188