1 /*
2  * Copyright 2005-2018 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 /**************************************
12  *  Enrico Fucile
13  **************************************/
14 
15 
16 #include "grib_api_internal.h"
17 #include <ctype.h>
18 /*
19    This is used by make_class.pl
20 
21    START_CLASS_DEF
22    CLASS      = dumper
23    IMPLEMENTS = dump_long;dump_bits
24    IMPLEMENTS = dump_double;dump_string
25    IMPLEMENTS = dump_bytes;dump_values
26    IMPLEMENTS = dump_label;dump_section
27    IMPLEMENTS = init;destroy
28    MEMBERS = long section_offset
29    MEMBERS = long begin
30    MEMBERS = long theEnd
31    END_CLASS_DEF
32 
33  */
34 
35 
36 /* START_CLASS_IMP */
37 
38 /*
39 
40 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
41 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
42 or edit "dumper.class" and rerun ./make_class.pl
43 
44 */
45 
46 static void init_class      (grib_dumper_class*);
47 static int init            (grib_dumper* d);
48 static int destroy         (grib_dumper*);
49 static void dump_long       (grib_dumper* d, grib_accessor* a,const char* comment);
50 static void dump_bits       (grib_dumper* d, grib_accessor* a,const char* comment);
51 static void dump_double     (grib_dumper* d, grib_accessor* a,const char* comment);
52 static void dump_string     (grib_dumper* d, grib_accessor* a,const char* comment);
53 static void dump_bytes      (grib_dumper* d, grib_accessor* a,const char* comment);
54 static void dump_values     (grib_dumper* d, grib_accessor* a);
55 static void dump_label      (grib_dumper* d, grib_accessor* a,const char* comment);
56 static void dump_section    (grib_dumper* d, grib_accessor* a,grib_block_of_accessors* block);
57 
58 typedef struct grib_dumper_keys {
59     grib_dumper          dumper;
60 /* Members defined in keys */
61 	long section_offset;
62 	long begin;
63 	long theEnd;
64 } grib_dumper_keys;
65 
66 
67 static grib_dumper_class _grib_dumper_class_keys = {
68     0,                              /* super                     */
69     "keys",                              /* name                      */
70     sizeof(grib_dumper_keys),     /* size                      */
71     0,                                   /* inited */
72     &init_class,                         /* init_class */
73     &init,                               /* init                      */
74     &destroy,                            /* free mem                       */
75     &dump_long,                          /* dump long         */
76     &dump_double,                        /* dump double    */
77     &dump_string,                        /* dump string    */
78     &dump_label,                         /* dump labels  */
79     &dump_bytes,                         /* dump bytes  */
80     &dump_bits,                          /* dump bits   */
81     &dump_section,                       /* dump section      */
82     &dump_values,                        /* dump values   */
83     0,                             /* header   */
84     0,                             /* footer   */
85 };
86 
87 grib_dumper_class* grib_dumper_class_keys = &_grib_dumper_class_keys;
88 
89 /* END_CLASS_IMP */
90 
91 static void print_offset(FILE* out,grib_dumper* d,grib_accessor* a);
92 
init_class(grib_dumper_class * c)93 static void init_class      (grib_dumper_class* c){}
94 
init(grib_dumper * d)95 static int  init(grib_dumper* d)
96 {
97   grib_dumper_keys *self = (grib_dumper_keys*)d;
98   self->section_offset=0;
99 
100   return GRIB_SUCCESS;
101 }
102 
destroy(grib_dumper * d)103 static int  destroy  (grib_dumper* d){
104   return GRIB_SUCCESS;
105 }
106 
aliases(grib_dumper * d,grib_accessor * a)107 static void aliases(grib_dumper* d,grib_accessor* a)
108 {
109 int i;
110   grib_dumper_keys *self = (grib_dumper_keys*)d;
111 
112   if( (d->option_flags & GRIB_DUMP_FLAG_ALIASES) == 0)
113     return;
114 
115   if(a->all_names[1])
116   {
117     char *sep = "";
118     fprintf(self->dumper.out," ( ALIASES: ");
119 
120     for(i = 1; i < MAX_ACCESSOR_NAMES; i++)
121     {
122       if(a->all_names[i])
123       {
124         if(a->all_name_spaces[i])
125           fprintf(self->dumper.out,"%s%s.%s", sep,a->all_name_spaces[i],a->all_names[i]);
126         else
127           fprintf(self->dumper.out,"%s%s", sep,a->all_names[i]);
128       }
129     sep = ", ";
130     }
131     printf(") ");
132   }
133 }
134 
dump_name_only(grib_dumper * d,grib_accessor * a,const char * comment)135 static void dump_name_only(grib_dumper* d,grib_accessor* a,const char* comment) {
136   grib_dumper_keys *self = (grib_dumper_keys*)d;
137 
138   print_offset(self->dumper.out,d,a);
139 
140 
141   if (a->flags & GRIB_ACCESSOR_FLAG_HIDDEN) {
142     return;
143   }
144 
145   if( a->length == 0  &&
146       (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
147     return;
148 
149   if( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 &&
150       (d->option_flags & GRIB_DUMP_FLAG_DUMP_OK) != 0)
151     return;
152 
153   fprintf(self->dumper.out,"%s",a->name);
154 
155   if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) {
156     fprintf(self->dumper.out," (read only)");
157   }
158   if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0){
159     fprintf(self->dumper.out," (type %s) ",a->creator->op);
160   }
161 
162   aliases(d,a);
163 
164   fprintf(self->dumper.out,"\n");
165 }
166 
dump_long(grib_dumper * d,grib_accessor * a,const char * comment)167 static void dump_long(grib_dumper* d,grib_accessor* a,const char* comment)
168 {
169   dump_name_only(d,a,comment);
170 }
171 
dump_bits(grib_dumper * d,grib_accessor * a,const char * comment)172 static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment)
173 {
174   dump_name_only(d,a,comment);
175 }
176 
dump_double(grib_dumper * d,grib_accessor * a,const char * comment)177 static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment)
178 {
179   dump_name_only(d,a,comment);
180 }
181 
dump_string(grib_dumper * d,grib_accessor * a,const char * comment)182 static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment)
183 {
184   dump_name_only(d,a,comment);
185 }
186 
dump_bytes(grib_dumper * d,grib_accessor * a,const char * comment)187 static void dump_bytes(grib_dumper* d,grib_accessor* a,const char* comment)
188 {
189 
190 #if 0
191   grib_dumper_keys *self = (grib_dumper_keys*)d;
192   int i,k,err =0;
193   int more = 0;
194   size_t size = a->length;
195   unsigned char* buf = grib_context_malloc(d->handle->context,size);
196 
197   if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0)
198     return;
199 
200 
201   if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY)
202     fprintf(self->dumper.out,"-READ ONLY- ");
203 
204   /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
205   /*print_offset(self->dumper.out,self->begin,self->theEnd);*/
206   if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0)
207         fprintf(self->dumper.out,"%s ",a->creator->op);
208 
209   fprintf(self->dumper.out,"%s = %ld",a->name,a->length);
210   aliases(d,a);
211   fprintf(self->dumper.out," {");
212 
213   if(!buf)
214   {
215     if(size == 0)
216       fprintf(self->dumper.out,"}\n");
217     else
218       fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size);
219     return;
220   }
221 
222   fprintf(self->dumper.out,"\n");
223 
224   err = grib_unpack_bytes(a,buf,&size);
225   if(err){
226     grib_context_free(d->handle->context,buf);
227     fprintf(self->dumper.out," *** ERR=%d (%s) \n}",err,grib_get_error_message(err));
228     return ;
229   }
230 
231   if(size > 100) {
232     more = size - 100;
233     size = 100;
234   }
235 
236   k = 0;
237   /* if(size > 100) size = 100;  */
238   while(k < size)
239   {
240     int j;
241     for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
242     for(j = 0; j < 16 && k < size; j++, k++)
243     {
244       fprintf(self->dumper.out,"%02x",buf[k]);
245       if(k != size-1)
246         fprintf(self->dumper.out,", ");
247     }
248     fprintf(self->dumper.out,"\n");
249   }
250 
251   if(more)
252   {
253     for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
254     fprintf(self->dumper.out,"... %d more values\n",more);
255   }
256 
257   for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
258   fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
259   grib_context_free(d->handle->context,buf);
260 #endif
261 }
262 
dump_values(grib_dumper * d,grib_accessor * a)263 static void dump_values(grib_dumper* d,grib_accessor* a)
264 {
265   dump_name_only(d,a,0);
266 }
267 
dump_label(grib_dumper * d,grib_accessor * a,const char * comment)268 static void dump_label(grib_dumper* d,grib_accessor* a,const char* comment)
269 {
270   /*grib_dumper_keys *self = (grib_dumper_keys*)d;
271 
272   for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
273   fprintf(self->dumper.out,"----> %s %s %s\n",a->creator->op, a->name,comment?comment:"");*/
274 }
275 
dump_section(grib_dumper * d,grib_accessor * a,grib_block_of_accessors * block)276 static void dump_section(grib_dumper* d,grib_accessor* a,grib_block_of_accessors* block)
277 {
278   grib_dumper_keys *self = (grib_dumper_keys*)d;
279   /*grib_section* s = grib_get_sub_section(a);*/
280   int is_default_section=0;
281   char* upper=NULL;
282   char *p=NULL,*q=NULL;
283   if (!strncmp(a->name,"section",7)) is_default_section=1;
284 
285   /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
286   if (is_default_section) {
287     upper=(char*)malloc(strlen(a->name)+1);
288     Assert(upper);
289     p=(char*)a->name;
290     q=upper;
291     while (*p != '\0') {
292       *q=toupper(*p);
293       if (*q == '_' ) *q=' ';
294       q++;
295       p++;
296     }
297     *q='\0';
298 
299     /*sprintf(tmp,"%s ",upper,(long)s->length,(long)s->padding);*/
300 
301     fprintf(self->dumper.out,"====> %s <==== \n",upper);
302 
303     free(upper);
304     self->section_offset=a->offset;
305   } else {
306 
307   }
308 
309   /*printf("------------- section_offset = %ld\n",self->section_offset);*/
310   d->depth += 3;
311   grib_dump_accessors_block(d,block);
312   d->depth -= 3;
313 
314   /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
315   /*fprintf(self->dumper.out,"<===== %s %s\n",a->creator->op, a->name);*/
316 }
317 
print_offset(FILE * out,grib_dumper * d,grib_accessor * a)318 static void print_offset(FILE* out,grib_dumper* d,grib_accessor* a) {
319 
320 }
321 
322