From 89376c08909d8ee3c0ca37a97fc4a3dd5e258c52 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sat, 11 Nov 2017 08:41:50 +0100 Subject: [PATCH] Port _matrix_json_node_deep_copy() to C --- .gitignore | 1 - src/Makefile.am | 5 +- ...{matrix-c-compacts.c => matrix-compacts.c} | 2 +- ...{matrix-c-compacts.h => matrix-compacts.h} | 0 src/matrix-compacts.vala | 68 ------------------- src/utils.c | 68 +++++++++++++++++++ src/utils.h | 2 + vapi/c-api.vapi | 25 ++++--- 8 files changed, 87 insertions(+), 84 deletions(-) rename src/{matrix-c-compacts.c => matrix-compacts.c} (99%) rename src/{matrix-c-compacts.h => matrix-compacts.h} (100%) delete mode 100644 src/matrix-compacts.vala diff --git a/.gitignore b/.gitignore index 4be754c..5b60f95 100644 --- a/.gitignore +++ b/.gitignore @@ -51,7 +51,6 @@ Makefile.in /src/matrix-client.c /src/matrix-http-api.c /src/matrix-http-client.c -/src/matrix-compacts.c /src/matrix-event-base.c /src/matrix-event-presence.c /src/matrix-event-room-base.c diff --git a/src/Makefile.am b/src/Makefile.am index d58ac06..53e35b6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-client.vala \ matrix-http-api.vala \ matrix-http-client.vala \ - matrix-compacts.vala \ matrix-event-base.vala \ matrix-event-room-base.vala \ matrix-event-state-base.vala \ @@ -113,7 +112,7 @@ bin_PROGRAMS = test-api-client test-client INST_H_SRC_FILES = \ matrix-types.h \ - matrix-c-compacts.h \ + matrix-compacts.h \ utils.h \ matrix-profile.h \ $(NULL) @@ -135,7 +134,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-types.c \ matrix-version.c \ matrix-types.c \ - matrix-c-compacts.c \ + matrix-compacts.c \ matrix-profile.c \ utils.c \ matrix-enumtypes.c \ diff --git a/src/matrix-c-compacts.c b/src/matrix-compacts.c similarity index 99% rename from src/matrix-c-compacts.c rename to src/matrix-compacts.c index a4bdeb8..d6803d4 100644 --- a/src/matrix-c-compacts.c +++ b/src/matrix-compacts.c @@ -18,7 +18,7 @@ #include #include -#include "matrix-c-compacts.h" +#include "matrix-compacts.h" #include "matrix-enumtypes.h" #include "matrix-types.h" #include "utils.h" diff --git a/src/matrix-c-compacts.h b/src/matrix-compacts.h similarity index 100% rename from src/matrix-c-compacts.h rename to src/matrix-compacts.h diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala deleted file mode 100644 index 2efa382..0000000 --- a/src/matrix-compacts.vala +++ /dev/null @@ -1,68 +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 - * . - */ - -namespace Matrix { - public 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/utils.c b/src/utils.c index 822f0cd..36f9e72 100644 --- a/src/utils.c +++ b/src/utils.c @@ -106,3 +106,71 @@ _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error) return ret; } + +static void +_copy_obj_member(JsonObject *old_obj, const gchar *member_name, JsonNode *member_node, gpointer user_data) +{ + JsonObject *new_obj = user_data; + + json_object_set_member(new_obj, member_name, _matrix_json_node_deep_copy(member_node)); +} + +static void +_copy_ary_member(JsonArray *old_ary, guint idx, JsonNode *element_node, gpointer user_data) +{ + JsonArray *new_ary = user_data; + + json_array_add_element(new_ary, _matrix_json_node_deep_copy(element_node)); +} + +JsonNode * +_matrix_json_node_deep_copy(JsonNode *node) +{ + JsonNode *ret; + JsonNodeType node_type; + + g_return_val_if_fail(node != NULL, NULL); + + node_type = json_node_get_node_type(node); + ret = json_node_new(node_type); + + switch (node_type) { + case JSON_NODE_OBJECT: + { + JsonObject *new_obj = json_object_new(); + JsonObject *old_obj = json_node_get_object(node); + + json_object_foreach_member(old_obj, _copy_obj_member, new_obj); + + json_node_set_object(ret, new_obj); + + break; + } + case JSON_NODE_ARRAY: + { + JsonArray *new_ary = json_array_new(); + JsonArray *old_ary = json_node_get_array(node); + + json_array_foreach_element(old_ary, _copy_ary_member, new_ary); + + json_node_set_array(ret, new_ary); + + break; + } + case JSON_NODE_VALUE: + { + GValue node_value = G_VALUE_INIT; + + json_node_get_value(node, &node_value); + json_node_set_value(ret, &node_value); + + g_value_unset(&node_value); + + break; + } + case JSON_NODE_NULL: + break; + } + + return ret; +} diff --git a/src/utils.h b/src/utils.h index ab432a1..beb45c7 100644 --- a/src/utils.h +++ b/src/utils.h @@ -20,11 +20,13 @@ # define __MATRIX_GLIB_SDK_UTILS_H__ # include +# include G_BEGIN_DECLS gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gchar convert_dashes); gint _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error); +JsonNode *_matrix_json_node_deep_copy(JsonNode *node); G_END_DECLS diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 35ef228..6ba2089 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -285,7 +285,7 @@ namespace Matrix { } /* Compact classes */ - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public abstract class JsonCompact { public JsonCompact(); @@ -296,7 +296,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class FilterRules : JsonCompact { public uint limit { get; set; } public string[] types { get; set; } @@ -309,7 +309,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class RoomFilter : JsonCompact { public bool include_leave { get; set; default=true; } public FilterRules? ephemeral { get; set; default = null; } @@ -320,7 +320,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class Filter : JsonCompact { public string[] event_fields { get; set; } public EventFormat event_format { get; set; default = Matrix.EventFormat.CLIENT; } @@ -330,7 +330,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class @3PidCredential : JsonCompact { public string? id_server { get; set; default = null; } public string? session_id { get; set; default = null; } @@ -340,7 +340,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class Pusher : JsonCompact { public string? device_display_name { get; set; default = null; } public string? app_display_name { get; set; default = null; } @@ -355,7 +355,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class EventContext : JsonCompact { public int before_limit { get; set; default = -1; } public int after_limit { get; set; default = -1; } @@ -365,7 +365,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class SearchGrouping : JsonCompact { public SearchGroupBy key { get; set; default = SearchGroupBy.NONE; } @@ -373,7 +373,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class SearchGroupings : JsonCompact { public SearchGrouping[] group_by { get; set; } @@ -382,7 +382,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class SearchRoomEvents : JsonCompact { public SearchOrder order_by { get; set; default = SearchOrder.RECENT; } public SearchKey[] keys { get; set; } @@ -397,7 +397,7 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-c-compacts.h")] + [CCode (cheader_filename = "matrix-compacts.h")] public class SearchCategories : JsonCompact { public SearchRoomEvents? room_events { get; set; default = null; } @@ -413,6 +413,9 @@ namespace Matrix { public int _g_enum_nick_to_value(GLib.Type enum_type, string nick) throws Matrix.Error; + [CCode (cheader_filename = "utils.h", cname = "_matrix_json_node_deep_copy")] + public Json.Node? _json_node_deep_copy(Json.Node? node); + [CCode (cheader_filename = "matrix-profile.h")] public class Profile : GLib.Object { public GLib.TimeSpan age {get;}