2013-03-25 12:04:10 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2013-03-25 12:04:10 +00:00
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the Apache Licence Version 2.0
|
|
|
|
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
|
|
|
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
|
|
|
/*
|
|
|
|
This is used by make_class.pl
|
|
|
|
|
|
|
|
START_CLASS_DEF
|
|
|
|
CLASS = action
|
2023-10-20 19:14:32 +00:00
|
|
|
IMPLEMENTS = dump
|
2013-03-25 12:04:10 +00:00
|
|
|
IMPLEMENTS = destroy;execute
|
|
|
|
MEMBERS = grib_expression *expression
|
|
|
|
MEMBERS = char *name
|
|
|
|
MEMBERS = int nofail
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-06-14 14:07:16 +00:00
|
|
|
static void init_class (grib_action_class*);
|
|
|
|
static void dump (grib_action* d, FILE*,int);
|
|
|
|
static void destroy (grib_context*,grib_action*);
|
|
|
|
static int execute(grib_action* a,grib_handle* h);
|
2020-01-22 13:10:59 +00:00
|
|
|
|
|
|
|
|
2022-06-14 14:07:16 +00:00
|
|
|
typedef struct grib_action_set {
|
2022-12-07 19:25:57 +00:00
|
|
|
grib_action act;
|
2020-01-22 13:10:59 +00:00
|
|
|
/* Members defined in set */
|
2022-06-14 14:07:16 +00:00
|
|
|
grib_expression *expression;
|
|
|
|
char *name;
|
2020-01-22 13:10:59 +00:00
|
|
|
int nofail;
|
2013-03-25 12:04:10 +00:00
|
|
|
} grib_action_set;
|
|
|
|
|
|
|
|
|
|
|
|
static grib_action_class _grib_action_class_set = {
|
2022-06-14 14:07:16 +00:00
|
|
|
0, /* super */
|
|
|
|
"action_class_set", /* name */
|
|
|
|
sizeof(grib_action_set), /* size */
|
|
|
|
0, /* inited */
|
|
|
|
&init_class, /* init_class */
|
|
|
|
0, /* init */
|
|
|
|
&destroy, /* destroy */
|
|
|
|
|
|
|
|
&dump, /* dump */
|
2023-10-20 19:14:32 +00:00
|
|
|
0, /* xref */
|
2022-06-14 14:07:16 +00:00
|
|
|
|
|
|
|
0, /* create_accessor*/
|
|
|
|
|
|
|
|
0, /* notify_change */
|
|
|
|
0, /* reparse */
|
|
|
|
&execute, /* execute */
|
2013-03-25 12:04:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
grib_action_class* grib_action_class_set = &_grib_action_class_set;
|
|
|
|
|
|
|
|
static void init_class(grib_action_class* c)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/* END_CLASS_IMP */
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_action* grib_action_create_set(grib_context* context,
|
|
|
|
const char* name, grib_expression* expression, int nofail)
|
2013-03-25 12:04:10 +00:00
|
|
|
{
|
2015-12-31 13:28:24 +00:00
|
|
|
char buf[1024];
|
2020-01-22 13:10:59 +00:00
|
|
|
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");
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
act->cclass = c;
|
|
|
|
a = (grib_action_set*)act;
|
|
|
|
act->context = context;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
a->expression = expression;
|
|
|
|
a->name = grib_context_strdup_persistent(context, name);
|
|
|
|
a->nofail = nofail;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2022-11-10 19:18:43 +00:00
|
|
|
snprintf(buf, 1024, "set%p", (void*)expression);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
act->name = grib_context_strdup_persistent(context, buf);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2015-12-31 13:28:24 +00:00
|
|
|
return act;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
static int execute(grib_action* a, grib_handle* h)
|
2013-03-25 12:04:10 +00:00
|
|
|
{
|
2020-01-22 13:10:59 +00:00
|
|
|
int ret = 0;
|
|
|
|
grib_action_set* self = (grib_action_set*)a;
|
|
|
|
ret = grib_set_expression(h, self->name, self->expression);
|
|
|
|
if (self->nofail)
|
|
|
|
return 0;
|
2015-12-31 13:28:24 +00:00
|
|
|
if (ret != GRIB_SUCCESS) {
|
2023-01-05 16:41:09 +00:00
|
|
|
grib_context_log(h->context, GRIB_LOG_ERROR, "Error while setting key '%s' (%s)",
|
2020-01-22 13:10:59 +00:00
|
|
|
self->name, grib_get_error_message(ret));
|
2015-12-31 13:28:24 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dump(grib_action* act, FILE* f, int lvl)
|
|
|
|
{
|
2024-03-06 20:37:10 +00:00
|
|
|
int i = 0;
|
|
|
|
const grib_action_set* self = (grib_action_set*)act;
|
2020-01-22 13:10:59 +00:00
|
|
|
for (i = 0; i < lvl; i++)
|
|
|
|
grib_context_print(act->context, f, " ");
|
|
|
|
grib_context_print(act->context, f, self->name);
|
2015-12-31 13:28:24 +00:00
|
|
|
printf("\n");
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
static void destroy(grib_context* context, grib_action* act)
|
2013-03-25 12:04:10 +00:00
|
|
|
{
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_action_set* a = (grib_action_set*)act;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2015-12-31 13:28:24 +00:00
|
|
|
grib_context_free_persistent(context, a->name);
|
2020-01-22 13:10:59 +00:00
|
|
|
grib_expression_free(context, a->expression);
|
2015-12-31 13:28:24 +00:00
|
|
|
grib_context_free_persistent(context, act->name);
|
|
|
|
grib_context_free_persistent(context, act->op);
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|