From a99ec3ba617b6c6cb7f15f7056dd1c1b564290b1 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 4 Mar 2016 08:43:45 +0100 Subject: [PATCH] Rework _json_node_deep_copy in Vala --- src/Makefile.am | 1 - src/c-api.vapi | 4 -- src/matrix-compacts.vala | 49 ++++++++++++++++++++ src/matrix-types.c | 1 - src/utils.c | 98 ---------------------------------------- src/utils.h | 28 ------------ 6 files changed, 49 insertions(+), 132 deletions(-) delete mode 100644 src/utils.c delete mode 100644 src/utils.h diff --git a/src/Makefile.am b/src/Makefile.am index 52ee23a..b5dabc6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -93,7 +93,6 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-version.c \ matrix-types.c \ matrix-enumtypes.c \ - utils.c \ $(INST_H_SRC_FILES) \ $(NULL) diff --git a/src/c-api.vapi b/src/c-api.vapi index 5fd433d..6cbe674 100644 --- a/src/c-api.vapi +++ b/src/c-api.vapi @@ -91,8 +91,4 @@ namespace Matrix { public class StateEvent { public Json.Node? get_json_node(); } - - [CCode (cheader_filename = "utils.h", cname = "_json_node_deep_copy")] - public Json.Node? - _json_node_deep_copy(Json.Node? node); } diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala index 229168e..d9f88ab 100644 --- a/src/matrix-compacts.vala +++ b/src/matrix-compacts.vala @@ -651,4 +651,53 @@ namespace Matrix { return builder.get_root(); } } + + private Json.Node? + _json_node_deep_copy(Json.Node? node) + { + Json.Node ret; + + if (node == null) { + return null; + } + + ret = new Json.Node(node.get_node_type()); + + switch (node.get_node_type()) { + case Json.NodeType.OBJECT: + var new_obj = new Json.Object(); + + node.get_object().foreach_member( + (old_obj, member_name, member_node) => { + new_obj.set_member( + member_name, + _json_node_deep_copy(member_node)); + }); + + ret.set_object(new_obj); + + break; + + case Json.NodeType.ARRAY: + var new_ary = new Json.Array(); + + node.get_array().foreach_element( + (old_ary, idx, element_node) => { + new_ary.add_element( + _json_node_deep_copy(element_node)); + }); + + break; + + case Json.NodeType.VALUE: + ret.set_value(node.get_value()); + + break; + + case Json.NodeType.NULL: + break; + } + + return ret; + } } diff --git a/src/matrix-types.c b/src/matrix-types.c index e5988e3..ab2c6ad 100644 --- a/src/matrix-types.c +++ b/src/matrix-types.c @@ -19,7 +19,6 @@ #include "matrix-types.h" #include "matrix-enumtypes.h" #include "matrix-glib.h" -#include "utils.h" /** * SECTION:matrix-types diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index d9ed925..0000000 --- a/src/utils.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * This file is part of matrix-glib-sdk - * - * matrix-glib-sdk is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * matrix-glib-sdk is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with matrix-glib-sdk. If not, see - * . - */ - -#include "utils.h" -#include "matrix-types.h" - -static void -deep_copy_object(JsonObject *object, - const gchar *member_name, - JsonNode *member_node, - JsonObject *new_obj) -{ - json_object_set_member(new_obj, - member_name, - _json_node_deep_copy((const JsonNode *)member_node)); -} - -static void -deep_copy_array(JsonArray *array, - guint idx, - JsonNode *element_node, - JsonArray *new_array) -{ - json_array_add_element(new_array, - _json_node_deep_copy((const JsonNode *)element_node)); -} - -JsonNode * -_json_node_deep_copy(const JsonNode *node) -{ - JsonNode *ret; - - if (node == NULL) { - return NULL; - } - - ret = json_node_new(JSON_NODE_TYPE((JsonNode *)node)); - - switch (JSON_NODE_TYPE((JsonNode *)node)) { - case JSON_NODE_OBJECT: - { - JsonObject *new_obj = json_object_new(); - - json_object_foreach_member(json_node_get_object((JsonNode *)node), - (JsonObjectForeach)deep_copy_object, - new_obj); - - json_node_set_object(ret, new_obj); - - break; - } - - case JSON_NODE_ARRAY: - { - JsonArray *new_ary = json_array_new(); - - json_array_foreach_element(json_node_get_array((JsonNode *)node), - (JsonArrayForeach)deep_copy_array, - new_ary); - - json_node_set_array(ret, new_ary); - - break; - } - - case JSON_NODE_VALUE: - { - GValue val = G_VALUE_INIT; - - json_node_get_value((JsonNode *)node, &val); - json_node_set_value(ret, &val); - - g_value_unset(&val); - - break; - } - - case JSON_NODE_NULL: - break; - } - - return ret; -} diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index a929970..0000000 --- a/src/utils.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of matrix-glib-sdk - * - * matrix-glib-sdk is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * matrix-glib-sdk is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with matrix-glib-sdk. If not, see - * . - */ - -#ifndef __MATRIX_UTILS_H__ -#define __MATRIX_UTILS_H__ - -#include -#include -#include - -JsonNode *_json_node_deep_copy(const JsonNode *node); - -#endif /* __MATRIX_UTILS_H__ */