From 04234d29385666ffde6e982b5af696d74d184875 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Fri, 24 Mar 2023 20:45:16 +0100 Subject: [PATCH] implemented get_string_from_json in why2_read_socket --- src/chat/misc.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/chat/misc.c b/src/chat/misc.c index 53a3c7c..4ba1f33 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -153,6 +153,15 @@ void add_brackets(char **json) *json = output; } +char *get_string_from_json(struct json_object *json, char *string) +{ + struct json_object *object; + json_object_object_get_ex(json, string, &object); + + //GET STRINGS + return (char*) json_object_get_string(object); +} + //GLOBAL void why2_send_socket(char *text, int socket) { @@ -273,17 +282,12 @@ char *why2_read_socket(int socket) content_buffer[content_size - 2] = '\0'; //TODO: Possible problems - //DECODE + //PARSE JSON struct json_object *json_obj = json_tokener_parse(content_buffer); - struct json_object *message_obj; - struct json_object *username_obj; - - json_object_object_get_ex(json_obj, "message", &message_obj); - json_object_object_get_ex(json_obj, "username", &username_obj); //GET STRINGS - const char *message = json_object_get_string(message_obj); - const char *username = json_object_get_string(username_obj); + char *message = get_string_from_json(json_obj, "message"); //TODO: Check deallocation problems + char *username = get_string_from_json(json_obj, "username"); //ALLOCATE final_message; final_message = why2_calloc(strlen(message) + strlen(username) + 3, sizeof(char));