1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 /***************************************************************************
12  *   Jean Baptiste Filippi - 01.11.2005                                                           *
13  *                                                                         *
14  ***************************************************************************/
15 #include "grib_api_internal.h"
16 /*
17    This is used by make_class.pl
18 
19    START_CLASS_DEF
20    CLASS      = action
21    IMPLEMENTS = create_accessor
22    IMPLEMENTS = dump
23    IMPLEMENTS = destroy; xref
24    MEMBERS    = char* the_old
25    MEMBERS    = char* the_new
26    END_CLASS_DEF
27 
28  */
29 
30 /* START_CLASS_IMP */
31 
32 /*
33 
34 Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
35 Instead edit values between START_CLASS_DEF and END_CLASS_DEF
36 or edit "action.class" and rerun ./make_class.pl
37 
38 */
39 
40 static void init_class(grib_action_class*);
41 static void dump(grib_action* d, FILE*, int);
42 static void xref(grib_action* d, FILE* f, const char* path);
43 static void destroy(grib_context*, grib_action*);
44 static int create_accessor(grib_section*, grib_action*, grib_loader*);
45 
46 
47 typedef struct grib_action_rename
48 {
49     grib_action act;
50     /* Members defined in rename */
51     char* the_old;
52     char* the_new;
53 } grib_action_rename;
54 
55 
56 static grib_action_class _grib_action_class_rename = {
57     0,                          /* super                     */
58     "action_class_rename",      /* name                      */
59     sizeof(grib_action_rename), /* size                      */
60     0,                          /* inited */
61     &init_class,                /* init_class */
62     0,                          /* init                      */
63     &destroy,                   /* destroy */
64 
65     &dump, /* dump                      */
66     &xref, /* xref                      */
67 
68     &create_accessor, /* create_accessor*/
69 
70     0, /* notify_change */
71     0, /* reparse */
72     0, /* execute */
73 };
74 
75 grib_action_class* grib_action_class_rename = &_grib_action_class_rename;
76 
init_class(grib_action_class * c)77 static void init_class(grib_action_class* c)
78 {
79 }
80 /* END_CLASS_IMP */
81 
grib_action_create_rename(grib_context * context,char * the_old,char * the_new)82 grib_action* grib_action_create_rename(grib_context* context, char* the_old, char* the_new)
83 {
84     grib_action_rename* a = NULL;
85     grib_action_class* c  = grib_action_class_rename;
86     grib_action* act      = (grib_action*)grib_context_malloc_clear_persistent(context, c->size);
87     act->next             = NULL;
88     act->name             = grib_context_strdup_persistent(context, "RENAME");
89     act->op               = grib_context_strdup_persistent(context, "rename");
90     act->cclass           = c;
91     act->context          = context;
92     a                     = (grib_action_rename*)act;
93     a->the_old            = grib_context_strdup_persistent(context, the_old);
94     a->the_new            = grib_context_strdup_persistent(context, the_new);
95     return act;
96 }
97 
rename_accessor(grib_accessor * a,char * name)98 static void rename_accessor(grib_accessor* a, char* name)
99 {
100     int id;
101     char* the_old = (char*)a->all_names[0];
102 
103     if (grib_handle_of_accessor(a)->use_trie && *(a->all_names[0]) != '_') {
104         id                                        = grib_hash_keys_get_id(a->context->keys, a->all_names[0]);
105         grib_handle_of_accessor(a)->accessors[id] = NULL;
106         id                                        = grib_hash_keys_get_id(a->context->keys, name);
107         grib_handle_of_accessor(a)->accessors[id] = a;
108     }
109     a->all_names[0] = grib_context_strdup_persistent(a->context, name);
110     a->name         = a->all_names[0];
111     grib_context_log(a->context, GRIB_LOG_DEBUG, "Renaming %s to %s", the_old, name);
112     /* grib_context_free(a->context,the_old); */
113 }
114 
create_accessor(grib_section * p,grib_action * act,grib_loader * h)115 static int create_accessor(grib_section* p, grib_action* act, grib_loader* h)
116 {
117     grib_action_rename* a = (grib_action_rename*)act;
118     grib_accessor* ga     = NULL;
119 
120     ga = grib_find_accessor(p->h, a->the_old);
121 
122     if (ga) {
123         rename_accessor(ga, a->the_new);
124     }
125     else {
126         grib_context_log(act->context, GRIB_LOG_DEBUG, "Action_class_rename  : create_accessor_buffer : No accessor named %s to rename ", a->the_old);
127     }
128 
129     return GRIB_SUCCESS;
130 }
131 
dump(grib_action * act,FILE * f,int lvl)132 static void dump(grib_action* act, FILE* f, int lvl)
133 {
134     grib_action_rename* a = (grib_action_rename*)act;
135 
136     int i = 0;
137 
138     for (i = 0; i < lvl; i++)
139         grib_context_print(act->context, f, "     ");
140 
141     grib_context_print(act->context, f, "rename %s as %s in %s\n", a->the_old, act->name, a->the_new);
142 }
143 
destroy(grib_context * context,grib_action * act)144 static void destroy(grib_context* context, grib_action* act)
145 {
146     grib_action_rename* a = (grib_action_rename*)act;
147 
148     grib_context_free_persistent(context, a->the_old);
149     grib_context_free_persistent(context, a->the_new);
150     grib_context_free_persistent(context, act->name);
151     grib_context_free_persistent(context, act->op);
152 }
153 
xref(grib_action * d,FILE * f,const char * path)154 static void xref(grib_action* d, FILE* f, const char* path)
155 {
156 }
157