This commit is contained in:
Shahram Najm 2016-06-22 14:50:29 +01:00
parent e0784c99b9
commit a5df68e861
1 changed files with 103 additions and 102 deletions

View File

@ -131,55 +131,55 @@ static void init_class(grib_accessor_class* c)
static int get_native_type(grib_accessor* a){ static int get_native_type(grib_accessor* a)
return GRIB_TYPE_LONG; {
return GRIB_TYPE_LONG;
} }
static void dump(grib_accessor* a,grib_dumper* dumper) static void dump(grib_accessor* a,grib_dumper* dumper)
{ {
grib_dump_long(dumper,a,NULL); grib_dump_long(dumper,a,NULL);
} }
static int unpack_string(grib_accessor*a , char* v, size_t *len)
{
long val = 0;
size_t l = 1;
char repres[1024];
grib_unpack_long (a , &val, &l);
static int unpack_string(grib_accessor*a , char* v, size_t *len){ if ((val == GRIB_MISSING_LONG) && ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) )
sprintf(repres,"MISSING");
else
sprintf(repres,"%ld", val);
long val = 0; l = strlen(repres)+1;
size_t l = 1;
char repres[1024];
grib_unpack_long (a , &val, &l); if(l >*len ){
grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_long : unpack_string : Buffer too small for %s ", a->name );
if ((val == GRIB_MISSING_LONG) && ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) ) *len = l;
sprintf(repres,"MISSING"); return GRIB_BUFFER_TOO_SMALL;
else }
sprintf(repres,"%ld", val); grib_context_log(a->context,GRIB_LOG_DEBUG, "grib_accessor_long: Casting long %s to string ", a->name);
l = strlen(repres)+1;
if(l >*len ){
grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_long : unpack_string : Buffer too small for %s ", a->name );
*len = l; *len = l;
return GRIB_BUFFER_TOO_SMALL;
}
grib_context_log(a->context,GRIB_LOG_DEBUG, "grib_accessor_long: Casting long %s to string ", a->name);
*len = l;
strcpy(v,repres);
return GRIB_SUCCESS;
strcpy(v,repres);
return GRIB_SUCCESS;
} }
static int pack_missing(grib_accessor* a){
size_t one = 1; static int pack_missing(grib_accessor* a)
long value = GRIB_MISSING_LONG; {
if(a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) size_t one = 1;
return grib_pack_long(a,&value,&one); long value = GRIB_MISSING_LONG;
return GRIB_VALUE_CANNOT_BE_MISSING; if(a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING)
return grib_pack_long(a,&value,&one);
return GRIB_VALUE_CANNOT_BE_MISSING;
} }
@ -199,99 +199,100 @@ static int is_missing(grib_accessor* a){
return 0; return 0;
} }
*/ */
static int unpack_double(grib_accessor* a, double* val,size_t *len){ static int unpack_double(grib_accessor* a, double* val,size_t *len)
size_t rlen = 0; {
long count=0; size_t rlen = 0;
unsigned long i = 0; long count=0;
long *values = NULL; unsigned long i = 0;
long oneval = 0; long *values = NULL;
int ret = GRIB_SUCCESS; long oneval = 0;
int ret = GRIB_SUCCESS;
ret=grib_value_count(a,&count); ret=grib_value_count(a,&count);
if (ret) return ret; if (ret) return ret;
rlen=count; rlen=count;
if(*len < rlen) if(*len < rlen)
{ {
grib_context_log(a->context, GRIB_LOG_ERROR, " wrong size for %s it contains %d values ", a->name , rlen); grib_context_log(a->context, GRIB_LOG_ERROR, " wrong size for %s it contains %d values ", a->name , rlen);
*len = 0; *len = 0;
return GRIB_ARRAY_TOO_SMALL; return GRIB_ARRAY_TOO_SMALL;
} }
if(rlen == 1){ if(rlen == 1){
ret = grib_unpack_long(a,&oneval,&rlen); ret = grib_unpack_long(a,&oneval,&rlen);
if(ret != GRIB_SUCCESS) return ret; if(ret != GRIB_SUCCESS) return ret;
*val = oneval; *val = oneval;
*len = 1; *len = 1;
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
values = (long*)grib_context_malloc(a->context,rlen*sizeof(long)); values = (long*)grib_context_malloc(a->context,rlen*sizeof(long));
if(!values) return GRIB_INTERNAL_ERROR; if(!values) return GRIB_INTERNAL_ERROR;
ret = grib_unpack_long(a,values,&rlen); ret = grib_unpack_long(a,values,&rlen);
if(ret != GRIB_SUCCESS){ if(ret != GRIB_SUCCESS){
grib_context_free(a->context,values);
return ret;
}
for(i=0; i< rlen;i++)
val[i] = values[i];
grib_context_free(a->context,values); grib_context_free(a->context,values);
return ret;
}
for(i=0; i< rlen;i++)
val[i] = values[i];
grib_context_free(a->context,values); *len = rlen;
return GRIB_SUCCESS;
*len = rlen;
return GRIB_SUCCESS;
} }
static int compare(grib_accessor* a,grib_accessor* b) { static int compare(grib_accessor* a,grib_accessor* b)
int retval=0; {
long *aval=0; int retval=0;
long *bval=0; long *aval=0;
long count=0; long *bval=0;
long count=0;
size_t alen = 0; size_t alen = 0;
size_t blen = 0; size_t blen = 0;
int err=0; int err=0;
err=grib_value_count(a,&count); err=grib_value_count(a,&count);
if (err) return err; if (err) return err;
alen=count; alen=count;
err=grib_value_count(b,&count); err=grib_value_count(b,&count);
if (err) return err; if (err) return err;
blen=count; blen=count;
if (alen != blen) return GRIB_COUNT_MISMATCH; if (alen != blen) return GRIB_COUNT_MISMATCH;
aval=(long*)grib_context_malloc(a->context,alen*sizeof(long)); aval=(long*)grib_context_malloc(a->context,alen*sizeof(long));
bval=(long*)grib_context_malloc(b->context,blen*sizeof(long)); bval=(long*)grib_context_malloc(b->context,blen*sizeof(long));
grib_unpack_long(a,aval,&alen); grib_unpack_long(a,aval,&alen);
grib_unpack_long(b,bval,&blen); grib_unpack_long(b,bval,&blen);
retval = GRIB_SUCCESS; retval = GRIB_SUCCESS;
while (alen != 0) { while (alen != 0) {
if (*bval != *aval) retval = GRIB_LONG_VALUE_MISMATCH; if (*bval != *aval) retval = GRIB_LONG_VALUE_MISMATCH;
alen--; alen--;
} }
grib_context_free(a->context,aval); grib_context_free(a->context,aval);
grib_context_free(b->context,bval); grib_context_free(b->context,bval);
return retval; return retval;
} }
static int pack_string(grib_accessor* a, const char* val, size_t *len) static int pack_string(grib_accessor* a, const char* val, size_t *len)
{ {
char* theEnd=NULL; char* theEnd=NULL;
long v=strtol(val,&theEnd,10); long v=strtol(val,&theEnd,10);
if (theEnd) { if (theEnd) {
grib_context_log(a->context,GRIB_LOG_ERROR,"trying to pack \"%s\" as long",val); grib_context_log(a->context,GRIB_LOG_ERROR,"trying to pack \"%s\" as long",val);
return GRIB_WRONG_TYPE; return GRIB_WRONG_TYPE;
} }
return grib_pack_long( a,&v,len); return grib_pack_long( a,&v,len);
} }