1 /*
2  * (C) Copyright 2005- 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 #include "grib_api_internal.h"
12 
13 /*
14    This is used by make_class.pl
15 
16    START_CLASS_DEF
17    CLASS      = accessor
18    SUPER      = grib_accessor_class_gen
19    IMPLEMENTS = init;unpack_string; get_native_type
20    MEMBERS    = const char* codetable
21    END_CLASS_DEF
22 
23  */
24 
25 /* START_CLASS_IMP */
26 
27 /*
28 
29 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
30 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
31 or edit "accessor.class" and rerun ./make_class.pl
32 
33 */
34 
35 static int get_native_type(grib_accessor*);
36 static int unpack_string(grib_accessor*, char*, size_t* len);
37 static void init(grib_accessor*, const long, grib_arguments*);
38 static void init_class(grib_accessor_class*);
39 
40 typedef struct grib_accessor_codetable_units
41 {
42     grib_accessor att;
43     /* Members defined in gen */
44     /* Members defined in codetable_units */
45     const char* codetable;
46 } grib_accessor_codetable_units;
47 
48 extern grib_accessor_class* grib_accessor_class_gen;
49 
50 static grib_accessor_class _grib_accessor_class_codetable_units = {
51     &grib_accessor_class_gen,              /* super                     */
52     "codetable_units",                     /* name                      */
53     sizeof(grib_accessor_codetable_units), /* size                      */
54     0,                                     /* inited */
55     &init_class,                           /* init_class */
56     &init,                                 /* init                      */
57     0,                                     /* post_init                      */
58     0,                                     /* free mem                       */
59     0,                                     /* describes himself         */
60     0,                                     /* get length of section     */
61     0,                                     /* get length of string      */
62     0,                                     /* get number of values      */
63     0,                                     /* get number of bytes      */
64     0,                                     /* get offset to bytes           */
65     &get_native_type,                      /* get native type               */
66     0,                                     /* get sub_section                */
67     0,                                     /* grib_pack procedures long      */
68     0,                                     /* grib_pack procedures long      */
69     0,                                     /* grib_pack procedures long      */
70     0,                                     /* grib_unpack procedures long    */
71     0,                                     /* grib_pack procedures double    */
72     0,                                     /* grib_unpack procedures double  */
73     0,                                     /* grib_pack procedures string    */
74     &unpack_string,                        /* grib_unpack procedures string  */
75     0,                                     /* grib_pack array procedures string    */
76     0,                                     /* grib_unpack array procedures string  */
77     0,                                     /* grib_pack procedures bytes     */
78     0,                                     /* grib_unpack procedures bytes   */
79     0,                                     /* pack_expression */
80     0,                                     /* notify_change   */
81     0,                                     /* update_size   */
82     0,                                     /* preferred_size   */
83     0,                                     /* resize   */
84     0,                                     /* nearest_smaller_value */
85     0,                                     /* next accessor    */
86     0,                                     /* compare vs. another accessor   */
87     0,                                     /* unpack only ith value          */
88     0,                                     /* unpack a subarray         */
89     0,                                     /* clear          */
90     0,                                     /* clone accessor          */
91 };
92 
93 
94 grib_accessor_class* grib_accessor_class_codetable_units = &_grib_accessor_class_codetable_units;
95 
96 
init_class(grib_accessor_class * c)97 static void init_class(grib_accessor_class* c)
98 {
99     c->dump                   = (*(c->super))->dump;
100     c->next_offset            = (*(c->super))->next_offset;
101     c->string_length          = (*(c->super))->string_length;
102     c->value_count            = (*(c->super))->value_count;
103     c->byte_count             = (*(c->super))->byte_count;
104     c->byte_offset            = (*(c->super))->byte_offset;
105     c->sub_section            = (*(c->super))->sub_section;
106     c->pack_missing           = (*(c->super))->pack_missing;
107     c->is_missing             = (*(c->super))->is_missing;
108     c->pack_long              = (*(c->super))->pack_long;
109     c->unpack_long            = (*(c->super))->unpack_long;
110     c->pack_double            = (*(c->super))->pack_double;
111     c->unpack_double          = (*(c->super))->unpack_double;
112     c->pack_string            = (*(c->super))->pack_string;
113     c->pack_string_array      = (*(c->super))->pack_string_array;
114     c->unpack_string_array    = (*(c->super))->unpack_string_array;
115     c->pack_bytes             = (*(c->super))->pack_bytes;
116     c->unpack_bytes           = (*(c->super))->unpack_bytes;
117     c->pack_expression        = (*(c->super))->pack_expression;
118     c->notify_change          = (*(c->super))->notify_change;
119     c->update_size            = (*(c->super))->update_size;
120     c->preferred_size         = (*(c->super))->preferred_size;
121     c->resize                 = (*(c->super))->resize;
122     c->nearest_smaller_value  = (*(c->super))->nearest_smaller_value;
123     c->next                   = (*(c->super))->next;
124     c->compare                = (*(c->super))->compare;
125     c->unpack_double_element  = (*(c->super))->unpack_double_element;
126     c->unpack_double_subarray = (*(c->super))->unpack_double_subarray;
127     c->clear                  = (*(c->super))->clear;
128     c->make_clone             = (*(c->super))->make_clone;
129 }
130 
131 /* END_CLASS_IMP */
132 
133 typedef struct grib_accessor_codetable
134 {
135     grib_accessor att;
136     /* Members defined in gen */
137     /* Members defined in long */
138     /* Members defined in unsigned */
139     long nbytes;
140     grib_arguments* arg;
141     /* Members defined in codetable */
142     const char* tablename;
143     const char* masterDir;
144     const char* localDir;
145     grib_codetable* table;
146 } grib_accessor_codetable;
147 
init(grib_accessor * a,const long len,grib_arguments * params)148 static void init(grib_accessor* a, const long len, grib_arguments* params)
149 {
150     grib_accessor_codetable_units* self = (grib_accessor_codetable_units*)a;
151     int n                               = 0;
152     self->codetable                     = grib_arguments_get_name(grib_handle_of_accessor(a), params, n++);
153     a->length                           = 0;
154     a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
155 }
156 
get_native_type(grib_accessor * a)157 static int get_native_type(grib_accessor* a)
158 {
159     return GRIB_TYPE_STRING;
160 }
161 
unpack_string(grib_accessor * a,char * buffer,size_t * len)162 static int unpack_string(grib_accessor* a, char* buffer, size_t* len)
163 {
164     grib_accessor_codetable_units* self = (grib_accessor_codetable_units*)a;
165     grib_codetable* table               = NULL;
166 
167     size_t size = 1;
168     long value;
169     int err = GRIB_SUCCESS;
170     char tmp[1024];
171     size_t l                    = 1024;
172     grib_accessor_codetable* ca = (grib_accessor_codetable*)grib_find_accessor(grib_handle_of_accessor(a), self->codetable);
173 
174     if ((err = grib_unpack_long((grib_accessor*)ca, &value, &size)) != GRIB_SUCCESS)
175         return err;
176 
177     table = ca->table;
178 
179     if (table && (value >= 0) && (value < table->size) && table->entries[value].units) {
180         strcpy(tmp, table->entries[value].units);
181     }
182     else {
183         sprintf(tmp, "%d", (int)value);
184     }
185 
186     l = strlen(tmp) + 1;
187 
188     if (*len < l) {
189         *len = l;
190         return GRIB_BUFFER_TOO_SMALL;
191     }
192 
193     strcpy(buffer, tmp);
194     *len = l;
195 
196     return GRIB_SUCCESS;
197 }
198