From c08d2016c16d1748c4c527c8055f90f5a7669004 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Mon, 5 Nov 2012 00:39:01 +0100 Subject: Add json_util.c with helper functions. Keeping them in a separate file to avoid making *any* changes to the third-party json.c. --- lib/json_util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/json_util.c (limited to 'lib/json_util.c') diff --git a/lib/json_util.c b/lib/json_util.c new file mode 100644 index 00000000..67cde749 --- /dev/null +++ b/lib/json_util.c @@ -0,0 +1,51 @@ +/***************************************************************************\ +* * +* BitlBee - An IRC to IM gateway * +* Helper functions for json.c * +* * +* Copyright 2012 Wilmer van der Gaast * +* * +* This library 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, version * +* 2.1. * +* * +* This library 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 this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * +* * +****************************************************************************/ + +#include +#include + +#include "json_util.h" + +json_value *json_o_get( json_value *obj, json_char *name ) +{ + int i; + + if( obj->type != json_object ) + return NULL; + + for( i = 0; i < obj->u.object.length; ++ i) + if( strcmp( obj->u.object.values[i].name, name ) == 0 ) + return obj->u.object.values[i].value; + + return NULL; +} + +const char *json_o_str( json_value *obj, json_char *name ) +{ + json_value *ret = json_o_get( obj, name ); + + if( ret ) + return ret->u.string.ptr; + else + return NULL; +} -- cgit v1.2.3 From 8e3b7acdfc96e086f1cd1c606e46d9a91d46a842 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 8 Nov 2012 22:38:20 +0000 Subject: It logs in and fetches statuses! \o/ But, some corruption.. --- lib/json_util.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lib/json_util.c') diff --git a/lib/json_util.c b/lib/json_util.c index 67cde749..43e687e3 100644 --- a/lib/json_util.c +++ b/lib/json_util.c @@ -26,7 +26,7 @@ #include "json_util.h" -json_value *json_o_get( json_value *obj, json_char *name ) +json_value *json_o_get( const json_value *obj, const json_char *name ) { int i; @@ -40,12 +40,22 @@ json_value *json_o_get( json_value *obj, json_char *name ) return NULL; } -const char *json_o_str( json_value *obj, json_char *name ) +const char *json_o_str( const json_value *obj, const json_char *name ) { json_value *ret = json_o_get( obj, name ); - if( ret ) + if( ret && ret->type == json_string ) return ret->u.string.ptr; else return NULL; } + +char *json_o_strdup( const json_value *obj, const json_char *name ) +{ + json_value *ret = json_o_get( obj, name ); + + if( ret && ret->type == json_string && ret->u.string.ptr ) + return g_memdup( ret->u.string.ptr, ret->u.string.length + 1 ); + else + return NULL; +} -- cgit v1.2.3 From 5246133a607561abe8096c471de02bddeb6be67c Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Fri, 9 Nov 2012 00:23:44 +0000 Subject: Updated error response parsing. Also, use this for 401 responses so for example the auth errors caused by NTP desync are clearer: twitter - Login error: Authentication failure (401 Unauthorized (Timestamp out of bounds)) --- lib/json_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/json_util.c') diff --git a/lib/json_util.c b/lib/json_util.c index 43e687e3..470a6b02 100644 --- a/lib/json_util.c +++ b/lib/json_util.c @@ -30,7 +30,7 @@ json_value *json_o_get( const json_value *obj, const json_char *name ) { int i; - if( obj->type != json_object ) + if( !obj || obj->type != json_object ) return NULL; for( i = 0; i < obj->u.object.length; ++ i) -- cgit v1.2.3 From 398b2533afe5860e1b83edb0fcf5d7cf80207162 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Fri, 9 Nov 2012 23:50:09 +0000 Subject: Fix compiler warning (missed glib include in json_util). --- lib/json_util.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/json_util.c') diff --git a/lib/json_util.c b/lib/json_util.c index 470a6b02..941589ba 100644 --- a/lib/json_util.c +++ b/lib/json_util.c @@ -23,6 +23,7 @@ #include #include +#include #include "json_util.h" -- cgit v1.2.3