static int create_accessor(grib_section* p, grib_action* act, grib_loader *h ){ int ret = GRIB_SUCCESS; grib_action_template* a = ( grib_action_template*)act; grib_action* la = NULL; grib_action* next = NULL; grib_accessor* as = NULL; grib_section* gs = NULL; char fname[1024]={0,}; char *fpath=0; as = grib_accessor_factory(p, act,0,NULL); if(!as) return GRIB_INTERNAL_ERROR; if(a->arg){ ret = grib_recompose_name(p->h,as,a->arg,fname,1); if ((fpath=grib_context_full_defs_path(p->h->context,fname))==NULL) { if (!a->nofail) { grib_context_log(p->h->context,GRIB_LOG_ERROR, "Unable to find template %s from %s ",act->name,fname); return GRIB_FILE_NOT_FOUND; } la = get_empty_template(p->h->context,&ret); if (ret) return ret; } else la = grib_parse_file(p->h->context, fpath); } as->flags |= GRIB_ACCESSOR_FLAG_HIDDEN; gs = as->sub_section; gs->branch = la; /* Will be used to prevent unecessary reparse */ grib_push_accessor(as,p->block); if(la){ next = la; while(next){ ret = grib_create_accessor(gs, next,h); if(ret != GRIB_SUCCESS) { if(p->h->context->debug) { grib_context_log(p->h->context,GRIB_LOG_ERROR, "Error processing template %s: %s [%s] %04lx", fname,grib_get_error_message(ret),next->name,next->flags); } return ret; } next= next->next; } } return GRIB_SUCCESS; }
static void dump(grib_accessor* a, grib_dumper* dumper) { grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; long v; char flagname[1024]; char fname[1024]; size_t llen = 1; grib_recompose_name(a->parent->h,NULL, self->tablename, fname,1); grib_unpack_long(a, &v, &llen); grib_get_codeflag(a, v, flagname); grib_dump_bits(dumper,a,flagname); }
static grib_action* reparse(grib_action* a,grib_accessor* acc,int *doit) { grib_action_template* self = (grib_action_template*)a; char *fpath=0; if(self->arg){ char fname[1024]; grib_recompose_name(acc->parent->h,NULL,self->arg,fname,1); if ((fpath=grib_context_full_defs_path(acc->parent->h->context,fname))==NULL) { if (!self->nofail) { grib_context_log(acc->parent->h->context,GRIB_LOG_ERROR, "Unable to find template %s from %s ",a->name,fname); return NULL; } return a; } /* printf("REPARSE %s\n",fpath); */ return grib_parse_file(acc->parent->h->context, fpath); } return NULL; }
static int execute(grib_action* act, grib_handle *h) { grib_action_write* a = (grib_action_write*) act; int err = GRIB_SUCCESS; size_t size; const void* buffer = NULL; const char* filename = NULL; char string[1024] = { 0, }; grib_file* of = NULL; if ((err = grib_get_message(h, &buffer, &size)) != GRIB_SUCCESS) { grib_context_log(act->context, GRIB_LOG_ERROR,"unable to get message\n"); return err; } if (strlen(a->name) != 0) { err = grib_recompose_name(h, NULL, a->name, string, 0); filename = string; } else { if (act->context->outfilename) { filename = act->context->outfilename; err = grib_recompose_name(h, NULL, act->context->outfilename, string, 0); if (!err) filename = string; } else { filename = "filter.out"; } } Assert(filename); if (a->append) of = grib_file_open(filename, "a", &err); else of = grib_file_open(filename, "w", &err); if (!of || !of->handle) { grib_context_log(act->context, GRIB_LOG_ERROR,"unable to open file %s\n", filename); return GRIB_IO_PROBLEM; } if (h->gts_header) { if (fwrite(h->gts_header, 1, h->gts_header_len, of->handle) != h->gts_header_len) { grib_context_log(act->context, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "Error writing GTS header to %s", filename); return GRIB_IO_PROBLEM; } } if (fwrite(buffer, 1, size, of->handle) != size) { grib_context_log(act->context, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "Error writing to %s", filename); return GRIB_IO_PROBLEM; } if (a->padtomultiple) { char* zeros; size_t padding = a->padtomultiple - size % a->padtomultiple; /* printf("XXX padding=%d size=%d padtomultiple=%d\n",padding,size,a->padtomultiple); */ zeros = (char*)calloc(padding, 1); Assert(zeros); if (fwrite(zeros, 1, padding, of->handle) != padding) { grib_context_log(act->context, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "Error writing to %s", filename); free(zeros); return GRIB_IO_PROBLEM; } free(zeros); } if (h->gts_header) { char gts_trailer[4] = { '\x0D', '\x0D', '\x0A', '\x03' }; if (fwrite(gts_trailer, 1, 4, of->handle) != 4) { grib_context_log(act->context, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "Error writing GTS trailer to %s", filename); return GRIB_IO_PROBLEM; } } grib_file_close(filename, 0, &err); if (err != GRIB_SUCCESS) { grib_context_log(act->context, GRIB_LOG_ERROR,"unable to write message\n"); return err; } return err; }
static grib_trie* load_dictionary(grib_context* c,grib_accessor* a, int* err) { grib_accessor_dictionary* self = (grib_accessor_dictionary*)a; char* filename=NULL; char line[1024]={0,}; char key[1024]={0,}; char masterDir[1024]={0,}; char localDir[1024]={0,}; char dictName[1024]={0,}; char *localFilename=0; char* list=0; size_t len=1024; grib_trie* dictionary=NULL; FILE* f=NULL; int i=0; grib_handle* h=grib_handle_of_accessor(a); *err=GRIB_SUCCESS; len=1024; if (self->masterDir != NULL) grib_get_string(h,self->masterDir,masterDir,&len); len=1024; if (self->localDir != NULL) grib_get_string(h,self->localDir,localDir,&len); if (*masterDir!=0) { char name[1024]={0,}; char recomposed[1024]={0,}; sprintf(name,"%s/%s",masterDir,self->dictionary); grib_recompose_name(h, NULL,name, recomposed,0); filename=grib_context_full_defs_path(c,recomposed); } else { filename=grib_context_full_defs_path(c,self->dictionary); } if (*localDir!=0) { char localName[1024]={0,}; char localRecomposed[1024]={0,}; sprintf(localName,"%s/%s",localDir,self->dictionary); grib_recompose_name(h, NULL,localName, localRecomposed,0); localFilename=grib_context_full_defs_path(c,localRecomposed); sprintf(dictName,"%s:%s",localFilename,filename); } else { sprintf(dictName,"%s",filename); } if (!filename) { grib_context_log(c,GRIB_LOG_ERROR,"unable to find def file %s",self->dictionary); *err=GRIB_FILE_NOT_FOUND; return NULL; } else { grib_context_log(c,GRIB_LOG_DEBUG,"found def file %s",filename); } dictionary=(grib_trie*)grib_trie_get(c->lists,dictName); if (dictionary) { grib_context_log(c,GRIB_LOG_DEBUG,"using dictionary %s from cache",self->dictionary); return dictionary; } else { grib_context_log(c,GRIB_LOG_DEBUG,"using dictionary %s from file %s",self->dictionary,filename); } f=codes_fopen(filename,"r"); if (!f) {*err=GRIB_IO_PROBLEM; return NULL;} dictionary=grib_trie_new(c); while(fgets(line,sizeof(line)-1,f)) { i=0; while (line[i] != '|' && line[i] != 0) { key[i]=line[i]; i++; } key[i]=0; list=(char*)grib_context_malloc_clear(c,strlen(line)+1); memcpy(list,line,strlen(line)); grib_trie_insert(dictionary,key,list); } fclose(f); if (localFilename!=0) { f=codes_fopen(localFilename,"r"); if (!f) {*err=GRIB_IO_PROBLEM; return NULL;} while(fgets(line,sizeof(line)-1,f)) { i=0; while (line[i] != '|' && line[i] != 0) { key[i]=line[i]; i++; } key[i]=0; list=(char*)grib_context_malloc_clear(c,strlen(line)+1); memcpy(list,line,strlen(line)); grib_trie_insert(dictionary,key,list); } fclose(f); } grib_trie_insert(c->lists,filename,dictionary); return dictionary; }
static grib_codetable* load_table(grib_accessor_codetable* self) { size_t size = 0; grib_handle* h = ((grib_accessor*)self)->parent->h; grib_context* c = h->context; grib_codetable* t = NULL; grib_codetable* next=NULL ; grib_accessor* a=(grib_accessor*)self; char *filename=0; char name[1024]={0,}; char recomposed[1024]={0,}; char localRecomposed[1024]={0,}; char *localFilename=0; char localName[1024]={0,}; char masterDir[1024]={0,}; char localDir[1024]={0,}; size_t len=1024; if (self->masterDir != NULL) grib_get_string(h,self->masterDir,masterDir,&len); len=1024; if (self->localDir != NULL) grib_get_string(h,self->localDir,localDir,&len); if (*masterDir!=0) { sprintf(name,"%s/%s",masterDir,self->tablename); grib_recompose_name(h, NULL,name, recomposed,0); filename=grib_context_full_path(c,recomposed); } else { grib_recompose_name(h, NULL,self->tablename, recomposed,0); filename=grib_context_full_path(c,recomposed); } if (*localDir!=0) { sprintf(localName,"%s/%s",localDir,self->tablename); grib_recompose_name(h, NULL,localName, localRecomposed,0); localFilename=grib_context_full_path(c,localRecomposed); } next=c->codetable; while(next) { if((filename && next->filename[0] && strcmp(filename,next->filename[0]) == 0) && ((localFilename==0 && next->filename[1]==NULL) || ((localFilename!=0 && next->filename[1]!=NULL) && strcmp(localFilename,next->filename[1]) ==0)) ) return next; next = next->next; } if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) { Assert(a->vvalue!=NULL); size=a->vvalue->length*8; } else { size = grib_byte_count((grib_accessor*)self) * 8; } size = grib_power(size,2); t = (grib_codetable*)grib_context_malloc_clear_persistent(c,sizeof(grib_codetable) + (size-1)*sizeof(code_table_entry)); if (filename!=0) grib_load_codetable(c,filename,recomposed,size,t); if (localFilename!=0) grib_load_codetable(c,localFilename,localRecomposed,size,t); if (t->filename[0]==NULL && t->filename[1]==NULL) { grib_context_free_persistent(c,t); return NULL; } return t; }
static int grib_get_codeflag(grib_accessor* a, long code, char* codename) { grib_accessor_codeflag* self = (grib_accessor_codeflag*)a; FILE* f = NULL; char fname[1024]; char bval[50]; char num[50]; char* filename=0; char line[1024]; size_t i =0; int j =0; grib_recompose_name(a->parent->h,NULL, self->tablename, fname,1); if ((filename=grib_context_full_defs_path(a->parent->h->context,fname))==NULL) { grib_context_log(a->parent->h->context,GRIB_LOG_WARNING,"Cannot open flag table %s",filename); strcpy(codename, "Cannot open flag table"); return GRIB_FILE_NOT_FOUND; } f=fopen(filename, "r"); if (!f) { grib_context_log(a->parent->h->context,(GRIB_LOG_WARNING)|(GRIB_LOG_PERROR),"Cannot open flag table %s",filename); strcpy(codename, "Cannot open flag table"); return GRIB_FILE_NOT_FOUND; } #if 0 strcpy(codename, self->tablename); strcat(codename,": "); j = strlen(codename); #endif while(fgets(line,sizeof(line)-1,f)) { sscanf(line,"%49s %49s", num, bval); if(num[0] != '#') { if((test_bit(code, a->length*8-atol(num))>0) == atol(bval)) { size_t linelen = strlen(line); codename[j++] = '('; codename[j++] = num[0]; codename[j++] = '='; codename[j++] = bval[0]; codename[j++] = ')'; codename[j++] = ' '; if(j) codename[j++] = ' '; for(i=(strlen(num)+strlen(bval)+2); i < linelen-1;i++) codename[j++] = line[i]; if(line[i]!='\n') codename[j++] = line[i]; codename[j++] = ';'; } } } if(j>1 && codename[j-1] == ';') j--; codename[j] = 0; strcat(codename,":"); strcat(codename,self->tablename); fclose(f); return GRIB_SUCCESS; }