File: action_class_if.c

package info (click to toggle)
grib-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,008 kB
  • ctags: 10,015
  • sloc: ansic: 63,157; sh: 9,355; f90: 2,545; makefile: 2,496; yacc: 519; perl: 240; lex: 217
file content (274 lines) | stat: -rw-r--r-- 7,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Copyright 2005-2007 ECMWF
*
* Licensed under the GNU Lesser General Public License which
* incorporates the terms and conditions of version 3 of the GNU
* General Public License.
* See LICENSE and gpl-3.0.txt for details.
*/

/***************************************************************************
 *   Jean Baptiste Filippi - 01.11.2005                                                           *
 *   Enrico Fucile
 *                                                                         *
 ***************************************************************************/
#include "grib_api_internal.h"
/*
   This is used by make_class.pl

   START_CLASS_DEF
   CLASS      = action
   SUPER      = action_class_section
   IMPLEMENTS = create_accessor
   IMPLEMENTS = dump
   IMPLEMENTS = destroy
   IMPLEMENTS = xref   
   IMPLEMENTS = reparse;execute
   MEMBERS    = grib_expression *expression
   MEMBERS    = grib_action     *block_true
   MEMBERS    = grib_action     *block_false
   END_CLASS_DEF

 */

/* START_CLASS_IMP */

/*

Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
Instead edit values between START_CLASS_DEF and END_CLASS_DEF
or edit "action.class" and rerun ./make_class.pl

*/

static void init_class      (grib_action_class*);
static void dump            (grib_action* d, FILE*,int);
static void xref            (grib_action* d, FILE* f,const char* path);
static void destroy         (grib_context*,grib_action*);
static int create_accessor(grib_section*,grib_action*,grib_loader*);
static grib_action* reparse(grib_action* a,grib_accessor* acc,int *doit);
static int execute(grib_action* a,grib_handle* h);


typedef struct grib_action_if {
    grib_action          act;  
/* Members defined in section */
/* Members defined in if */
	grib_expression *expression;
	grib_action     *block_true;
	grib_action     *block_false;
} grib_action_if;

extern grib_action_class* grib_action_class_section;

static grib_action_class _grib_action_class_if = {
    &grib_action_class_section,                              /* super                     */
    "action_class_if",                              /* name                      */
    sizeof(grib_action_if),            /* size                      */
    0,                                   /* inited */
    &init_class,                         /* init_class */
    0,                               /* init                      */
    &destroy,                            /* destroy */

    &dump,                               /* dump                      */
    &xref,                               /* xref                      */

    &create_accessor,             /* create_accessor*/

    0,                            /* notify_change */
    &reparse,                            /* reparse */
    &execute,                            /* execute */
};

grib_action_class* grib_action_class_if = &_grib_action_class_if;

static void init_class(grib_action_class* c)
{
	c->notify_change	=	(*(c->super))->notify_change;
}
/* END_CLASS_IMP */

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;

  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;
}

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 = grib_get_sub_section(as); */
  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 0
if(p->h->context->debug > 1)
{
  printf("EVALUATE create_accessor_handle ");
grib_expression_print(p->h->context,a->expression,p->h);
printf(" [%d]\n", next == a->block_true);

  grib_dump_action_branch(stdout,next,5);
}
#endif

  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;
}

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;

  if ((ret=grib_expression_evaluate_long(h,a->expression,&lres)) != 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;
}

static void dump(grib_action* act, FILE* f, int lvl)
{
  grib_action_if* a = (grib_action_if*)act;
  int i = 0;

  for (i=0;i<lvl;i++)
    grib_context_print(act->context,f,"     ");

  printf("if(%s) { ",act->name);  grib_expression_print(act->context,a->expression,0);
    printf("\n");

  if(a->block_true){
    /*      grib_context_print(act->context,f,"IF \t TODO \n");  TODO */
    grib_dump_action_branch(f,a->block_true,lvl+1);
  }
  if(a->block_false){
    printf("}\n");
  for (i=0;i<lvl;i++)
    grib_context_print(act->context,f,"     ");
  printf("else(%s) { ",act->name);  grib_expression_print(act->context,a->expression,0);
    /*     grib_context_print(act->context,f,"ELSE \n" );*/
    grib_dump_action_branch(f,a->block_false,lvl+1);
  }
  for (i=0;i<lvl;i++)
    grib_context_print(act->context,f,"     ");
    printf("}\n");
}


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 void destroy(grib_context* context,grib_action* act)
{
  grib_action_if* a = (grib_action_if*) act;
  grib_action *t = a->block_true;
  grib_action *f = a->block_false;

  while(t)
  {
    grib_action *nt = t->next;
    grib_free_action(context,t);
    t = nt;
  }

  while(f)
  {
    grib_action *nf = f->next;
    grib_free_action(context,f);
    f = nf;
  }


  grib_expression_free(context,a->expression);

  grib_context_free_persistent(context, act->name);
  grib_context_free_persistent(context, act->op);
}

static void xref(grib_action* d, FILE* f,const char* path)
{
}