eccodes/src/grib_expression_class_length.c

185 lines
5.3 KiB
C
Raw Normal View History

2014-06-20 16:25:20 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2014-06-20 16:25:20 +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"
2014-06-20 16:25:20 +00:00
/*
This is used by make_class.pl
START_CLASS_DEF
CLASS = expression
IMPLEMENTS = destroy
IMPLEMENTS = native_type
IMPLEMENTS = get_name
IMPLEMENTS = evaluate_long
IMPLEMENTS = evaluate_double
IMPLEMENTS = evaluate_string
IMPLEMENTS = print
IMPLEMENTS = add_dependency
MEMBERS = char *name
MEMBERS = size_t start
MEMBERS = size_t length
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 "expression.class" and rerun ./make_class.pl
*/
typedef const char* string; /* to keep make_class.pl happy */
2020-01-22 13:10:59 +00:00
static void init_class(grib_expression_class*);
2014-06-20 16:25:20 +00:00
2020-01-22 13:10:59 +00:00
static void destroy(grib_context*, grib_expression* e);
2014-06-20 16:25:20 +00:00
2020-01-22 13:10:59 +00:00
static void print(grib_context*, grib_expression*, grib_handle*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
2014-06-20 16:25:20 +00:00
static string get_name(grib_expression* e);
2020-01-22 13:10:59 +00:00
static int native_type(grib_expression*, grib_handle*);
2014-06-20 16:25:20 +00:00
2020-01-22 13:10:59 +00:00
static int evaluate_long(grib_expression*, grib_handle*, long*);
static int evaluate_double(grib_expression*, grib_handle*, double*);
static string evaluate_string(grib_expression*, grib_handle*, char*, size_t*, int*);
2014-06-20 16:25:20 +00:00
2020-01-22 13:10:59 +00:00
typedef struct grib_expression_length
{
grib_expression base;
/* Members defined in length */
char* name;
size_t start;
size_t length;
2014-06-20 16:25:20 +00:00
} grib_expression_length;
static grib_expression_class _grib_expression_class_length = {
2020-01-22 13:10:59 +00:00
0, /* super */
"length", /* name */
sizeof(grib_expression_length), /* size of instance */
0, /* inited */
&init_class, /* init_class */
0, /* constructor */
&destroy, /* destructor */
&print,
&add_dependency,
&native_type,
&get_name,
&evaluate_long,
&evaluate_double,
&evaluate_string,
2014-06-20 16:25:20 +00:00
};
grib_expression_class* grib_expression_class_length = &_grib_expression_class_length;
static void init_class(grib_expression_class* c)
{
}
/* END_CLASS_IMP */
static const char* get_name(grib_expression* g)
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
return e->name;
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static int evaluate_long(grib_expression* g, grib_handle* h, long* result)
2014-06-20 16:25:20 +00:00
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 13:10:59 +00:00
int err = 0;
2020-01-22 14:48:06 +00:00
char mybuf[1024] = {0,};
2020-01-22 13:10:59 +00:00
size_t size = 1024;
if ((err = grib_get_string_internal(h, e->name, mybuf, &size)) != GRIB_SUCCESS)
2016-02-09 18:00:49 +00:00
return err;
2020-01-22 13:10:59 +00:00
*result = strlen(mybuf);
2016-02-09 18:00:49 +00:00
return err;
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static int evaluate_double(grib_expression* g, grib_handle* h, double* result)
2014-06-20 16:25:20 +00:00
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 14:48:06 +00:00
char mybuf[1024] = {0,};
2020-01-22 13:10:59 +00:00
size_t size = 1024;
int err = 0;
if ((err = grib_get_string_internal(h, e->name, mybuf, &size)) != GRIB_SUCCESS)
2016-02-09 18:00:49 +00:00
return err;
2020-01-22 13:10:59 +00:00
*result = strlen(mybuf);
2016-02-09 18:00:49 +00:00
return err;
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, size_t* size, int* err)
2014-06-20 16:25:20 +00:00
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 14:48:06 +00:00
char mybuf[1024] = {0,};
2016-02-09 18:00:49 +00:00
Assert(buf);
2020-01-22 13:10:59 +00:00
if ((*err = grib_get_string_internal(h, e->name, mybuf, size)) != GRIB_SUCCESS)
2016-02-09 18:00:49 +00:00
return NULL;
2020-01-22 13:10:59 +00:00
sprintf(buf, "%ld", (long)strlen(mybuf));
2016-02-09 18:00:49 +00:00
return buf;
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static void print(grib_context* c, grib_expression* g, grib_handle* f)
2014-06-20 16:25:20 +00:00
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 13:10:59 +00:00
printf("access('%s", e->name);
if (f) {
2016-02-09 18:00:49 +00:00
long s = 0;
2020-01-22 13:10:59 +00:00
grib_get_long(f, e->name, &s);
printf("=%ld", s);
2016-02-09 18:00:49 +00:00
}
printf("')");
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static void destroy(grib_context* c, grib_expression* g)
2014-06-20 16:25:20 +00:00
{
2016-02-09 18:00:49 +00:00
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 13:10:59 +00:00
grib_context_free_persistent(c, e->name);
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static void add_dependency(grib_expression* g, grib_accessor* observer)
2016-02-09 18:00:49 +00:00
{
grib_expression_length* e = (grib_expression_length*)g;
2020-01-22 13:10:59 +00:00
grib_accessor* observed = grib_find_accessor(grib_handle_of_accessor(observer), e->name);
2016-02-09 18:00:49 +00:00
2020-01-22 13:10:59 +00:00
if (!observed) {
2016-02-09 18:00:49 +00:00
/* grib_context_log(observer->context, GRIB_LOG_ERROR, */
/* "Error in accessor_add_dependency: cannot find [%s]", e->name); */
/* Assert(observed); */
return;
}
2020-01-22 13:10:59 +00:00
grib_dependency_add(observer, observed);
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
grib_expression* new_length_expression(grib_context* c, const char* name)
2014-06-20 16:25:20 +00:00
{
2020-01-22 13:10:59 +00:00
grib_expression_length* e = (grib_expression_length*)grib_context_malloc_clear_persistent(c, sizeof(grib_expression_length));
2016-02-09 18:00:49 +00:00
e->base.cclass = grib_expression_class_length;
2020-01-22 13:10:59 +00:00
e->name = grib_context_strdup_persistent(c, name);
2016-02-09 18:00:49 +00:00
return (grib_expression*)e;
2014-06-20 16:25:20 +00:00
}
2020-01-22 13:10:59 +00:00
static int native_type(grib_expression* g, grib_handle* h)
2014-06-20 16:25:20 +00:00
{
2017-05-30 13:55:43 +00:00
return GRIB_TYPE_LONG;
2014-06-20 16:25:20 +00:00
}