|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Attila Szakacs |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As an additional exemption you are allowed to compile & link against the |
| 19 | + * OpenSSL libraries as published by the OpenSSL project. See the file |
| 20 | + * COPYING for details. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include "func-vars.h" |
| 25 | +#include "filterx-eval.h" |
| 26 | +#include "object-json.h" |
| 27 | +#include "object-string.h" |
| 28 | + |
| 29 | +static gboolean |
| 30 | +_add_to_dict(FilterXVariable *variable, gpointer user_data) |
| 31 | +{ |
| 32 | + FilterXObject *vars = (FilterXObject *) user_data; |
| 33 | + |
| 34 | + gssize name_len; |
| 35 | + const gchar *name_str = filterx_variable_get_name(variable, &name_len); |
| 36 | + FilterXObject *name = filterx_string_new(name_str, name_len); |
| 37 | + |
| 38 | + FilterXObject *value = filterx_variable_get_value(variable); |
| 39 | + FilterXObject *cloned_value = filterx_object_clone(value); |
| 40 | + filterx_object_unref(value); |
| 41 | + |
| 42 | + gboolean success = filterx_object_set_subscript(vars, name, &cloned_value); |
| 43 | + |
| 44 | + filterx_object_unref(cloned_value); |
| 45 | + filterx_object_unref(name); |
| 46 | + return success; |
| 47 | +} |
| 48 | + |
| 49 | +FilterXObject * |
| 50 | +filterx_simple_function_vars(GPtrArray *args) |
| 51 | +{ |
| 52 | + if (args && args->len != 0) |
| 53 | + { |
| 54 | + msg_error("filterx: vars() function does not take any arguments"); |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + |
| 58 | + FilterXEvalContext *context = filterx_eval_get_context(); |
| 59 | + FilterXObject *vars = filterx_json_object_new_empty(); |
| 60 | + |
| 61 | + if (filterx_scope_foreach_variable(context->scope, _add_to_dict, vars)) |
| 62 | + return vars; |
| 63 | + |
| 64 | + filterx_object_unref(vars); |
| 65 | + return NULL; |
| 66 | +} |
0 commit comments