static int evaluate_long(grib_expression *g,grib_handle* h,long* lres)
{
    long v1=0;
    long v2=0;
    int ret;
    grib_expression_binop* e = (grib_expression_binop*)g;

#if 0 /* DEBUGGING */
    {
        int typeLeft, typeRight;
        const char* nameLeft;
        const char* nameRight;
        typeLeft = grib_expression_native_type(h, e->left);
        typeRight = grib_expression_native_type(h, e->right);
        nameLeft = grib_expression_get_name(e->left);
        nameRight= grib_expression_get_name(e->right);
        printf("eval_long nameLeft=%s (type=%d), nameRight=%s (type=%d)\n",nameLeft,typeLeft, nameRight,typeRight);
        grib_expression_print(h->context, g, h);
        printf("\n");
    }
#endif

    ret = grib_expression_evaluate_long(h,e->left,&v1);
    if (ret != GRIB_SUCCESS) return ret;

    ret = grib_expression_evaluate_long(h,e->right,&v2);
    if (ret != GRIB_SUCCESS) return ret;

    *lres=e->long_func(v1,v2);
    return GRIB_SUCCESS;
}
static size_t preferred_size(grib_accessor* a,int from_handle)
{
	grib_accessor_padtomultiple* self = (grib_accessor_padtomultiple*)a;
	long padding=0;
	long begin = 0;
	long multiple = 0;

	grib_expression_evaluate_long(a->parent->h,self->begin,&begin);
	grib_expression_evaluate_long(a->parent->h,self->multiple,&multiple);

	padding = a->offset - begin;
	padding = ((padding + multiple - 1)/multiple)*multiple - padding;

	return padding == 0 ? multiple : padding;

}
static size_t preferred_size(grib_accessor* a,int from_handle)
{
	grib_accessor_pad* self = (grib_accessor_pad*)a;

	long length = 0;

	grib_expression_evaluate_long(a->parent->h,self->expression,&length);

	return  length > 0 ? length : 0;
}
static int evaluate_long(grib_expression* g,grib_handle* h,long* lres)
{
	int ret;
	long v=0;
	grib_expression_unop* e = (grib_expression_unop*)g;
	ret = grib_expression_evaluate_long(h,e->exp,&v);
	if (ret != GRIB_SUCCESS) return ret;
	*lres=e->long_func(v);
	return GRIB_SUCCESS;
}
static void init(grib_accessor* a, const long len, grib_arguments* params) {
  int n=0;
  grib_accessor_codetable* self  = (grib_accessor_codetable*)a;
  grib_action* act=(grib_action*)(a->creator);

  self->tablename = grib_arguments_get_string(a->parent->h,params,n++);
  self->masterDir = grib_arguments_get_name(a->parent->h,params,n++);
  self->localDir = grib_arguments_get_name(a->parent->h,params,n++);

  /*if (a->flags & GRIB_ACCESSOR_FLAG_STRING_TYPE)
    printf("-------- %s type string (%ld)\n",a->name,a->flags);*/

  if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) {
    a->length = 0;
	if (!a->vvalue) 
		a->vvalue = grib_context_malloc_clear(a->parent->h->context,sizeof(grib_virtual_value));
    a->vvalue->type=grib_accessor_get_native_type(a);
    a->vvalue->length=len;
    if (act->default_value!=NULL) {
      const char* p = 0;
      size_t len = 1;
      long l;
      int ret=0;
      double d;
      char tmp[1024];
      grib_expression* expression=grib_arguments_get_expression(a->parent->h,act->default_value,0);
      int type = grib_expression_native_type(a->parent->h,expression);
      switch(type) {
        case GRIB_TYPE_DOUBLE:
          grib_expression_evaluate_double(a->parent->h,expression,&d);
          grib_pack_double(a,&d,&len);
          break;

        case GRIB_TYPE_LONG:
          grib_expression_evaluate_long(a->parent->h,expression,&l);
          grib_pack_long(a,&l,&len);
          break;

        default:
          len = sizeof(tmp);
          p = grib_expression_evaluate_string(a->parent->h,expression,tmp,&len,&ret);
          if (ret != GRIB_SUCCESS) {
			  grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,
							   "unable to evaluate %s as string",a->name);
          }
          len = strlen(p)+1;
          pack_string(a,p,&len);
          break;
      }
    }
  } else
    a->length = len;

}
static int unpack_string(grib_accessor*a , char*  v, size_t *len){
    grib_accessor_md5* self = (grib_accessor_md5*)a;
    unsigned mess_len;
    unsigned char* mess;
    unsigned char* p;
    long offset,length;
    grib_string_list* blacklist=NULL;
    grib_accessor* b=NULL;
    int ret=0;
    int i=0;
    struct grib_md5_state md5c;

    if (*len <32 ) {
        grib_context_log(a->context,GRIB_LOG_ERROR,"md5: array too small");
        return GRIB_ARRAY_TOO_SMALL;
    }

    if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->offset,&offset))
            != GRIB_SUCCESS)
        return ret;
  if((ret = grib_expression_evaluate_long(grib_handle_of_accessor(a),self->length,&length))
            != GRIB_SUCCESS)
        return ret;

    mess=(unsigned char*)grib_context_malloc(a->context,length);
    memcpy(mess,grib_handle_of_accessor(a)->buffer->data+offset,length);
    mess_len=length;

    blacklist=a->context->blacklist;
  /* passed blacklist overrides context blacklist. 
     Consider to modify following line to extend context blacklist.
  */
  if (self->blacklist) blacklist=self->blacklist;
    while (blacklist && blacklist->value) {
        b=grib_find_accessor(grib_handle_of_accessor(a),blacklist->value);
        if (!b) {
            grib_context_free(a->context,mess);
            return GRIB_NOT_FOUND;
        }

        p=mess+b->offset-offset;
        for (i=0;i<b->length;i++) *(p++)=0;

        blacklist=blacklist->next;
    }

    grib_md5_init(&md5c);
    grib_md5_add(&md5c,mess,mess_len);
    grib_md5_end(&md5c,v);
    grib_context_free(a->context,mess);

    return ret;
}
static int unpack_long(grib_accessor* a, long* val, size_t *len)
{
  int ret=0;
  grib_accessor_evaluate* self = (grib_accessor_evaluate*)a;
  grib_expression* e = grib_arguments_get_expression(a->parent->h,self->arg,0);


  ret = grib_expression_evaluate_long(a->parent->h,e,val);
  *len = 1;

  return ret;
}
Esempio n. 8
0
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);
}
Esempio n. 9
0
static int create_accessor( grib_section* p, grib_action* act, grib_loader *h)
{
    grib_action_if* a = (grib_action_if*)act;
    grib_action* next = NULL;
    int ret = 0;
    long lres=0;

    grib_accessor* as = NULL;
    grib_section*  gs = NULL;

    as = grib_accessor_factory(p, act,0,NULL);
    if(!as)return GRIB_INTERNAL_ERROR;
    gs = as->sub_section;
    grib_push_accessor(as,p->block);

    if ((ret=grib_expression_evaluate_long(p->h,a->expression,&lres)) != GRIB_SUCCESS)
        return ret;

    if(lres)
        next = a->block_true;
    else
        next = a->block_false;

    if(p->h->context->debug > 1)
    {
        printf("EVALUATE create_accessor_handle ");
        grib_expression_print(p->h->context,a->expression,p->h);
        printf(" [%s][_if%p]\n", (next == a->block_true ? "true":"false"), (void*)a);

        /*grib_dump_action_branch(stdout,next,5);*/
    }

    gs->branch = next;
    grib_dependency_observe_expression(as,a->expression);

    while(next){

        ret = grib_create_accessor(gs, next, h);
        if(ret != GRIB_SUCCESS) return ret;
        next= next->next;
    }

    return GRIB_SUCCESS;
}
Esempio n. 10
0
static grib_action* reparse(grib_action* a,grib_accessor* acc,int* doit)
{
    int ret=0;
    long lres=0;
    grib_action_if* self = (grib_action_if*)a;

    /* printf("reparse %s %s\n",a->name,acc->name); */

    if((ret=grib_expression_evaluate_long(acc->parent->h,self->expression,&lres)) != GRIB_SUCCESS)
        grib_context_log(acc->parent->h->context,
                GRIB_LOG_ERROR,"if reparse  grib_expression_evaluate_long %s",
                grib_get_error_message(ret));

    if(lres)
        return self->block_true;
    else
        return self->block_false;

}
static int pack_expression(grib_accessor* a, grib_expression *e){
  const char* cval;
  int ret=0;
  long lval=0;
  size_t len = 1;
  char tmp[1024];

  if (strcmp(e->cclass->name,"long")==0) {
    ret=grib_expression_evaluate_long(a->parent->h,e,&lval);
    ret = grib_pack_long(a,&lval,&len);
  } else {
    len = sizeof(tmp);
    cval = grib_expression_evaluate_string(a->parent->h,e,tmp,&len,&ret);
    if (ret!=GRIB_SUCCESS) {
      grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"grib_accessor_codetable.pack_expression: unable to evaluate string %s to be set in %s\n",grib_expression_get_name(e),a->name);
	  return ret;
    }
    len = strlen(cval) + 1;
    ret = grib_pack_string(a,cval,&len);
  }
  return ret;
}
Esempio n. 12
0
static int execute(grib_action* act, grib_handle *h)
{
    grib_action_if* a = (grib_action_if*)act;
    grib_action* next = NULL;
    int ret = 0;
    long lres=0;

    /* See GRIB-394 */
    int type = grib_expression_native_type(h, a->expression);
    if (type != GRIB_TYPE_DOUBLE) {
        if ((ret=grib_expression_evaluate_long(h,a->expression,&lres)) != GRIB_SUCCESS) {
            if (ret == GRIB_NOT_FOUND) lres=0;
            else return ret;
        }
    }
    else {
        double dres = 0.0;
        ret = grib_expression_evaluate_double(h, a->expression, &dres);
        lres = (long)dres;
        if ( ret != GRIB_SUCCESS ) {
            if (ret == GRIB_NOT_FOUND) lres=0;
            else return ret;
        }
    }

    if(lres)
        next = a->block_true;
    else
        next = a->block_false;

    while(next){
        ret = grib_action_execute(next, h);
        if(ret != GRIB_SUCCESS) return ret;
        next= next->next;
    }

    return GRIB_SUCCESS;
}
Esempio n. 13
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  grib_rule* r = options->rules;
  int i;
  int err=0;
  double scale = 1;
  double offset = 0;

  int set = options->set_values_count;

  while(r) {
    if(r->condition)  {
      long ok = 0;
      GRIB_CHECK_NOLINE(grib_expression_evaluate_long(h,r->condition,&ok),NULL);
      if(ok) {
        grib_rule_entry* e = r->entries;
        while(e) {
          if (!strcmp(e->name,"skip")) return 0;
          Assert(set < (sizeof(options->set_values)/sizeof(options->set_values[0])));
          options->set_values[set].name = e->name;
          options->set_values[set].error=grib_expression_set_value(h,
                                              e->value,&(options->set_values[set]));
          set++;
          e = e->next;
        }

      }
    }
    r = r->next;
  }

  if(options->verbose)
  {
    printf("Applying %d:\n",options->infile->handle_count);
    grib_print_values(options->set_values, set);
    printf("\n");
  }

  err=grib_set_values(h,options->set_values, set);

  if( err != GRIB_SUCCESS) {
      for(i = 0; i < options->set_values_count ; i++) {
        if(options->set_values[i].error != GRIB_SUCCESS &&
           options->set_values[i].error != GRIB_CONCEPT_NO_MATCH && options->fail)
           grib_context_log(h->context,GRIB_LOG_ERROR,"unable to set key \"%s\" (%s)",
              options->set_values[i].name,grib_get_error_message( options->set_values[i].error));
      }
  }

  grib_get_double(h,"scaleValuesBy",&scale);
  grib_get_double(h,"offsetValuesBy",&offset);

  if(scale != 1 || offset != 0)
  {
    double *v;
    size_t size;
    int i;

    GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);

    v    = (double*)calloc(size,sizeof(double));
    if(!v) {
      fprintf(stderr,"failed to allocate %ld bytes\n",(long)(size*sizeof(double)));
      exit(1);
    }

    GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);

    if(options->verbose)
    {
      printf("Applying scale=%g/offset=%g\n",scale,offset);
    }

    for(i = 0; i< size; i++)
      v[i] =  v[i] * scale + offset;

    GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
    free(v);
  }

  grib_tools_write_message(options,h);

  return 0;
}
static int pack_string(grib_accessor* a, const char* buffer, size_t *len)
{
  grib_accessor_codetable* self = (grib_accessor_codetable*)a;
  grib_codetable*          table ;

  long i;
  size_t size = 1;

  typedef int (*cmpproc)(const char*, const char*);

  cmpproc cmp = a->flags | GRIB_ACCESSOR_FLAG_LOWERCASE ? strcasecmp : strcmp;

  if(!self->table) self->table = load_table(self);
  table=self->table;

  if(!table)
    return GRIB_ENCODING_ERROR;

  if (a->set) {
	  int err=grib_set_string(a->parent->h,a->set,buffer,len);
	  if (err!=0) return err;
  }

  for(i = 0 ; i < table->size; i++)
    if(table->entries[i].abbreviation)
		  if(cmp(table->entries[i].abbreviation,buffer) == 0)
        return grib_pack_long(a,&i,&size);

  if (a->flags & GRIB_ACCESSOR_FLAG_NO_FAIL) {
	  grib_action* act=(grib_action*)(a->creator);
	  if (act->default_value!=NULL) {
		  const char* p = 0;
		  size_t len = 1;
		  long l;
		  int ret=0;
		  double d;
		  char tmp[1024];
		  grib_expression* expression=grib_arguments_get_expression(a->parent->h,act->default_value,0);
		  int type = grib_expression_native_type(a->parent->h,expression);
		  switch(type) {
			  case GRIB_TYPE_DOUBLE:
				  grib_expression_evaluate_double(a->parent->h,expression,&d);
				  grib_pack_double(a,&d,&len);
				  break;

			  case GRIB_TYPE_LONG:
				  grib_expression_evaluate_long(a->parent->h,expression,&l);
				  grib_pack_long(a,&l,&len);
				  break;

			  default:
				  len = sizeof(tmp);
				  p = grib_expression_evaluate_string(a->parent->h,expression,tmp,&len,&ret);
				  if (ret != GRIB_SUCCESS) {
					  grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,
									   "unable to evaluate %s as string",a->name);
					  return ret;
				  }
				  len = strlen(p)+1;
				  pack_string(a,p,&len);
				  break;
		  }
		  return GRIB_SUCCESS;
	  }
	
  }
  return GRIB_ENCODING_ERROR;
}
Esempio n. 15
0
static int execute(grib_action* act, grib_handle *h) {
    grib_action_switch* a = (grib_action_switch*)act;
    grib_case* c=a->Case;
    grib_action* next = a->Default;
    grib_arguments* args=a->args;
    grib_arguments* values;
    grib_expression* e;
    grib_expression* value;
    int ret = 0;
    long lres=0;
    double dres=0;
    long lval=0;
    double dval=0;
    int type=0;
    int  ok=0;
    const char *cval;
    const char *cres;
    char buf[80];
    char tmp[80];
    size_t len = sizeof(buf);
    size_t size=sizeof(tmp);
    int err=0;

    Assert(args);

    while (c) {
        e=args->expression;
        values=c->values;
        value=values->expression;
        ok=0;
        while (e && value) {
            if (!strcmp(value->cclass->name,"true"))  ok=1;
            else {
                type=grib_expression_native_type(h,value);

                switch(type) {
                case GRIB_TYPE_LONG:
                    ok= ( grib_expression_evaluate_long(h,value,&lres) == GRIB_SUCCESS) &&
                    ( grib_expression_evaluate_long(h,e,&lval) == GRIB_SUCCESS) &&
                    (lval == lres);
                    break;

                case GRIB_TYPE_DOUBLE:
                    ok = ( grib_expression_evaluate_double(h,value,&dres) == GRIB_SUCCESS) &&
                    ( grib_expression_evaluate_double(h,e,&dval) == GRIB_SUCCESS) &&
                    (dval == dres);
                    break;

                case GRIB_TYPE_STRING:
                    len = sizeof(buf);
                    size=sizeof(tmp);
                    ok = ((cres=grib_expression_evaluate_string(h,e,buf,&len,&err)) != NULL ) &&
                            (err==0) && ((cval = grib_expression_evaluate_string(h,value,tmp,&size,&err)) != NULL) &&
                            (err==0) && ((strcmp(buf,cval) == 0) || (strcmp(cval,"*")==0));
                    break;

                default:
                    /* TODO: */
                    break;
                }
            }
            if (!ok) break;

            args=args->next;
            if (args) e=args->expression;
            else e=NULL;

            values=values->next;
            if (values) value=values->expression;
            else value=NULL;
        }

        if(ok) { next=c->action; break; }

        c=c->next;
    }

    if (!next) return GRIB_SWITCH_NO_MATCH;

    while(next){
        ret = grib_action_execute(next, h);
        if(ret != GRIB_SUCCESS) return ret;
        next= next->next;
    }

    return GRIB_SUCCESS;
}