Patchwork [1/3] lib: fwts_json: wrapper for json error returns

login
register
mail settings
Submitter Colin King
Date Oct. 14, 2012, 9:16 p.m.
Message ID <1350249368-10846-2-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/191434/
State Accepted
Headers show

Comments

Colin King - Oct. 14, 2012, 9:16 p.m.
From: Colin Ian King <colin.king@canonical.com>

Older versions of json-c may return an error in an
object as a ((json_object*)-1), where as newer
versions return NULL.  To cope with this API change
we create a macro FWTS_JSON_ERROR that will return
true if an object pointer is flagging an error
condition as a NULL or a ((json_object*)-1) pointer.
This way we can cope with errors no matter which
version of json-c we use.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts.h      |    1 +
 src/lib/include/fwts_json.h |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 src/lib/include/fwts_json.h
Keng-Yu Lin - Oct. 17, 2012, 8:15 a.m.
On Mon, Oct 15, 2012 at 5:16 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Older versions of json-c may return an error in an
> object as a ((json_object*)-1), where as newer
> versions return NULL.  To cope with this API change
> we create a macro FWTS_JSON_ERROR that will return
> true if an object pointer is flagging an error
> condition as a NULL or a ((json_object*)-1) pointer.
> This way we can cope with errors no matter which
> version of json-c we use.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts.h      |    1 +
>  src/lib/include/fwts_json.h |   34 ++++++++++++++++++++++++++++++++++
>  2 files changed, 35 insertions(+)
>  create mode 100644 src/lib/include/fwts_json.h
>
> diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
> index a0f3c1b..a1e0fd2 100644
> --- a/src/lib/include/fwts.h
> +++ b/src/lib/include/fwts.h
> @@ -75,5 +75,6 @@
>  #include "fwts_ac_adapter.h"
>  #include "fwts_battery.h"
>  #include "fwts_button.h"
> +#include "fwts_json.h"
>
>  #endif
> diff --git a/src/lib/include/fwts_json.h b/src/lib/include/fwts_json.h
> new file mode 100644
> index 0000000..a9be1af
> --- /dev/null
> +++ b/src/lib/include/fwts_json.h
> @@ -0,0 +1,34 @@
> +/*
> + * Copyright (C) 2012 Canonical
> + *
> + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
> + *
> + */
> +
> +#ifndef __FWTS_JSON_H__
> +#define __FWTS_JSON_H__
> +
> +#include <json/json.h>
> +
> +#define __FWTS_JSON_ERR_PTR__ ((json_object*) -1)
> +/*
> + *  Older versions of json-c may return an error in an
> + *  object as a ((json_object*)-1), where as newer
> + *  versions return NULL, so check for these. Sigh.
> + */
> +#define FWTS_JSON_ERROR(ptr) \
> +       ( (ptr == NULL) || ((json_object*)ptr == __FWTS_JSON_ERR_PTR__) )
> +
> +#endif
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu - Oct. 22, 2012, 8:47 a.m.
On 10/15/2012 05:16 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Older versions of json-c may return an error in an
> object as a ((json_object*)-1), where as newer
> versions return NULL.  To cope with this API change
> we create a macro FWTS_JSON_ERROR that will return
> true if an object pointer is flagging an error
> condition as a NULL or a ((json_object*)-1) pointer.
> This way we can cope with errors no matter which
> version of json-c we use.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/include/fwts.h      |    1 +
>   src/lib/include/fwts_json.h |   34 ++++++++++++++++++++++++++++++++++
>   2 files changed, 35 insertions(+)
>   create mode 100644 src/lib/include/fwts_json.h
>
> diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
> index a0f3c1b..a1e0fd2 100644
> --- a/src/lib/include/fwts.h
> +++ b/src/lib/include/fwts.h
> @@ -75,5 +75,6 @@
>   #include "fwts_ac_adapter.h"
>   #include "fwts_battery.h"
>   #include "fwts_button.h"
> +#include "fwts_json.h"
>
>   #endif
> diff --git a/src/lib/include/fwts_json.h b/src/lib/include/fwts_json.h
> new file mode 100644
> index 0000000..a9be1af
> --- /dev/null
> +++ b/src/lib/include/fwts_json.h
> @@ -0,0 +1,34 @@
> +/*
> + * Copyright (C) 2012 Canonical
> + *
> + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
> + *
> + */
> +
> +#ifndef __FWTS_JSON_H__
> +#define __FWTS_JSON_H__
> +
> +#include <json/json.h>
> +
> +#define __FWTS_JSON_ERR_PTR__ ((json_object*) -1)
> +/*
> + *  Older versions of json-c may return an error in an
> + *  object as a ((json_object*)-1), where as newer
> + *  versions return NULL, so check for these. Sigh.
> + */
> +#define FWTS_JSON_ERROR(ptr) \
> +	( (ptr == NULL) || ((json_object*)ptr == __FWTS_JSON_ERR_PTR__) )
> +
> +#endif
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
index a0f3c1b..a1e0fd2 100644
--- a/src/lib/include/fwts.h
+++ b/src/lib/include/fwts.h
@@ -75,5 +75,6 @@ 
 #include "fwts_ac_adapter.h"
 #include "fwts_battery.h"
 #include "fwts_button.h"
+#include "fwts_json.h"
 
 #endif
diff --git a/src/lib/include/fwts_json.h b/src/lib/include/fwts_json.h
new file mode 100644
index 0000000..a9be1af
--- /dev/null
+++ b/src/lib/include/fwts_json.h
@@ -0,0 +1,34 @@ 
+/*
+ * Copyright (C) 2012 Canonical
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef __FWTS_JSON_H__
+#define __FWTS_JSON_H__
+
+#include <json/json.h>
+
+#define __FWTS_JSON_ERR_PTR__ ((json_object*) -1)
+/*
+ *  Older versions of json-c may return an error in an
+ *  object as a ((json_object*)-1), where as newer
+ *  versions return NULL, so check for these. Sigh.
+ */
+#define FWTS_JSON_ERROR(ptr) \
+	( (ptr == NULL) || ((json_object*)ptr == __FWTS_JSON_ERR_PTR__) )
+
+#endif