static void init(grib_accessor* a, const long len , grib_arguments* arg ) { grib_accessor_md5* self = (grib_accessor_md5*)a; char* b=0; int n=0; grib_string_list* current=0; grib_context* context=a->context; self->offset = grib_arguments_get_name(grib_handle_of_accessor(a),arg,n++); self->length = grib_arguments_get_expression(grib_handle_of_accessor(a),arg,n++); self->blacklist=0; while ( (b=(char*)grib_arguments_get_name(grib_handle_of_accessor(a),arg,n++)) !=NULL) { if (! self->blacklist) { self->blacklist=(grib_string_list*)grib_context_malloc_clear(context,sizeof(grib_string_list)); self->blacklist->value=grib_context_strdup(context,b); current=self->blacklist; } else { current->next=(grib_string_list*)grib_context_malloc_clear(context,sizeof(grib_string_list)); current->next->value=grib_context_strdup(context,b); current=current->next; } } a->length = 0; a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; a->flags |= GRIB_ACCESSOR_FLAG_EDITION_SPECIFIC; }
static void index_add_conditions(grib_concept_index* index,grib_concept_condition* condition) { grib_concept_condition* c=condition; char s[512]={0,}; grib_concept_index_entry* e;; grib_concept_index_entry* entry=index_entry_new(index->context,index->keys);; while (c) { size_t size=512; int type; e=entry; type = grib_expression_native_type(0,c->expression); switch(type) { case GRIB_TYPE_LONG: grib_expression_evaluate_long(0,c->expression,&lres); sprintf(s,"%ld",lres); break; case GRIB_TYPE_DOUBLE: grib_expression_evaluate_double(0,c->expression,&dres); sprintf(s,"%g",dres); break; case GRIB_TYPE_STRING: grib_expression_evaluate_string(0,c->expression,s,&size,&err); break; default: Assert(0); break; } while (e->name && strcmp(e->name,c->name)) e=e->next; e->type=type; e->value=grib_context_strdup(index->context,s); if (!e->name) { e->name=grib_context_strdup(index->context,c->name); e->next=grib_context_malloc_clear_persistent(index->context,sizeof(grib_concept_index_entry)); if (!e->next) grib_context_log(index->context,GRIB_LOG_FATAL,"index_add_conditions unable to allocate"); } c=c->next; } index_insert_entry(index,entry,condition->name); index_entry_delete(index->context,entry); }
static grib_concept_index_entry* index_entry_new(grib_context* c,grib_concept_index_keys* keys) { grib_concept_index_entry* entry=NULL; Assert(keys); if (!c) c=grib_context_get_default(); entry=grib_context_malloc_clear(c,sizeof(grib_concept_index_entry)); if (!entry) grib_context_log(c,GRIB_LOG_FATAL,"grib_concept_index_entry unable to allocate"); e=entry; while (keys && keys->name) { e->name=grib_context_strdup(c,keys->name); e->type=keys->type; e->next=grib_context_malloc_clear(c,sizeof(grib_concept_index_entry)); if (!e->next) grib_context_log(c,GRIB_LOG_FATAL,"grib_concept_index_entry unable to allocate"); e=e->next; keys=keys->next; } return entry; }
/* --------------- grib_column functions ------------------*/ int grib_fieldset_new_column(grib_fieldset* set,int id,char* key,int type) { grib_column* column=0; grib_context* c; int err=0; if (!set) return GRIB_INVALID_ARGUMENT; c=set->context; set->columns[id].errors=(int*)grib_context_malloc_clear(c, sizeof(int)*GRIB_START_ARRAY_SIZE); switch (type) { case GRIB_TYPE_LONG: set->columns[id].long_values=(long*)grib_context_malloc_clear(c, sizeof(long)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].long_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(long)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; case GRIB_TYPE_DOUBLE: set->columns[id].double_values=(double*)grib_context_malloc_clear(c, sizeof(double)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].double_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(double)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; case GRIB_TYPE_STRING: set->columns[id].string_values=(char**)grib_context_malloc_clear(c, sizeof(char*)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].string_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(char*)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; default: grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : unknown column type %d",type); grib_context_free(c,column); return err; } set->columns[id].context=c; set->columns[id].name=grib_context_strdup(c,key); set->columns[id].type=type; set->columns[id].values_array_size=GRIB_START_ARRAY_SIZE; set->columns[id].size=0; return err; }
int grib_fieldset_column_copy_from_handle(grib_handle* h,grib_fieldset* set,int i) { int err=0; long lval=0; double dval=0; char sval[1024]; size_t slen=1024; if (!set || !h || set->columns[i].type == 0) return GRIB_INVALID_ARGUMENT; if (set->columns[i].size >= set->columns[i].values_array_size) grib_fieldset_columns_resize(set,set->columns[i].values_array_size+GRIB_ARRAY_INCREMENT); switch (set->columns[i].type) { case GRIB_TYPE_LONG: err=grib_get_long(h,set->columns[i].name,&lval); set->columns[i].long_values[set->columns[i].size]=lval; break; case GRIB_TYPE_DOUBLE: err=grib_get_double(h,set->columns[i].name,&dval); set->columns[i].double_values[set->columns[i].size]=dval; break; case GRIB_TYPE_STRING: err=grib_get_string(h,set->columns[i].name,sval,&slen); set->columns[i].string_values[set->columns[i].size]=grib_context_strdup(h->context,sval); break; } set->columns[i].errors[set->columns[i].size]=err; set->columns[i].size++; return err; }
static int init_definition_files_dir(grib_context* c) { int err=0; char* path=NULL; char* p=NULL; char* dir=NULL; grib_string_list* next=NULL; if (!c) c=grib_context_get_default(); if (c->grib_definition_files_dir) return 0; if (!c->grib_definition_files_path) return GRIB_NO_DEFINITIONS; path=c->grib_definition_files_path; GRIB_PTHREAD_ONCE(&once,&init); GRIB_MUTEX_LOCK(&mutex_c); p=path; while(*p!=':' && *p!='\0') p++; if (*p != ':') { c->grib_definition_files_dir=(grib_string_list*)grib_context_malloc_clear_persistent(c,sizeof(grib_string_list)); c->grib_definition_files_dir->value=grib_context_strdup(c,path); } else { dir=strtok(path,":"); while (dir != NULL) { if (next) { next->next=(grib_string_list*)grib_context_malloc_clear_persistent(c,sizeof(grib_string_list)); next=next->next; } else { c->grib_definition_files_dir=(grib_string_list*)grib_context_malloc_clear_persistent(c,sizeof(grib_string_list)); next=c->grib_definition_files_dir; } next->value=grib_context_strdup(c,dir); dir=strtok(NULL,":"); } } GRIB_MUTEX_UNLOCK(&mutex_c); return err; }
char *grib_context_full_path(grib_context* c,const char* basename) { int err=0; char full[1024]={0,}; grib_string_list* dir=NULL; grib_string_list* fullpath=0; if (!c) c=grib_context_get_default(); GRIB_PTHREAD_ONCE(&once,&init); GRIB_MUTEX_LOCK(&mutex_c); if(*basename == '/' || *basename == '.') { GRIB_MUTEX_UNLOCK(&mutex_c); return (char*)basename; } else { if ((fullpath=grib_trie_get(c->def_files,basename))!=NULL) { GRIB_MUTEX_UNLOCK(&mutex_c); return fullpath->value; } if (!c->grib_definition_files_dir) err=init_definition_files_dir(c); if (err != GRIB_SUCCESS) { grib_context_log(c,GRIB_LOG_ERROR, "Unable to find definition files directory"); GRIB_MUTEX_UNLOCK(&mutex_c); return NULL; } dir=c->grib_definition_files_dir; while (dir) { sprintf(full,"%s/%s",dir->value,basename); if (!access(full,F_OK)) { fullpath=grib_context_malloc_clear_persistent(c,sizeof(grib_string_list)); Assert(fullpath); fullpath->value=grib_context_strdup(c,full); grib_trie_insert(c->def_files,basename,fullpath); grib_context_log(c,GRIB_LOG_DEBUG,"Found def file %s",full); GRIB_MUTEX_UNLOCK(&mutex_c); return fullpath->value; } dir=dir->next; } } /* Store missing files so we don't check for them again and again */ grib_trie_insert(c->def_files,basename,&grib_file_not_found); /*grib_context_log(c,GRIB_LOG_ERROR,"Def file \"%s\" not found",basename);*/ GRIB_MUTEX_UNLOCK(&mutex_c); full[0]=0; return NULL; }
static char* try_template_path(grib_context* c,const char* dir,const char* name) { char path[1024]; sprintf(path,"%s/%s.tmpl",dir,name); if(access(path,R_OK) == 0) { return grib_context_strdup(c,path); } return NULL; }
static int index_insert_entry(grib_concept_index* index,grib_concept_index_entry* entry,void* object) { int err=0; int found=0; grib_conditions_tree* cur=index->conditions; grib_conditions_tree* prev=index->conditions; grib_concept_index_keys* keys=index->keys; while (keys->name) { if (!cur) { cur=grib_context_malloc_clear_persistent(index->context,sizeof(grib_conditions_tree)); if (!cur) grib_context_log(index->context,GRIB_LOG_FATAL,"index_insert_entry unable to allocate"); prev->next=cur; } value = entry->value ? entry->value : "*"; while (cur && (!cur->value || (found=!strcmp(cur->value,value))==0) ) cur=cur->next; if (!found) { cur->next=grib_context_malloc_clear_persistent(index->context,sizeof(grib_conditions_tree)); if (!cur->next) grib_context_log(index->context,GRIB_LOG_FATAL,"index_insert_entry unable to allocate"); cur=cur->next; } cur->value=grib_context_strdup(index->context,value); entry=entry->next; keys=keys->next; prev=cur; cur=cur->next_key; } while (cur) { prev=cur; cur=cur->next_key; } prev->object=object; return err; }
static int pack_double(grib_accessor* a, const double* cval, size_t *len) { grib_accessor_data_g1simple_packing* self = (grib_accessor_data_g1simple_packing*)a; grib_accessor_class* super = *(a->cclass->super); size_t n_vals = *len; long half_byte = 0; int ret = 0; long offsetdata = 0; long offsetsection = 0; double reference_value = 0; long binary_scale_factor = 0; long bits_per_value = 0; long decimal_scale_factor = 0; double decimal = 1; size_t buflen = 0; unsigned char* buf = NULL; unsigned char* encoded = NULL; double divisor = 1; int i; long off = 0; grib_context* c=a->parent->h->context; grib_handle* h=a->parent->h; char* ieee_packing_s=NULL; char* packingType_s=NULL; char* precision_s=NULL; double units_factor=1.0; double units_bias=0.0; double* val=(double*)cval; double missingValue=9999.0; long constantFieldHalfByte=0; int err=0; if(*len != 0) { if(self->units_factor && (grib_get_double_internal(a->parent->h,self->units_factor,&units_factor)== GRIB_SUCCESS)) { grib_set_double_internal(a->parent->h,self->units_factor,1.0); } if(self->units_bias && (grib_get_double_internal(a->parent->h,self->units_bias,&units_bias)== GRIB_SUCCESS)) { grib_set_double_internal(a->parent->h,self->units_bias,0.0); } if (units_factor != 1.0) { if (units_bias != 0.0) for (i=0;i<n_vals;i++) val[i]=val[i]*units_factor+units_bias; else for (i=0;i<n_vals;i++) val[i]*=units_factor; } else if (units_bias != 0.0) for (i=0;i<n_vals;i++) val[i]+=units_bias; if (c->ieee_packing && self->ieee_packing) { long precision=c->ieee_packing==32 ? 1 : 2; size_t lenstr=strlen(self->ieee_packing); packingType_s=grib_context_strdup(c,self->packingType); ieee_packing_s=grib_context_strdup(c,self->ieee_packing); precision_s=grib_context_strdup(c,self->precision); grib_set_string(h,packingType_s,ieee_packing_s,&lenstr); grib_set_long(h,precision_s,precision); grib_context_free(c,packingType_s); grib_context_free(c,ieee_packing_s); grib_context_free(c,precision_s); return grib_set_double_array(h,"values",val,*len); } } ret = super->pack_double(a,val,len); switch (ret) { case GRIB_CONSTANT_FIELD: ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte); if (ret) constantFieldHalfByte=0; if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; break; case GRIB_NO_VALUES: ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte); if (ret) constantFieldHalfByte=0; /* TODO move to def file */ grib_get_double(a->parent->h,"missingValue", &missingValue); if((err = grib_set_double_internal(a->parent->h,self->reference_value, missingValue)) != GRIB_SUCCESS) return err; if((ret = grib_set_long_internal(a->parent->h,self->binary_scale_factor, binary_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; break; case GRIB_INVALID_BPV: grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n"); return ret; case GRIB_SUCCESS: break; default: grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to compute packing parameters\n"); return ret; } if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->offsetdata,&offsetdata)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->offsetsection,&offsetsection)) != GRIB_SUCCESS) return ret; decimal = grib_power(decimal_scale_factor,10) ; divisor = grib_power(-binary_scale_factor,2); buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); if((buflen + (offsetdata-offsetsection)) %2) { buflen++; /* a->length++; a->parent->h->buffer->ulength++; */ } half_byte = (buflen*8)-((*len)*bits_per_value); grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG, "HALF byte: buflen=%d bits_per_value=%ld len=%d half_byte=%ld\n", buflen,bits_per_value,*len,half_byte); Assert(half_byte <= 0x0f); if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte)) != GRIB_SUCCESS) return ret; buf = (unsigned char*)grib_context_buffer_malloc_clear(a->parent->h->context,buflen); encoded = buf; grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG, "grib_accessor_data_g1simple_packing : pack_double : packing %s, %d values", a->name, n_vals); grib_buffer_replace(a, buf, buflen,1,1); grib_context_buffer_free(a->parent->h->context,buf); return GRIB_SUCCESS; }
static grib_fieldset* grib_fieldset_create_from_keys(grib_context* c,char** keys,int nkeys, int* err) { grib_fieldset* set=0; size_t msize=0,size=0; int i=0; int type=0; int default_type=GRIB_TYPE_STRING; if (!c) c=grib_context_get_default( ); size=GRIB_START_ARRAY_SIZE; msize=sizeof(grib_fieldset); set=(grib_fieldset*)grib_context_malloc_clear(c,msize); if (!set) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_create : Cannot malloc %d bytes",msize); return NULL; } set->context=c; set->fields_array_size=size; set->size=0; set->current=-1; set->fields=0; set->filter=0; set->order=0; set->columns=0; set->where=0; set->order_by=0; set->fields=grib_fieldset_create_fields(set->context,size); set->order=grib_fieldset_create_int_array(c,size); set->filter=grib_fieldset_create_int_array(c,size); for (i=0;i<set->filter->size;i++) set->filter->el[i]=i; set->columns=(grib_column*)grib_context_malloc_clear(c,sizeof(grib_column)*nkeys); if (!set->columns) { grib_context_log(c,GRIB_LOG_ERROR,"grib_fieldset_new_query: memory allocation error"); *err=GRIB_OUT_OF_MEMORY; return NULL; } for (i=0;i<nkeys;i++) { char* key=grib_context_strdup(c,keys[i]); char* p=key; while(*p != ':' && *p != '\0') p++; if (*p==':') { type = grib_type_to_int(*(p+1)); *p='\0'; } else { type = default_type; } *err=grib_fieldset_new_column(set,i,key,type); grib_context_free(c,key); } set->columns_size=nkeys; return set; }
static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_g1complex_packing* self = (grib_accessor_data_g1complex_packing*)a; int ret = GRIB_SUCCESS; long seclen=0; long sub_j= 0; long sub_k= 0; long sub_m= 0; long n= 0; long half_byte= 0; long bits_per_value =0; size_t buflen =0; grib_context* c=a->parent->h->context; grib_handle* h=a->parent->h; char* ieee_packing_s=NULL; char* packingType_s=NULL; char* precision_s=NULL; grib_accessor_class* super = *(a->cclass->super); if (*len ==0) return GRIB_NO_VALUES; if (c->ieee_packing && self->ieee_packing) { long precision=c->ieee_packing==32 ? 1 : 2; size_t lenstr=strlen(self->ieee_packing); packingType_s=grib_context_strdup(c,self->packingType); ieee_packing_s=grib_context_strdup(c,self->ieee_packing); precision_s=grib_context_strdup(c,self->precision); grib_set_string(h,packingType_s,ieee_packing_s,&lenstr); grib_set_long(h,precision_s,precision); grib_context_free(c,packingType_s); grib_context_free(c,ieee_packing_s); grib_context_free(c,precision_s); return grib_set_double_array(h,"values",val,*len); } if((ret = grib_get_long_internal(a->parent->h,self->sub_j,&sub_j)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->sub_k,&sub_k)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->sub_m,&sub_m)) != GRIB_SUCCESS) return ret; self->dirty=1; Assert ((sub_j== sub_k) && (sub_m== sub_j)); ret = super->pack_double(a,val,len); if(ret == GRIB_SUCCESS){ n = a->offset + 4*((sub_k+1)*(sub_k+2)); #if 1 /* Octet number starts from beginning of message but shouldn't */ if((ret = grib_set_long_internal(a->parent->h,self->N,n)) != GRIB_SUCCESS) return ret; #else ret = grib_get_long_internal(a->parent->h,self->offsetsection,&offsetsection); if(ret != GRIB_SUCCESS) return ret; if((ret = grib_set_long_internal(a->parent->h,self->N,n-offsetsection)) != GRIB_SUCCESS) return ret; #endif ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value); if(ret != GRIB_SUCCESS) return ret; ret = grib_get_long_internal(a->parent->h,self->seclen,&seclen); if(ret != GRIB_SUCCESS) return ret; buflen = 32*(sub_k+1)*(sub_k+2)+(*len-(sub_k+1)*(sub_k+2))*bits_per_value+18*8; half_byte = seclen*8-buflen; if (a->parent->h->context->debug==-1) { printf("GRIB_API DEBUG: half_byte=%ld\n",half_byte); } ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte); if(ret != GRIB_SUCCESS) return ret; } return ret; }