From 89fc1fa5b83ac4780525308f1822b7117d99887b Mon Sep 17 00:00:00 2001 From: chme Date: Sun, 27 Aug 2017 08:44:00 +0200 Subject: [PATCH] [misc/spotify] Move json helper functions into its own file --- src/Makefile.am | 1 + src/misc_json.c | 125 +++++++++++++++++++++++++++++++++++++++++++ src/misc_json.h | 57 ++++++++++++++++++++ src/spotify_webapi.c | 96 +-------------------------------- 4 files changed, 184 insertions(+), 95 deletions(-) create mode 100644 src/misc_json.c create mode 100644 src/misc_json.h diff --git a/src/Makefile.am b/src/Makefile.am index f10e02f6..45b1d8ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -113,6 +113,7 @@ forked_daapd_SOURCES = main.c \ dmap_common.c dmap_common.h \ $(FFMPEG_SRC) \ misc.c misc.h \ + misc_json.c misc_json.h \ rng.c rng.h \ rsp_query.c rsp_query.h \ daap_query.c daap_query.h \ diff --git a/src/misc_json.c b/src/misc_json.c new file mode 100644 index 00000000..2e872971 --- /dev/null +++ b/src/misc_json.c @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2017 Christian Meffert + * + * Some code included below is in the public domain, check comments + * in the file. + * + * Pieces of code adapted from mt-daapd: + * Copyright (C) 2003-2007 Ron Pedde (ron@pedde.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +#include "logger.h" + +void +jparse_free(json_object* haystack) +{ + if (haystack) + { +#ifdef HAVE_JSON_C_OLD + json_object_put(haystack); +#else + if (json_object_put(haystack) != 1) + DPRINTF(E_LOG, L_MISC, "Memleak: JSON parser did not free object\n"); +#endif + } +} + +int +jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle) +{ + if (! (json_object_object_get_ex(haystack, key, needle) && json_object_get_type(*needle) == json_type_array) ) + return -1; + else + return 0; +} + +const char * +jparse_str_from_obj(json_object *haystack, const char *key) +{ + json_object *needle; + + if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_string) + return json_object_get_string(needle); + else + return NULL; +} + +int +jparse_int_from_obj(json_object *haystack, const char *key) +{ + json_object *needle; + + if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_int) + return json_object_get_int(needle); + else + return 0; +} + +int +jparse_bool_from_obj(json_object *haystack, const char *key) +{ + json_object *needle; + + if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_boolean) + return json_object_get_boolean(needle); + else + return false; +} + +time_t +jparse_time_from_obj(json_object *haystack, const char *key) +{ + const char *tmp; + struct tm tp; + time_t parsed_time; + + memset(&tp, 0, sizeof(struct tm)); + + tmp = jparse_str_from_obj(haystack, key); + if (!tmp) + return 0; + + strptime(tmp, "%Y-%m-%dT%H:%M:%SZ", &tp); + parsed_time = mktime(&tp); + if (parsed_time < 0) + return 0; + + return parsed_time; +} + +const char * +jparse_str_from_array(json_object *array, int index, const char *key) +{ + json_object *item; + int count; + + if (json_object_get_type(array) != json_type_array) + return NULL; + + count = json_object_array_length(array); + if (count <= 0 || count <= index) + return NULL; + + item = json_object_array_get_idx(array, index); + return jparse_str_from_obj(item, key); +} diff --git a/src/misc_json.h b/src/misc_json.h new file mode 100644 index 00000000..0adc34cd --- /dev/null +++ b/src/misc_json.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 Christian Meffert + * + * Some code included below is in the public domain, check comments + * in the file. + * + * Pieces of code adapted from mt-daapd: + * Copyright (C) 2003-2007 Ron Pedde (ron@pedde.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef SRC_MISC_JSON_H_ +#define SRC_MISC_JSON_H_ + + +#include +#include +#include +#include + +void +jparse_free(json_object *haystack); + +int +jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle); + +const char * +jparse_str_from_obj(json_object *haystack, const char *key); + +int +jparse_int_from_obj(json_object *haystack, const char *key); + +int +jparse_bool_from_obj(json_object *haystack, const char *key); + +time_t +jparse_time_from_obj(json_object *haystack, const char *key); + +const char * +jparse_str_from_array(json_object *array, int index, const char *key); + + +#endif /* SRC_MISC_JSON_H_ */ diff --git a/src/spotify_webapi.c b/src/spotify_webapi.c index 79a8906a..81065286 100644 --- a/src/spotify_webapi.c +++ b/src/spotify_webapi.c @@ -30,6 +30,7 @@ #include "http.h" #include "library.h" #include "logger.h" +#include "misc_json.h" @@ -54,101 +55,6 @@ static const char *spotify_me_uri = "https://api.spotify.com/v1/me"; /*--------------------- HELPERS FOR SPOTIFY WEB API -------------------------*/ /* All the below is in the httpd thread */ - -static void -jparse_free(json_object* haystack) -{ - if (haystack) - { -#ifdef HAVE_JSON_C_OLD - json_object_put(haystack); -#else - if (json_object_put(haystack) != 1) - DPRINTF(E_LOG, L_SPOTIFY, "Memleak: JSON parser did not free object\n"); -#endif - } -} - -static int -jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle) -{ - if (! (json_object_object_get_ex(haystack, key, needle) && json_object_get_type(*needle) == json_type_array) ) - return -1; - else - return 0; -} - -static const char * -jparse_str_from_obj(json_object *haystack, const char *key) -{ - json_object *needle; - - if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_string) - return json_object_get_string(needle); - else - return NULL; -} - -static int -jparse_int_from_obj(json_object *haystack, const char *key) -{ - json_object *needle; - - if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_int) - return json_object_get_int(needle); - else - return 0; -} - -static int -jparse_bool_from_obj(json_object *haystack, const char *key) -{ - json_object *needle; - - if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_boolean) - return json_object_get_boolean(needle); - else - return false; -} - -static time_t -jparse_time_from_obj(json_object *haystack, const char *key) -{ - const char *tmp; - struct tm tp; - time_t parsed_time; - - memset(&tp, 0, sizeof(struct tm)); - - tmp = jparse_str_from_obj(haystack, key); - if (!tmp) - return 0; - - strptime(tmp, "%Y-%m-%dT%H:%M:%SZ", &tp); - parsed_time = mktime(&tp); - if (parsed_time < 0) - return 0; - - return parsed_time; -} - -static const char * -jparse_str_from_array(json_object *array, int index, const char *key) -{ - json_object *item; - int count; - - if (json_object_get_type(array) != json_type_array) - return NULL; - - count = json_object_array_length(array); - if (count <= 0 || count <= index) - return NULL; - - item = json_object_array_get_idx(array, index); - return jparse_str_from_obj(item, key); -} - static void free_http_client_ctx(struct http_client_ctx *ctx) {