Fix compiler errors when using g++

This commit is contained in:
Shahram Najm 2015-06-01 17:07:16 +01:00
parent e3389fe601
commit 26ae520583
9 changed files with 412 additions and 426 deletions

View File

@ -691,7 +691,7 @@ int grib_accessor_has_attributes(grib_accessor* a) {
} }
grib_accessor* grib_accessor_get_attribute(grib_accessor* a,const char* name) { grib_accessor* grib_accessor_get_attribute(grib_accessor* a,const char* name) {
int i=0,index=0; int index=0;
char* p=0; char* p=0;
char* basename=NULL; char* basename=NULL;
char* attribute_name=NULL; char* attribute_name=NULL;
@ -703,8 +703,8 @@ grib_accessor* grib_accessor_get_attribute(grib_accessor* a,const char* name) {
} else { } else {
size_t size=p-name; size_t size=p-name;
attribute_name=p+2; attribute_name=p+2;
basename=grib_context_malloc_clear(a->parent->h->context,size+1); basename=(char*)grib_context_malloc_clear(a->parent->h->context,size+1);
basename=memcpy(basename,name,size); basename=(char*)memcpy(basename,name,size);
acc=_grib_accessor_get_attribute(a,basename,&index); acc=_grib_accessor_get_attribute(a,basename,&index);
grib_context_free(a->parent->h->context,basename); grib_context_free(a->parent->h->context,basename);
if (acc) return grib_accessor_get_attribute(acc,attribute_name); if (acc) return grib_accessor_get_attribute(acc,attribute_name);
@ -712,11 +712,13 @@ grib_accessor* grib_accessor_get_attribute(grib_accessor* a,const char* name) {
} }
} }
grib_accessors_list* grib_accessors_list_create(grib_context* c) { grib_accessors_list* grib_accessors_list_create(grib_context* c)
return grib_context_malloc_clear(c,sizeof(grib_accessors_list)); {
return (grib_accessors_list*)grib_context_malloc_clear(c,sizeof(grib_accessors_list));
} }
void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a) { void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a)
{
grib_accessors_list* last; grib_accessors_list* last;
grib_context* c=a->parent->h->context; grib_context* c=a->parent->h->context;
@ -732,8 +734,9 @@ void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a) {
} }
} }
grib_accessors_list* grib_accessors_list_last(grib_accessors_list* al) { grib_accessors_list* grib_accessors_list_last(grib_accessors_list* al)
grib_accessors_list* last=al; {
/*grib_accessors_list* last=al;*/
grib_accessors_list* next=al->next; grib_accessors_list* next=al->next;
/* /*

View File

@ -178,7 +178,7 @@ char** str_split(char* a_str, const char a_delim)
knows where the list of returned strings ends. */ knows where the list of returned strings ends. */
count++; count++;
result = malloc(sizeof(char*) * count); result = (char**)malloc(sizeof(char*) * count);
if (result) if (result)
{ {

View File

@ -360,7 +360,7 @@ static int unpack_string_array(grib_accessor*a , char** v, size_t *len){
err= _grib_get_string_length(a,&length); err= _grib_get_string_length(a,&length);
if (err) return err; if (err) return err;
v[0]=grib_context_malloc_clear(a->parent->h->context,length); v[0]=(char*)grib_context_malloc_clear(a->parent->h->context,length);
grib_unpack_string(a,v[0],&length); grib_unpack_string(a,v[0],&length);
*len=1; *len=1;

View File

@ -134,128 +134,122 @@ static void init_class(grib_accessor_class* c)
/* END_CLASS_IMP */ /* END_CLASS_IMP */
static long compute_byte_count(grib_accessor* a){ static long compute_byte_count(grib_accessor* a){
grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a; grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a;
long numberOfUnexpandedDescriptors; long numberOfUnexpandedDescriptors;
int ret=0; int ret=0;
ret=grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,&numberOfUnexpandedDescriptors); ret=grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,&numberOfUnexpandedDescriptors);
if (ret) { if (ret) {
grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
"%s unable to get %s to compute size",a->name,self->numberOfUnexpandedDescriptors); "%s unable to get %s to compute size",a->name,self->numberOfUnexpandedDescriptors);
return 0; return 0;
} }
return 2*numberOfUnexpandedDescriptors; return 2*numberOfUnexpandedDescriptors;
} }
static void init(grib_accessor* a, const long len , grib_arguments* args ) static void init(grib_accessor* a, const long len , grib_arguments* args )
{ {
grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a; grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a;
int n=0; int n=0;
self->numberOfUnexpandedDescriptors=grib_arguments_get_name(a->parent->h,args,n++); self->numberOfUnexpandedDescriptors=grib_arguments_get_name(a->parent->h,args,n++);
a->length = compute_byte_count(a); a->length = compute_byte_count(a);
} }
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_long (grib_accessor* a, long* val, size_t *len) static int unpack_long (grib_accessor* a, long* val, size_t *len)
{ {
grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a; grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a;
int ret=0; int ret=0;
long pos = a->offset*8; long pos = a->offset*8;
long rlen =0; long rlen =0;
long numberOfUnexpandedDescriptors = 0; long numberOfUnexpandedDescriptors = 0;
long f,x,y; long f,x,y;
long *v=val; long *v=val;
int i; int i;
ret=value_count(a,&rlen); ret=value_count(a,&rlen);
if (ret) return ret; if (ret) return ret;
if (rlen==0) { if (rlen==0) {
grib_context_log(a->parent->h->context,GRIB_LOG_FATAL, grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,
"No descriptors in section 3. Malformed message."); "No descriptors in section 3. Malformed message.");
return GRIB_MESSAGE_MALFORMED; return GRIB_MESSAGE_MALFORMED;
} }
if(*len < rlen) if(*len < rlen)
{ {
grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
" wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen); " wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen);
*len = 0; *len = 0;
return GRIB_ARRAY_TOO_SMALL; return GRIB_ARRAY_TOO_SMALL;
} }
ret=grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,&numberOfUnexpandedDescriptors); ret=grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,&numberOfUnexpandedDescriptors);
if (ret) return ret; if (ret) return ret;
for (i=0;i<rlen;i++) { for (i=0;i<rlen;i++) {
f=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,2); f=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,2);
x=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,6); x=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,6);
y=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,8); y=grib_decode_unsigned_long(a->parent->h->buffer->data,&pos,8);
*v++=f*100000+x*1000+y; *v++=f*100000+x*1000+y;
} }
*len = rlen;
*len = rlen; 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)
{ {
int ret=0,i; int ret=0,i;
long pos = 0; long pos = 0;
unsigned long f,x,y; unsigned long f,x,y;
unsigned char* buf = NULL; unsigned char* buf = NULL;
size_t buflen=*len*2; size_t buflen=*len*2;
buf=grib_context_malloc_clear(a->parent->h->context,buflen); buf=(unsigned char*)grib_context_malloc_clear(a->parent->h->context,buflen);
for (i=0;i<*len;i++) {
f=val[i]/100000;
x=(val[i]%100000)/1000;
y=(val[i]%100000)%1000;
grib_encode_unsigned_longb(buf,f,&pos,2);
grib_encode_unsigned_longb(buf,x,&pos,6);
grib_encode_unsigned_longb(buf,y,&pos,8);
}
grib_buffer_replace(a,buf,buflen,1,1);
for (i=0;i<*len;i++) { /* update_size(a,buflen); */
f=val[i]/100000;
x=(val[i]%100000)/1000;
y=(val[i]%100000)%1000;
grib_encode_unsigned_longb(buf,f,&pos,2);
grib_encode_unsigned_longb(buf,x,&pos,6);
grib_encode_unsigned_longb(buf,y,&pos,8);
}
grib_buffer_replace(a,buf,buflen,1,1);
/* update_size(a,buflen); */
return ret;
return ret;
} }
static long byte_count(grib_accessor* a){ static long byte_count(grib_accessor* a){
return a->length; return a->length;
} }
static int value_count(grib_accessor* a,long* numberOfUnexpandedDescriptors) static int value_count(grib_accessor* a,long* numberOfUnexpandedDescriptors)
{ {
grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a; grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a;
*numberOfUnexpandedDescriptors=0; *numberOfUnexpandedDescriptors=0;
return grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,numberOfUnexpandedDescriptors); return grib_get_long(a->parent->h,self->numberOfUnexpandedDescriptors,numberOfUnexpandedDescriptors);
} }
static long byte_offset(grib_accessor* a){ static long byte_offset(grib_accessor* a){
return a->offset; return a->offset;
} }
static void update_size(grib_accessor* a,size_t s) static void update_size(grib_accessor* a,size_t s)
{ {
a->length = s; a->length = s;
} }
static long next_offset(grib_accessor* a){ static long next_offset(grib_accessor* a){
return byte_offset(a)+a->length; return byte_offset(a)+a->length;
} }

View File

@ -24,7 +24,7 @@ bufr_descriptor* grib_bufr_descriptor_clone(bufr_descriptor* d) {
if (!d) return NULL; if (!d) return NULL;
cd=grib_context_malloc_clear(d->context,sizeof(bufr_descriptor)); cd=(bufr_descriptor*)grib_context_malloc_clear(d->context,sizeof(bufr_descriptor));
cd->code=d->code; cd->code=d->code;
cd->F=d->F; cd->F=d->F;

View File

@ -51,9 +51,7 @@ struct grib_keys_hash { char* name; int id;};
#endif #endif
#endif #endif
static unsigned int static unsigned int
hash_keys (str, len) hash_keys (const char *str, unsigned int len)
register const char *str;
register unsigned int len;
{ {
static unsigned short asso_values[] = static unsigned short asso_values[] =
{ {
@ -7171,9 +7169,7 @@ static struct grib_keys_hash wordlist[] =
#endif #endif
#endif #endif
struct grib_keys_hash * struct grib_keys_hash *
grib_keys_hash_get (str, len) grib_keys_hash_get (const char *str, unsigned int len)
register const char *str;
register unsigned int len;
{ {
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
{ {

View File

@ -284,7 +284,7 @@ int grib_accessors_list_print(grib_accessors_list* al,const char* name,int type,
} else { } else {
int i=0; int i=0;
int cols=0; int cols=0;
cvals=grib_context_malloc_clear(h->context,sizeof(char*)*size); cvals=(char**)grib_context_malloc_clear(h->context,sizeof(char*)*size);
grib_accessors_list_unpack_string(al,cvals,&size); grib_accessors_list_unpack_string(al,cvals,&size);
for (i=0;i<size;i++) { for (i=0;i<size;i++) {
*newline=1; *newline=1;

View File

@ -23,476 +23,469 @@ GRIB_INLINE static int strcmp(const char* a,const char* b) {
} }
#endif #endif
static int matching(grib_accessor* a,const char* name,const char* name_space) static int matching(grib_accessor* a,const char* name,const char* name_space)
{ {
int i = 0; int i = 0;
while(i < MAX_ACCESSOR_NAMES) while(i < MAX_ACCESSOR_NAMES)
{ {
if(a->all_names[i] == 0) return 0; if(a->all_names[i] == 0) return 0;
if((strcmp(name,a->all_names[i]) == 0) && if((strcmp(name,a->all_names[i]) == 0) &&
((name_space == NULL) || (a->all_name_spaces[i] != NULL && ((name_space == NULL) || (a->all_name_spaces[i] != NULL &&
strcmp(a->all_name_spaces[i],name_space) == 0)) strcmp(a->all_name_spaces[i],name_space) == 0))
) )
return 1; return 1;
i++; i++;
} }
return 0; return 0;
} }
static grib_accessor* search(grib_section* s,const char* name,const char* name_space) static grib_accessor* search(grib_section* s,const char* name,const char* name_space)
{ {
grib_accessor* match = NULL; grib_accessor* match = NULL;
grib_accessor* a = s ? s->block->first : NULL; grib_accessor* a = s ? s->block->first : NULL;
grib_accessor* b=NULL; grib_accessor* b=NULL;
if(!a || !s) return NULL; if(!a || !s) return NULL;
while(a) while(a)
{ {
grib_section* sub = a->sub_section; grib_section* sub = a->sub_section;
if(matching(a,name,name_space)) if(matching(a,name,name_space))
match = a; match = a;
if((b = search(sub,name,name_space) ) != NULL ) if((b = search(sub,name,name_space) ) != NULL )
match = b; match = b;
a = a->next; a = a->next;
} }
return match; return match;
} }
static void rebuild_hash_keys (grib_handle* h,grib_section* s) static void rebuild_hash_keys (grib_handle* h,grib_section* s)
{ {
grib_accessor* a = s ? s->block->first : NULL; grib_accessor* a = s ? s->block->first : NULL;
while(a) while(a)
{ {
grib_section* sub = a->sub_section; grib_section* sub = a->sub_section;
int i=0; int i=0;
int id=-1; int id=-1;
const char *p; const char *p;
Assert(h == a->parent->h); Assert(h == a->parent->h);
while(i<MAX_ACCESSOR_NAMES && ((p = a->all_names[i]) != NULL)) { while(i<MAX_ACCESSOR_NAMES && ((p = a->all_names[i]) != NULL)) {
if(*p != '_') { if(*p != '_') {
id=grib_hash_keys_get_id(a->parent->h->context->keys,p); id=grib_hash_keys_get_id(a->parent->h->context->keys,p);
if (a->same != a && i==0) { if (a->same != a && i==0) {
a->same=a->parent->h->accessors[id]; a->same=a->parent->h->accessors[id];
a->parent->h->accessors[id]=a; a->parent->h->accessors[id]=a;
Assert(a->same != a); Assert(a->same != a);
} }
} }
i++; i++;
} }
rebuild_hash_keys(h,sub); rebuild_hash_keys(h,sub);
a = a->next; a = a->next;
} }
} }
static grib_accessor* search_and_cache(grib_handle* h, const char* name,const char *the_namespace); static grib_accessor* search_and_cache(grib_handle* h, const char* name,const char *the_namespace);
static grib_accessor* _search_and_cache(grib_handle* h, const char* name,const char *the_namespace) static grib_accessor* _search_and_cache(grib_handle* h, const char* name,const char *the_namespace)
{ {
if (h->use_trie) if (h->use_trie)
{
grib_accessor* a = NULL;
int id=-1;
if (h->trie_invalid && h->kid == NULL)
{ {
int i=0; grib_accessor* a = NULL;
for (i=0;i<ACCESSORS_ARRAY_SIZE;i++) int id=-1;
h->accessors[i]=NULL;
if (h->root) if (h->trie_invalid && h->kid == NULL)
rebuild_hash_keys(h,h->root); {
int i=0;
for (i=0;i<ACCESSORS_ARRAY_SIZE;i++)
h->accessors[i]=NULL;
h->trie_invalid=0; if (h->root)
id = grib_hash_keys_get_id(h->context->keys,name); rebuild_hash_keys(h,h->root);
} else { h->trie_invalid=0;
id = grib_hash_keys_get_id(h->context->keys,name); id = grib_hash_keys_get_id(h->context->keys,name);
} else {
id = grib_hash_keys_get_id(h->context->keys,name);
if ((a=h->accessors[id])!=NULL &&
(the_namespace==NULL || matching(a,name,the_namespace) ))
return a;
}
a = search(h->root,name,the_namespace);
h->accessors[id] = a;
if ((a=h->accessors[id])!=NULL &&
(the_namespace==NULL || matching(a,name,the_namespace) ))
return a; return a;
} else {
return search(h->root,name,the_namespace);
} }
a = search(h->root,name,the_namespace);
h->accessors[id] = a;
return a;
} else {
return search(h->root,name,the_namespace);
}
} }
char* get_rank(const char* name,long *rank) { char* get_rank(const char* name,long *rank) {
char* p=(char*)name; char* p=(char*)name;
char* end=p; char* end=p;
char* ret=NULL; char* ret=NULL;
size_t len; size_t len;
*rank=-1; *rank=-1;
while (*p!=0 && *p!='#') p++; while (*p!=0 && *p!='#') p++;
if (*p=='#') { if (*p=='#') {
*rank=strtol(++p,&end,10); *rank=strtol(++p,&end,10);
if ( *end != 0) { if ( *end != 0) {
*rank=-1; *rank=-1;
} else { } else {
grib_context* c=grib_context_get_default(); grib_context* c=grib_context_get_default();
len=p-name; len=p-name;
ret=grib_context_malloc_clear(c,len); ret=(char*)grib_context_malloc_clear(c,len);
memcpy(ret,name,len-1); memcpy(ret,name,len-1);
}
} }
} return ret;
return ret;
} }
char* get_condition(const char* name,codes_condition* condition) { char* get_condition(const char* name,codes_condition* condition)
char* equal=(char*)name; {
char* endCondition=NULL; char* equal=(char*)name;
char* startLeft=NULL; char* endCondition=NULL;
char* str=NULL; char* str=NULL;
char* end=NULL; char* end=NULL;
size_t len; long lval;
long lval; double dval;
double dval; grib_context* c=grib_context_get_default();
grib_context* c=grib_context_get_default();
condition->rightType=GRIB_TYPE_UNDEFINED; condition->rightType=GRIB_TYPE_UNDEFINED;
Assert(name[0]=='/'); Assert(name[0]=='/');
while (*equal!=0 && *equal!='=') equal++; while (*equal!=0 && *equal!='=') equal++;
if (*equal==0) return NULL; if (*equal==0) return NULL;
endCondition=equal; endCondition=equal;
while (*endCondition!=0 && *endCondition!='/') endCondition++; while (*endCondition!=0 && *endCondition!='/') endCondition++;
if (*endCondition==0) return NULL; if (*endCondition==0) return NULL;
str=grib_context_malloc_clear(c,strlen(name)); str=(char*)grib_context_malloc_clear(c,strlen(name));
memcpy(str,equal+1,endCondition-equal-1); memcpy(str,equal+1,endCondition-equal-1);
end=NULL; end=NULL;
lval=strtol(str,&end,10); lval=strtol(str,&end,10);
if (*end != 0) {
dval=strtod(str,&end);
if (*end != 0) { if (*end != 0) {
condition->rightType=GRIB_TYPE_DOUBLE; dval=strtod(str,&end);
condition->rightDouble=dval; if (*end != 0) {
condition->rightType=GRIB_TYPE_DOUBLE;
condition->rightDouble=dval;
}
} else {
condition->rightType=GRIB_TYPE_LONG;
condition->rightLong=lval;
} }
} else {
condition->rightType=GRIB_TYPE_LONG;
condition->rightLong=lval;
}
if (condition->rightType!=GRIB_TYPE_UNDEFINED) { if (condition->rightType!=GRIB_TYPE_UNDEFINED) {
strcpy(str,endCondition+1); strcpy(str,endCondition+1);
condition->left=grib_context_malloc_clear(c,equal-name); condition->left=(char*)grib_context_malloc_clear(c,equal-name);
memcpy(condition->left,name+1,equal-name-1); memcpy(condition->left,name+1,equal-name-1);
} else { } else {
grib_context_free(c,str); grib_context_free(c,str);
str=NULL; str=NULL;
} }
return str; return str;
} }
static grib_accessor* _search_by_rank(grib_accessor* a,const char* name,long rank) { static grib_accessor* _search_by_rank(grib_accessor* a,const char* name,long rank) {
long r=1; long r=1;
grib_accessors_list* al=accessor_bufr_data_array_get_dataAccessors(a); grib_accessors_list* al=accessor_bufr_data_array_get_dataAccessors(a);
while (al) { while (al) {
if (!strcmp(al->accessor->name,name)) { if (!strcmp(al->accessor->name,name)) {
if (r==rank) return al->accessor; if (r==rank) return al->accessor;
r++; r++;
}
al=al->next;
} }
al=al->next;
}
return NULL; return NULL;
} }
static grib_accessor* search_by_rank(grib_handle* h, const char* name,const char *the_namespace,long rank) static grib_accessor* search_by_rank(grib_handle* h, const char* name,const char *the_namespace,long rank)
{ {
grib_accessor* data=search_and_cache(h,"dataAccessors",the_namespace); grib_accessor* data=search_and_cache(h,"dataAccessors",the_namespace);
if (data) { if (data) {
return _search_by_rank(data,name,rank); return _search_by_rank(data,name,rank);
} else { } else {
return _search_and_cache(h,name,the_namespace); return _search_and_cache(h,name,the_namespace);
} }
} }
static int condition_true(grib_accessor* a,codes_condition* condition) { static int condition_true(grib_accessor* a,codes_condition* condition) {
int ret=0; int ret=0;
size_t size=1; size_t size=1;
long lval; long lval;
double dval; double dval;
switch (condition->rightType) { switch (condition->rightType) {
case GRIB_TYPE_LONG: case GRIB_TYPE_LONG:
grib_unpack_long(a,&lval,&size); grib_unpack_long(a,&lval,&size);
ret = lval==condition->rightLong ? 1 : 0; ret = lval==condition->rightLong ? 1 : 0;
break; break;
case GRIB_TYPE_DOUBLE: case GRIB_TYPE_DOUBLE:
grib_unpack_double(a,&dval,&size); grib_unpack_double(a,&dval,&size);
ret = dval==condition->rightDouble ? 1 : 0; ret = dval==condition->rightDouble ? 1 : 0;
break; break;
default : default :
ret=0; ret=0;
break; break;
} }
return ret; return ret;
} }
static void search_from_accessors_list(grib_accessors_list* al,grib_accessors_list* end,const char* name,grib_accessors_list* result) { static void search_from_accessors_list(grib_accessors_list* al,grib_accessors_list* end,const char* name,grib_accessors_list* result) {
while (al && al!=end && al->accessor) { while (al && al!=end && al->accessor) {
if (!strcmp(al->accessor->name,name)) if (!strcmp(al->accessor->name,name))
grib_accessors_list_push(result,al->accessor); grib_accessors_list_push(result,al->accessor);
al=al->next; al=al->next;
} }
} }
static void search_accessors_list_by_condition(grib_accessors_list* al,const char* name,codes_condition* condition,grib_accessors_list* result) { static void search_accessors_list_by_condition(grib_accessors_list* al,const char* name,codes_condition* condition,grib_accessors_list* result) {
grib_accessors_list* start=NULL; grib_accessors_list* start=NULL;
grib_accessors_list* end=NULL; grib_accessors_list* end=NULL;
while (al) { while (al) {
if (!strcmp(al->accessor->name,condition->left)) { if (!strcmp(al->accessor->name,condition->left)) {
if (start==NULL && condition_true(al->accessor,condition)) start=al; if (start==NULL && condition_true(al->accessor,condition)) start=al;
if (start && !condition_true(al->accessor,condition)) end=al; if (start && !condition_true(al->accessor,condition)) end=al;
}
al=al->next;
if (start && end) {
search_from_accessors_list(start,end,name,result);
al=end;
start=NULL;
end=NULL;
}
} }
al=al->next;
if (start && end) {
search_from_accessors_list(start,end,name,result);
al=end;
start=NULL;
end=NULL;
}
}
} }
static grib_accessors_list* search_by_condition(grib_handle* h,const char* name,codes_condition* condition) { static grib_accessors_list* search_by_condition(grib_handle* h,const char* name,codes_condition* condition) {
grib_accessors_list* al; grib_accessors_list* al;
grib_accessors_list* result=NULL; grib_accessors_list* result=NULL;
grib_accessor* data=search_and_cache(h,"dataAccessors",0); grib_accessor* data=search_and_cache(h,"dataAccessors",0);
if (data && condition->left) { if (data && condition->left) {
al=accessor_bufr_data_array_get_dataAccessors(data); al=accessor_bufr_data_array_get_dataAccessors(data);
result=grib_context_malloc_clear(al->accessor->parent->h->context,sizeof(grib_accessors_list)); result=(grib_accessors_list*)grib_context_malloc_clear(al->accessor->parent->h->context,sizeof(grib_accessors_list));
search_accessors_list_by_condition(al,name,condition,result); search_accessors_list_by_condition(al,name,condition,result);
if (!result->accessor) { if (!result->accessor) {
grib_accessors_list_delete(h->context,result); grib_accessors_list_delete(h->context,result);
result=NULL; result=NULL;
}
} }
}
return result; return result;
} }
static void grib_find_same_and_push(grib_accessors_list* al,grib_accessor* a) { static void grib_find_same_and_push(grib_accessors_list* al,grib_accessor* a) {
if (a) { if (a) {
grib_find_same_and_push(al,a->same); grib_find_same_and_push(al,a->same);
grib_accessors_list_push(al,a); grib_accessors_list_push(al,a);
} }
} }
grib_accessors_list* grib_find_accessors_list(grib_handle* h,const char* name) { grib_accessors_list* grib_find_accessors_list(grib_handle* h,const char* name) {
char* str=NULL; char* str=NULL;
grib_accessors_list* al=NULL; grib_accessors_list* al=NULL;
codes_condition* condition=NULL; codes_condition* condition=NULL;
grib_accessor* a=NULL; grib_accessor* a=NULL;
if (name[0]=='/') { if (name[0]=='/') {
condition=grib_context_malloc_clear(h->context,sizeof(codes_condition)); condition=(codes_condition*)grib_context_malloc_clear(h->context,sizeof(codes_condition));
str=get_condition(name,condition); str=get_condition(name,condition);
if (str) { if (str) {
al=search_by_condition(h,str,condition); al=search_by_condition(h,str,condition);
grib_context_free(h->context,str); grib_context_free(h->context,str);
if (condition->left) grib_context_free(h->context,condition->left); if (condition->left) grib_context_free(h->context,condition->left);
if (condition->rightString) grib_context_free(h->context,condition->rightString); if (condition->rightString) grib_context_free(h->context,condition->rightString);
}
grib_context_free(h->context,condition);
} else if (has_rank(name)) {
a=grib_find_accessor(h, name);
if (a) {
al=(grib_accessors_list*)grib_context_malloc_clear(h->context,sizeof(grib_accessors_list));
grib_accessors_list_push(al,a);
}
} else {
a=grib_find_accessor(h, name);
if (a) {
al=(grib_accessors_list*)grib_context_malloc_clear(h->context,sizeof(grib_accessors_list));
grib_find_same_and_push(al,a);
}
} }
grib_context_free(h->context,condition);
} else if (has_rank(name)) {
a=grib_find_accessor(h, name);
if (a) {
al=grib_context_malloc_clear(h->context,sizeof(grib_accessors_list));
grib_accessors_list_push(al,a);
}
} else {
a=grib_find_accessor(h, name);
if (a) {
al=grib_context_malloc_clear(h->context,sizeof(grib_accessors_list));
grib_find_same_and_push(al,a);
}
}
return al; return al;
} }
static grib_accessor* search_and_cache(grib_handle* h, const char* name,const char *the_namespace) static grib_accessor* search_and_cache(grib_handle* h, const char* name,const char *the_namespace)
{ {
char* str=0; char* str=0;
grib_accessor* a=NULL; grib_accessor* a=NULL;
codes_condition* condition=NULL; long rank;
long rank;
str=get_rank(name,&rank); str=get_rank(name,&rank);
if (rank>0) { if (rank>0) {
a=search_by_rank(h,str,the_namespace,rank); a=search_by_rank(h,str,the_namespace,rank);
grib_context_free(h->context,str); grib_context_free(h->context,str);
} else { } else {
a=_search_and_cache(h,name,the_namespace); a=_search_and_cache(h,name,the_namespace);
} }
return a; return a;
} }
static grib_accessor* _grib_find_accessor(grib_handle* h, const char* name) static grib_accessor* _grib_find_accessor(grib_handle* h, const char* name)
{ {
grib_accessor* a = NULL; grib_accessor* a = NULL;
char* p=NULL; char* p=NULL;
char* basename=NULL; char* basename=NULL;
char name_space[1024]; char name_space[1024];
int i=0,len=0; int i=0,len=0;
p=(char*)name; p=(char*)name;
Assert(name); Assert(name);
while ( *p != '.' && *p != '\0' ) p++; while ( *p != '.' && *p != '\0' ) p++;
if ( *p == '.' ) { if ( *p == '.' ) {
basename=p+1; basename=p+1;
p--; p--;
i=0; i=0;
len=p-name+1; len=p-name+1;
for ( i=0;i<len;i++ ) name_space[i] = *(name+i); for ( i=0;i<len;i++ ) name_space[i] = *(name+i);
name_space[len]='\0'; name_space[len]='\0';
a = search_and_cache(h,basename,name_space); a = search_and_cache(h,basename,name_space);
} else { } else {
a = search_and_cache(h,name,NULL); a = search_and_cache(h,name,NULL);
} }
if(a == NULL && h->main) if(a == NULL && h->main)
a = grib_find_accessor(h->main,name); a = grib_find_accessor(h->main,name);
return a; return a;
} }
int has_rank(const char* name) { int has_rank(const char* name) {
char* p=(char*)name; char* p=(char*)name;
while (*p!=0 && *p!='#') p++; while (*p!=0 && *p!='#') p++;
return *p ? 1 : 0 ; return *p ? 1 : 0 ;
} }
char* grib_split_name_attribute(grib_context* c,const char* name,char* attribute_name) { char* grib_split_name_attribute(grib_context* c,const char* name,char* attribute_name) {
/*returns accessor name and attribute*/ /*returns accessor name and attribute*/
char* p=0; char* p=0;
size_t size=0; size_t size=0;
char* accessor_name=NULL; char* accessor_name=NULL;
p=(char*)name; p=(char*)name;
while ( *(p+1) != '\0' && ( *p != '-' || *(p+1)!= '>' ) ) p++; while ( *(p+1) != '\0' && ( *p != '-' || *(p+1)!= '>' ) ) p++;
if (*(p+1) == '\0') { if (*(p+1) == '\0') {
*attribute_name=0; *attribute_name=0;
return (char*)name; return (char*)name;
} }
size=p-name; size=p-name;
accessor_name=grib_context_malloc_clear(c,size+1); accessor_name=(char*)grib_context_malloc_clear(c,size+1);
accessor_name=memcpy(accessor_name,name,size); accessor_name=(char*)memcpy(accessor_name,name,size);
p+=2; p+=2;
strcpy(attribute_name,p); strcpy(attribute_name,p);
return accessor_name; return accessor_name;
} }
grib_accessor* grib_find_accessor(grib_handle* h, const char* name) grib_accessor* grib_find_accessor(grib_handle* h, const char* name)
{ {
char* accessor_name=NULL; char* accessor_name=NULL;
char attribute_name[512]={0,}; char attribute_name[512]={0,};
grib_accessor* a=NULL; grib_accessor* a=NULL;
grib_accessor* aret=NULL; grib_accessor* aret=NULL;
accessor_name=grib_split_name_attribute(h->context,name,attribute_name); accessor_name=grib_split_name_attribute(h->context,name,attribute_name);
a=_grib_find_accessor(h,accessor_name); a=_grib_find_accessor(h,accessor_name);
if (*attribute_name==0) { if (*attribute_name==0) {
aret=a; aret=a;
} else if (a) { } else if (a) {
aret=grib_accessor_get_attribute(a,attribute_name); aret=grib_accessor_get_attribute(a,attribute_name);
grib_context_free(h->context,accessor_name); grib_context_free(h->context,accessor_name);
} }
return aret; return aret;
} }
grib_accessor* grib_find_attribute(grib_handle* h, const char* name,const char* attr_name, int* err) grib_accessor* grib_find_attribute(grib_handle* h, const char* name,const char* attr_name, int* err)
{ {
grib_accessor* a = NULL; grib_accessor* a = NULL;
grib_accessor* act = NULL; grib_accessor* act = NULL;
int id=0; int id=0;
if ((a = grib_find_accessor(h, name))==NULL) { if ((a = grib_find_accessor(h, name))==NULL) {
*err=GRIB_NOT_FOUND; *err=GRIB_NOT_FOUND;
return NULL; return NULL;
} }
if((act=grib_accessor_get_attribute(a,attr_name))==NULL) { if((act=grib_accessor_get_attribute(a,attr_name))==NULL) {
*err=GRIB_ATTRIBUTE_NOT_FOUND; *err=GRIB_ATTRIBUTE_NOT_FOUND;
return NULL; return NULL;
} }
return act; return act;
} }
/* Only look in trie. Used only in alias. Should not be used in other cases.*/ /* Only look in trie. Used only in alias. Should not be used in other cases.*/
grib_accessor* grib_find_accessor_fast(grib_handle* h, const char* name) grib_accessor* grib_find_accessor_fast(grib_handle* h, const char* name)
{ {
grib_accessor* a = NULL; grib_accessor* a = NULL;
char* p=NULL; char* p=NULL;
char* basename=NULL; char* basename=NULL;
char name_space[1024]; char name_space[1024];
int i=0,len=0; int i=0,len=0;
p=(char*)name; p=(char*)name;
while ( *p != '.' && *p != '\0' ) p++; while ( *p != '.' && *p != '\0' ) p++;
if ( *p == '.' ) { if ( *p == '.' ) {
basename=p+1; basename=p+1;
p--; p--;
i=0; i=0;
len=p-name+1; len=p-name+1;
for ( i=0;i<len;i++ ) name_space[i] = *(name+i); for ( i=0;i<len;i++ ) name_space[i] = *(name+i);
name_space[len]='\0'; name_space[len]='\0';
a = h->accessors[grib_hash_keys_get_id(h->context->keys,name)]; a = h->accessors[grib_hash_keys_get_id(h->context->keys,name)];
if(a && !matching(a,name,name_space)) if(a && !matching(a,name,name_space))
a = NULL; a = NULL;
} else { } else {
a = h->accessors[grib_hash_keys_get_id(h->context->keys,name)]; a = h->accessors[grib_hash_keys_get_id(h->context->keys,name)];
} }
if(a == NULL && h->main) if(a == NULL && h->main)
a = grib_find_accessor_fast(h->main,name); a = grib_find_accessor_fast(h->main,name);
return a; return a;
} }

View File

@ -243,30 +243,30 @@ grib_handle* merge(grib_handle* h1,grib_handle* h2) {
grib_get_long(h,"numberOfPoints",&n); grib_get_long(h,"numberOfPoints",&n);
grib_get_double(h,"missingValue",&missingValue); grib_get_double(h,"missingValue",&missingValue);
v=grib_context_malloc_clear(h->context,sizeof(double)*n); v=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n);
for (i=0;i<n;i++) v[i]=1.0; for (i=0;i<n;i++) v[i]=1.0;
sn=n; sn=n;
grib_set_double_array(h,"values",v,sn); grib_set_double_array(h,"values",v,sn);
for (i=0;i<n;i++) v[i]=missingValue; for (i=0;i<n;i++) v[i]=missingValue;
lat=grib_context_malloc_clear(h->context,sizeof(double)*n); lat=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n);
lon=grib_context_malloc_clear(h->context,sizeof(double)*n); lon=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n);
sn=n; sn=n;
grib_get_double_array(h,"latitudes",lat,&sn); grib_get_double_array(h,"latitudes",lat,&sn);
grib_get_double_array(h,"longitudes",lon,&sn); grib_get_double_array(h,"longitudes",lon,&sn);
grib_get_long(h1,"numberOfPoints",&n1); grib_get_long(h1,"numberOfPoints",&n1);
v1=grib_context_malloc_clear(h->context,sizeof(double)*n1); v1=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n1);
lat1=grib_context_malloc_clear(h->context,sizeof(double)*n1); lat1=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n1);
lon1=grib_context_malloc_clear(h->context,sizeof(double)*n1); lon1=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n1);
sn=n1; sn=n1;
grib_get_double_array(h1,"latitudes",lat1,&sn); grib_get_double_array(h1,"latitudes",lat1,&sn);
grib_get_double_array(h1,"longitudes",lon1,&sn); grib_get_double_array(h1,"longitudes",lon1,&sn);
grib_get_double_array(h1,"values",v1,&sn); grib_get_double_array(h1,"values",v1,&sn);
grib_get_long(h2,"numberOfPoints",&n2); grib_get_long(h2,"numberOfPoints",&n2);
v2=grib_context_malloc_clear(h->context,sizeof(double)*n2); v2=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n2);
lat2=grib_context_malloc_clear(h->context,sizeof(double)*n2); lat2=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n2);
lon2=grib_context_malloc_clear(h->context,sizeof(double)*n2); lon2=(double*)grib_context_malloc_clear(h->context,sizeof(double)*n2);
sn=n2; sn=n2;
grib_get_double_array(h2,"latitudes",lat2,&sn); grib_get_double_array(h2,"latitudes",lat2,&sn);
grib_get_double_array(h2,"longitudes",lon2,&sn); grib_get_double_array(h2,"longitudes",lon2,&sn);