Port _matrix_json_node_deep_copy() to C
This commit is contained in:
@@ -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 \
|
||||
|
@@ -18,7 +18,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <gobject/gvaluecollector.h>
|
||||
#include "matrix-c-compacts.h"
|
||||
#include "matrix-compacts.h"
|
||||
#include "matrix-enumtypes.h"
|
||||
#include "matrix-types.h"
|
||||
#include "utils.h"
|
@@ -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
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
68
src/utils.c
68
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;
|
||||
}
|
||||
|
@@ -20,11 +20,13 @@
|
||||
# define __MATRIX_GLIB_SDK_UTILS_H__
|
||||
|
||||
# include <glib-object.h>
|
||||
# include <json-glib/json-glib.h>
|
||||
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user