ECC-319: GRIBEditionNumber returns '?' if queried as a string

This commit is contained in:
Shahram Najm 2016-08-22 10:54:47 +01:00
parent c8bc74c8a9
commit f2111f03e4
1 changed files with 81 additions and 78 deletions

View File

@ -141,127 +141,130 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a, const long len, grib_arguments *arg ) static void init(grib_accessor* a, const long len, grib_arguments *arg )
{ {
grib_accessor_lookup* self = (grib_accessor_lookup*)a; grib_accessor_lookup* self = (grib_accessor_lookup*)a;
a->length = 0; a->length = 0;
self->llength = len; self->llength = len;
self->loffset = grib_arguments_get_long(grib_handle_of_accessor(a),arg,0); self->loffset = grib_arguments_get_long(grib_handle_of_accessor(a),arg,0);
a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
self->real_name = grib_arguments_get_expression(grib_handle_of_accessor(a),arg,1); self->real_name = grib_arguments_get_expression(grib_handle_of_accessor(a),arg,1);
} }
static void post_init(grib_accessor* a) static void post_init(grib_accessor* a)
{ {
grib_accessor_lookup* self = (grib_accessor_lookup*)a; grib_accessor_lookup* self = (grib_accessor_lookup*)a;
if(self->real_name ) if(self->real_name ) {
grib_dependency_observe_expression(a,self->real_name ); grib_dependency_observe_expression(a,self->real_name );
}
} }
static void dump(grib_accessor* a,grib_dumper* dumper) static void dump(grib_accessor* a,grib_dumper* dumper)
{ {
grib_accessor_lookup* self = (grib_accessor_lookup*)a; grib_accessor_lookup* self = (grib_accessor_lookup*)a;
unsigned char bytes[1024] = {0,}; unsigned char bytes[1024] = {0,};
char msg[1024]= {0,}; char msg[1024]= {0,};
char buf[1024]; char buf[1024];
int i; int i;
unsigned long v = 0; unsigned long v = 0;
size_t llen = self->llength; size_t llen = self->llength;
grib_unpack_bytes(a, bytes, &llen); /* TODO: Unpack byte unpack the wrong offset */ grib_unpack_bytes(a, bytes, &llen); /* TODO: Unpack byte unpack the wrong offset */
bytes[llen] = 0; bytes[llen] = 0;
for(i = 0; i < llen; i++) for(i = 0; i < llen; i++)
{ {
msg[i] = isprint(bytes[i]) ? bytes[i] : '?'; msg[i] = isprint(bytes[i]) ? bytes[i] : '?';
v <<= 8; v <<= 8;
v |= bytes[i]; v |= bytes[i];
} }
msg[llen] = 0; msg[llen] = 0;
sprintf(buf,"%s %lu %ld-%ld", msg, v, (long)a->offset+self->loffset, (long)self->llength); sprintf(buf,"%s %lu %ld-%ld", msg, v, (long)a->offset+self->loffset, (long)self->llength);
grib_dump_long(dumper,a,buf);
grib_dump_long(dumper,a,buf);
} }
static int unpack_string(grib_accessor*a , char* v, size_t *len){ static int unpack_string(grib_accessor*a , char* v, size_t *len)
{
grib_accessor_lookup* self = (grib_accessor_lookup*)a;
unsigned char bytes[1024] = {0,};
int i;
grib_accessor_lookup* self = (grib_accessor_lookup*)a; size_t llen = self->llength;
unsigned char bytes[1024] = {0,}; grib_unpack_bytes(a, bytes, &llen); /* TODO: Unpack byte unpack the wrong offset */
int i;
size_t llen = self->llength; bytes[llen] = 0;
grib_unpack_bytes(a, bytes, &llen); /* TODO: Unpack byte unpack the wrong offset */
bytes[llen] = 0; for(i = 0; i < llen; i++)
for(i = 0; i < llen; i++) {
{ v[i] = isprint(bytes[i]) ? bytes[i] : '?';
v[i] = isprint(bytes[i]) ? bytes[i] : '?'; }
}
v[llen] = 0; v[llen] = 0;
if (llen==1 && v[0] == '?') {
/* Try unpack as long */
return GRIB_SUCCESS; size_t length = 10;
long lval = 0;
int err = unpack_long(a, &lval, &length);
if (!err) {
char str[5];
int conv = sprintf(str, "%ld", lval);
if (conv == 1) {
v[0] = str[0];
}
}
}
return GRIB_SUCCESS;
} }
static int unpack_long(grib_accessor* a, long* val, size_t *len) static int unpack_long(grib_accessor* a, long* val, size_t *len)
{ {
grib_accessor_lookup* al = (grib_accessor_lookup*)a; grib_accessor_lookup* al = (grib_accessor_lookup*)a;
grib_handle *h = grib_handle_of_accessor(a); grib_handle *h = grib_handle_of_accessor(a);
long pos = (a->offset+al->loffset)*8;
long pos = (a->offset+al->loffset)*8; if(len[0] < 1)
{
grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , 1 );
len[0] = 0;
return GRIB_ARRAY_TOO_SMALL;
}
/* This is used when reparsing or rebuilding */
if(h->loader) {
Assert(*len == 1);
return h->loader->lookup_long(h->context,h->loader,a->name,val);
}
if(len[0] < 1) val[0] = grib_decode_unsigned_long(h->buffer->data , &pos, al->llength*8);
{ len[0] = 1;
grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , 1 );
len[0] = 0;
return GRIB_ARRAY_TOO_SMALL;
}
/* This is used when reparsing or rebuilding */ /*printf("###########lookup unpack_long: %s %ld %ld\n",a->name, pos/8, val[0]);*/
if(h->loader) {
Assert(*len == 1);
return h->loader->lookup_long(h->context,h->loader,a->name,val);
}
val[0] = grib_decode_unsigned_long(h->buffer->data , &pos, al->llength*8);
len[0] = 1;
/* printf("lookup: %s %ld %ld\n",a->name,pos/8,val[0]); */
return GRIB_SUCCESS;
return GRIB_SUCCESS;
} }
static int pack_long (grib_accessor* a, const long* val, size_t *len) static int pack_long(grib_accessor* a, const long* val, size_t *len)
{ {
return GRIB_NOT_IMPLEMENTED; return GRIB_NOT_IMPLEMENTED;
} }
static long byte_count(grib_accessor* a) static long byte_count(grib_accessor* a)
{ {
grib_accessor_lookup* al = (grib_accessor_lookup*)a; grib_accessor_lookup* al = (grib_accessor_lookup*)a;
return al->llength; return al->llength;
} }
static long byte_offset(grib_accessor* a) static long byte_offset(grib_accessor* a)
{ {
grib_accessor_lookup* al = (grib_accessor_lookup*)a; grib_accessor_lookup* al = (grib_accessor_lookup*)a;
return al->loffset; return al->loffset;
} }
static int notify_change(grib_accessor* self,grib_accessor* changed) static int notify_change(grib_accessor* self,grib_accessor* changed)
{ {
/* Forward changes */ /* Forward changes */
return grib_dependency_notify_change(self); return grib_dependency_notify_change(self);
} }