Exemple #1
0
grib_rule* grib_new_rule(grib_context* c,grib_expression* condition,grib_rule_entry* entries)
{
	grib_rule* r = (grib_rule*)grib_context_malloc_clear_persistent(c,sizeof(grib_rule));
	r->condition = condition;
	r->entries   = entries;
	return r;
}
grib_action* grib_action_create_set_darray( grib_context* context,
  const char* name,
    grib_darray* darray)
{
  char buf[1024];

  grib_action_set_darray* a ;
  grib_action_class* c   = grib_action_class_set_darray;
  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_set_darray*)act;
  act->context      = context;

  a->darray  = darray;
  a->name        = grib_context_strdup_persistent(context,name);


  sprintf(buf,"set_darray%p",(void*)darray);

  act->name      = grib_context_strdup_persistent(context,buf);

  return act;
}
grib_expression* new_string_expression(grib_context* c,const char* value)
{
	grib_expression_string* e = grib_context_malloc_clear_persistent(c,sizeof(grib_expression_string));
	e->base.cclass                 = grib_expression_class_string;
	e->value               = grib_context_strdup_persistent(c,value);
	return (grib_expression*)e;
}
Exemple #4
0
grib_rule_entry *grib_new_rule_entry(grib_context* c,const char* name,grib_expression* expression)
{
	grib_rule_entry* e = (grib_rule_entry*)grib_context_malloc_clear_persistent(c,sizeof(grib_rule_entry));
	e->name            = grib_context_strdup_persistent(c,name);
	e->value           = expression;
	return e;
}
grib_expression* new_accessor_expression(grib_context* c,const char *name)
{
  grib_expression_accessor* e = grib_context_malloc_clear_persistent(c,sizeof(grib_expression_accessor));
  e->base.cclass                 = grib_expression_class_accessor;
  e->name                   = grib_context_strdup_persistent(c,name);
  return (grib_expression*)e;
}
grib_expression* new_double_expression(grib_context* c,double value)
{
	grib_expression_double* e = grib_context_malloc_clear_persistent(c,sizeof(grib_expression_double));
	e->base.cclass  = grib_expression_class_double;
	e->value = value;
	return (grib_expression*)e;
}
grib_action* grib_action_create_if( grib_context* context,
        grib_expression* expression,
        grib_action* block_true,grib_action* block_false,int transient)
{
    char name[1024];
    grib_action_if* a ;
    grib_action_class* c   = grib_action_class_if;
    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_if*)act;
    act->context      = context;

    a->expression  = expression;
    a->block_true  = block_true;
    a->block_false = block_false;
    a->transient   = transient;

    if (transient)
        sprintf(name,"__if%p",(void*)a);
    else
        sprintf(name,"_if%p",(void*)a);

    act->name      = grib_context_strdup_persistent(context,name);

    return act;
}
grib_action* grib_action_create_print( grib_context* context, const char* name,char* outname)
{
  char buf[1024];

  grib_action_print* a ;
  grib_action_class* c   = grib_action_class_print;
  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_print*)act;
  act->context           = context;

  a->name            = grib_context_strdup_persistent(context,name);

  if (outname) {
    FILE* out=NULL;
    int ioerr=0;
    a->outname      = grib_context_strdup_persistent(context,outname);
    out=fopen(outname,"w");
    ioerr=errno;
    if (!out)   {
      grib_context_log(act->context,(GRIB_LOG_ERROR)|(GRIB_LOG_PERROR),
        "IO ERROR: %s: %s",strerror(ioerr),outname);
    }
    if (out) fclose(out);
  }

  sprintf(buf,"print%p",(void*)a->name);

  act->name      = grib_context_strdup_persistent(context,buf);

  return act;
}
grib_action* grib_action_create_gen(grib_context* context, const char* name, const char* op, const long len,
        grib_arguments* params,  grib_arguments* default_value,int flags,const char* name_space,const char* set)
{
    grib_action_gen* a     =  NULL;
    grib_action_class* c   =  grib_action_class_gen;
    grib_action* act       =  (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
    act->next              =  NULL;
    act->name              =  grib_context_strdup_persistent(context, name);
    act->op                =  grib_context_strdup_persistent(context, op);
    if(name_space)
        act->name_space        =  grib_context_strdup_persistent(context, name_space);
    act->cclass            =  c;
    act->context           =  context;
    act->flags             =  flags;
    a                      =  (grib_action_gen*)act;

    a->len                 =  len;

    a->params              =  params;
    if (set)
        act->set				=	grib_context_strdup_persistent(context, set);
    act->default_value       =  default_value;

    return act;
}
grib_action* grib_action_create_set( grib_context* context,
  const char* name,    grib_expression* expression,int nofail)
{
  char buf[1024];

  grib_action_set* a ;
  grib_action_class* c   = grib_action_class_set;
  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_set*)act;
  act->context      = context;

  a->expression  = expression;
  a->name        = grib_context_strdup_persistent(context,name);
  a->nofail      = nofail;


  sprintf(buf,"set%p",(void*)expression);

  act->name      = grib_context_strdup_persistent(context,buf);

  return act;
}
grib_expression* new_is_in_list_expression(grib_context* c,const char* name,const char* list)
{
  grib_expression_is_in_list* e = (grib_expression_is_in_list*)grib_context_malloc_clear_persistent(c,sizeof(grib_expression_is_in_list));
  e->base.cclass            = grib_expression_class_is_in_list;
  e->name                   = grib_context_strdup_persistent(c,name);
  e->list                   = grib_context_strdup_persistent(c,list);
  return (grib_expression*)e;
}
grib_expression* new_string_compare_expression(grib_context* c,
  grib_expression* left,grib_expression* right)
{
  grib_expression_string_compare* e = grib_context_malloc_clear_persistent(c,sizeof(grib_expression_string_compare));
  e->base.cclass                 = grib_expression_class_string_compare;
  e->left                = left;
  e->right               = right;
  return (grib_expression*)e;
}
grib_concept_index* grib_concept_index_new_from_concept(grib_context* c,grib_concept_value* concept,int *err) {
	grib_concept_index* index;

	if (!c) c=grib_context_get_default();

	index=grib_context_malloc_clear_persistent(c,sizeof(grib_concept_index));
	index->keys=grib_context_malloc_clear_persistent(c,sizeof(grib_concept_index_key));
	index->conditions=grib_context_malloc_clear_persistent(c,sizeof(grib_conditions_tree));
	index->conditions=grib_context_malloc_clear_persistent(c,sizeof(grib_conditions_tree));
	index->context=c;

	while (concept) {
		index_add_conditions(index,concept->conditions,err);
		concept=concept->next;
	}

	return index;
}
grib_case* grib_case_new(grib_context* c,grib_arguments* values,grib_action* action)
{
    grib_case* Case = (grib_case*)grib_context_malloc_clear_persistent(c,sizeof(grib_case));

    Case->values=values;
    Case->action=action;

    return Case;
}
Exemple #15
0
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;
}
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;
}
Exemple #17
0
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;
}
grib_expression* new_unop_expression(grib_context* c,
	grib_unop_long_proc long_func,
	grib_unop_double_proc double_func,
	grib_expression* exp)
{
	grib_expression_unop* e = grib_context_malloc_clear_persistent(c,sizeof(grib_expression_unop));
	e->base.cclass                 = grib_expression_class_unop;
	e->exp                = exp;
	e->long_func          = long_func;
	e->double_func         = double_func;
	return (grib_expression*)e;
}
grib_expression* new_binop_expression(grib_context* c,
        grib_binop_long_proc  long_func,
        grib_binop_double_proc double_func,
        grib_expression* left,grib_expression* right)
{
    grib_expression_binop* e = (grib_expression_binop*)grib_context_malloc_clear_persistent(c,sizeof(grib_expression_binop));
    e->base.cclass                 = grib_expression_class_binop;
    e->left                = left;
    e->right               = right;
    e->long_func            = long_func;
    e->double_func          = double_func;
    return (grib_expression*)e;
}
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);
}
Exemple #21
0
grib_action* grib_action_create_put( grib_context* context, const char* name ,grib_arguments *args )
{
	grib_action_put*  a    =  NULL;
	grib_action_class* c   =  grib_action_class_put;
	grib_action* act       =  (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
	act->next              =  NULL;
	act->name              =  grib_context_strdup_persistent(context,name);
	act->op                =  grib_context_strdup_persistent(context,"forward");
	act->cclass            =  c;
	act->context           =  context;
	a                      =  (grib_action_put*)act; 
	a->args                =  args;    
	return act;
}
grib_action* grib_action_create_template( grib_context* context,int nofail,const char* name , const char* arg1)
{
  grib_action_template* a   ;
  grib_action_class* c   =  grib_action_class_template;
  grib_action* act       =  (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
  act-> name             =  grib_context_strdup_persistent(context,name);
  act-> op               =  grib_context_strdup_persistent(context,"section");
  act-> cclass           =  c;
  act-> next             =  NULL;
  act->context           =  context;
  a                      =  (grib_action_template*)act;
  a->nofail=nofail;
  if (arg1) a->arg       =  grib_context_strdup_persistent(context,arg1);
  else  a->arg = NULL;

  return act;
}
grib_action* grib_action_create_noop( grib_context* context,const char* fname)
{
    char buf[1024];

    grib_action_noop* a ;
    grib_action_class* c   = grib_action_class_noop;
    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_noop*)act;
    act->context      = context;

    sprintf(buf,"_noop%p",(void*)a);

    act->name      = grib_context_strdup_persistent(context,buf);

    return act;
}
Exemple #24
0
grib_context* grib_context_new(grib_context* parent)
{
	grib_context* c;
#if GRIB_PTHREADS
	pthread_mutexattr_t attr;
#endif

	if (!parent) parent=grib_context_get_default();

	GRIB_PTHREAD_ONCE(&once,&init);
	GRIB_MUTEX_LOCK(&(parent->mutex));

	c = (grib_context*)grib_context_malloc_clear_persistent(&default_grib_context,sizeof(grib_context));

	c->inited              = default_grib_context.inited;
	c->debug               = default_grib_context.debug;

	c->real_mode           = default_grib_context.real_mode;

	c->free_mem            = default_grib_context.free_mem;
	c->alloc_mem           = default_grib_context.alloc_mem;

	c->free_persistent_mem = default_grib_context.free_persistent_mem;
	c->alloc_persistent_mem= default_grib_context.alloc_persistent_mem;

	c->read                = default_grib_context.read;
	c->write               = default_grib_context.write;
	c->tell                = default_grib_context.tell;

	c->output_log          = default_grib_context.output_log;
	c->print               = default_grib_context.print    ;
	c->user_data           = default_grib_context.user_data;
	c->def_files           = default_grib_context.def_files;
	
#if GRIB_PTHREADS
	pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&mutex_c,&attr);
	pthread_mutexattr_destroy(&attr);
#endif

	GRIB_MUTEX_UNLOCK(&(parent->mutex));
	return c;
}
grib_action* grib_action_create_alias(grib_context* context, const char* name, const char* arg1,const char* name_space,int flags)
{
    grib_action_alias* a                            ;
    grib_action_class* c   =  grib_action_class_alias;
    grib_action* act       = (grib_action*)grib_context_malloc_clear_persistent(context,c->size);

    act->context = context;

    act->op           = NULL;
    act->name         = grib_context_strdup_persistent(context, name);
    if(name_space)
        act->name_space =  grib_context_strdup_persistent(context, name_space);

    act->cclass       = c;
    act->flags        = flags;
    a                 = (grib_action_alias*)act;
    a->target         = arg1 ? grib_context_strdup_persistent(context, arg1) : NULL;

    return act;
}
grib_action *grib_action_create_trigger(grib_context *context, grib_arguments *args, grib_action *block)
{
	char name[1024];

	grib_action_trigger* a = 0;
	grib_action_class* c   =  grib_action_class_trigger;
	grib_action* act       =  (grib_action*)grib_context_malloc_clear_persistent(context,c->size);

	sprintf(name,"_trigger%p",(void*)act);

	act-> name             =  grib_context_strdup_persistent(context,name);
	act-> op               =  grib_context_strdup_persistent(context,"section");
	act-> cclass           =  c;
	act-> next             =  NULL;
	act->context           =  context;

	a = ( grib_action_trigger*)act;
	a->trigger_on        = args;
	a->block             = block;

	return act;
}
grib_action* grib_action_create_modify( grib_context* context, 
	const char* name,
		long flags)
{  
	
	grib_action_modify* a ;
	grib_action_class* c   = grib_action_class_modify;
	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_modify*)act;
	act->context      = context;

	a->flags       = flags;
	a->name        = grib_context_strdup_persistent(context,name);


	act->name      = grib_context_strdup_persistent(context,"flags");

	return act;
}
grib_action* grib_action_create_variable( grib_context* context, const char* name, const char* op, const long len,  grib_arguments* params,  grib_arguments* default_value,int flags,const char* name_space)
{
	grib_action_variable* a     =  NULL;
	grib_action_class* c   =  grib_action_class_variable;
	grib_action* act       =  (grib_action*)grib_context_malloc_clear_persistent(context,c->size);
	act->next              =  NULL;
	act->name              =  grib_context_strdup_persistent(context, name);
	if(name_space)
	act->name_space              =  grib_context_strdup_persistent(context, name_space);
	act->op                =  grib_context_strdup_persistent(context, op);
	act->cclass            =  c;
	act->context           =  context;
	act->flags             =  flags;
	a                      =  (grib_action_variable*)act;
	a->len                 =  len;
	a->params              =  params;
	act->default_value       =  default_value;

	/* printf("CREATE %s\n",name); */

	return act;
}
grib_action* grib_action_create_meta( grib_context* context, const char* name, const char* op, 
		grib_arguments*   params,  grib_arguments*   default_value,unsigned long flags,const char* name_space)
{
	grib_action_meta*  a   =  (grib_action_meta*)grib_context_malloc_clear_persistent(context,sizeof(grib_action_meta));
	grib_action* act       =  (grib_action*)a;
	act->next              =  NULL;
	act->name              =  grib_context_strdup_persistent(context, name);
	act->op                =  grib_context_strdup_persistent(context, op);
	if(name_space)
	act->name_space                =  grib_context_strdup_persistent(context, name_space);
	act->cclass            =  grib_action_class_meta;
	act->context           = context;
	act->flags             = flags;
	a->params              =  params;
	act->default_value                = default_value;
	a->len                 = 0;
	

	/* grib_arguments_print(context,a->params,0); printf("\n"); */


	return act;
}
grib_action* grib_action_create_write( grib_context* context, const char* name,int append,int padtomultiple)
{
    char buf[1024];

    grib_action_write* a =NULL;
    grib_action_class* c   = grib_action_class_write;
    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_write*)act;
    act->context           = context;

    a->name            = grib_context_strdup_persistent(context,name);

    sprintf(buf,"write%p",(void*)a->name);

    act->name      = grib_context_strdup_persistent(context,buf);
    a->append=append;
    a->padtomultiple=padtomultiple;

    return act;
}