Refactoring

This commit is contained in:
Shahram Najm 2019-09-25 14:35:43 +01:00
parent e79a19c741
commit 9fd0f613d3
14 changed files with 174 additions and 174 deletions

View File

@ -79,81 +79,81 @@ static void init_class(grib_action_class* c)
grib_action* grib_action_create_assert( grib_context* context, grib_expression* expression)
{
grib_action_assert* a = NULL;
grib_action_class* c = grib_action_class_assert;
grib_action* act = (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
act->next = NULL;
act->name = grib_context_strdup_persistent(context, "assertion");
act->op = grib_context_strdup_persistent(context, "evaluate");
act->cclass = c;
act->context = context;
a = (grib_action_assert*)act;
a->expression = expression;
return act;
grib_action_assert* a = NULL;
grib_action_class* c = grib_action_class_assert;
grib_action* act = (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
act->next = NULL;
act->name = grib_context_strdup_persistent(context, "assertion");
act->op = grib_context_strdup_persistent(context, "evaluate");
act->cclass = c;
act->context = context;
a = (grib_action_assert*)act;
a->expression = expression;
return act;
}
static int create_accessor(grib_section* p, grib_action* act,grib_loader *h)
{
grib_action_assert* self = (grib_action_assert*)act;
grib_accessor* as = grib_accessor_factory(p, act,0,NULL);
if(!as)return GRIB_INTERNAL_ERROR;
grib_dependency_observe_expression(as,self->expression);
grib_action_assert* self = (grib_action_assert*)act;
grib_accessor* as = grib_accessor_factory(p, act,0,NULL);
if(!as)return GRIB_INTERNAL_ERROR;
grib_dependency_observe_expression(as,self->expression);
grib_push_accessor(as,p->block);
grib_push_accessor(as,p->block);
return GRIB_SUCCESS;
return GRIB_SUCCESS;
}
static void dump( grib_action* act, FILE* f, int lvl)
{
int i =0;
grib_action_assert* self=(grib_action_assert*)act;
for (i=0;i<lvl;i++)
grib_context_print(act->context,f," ");
grib_expression_print(act->context,self->expression,0);
printf("\n");
int i =0;
grib_action_assert* self=(grib_action_assert*)act;
for (i=0;i<lvl;i++)
grib_context_print(act->context,f," ");
grib_expression_print(act->context,self->expression,0);
printf("\n");
}
static void destroy(grib_context* context,grib_action* act)
{
grib_action_assert* a = ( grib_action_assert*)act;
grib_expression_free(context,a->expression);
grib_context_free_persistent(context, act->name);
grib_context_free_persistent(context, act->op);
grib_action_assert* a = ( grib_action_assert*)act;
grib_expression_free(context,a->expression);
grib_context_free_persistent(context, act->name);
grib_context_free_persistent(context, act->op);
}
static int execute(grib_action* a,grib_handle* h) {
int ret=0;
double res=0;
grib_action_assert* self= (grib_action_assert*)a;
static int execute(grib_action* a,grib_handle* h)
{
int ret=0;
double res=0;
grib_action_assert* self= (grib_action_assert*)a;
if ((ret=grib_expression_evaluate_double(h,self->expression,&res)) != GRIB_SUCCESS)
return ret;
if ((ret=grib_expression_evaluate_double(h,self->expression,&res)) != GRIB_SUCCESS)
return ret;
if (res != 0)
return GRIB_SUCCESS;
else {
grib_context_log(h->context,GRIB_LOG_ERROR,"Assertion failure: ");
grib_expression_print(h->context,self->expression,h);
printf("\n");
return GRIB_ASSERTION_FAILURE;
}
if (res != 0) {
return GRIB_SUCCESS;
}
else {
grib_context_log(h->context,GRIB_LOG_ERROR,"Assertion failure: ");
grib_expression_print(h->context,self->expression,h);
printf("\n");
return GRIB_ASSERTION_FAILURE;
}
}
static int notify_change(grib_action* a, grib_accessor* observer,grib_accessor* observed)
{
grib_action_assert* self = (grib_action_assert*) a;
grib_action_assert* self = (grib_action_assert*) a;
int ret = GRIB_SUCCESS;
long lres;
int ret = GRIB_SUCCESS;
long lres;
if ((ret = grib_expression_evaluate_long(grib_handle_of_accessor(observed), self->expression,&lres))
!= GRIB_SUCCESS) return ret;
if ((ret = grib_expression_evaluate_long(grib_handle_of_accessor(observed), self->expression,&lres))
!= GRIB_SUCCESS) return ret;
if (lres != 0)
return GRIB_SUCCESS;
else
return GRIB_ASSERTION_FAILURE;
if (lres != 0)
return GRIB_SUCCESS;
else
return GRIB_ASSERTION_FAILURE;
}

View File

@ -72,57 +72,54 @@ static void init_class(grib_action_class* c)
grib_action* grib_action_create_close( grib_context* context, char* filename)
{
char buf[1024];
char buf[1024];
grib_action_close* a ;
grib_action_class* c = grib_action_class_close;
grib_action* act = (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
act->op = grib_context_strdup_persistent(context,"section");
grib_action_close* a ;
grib_action_class* c = grib_action_class_close;
grib_action* act = (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
act->op = grib_context_strdup_persistent(context,"section");
act->cclass = c;
a = (grib_action_close*)act;
act->context = context;
act->cclass = c;
a = (grib_action_close*)act;
act->context = context;
a->filename = grib_context_strdup_persistent(context,filename);
a->filename = grib_context_strdup_persistent(context,filename);
sprintf(buf,"close_%p",(void*)a->filename);
sprintf(buf,"close_%p",(void*)a->filename);
act->name = grib_context_strdup_persistent(context,buf);
act->name = grib_context_strdup_persistent(context,buf);
return act;
return act;
}
static int execute(grib_action* act, grib_handle *h)
{
char filename[2048]={0,};
size_t len=2048;
grib_action_close* self = (grib_action_close*) act;
int err =0;
grib_file* file=0;
char filename[2048]={0,};
size_t len=2048;
grib_action_close* self = (grib_action_close*) act;
int err =0;
grib_file* file=0;
err=grib_get_string(h,self->filename,filename,&len);
/* fprintf(stderr,"++++ name %s\n",filename); */
if (err) return err;
/* grib_file_close(filename,1,&err); */
file=grib_get_file(filename,&err);
if (err) return err;
if (file) grib_file_pool_delete_file(file);
err=grib_get_string(h,self->filename,filename,&len);
/* fprintf(stderr,"++++ name %s\n",filename); */
if (err) return err;
/* grib_file_close(filename,1,&err); */
file=grib_get_file(filename,&err);
if (err) return err;
if (file) grib_file_pool_delete_file(file);
return err;
return err;
}
static void dump(grib_action* act, FILE* f, int lvl)
{
}
static void destroy(grib_context* context,grib_action* act)
{
grib_action_close* a = (grib_action_close*) act;
grib_context_free_persistent(context, a->filename);
grib_context_free_persistent(context, act->name);
grib_context_free_persistent(context, act->op);
grib_action_close* a = (grib_action_close*) act;
grib_context_free_persistent(context, a->filename);
grib_context_free_persistent(context, act->name);
grib_context_free_persistent(context, act->op);
}

View File

@ -199,11 +199,8 @@ void encode_double_array_unrolled(int numBits, long packStart, long datasize,
{
U_BYTEORDER;
long i, j, z = *gz;
#ifdef _ARCH_PWR6
#define __UNROLL_DEPTH_2 8
#else
#define __UNROLL_DEPTH_2 8
#endif
data += packStart;
datasize -= packStart;

View File

@ -129,28 +129,29 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a, const long len , grib_arguments* arg )
{
grib_get_long_internal(grib_handle_of_accessor(a), grib_arguments_get_name(a->parent->h, arg, 0), &a->length);
Assert(a->length>=0);
grib_get_long_internal(grib_handle_of_accessor(a), grib_arguments_get_name(a->parent->h, arg, 0), &a->length);
Assert(a->length>=0);
}
static int get_native_type(grib_accessor* a){
return GRIB_TYPE_BYTES;
static int get_native_type(grib_accessor* a)
{
return GRIB_TYPE_BYTES;
}
static int unpack_bytes (grib_accessor* a,unsigned char* buffer, size_t *len) {
if (*len < a->length) {
*len = a->length;
return GRIB_ARRAY_TOO_SMALL;
}
*len = a->length;
static int unpack_bytes (grib_accessor* a,unsigned char* buffer, size_t *len)
{
if (*len < a->length) {
*len = a->length;
return GRIB_ARRAY_TOO_SMALL;
}
*len = a->length;
memcpy(buffer, grib_handle_of_accessor(a)->buffer->data + a->offset, *len);
memcpy(buffer, grib_handle_of_accessor(a)->buffer->data + a->offset, *len);
return GRIB_SUCCESS;
return GRIB_SUCCESS;
}
static void dump(grib_accessor* a, grib_dumper* dumper)
{
grib_dump_bytes(dumper,a,NULL);
grib_dump_bytes(dumper,a,NULL);
}

View File

@ -183,7 +183,7 @@ static int build_long_array(grib_context* c, grib_handle* h, int compressed,
long** array, const char* key, long numberOfSubsets, int zero_on_error)
{
int err = 0;
size_t i;
long i;
size_t n=numberOfSubsets;
*array=(long*)grib_context_malloc_clear(c, sizeof(long)*numberOfSubsets);
if(compressed) {

View File

@ -129,47 +129,48 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a, const long len , grib_arguments* arg )
{
/*grib_accessor_signed* self = (grib_accessor_signed*)a; */
a->length = len;
Assert(a->length>=0);
/*grib_accessor_signed* self = (grib_accessor_signed*)a; */
a->length = len;
Assert(a->length>=0);
}
static int get_native_type(grib_accessor* a){
return GRIB_TYPE_BYTES;
static int get_native_type(grib_accessor* a)
{
return GRIB_TYPE_BYTES;
}
static int compare(grib_accessor* a, grib_accessor* b)
{
int retval=GRIB_SUCCESS;
static int compare(grib_accessor* a, grib_accessor* b) {
int retval=GRIB_SUCCESS;
size_t alen = (size_t)grib_byte_count(a);
size_t blen = (size_t)grib_byte_count(b);
size_t alen = (size_t)grib_byte_count(a);
size_t blen = (size_t)grib_byte_count(b);
if (alen != blen) return GRIB_COUNT_MISMATCH;
if (alen != blen) return GRIB_COUNT_MISMATCH;
return retval;
return retval;
}
static int unpack_string(grib_accessor *a , char* v, size_t *len){
unsigned char* p=NULL;
char* s=v;
int i;
long length=grib_byte_count(a);
static int unpack_string(grib_accessor *a , char* v, size_t *len)
{
unsigned char* p=NULL;
char* s=v;
int i;
long length=grib_byte_count(a);
if (*len < 2*length) {
*len=2*length;
return GRIB_ARRAY_TOO_SMALL;
}
p = grib_handle_of_accessor(a)->buffer->data + grib_byte_offset(a);
for (i = 0; i < length; i++) {
sprintf (s,"%02x", *(p++));
s+=2;
}
*len=length;
if (*len < 2*length) {
*len=2*length;
return GRIB_ARRAY_TOO_SMALL;
}
return GRIB_SUCCESS;
p = grib_handle_of_accessor(a)->buffer->data + grib_byte_offset(a);
for (i = 0; i < length; i++) {
sprintf (s,"%02x", *(p++));
s+=2;
}
*len=length;
return GRIB_SUCCESS;
}

View File

@ -331,7 +331,7 @@ static int _unpack_double(grib_accessor* a, double* val, size_t *len,unsigned c
GRIB_SUCCESS)
return err;
/*/
/*
* check we don't decode bpv > max(ulong) as it is
* not currently supported by the algorithm
*/
@ -557,7 +557,7 @@ static int pack_double(grib_accessor* a, const double* val, size_t *len)
if((err = grib_get_long_internal(gh,self->optimize_scaling_factor, &optimize_scaling_factor))
!= GRIB_SUCCESS)
return err;
/*/
/*
* check we don't encode bpv > max(ulong)-1 as it is
* not currently supported by the algorithm
*/

View File

@ -224,9 +224,9 @@ static int pack_long(grib_accessor* a, const long* val,size_t *len)
if((ret = pack_long_unsigned_helper(a,&tlen,len,/*check=*/0)) != GRIB_SUCCESS)
return ret;
/*
if((ret = super->pack_long(a,&tlen,len)) != GRIB_SUCCESS)
return ret;
*/
if((ret = super->pack_long(a,&tlen,len)) != GRIB_SUCCESS)
return ret;
*/
{
long total_length = -1, sec4_length = -1;

View File

@ -129,8 +129,6 @@ static void init_class(grib_accessor_class* c)
/* END_CLASS_IMP */
static int get_native_type(grib_accessor* a)
{
return GRIB_TYPE_LONG;
@ -216,8 +214,7 @@ static int unpack_double(grib_accessor* a, double* val,size_t *len)
if (ret) return ret;
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);
*len = 0;
return GRIB_ARRAY_TOO_SMALL;
@ -234,7 +231,6 @@ static int unpack_double(grib_accessor* a, double* val,size_t *len)
values = (long*)grib_context_malloc(a->context,rlen*sizeof(long));
if(!values) return GRIB_INTERNAL_ERROR;
ret = grib_unpack_long(a,values,&rlen);
if(ret != GRIB_SUCCESS){
grib_context_free(a->context,values);

View File

@ -160,28 +160,28 @@ static void init(grib_accessor* a, const long length , grib_arguments* args )
switch(self->type)
{
case GRIB_TYPE_DOUBLE:
grib_expression_evaluate_double(hand,expression,&d);
pack_double(a,&d,&len);
break;
case GRIB_TYPE_DOUBLE:
grib_expression_evaluate_double(hand,expression,&d);
pack_double(a,&d,&len);
break;
case GRIB_TYPE_LONG:
grib_expression_evaluate_long(hand,expression,&l);
pack_long(a,&l,&len);
break;
case GRIB_TYPE_LONG:
grib_expression_evaluate_long(hand,expression,&l);
pack_long(a,&l,&len);
break;
default: {
char tmp[1024];
len = sizeof(tmp);
p = grib_expression_evaluate_string(hand,expression,tmp,&len,&ret);
if (ret != GRIB_SUCCESS) {
grib_context_log(a->context,GRIB_LOG_ERROR,"unable to evaluate %s as string",a->name);
Assert(0);
}
len = strlen(p)+1;
pack_string(a,p,&len);
break;
default: {
char tmp[1024];
len = sizeof(tmp);
p = grib_expression_evaluate_string(hand,expression,tmp,&len,&ret);
if (ret != GRIB_SUCCESS) {
grib_context_log(a->context,GRIB_LOG_ERROR,"unable to evaluate %s as string",a->name);
Assert(0);
}
len = strlen(p)+1;
pack_string(a,p,&len);
break;
}
}
}
}
@ -367,7 +367,7 @@ static long byte_count(grib_accessor* a) {
return strlen(buf)+1;
}
}
*/
*/
static int compare(grib_accessor* a, grib_accessor* b)
{

View File

@ -205,7 +205,7 @@ static void dump_values(grib_dumper* d,grib_accessor* a)
depth-=2;
/* Note: In Python to make a tuple with one element, you need the trailing comma */
if (size > 4) fprintf(self->dumper.out,",) # %ld values\n", size);
if (size > 4) fprintf(self->dumper.out,",) # %lu values\n", (unsigned long)size);
else fprintf(self->dumper.out,",)\n");
grib_context_free(c,values);
@ -287,7 +287,7 @@ static void dump_values_attribute(grib_dumper* d,grib_accessor* a, const char* p
depth-=2;
/* Note: In python to make a tuple with one element, you need the trailing comma */
if (size > 4) fprintf(self->dumper.out,",) # %ld values\n", size);
if (size > 4) fprintf(self->dumper.out,",) # %lu values\n", (unsigned long)size);
else fprintf(self->dumper.out,",)\n");
grib_context_free(c,values);
@ -374,7 +374,7 @@ static void dump_long(grib_dumper* d, grib_accessor* a, const char* comment)
depth-=2;
/* Note: In python to make a tuple with one element, you need the trailing comma */
if (size > 4) fprintf(self->dumper.out,",) # %ld values\n", size);
if (size > 4) fprintf(self->dumper.out,",) # %lu values\n", (unsigned long)size);
else fprintf(self->dumper.out,",)\n");
grib_context_free(a->context,values);
@ -462,7 +462,7 @@ static void dump_long_attribute(grib_dumper* d, grib_accessor* a, const char* pr
depth-=2;
/* Note: In python to make a tuple with one element, you need the trailing comma */
if (size > 4) fprintf(self->dumper.out,",) # %ld values\n", size);
if (size > 4) fprintf(self->dumper.out,",) # %lu values\n", (unsigned long)size);
else fprintf(self->dumper.out,",)\n");
grib_context_free(a->context,values);
@ -697,7 +697,7 @@ static void _dump_long_array(grib_handle* h, FILE* f, const char* key, const cha
}
if (icount>cols) {fprintf(f," \n ");}
/* Note: In python to make a tuple with one element, you need the trailing comma */
if (size > 4) fprintf(f,"%ld ,) # %ld values\n",val[size-1], size);
if (size > 4) fprintf(f,"%ld ,) # %lu values\n",val[size-1], (unsigned long)size);
else fprintf(f,"%ld ,)\n",val[size-1]);
grib_context_free(h->context,val);

View File

@ -494,7 +494,9 @@ static int read_HDF5_offset(reader *r, int length, unsigned long* v, unsigned ch
static int read_HDF5(reader *r)
{
/* See: http://www.hdfgroup.org/HDF5/doc/H5.format.html#Superblock */
/*
* See: http://www.hdfgroup.org/HDF5/doc/H5.format.html#Superblock
*/
unsigned char tmp[49]; /* Should be enough */
unsigned char buf[4];
@ -629,7 +631,9 @@ static int read_HDF5(reader *r)
static int read_WRAP(reader *r)
{
/* See: http://www.hdfgroup.org/HDF5/doc/H5.format.html#Superblock */
/*
* See: http://www.hdfgroup.org/HDF5/doc/H5.format.html#Superblock
*/
unsigned char tmp[36]; /* Should be enough */
unsigned char buf[8];

View File

@ -177,7 +177,9 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args)
if((err = grib_get_long_internal(h, salternativeRowScanning,&alternativeRowScanning)) !=GRIB_SUCCESS)
return err;
/* See Wolfram MathWorld: http://mathworld.wolfram.com/LambertConformalConicProjection.html */
/*
* See Wolfram MathWorld: http://mathworld.wolfram.com/LambertConformalConicProjection.html
*/
latFirstInRadians = latFirstInDegrees * DEG2RAD;
lonFirstInRadians = lonFirstInDegrees * DEG2RAD;
Latin1InRadians = Latin1InDegrees * DEG2RAD;

View File

@ -211,7 +211,9 @@ cleanup:
/* OpenJPEG 2.1 version of grib_openjpeg_encoding.c */
/* opj_* Helper code from https://groups.google.com/forum/#!topic/openjpeg/8cebr0u7JgY */
/* opj_* Helper code from
* https://groups.google.com/forum/#!topic/openjpeg/8cebr0u7JgY
*/
/* These routines are added to use memory instead of a file for input and output */
/* struct need to treat memory as a stream */
typedef struct