From patchwork Thu Aug 3 15:54:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Fortin X-Patchwork-Id: 797294 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="XvA7X2/X"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xNZTy6FN1z9s8V for ; Fri, 4 Aug 2017 01:58:30 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751974AbdHCP62 (ORCPT ); Thu, 3 Aug 2017 11:58:28 -0400 Received: from mail-wr0-f169.google.com ([209.85.128.169]:34790 "EHLO mail-wr0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751158AbdHCPzi (ORCPT ); Thu, 3 Aug 2017 11:55:38 -0400 Received: by mail-wr0-f169.google.com with SMTP id 12so7403460wrb.1 for ; Thu, 03 Aug 2017 08:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PQsbd/jo2YWILwOyQzHFEA2TSsM44CWl5H3643eUPe0=; b=XvA7X2/XPLEvL7xK6niD2C6i6CAOVQBX/S7hwtCfDhLJEZgaeDDUHFR4xdrBGISzAs kNgYCGOKQAOL/DeBiyWcE6fv4nC9u7Q5hgHXeJgl06kmbEQCMWaFv6hRJBHSfiDw1hEv RD2OlEEVxb3uZYEtiaSGTcC+4LnQA+Mke4me8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PQsbd/jo2YWILwOyQzHFEA2TSsM44CWl5H3643eUPe0=; b=RSKUWTEV311IJ1MeRPgHChHw6BBii1+OCEqlUWnakrtqhvtlai3dTSmobqMvuHMJj0 zntw2PZ96j3EJnMowOglM6PL0yPm1qVaWmDPsNe1fnANSLgw6DmRyX09hs+zGmai+lwy ENMBEPyVS62goSN4PiXDuKqKkdkhGvnjGg10DpNOF+bUeZ0L0UI9s7Ug70vhpo1DJHy+ xBslNfn0UZ6U6KdM4NppxxGXFE16bWDqCpGMSCtOpTVVT/sodS/2ogFlyp8Id6X3DG6I Dn8q1VcxMzE9w5oXcsvwIlbbA0bSBeAJld0zk1K/5dWfLN6W1YJqFYhCnkz8PKS4yTJt qNeA== X-Gm-Message-State: AIVw112GHdqISx8O9R0QGdPuZRojMGiFNiQAc6oe1734R1xYf+tKx6T4 J9Kno2lRZtxDxrtWUKg= X-Received: by 10.223.173.246 with SMTP id w109mr1664815wrc.113.1501775736457; Thu, 03 Aug 2017 08:55:36 -0700 (PDT) Received: from localhost.localdomain (91-160-18-219.subs.proxad.net. [91.160.18.219]) by smtp.googlemail.com with ESMTPSA id d91sm2213309wma.23.2017.08.03.08.55.35 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 03 Aug 2017 08:55:35 -0700 (PDT) From: Julien Fortin X-Google-Original-From: Julien Fortin To: netdev@vger.kernel.org Cc: roopa@cumulusnetworks.com, nikolay@cumulusnetworks.com, dsa@cumulusnetworks.com, Julien Fortin Subject: [PATCH 03/27] json_writer: add new json handlers (null, float with format, lluint, hu) Date: Thu, 3 Aug 2017 17:54:51 +0200 Message-Id: <20170803155515.99226-4-julien@cumulusnetworks.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170803155515.99226-1-julien@cumulusnetworks.com> References: <20170803155515.99226-1-julien@cumulusnetworks.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Julien Fortin Signed-off-by: Julien Fortin --- include/json_writer.h | 9 +++++++++ lib/json_writer.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/json_writer.h b/include/json_writer.h index ab9a008a..1516aafb 100644 --- a/include/json_writer.h +++ b/include/json_writer.h @@ -33,20 +33,29 @@ void jsonw_pretty(json_writer_t *self, bool on); void jsonw_name(json_writer_t *self, const char *name); /* Add value */ +void jsonw_printf(json_writer_t *self, const char *fmt, ...); void jsonw_string(json_writer_t *self, const char *value); void jsonw_bool(json_writer_t *self, bool value); void jsonw_float(json_writer_t *self, double number); +void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); void jsonw_uint(json_writer_t *self, uint64_t number); +void jsonw_hu(json_writer_t *self, unsigned short number); void jsonw_int(json_writer_t *self, int64_t number); void jsonw_null(json_writer_t *self); +void jsonw_lluint(json_writer_t *self, unsigned long long int num); /* Useful Combinations of name and value */ void jsonw_string_field(json_writer_t *self, const char *prop, const char *val); void jsonw_bool_field(json_writer_t *self, const char *prop, bool value); void jsonw_float_field(json_writer_t *self, const char *prop, double num); void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); +void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); void jsonw_null_field(json_writer_t *self, const char *prop); +void jsonw_lluint_field(json_writer_t *self, const char *prop, + unsigned long long int num); +void jsonw_float_field_fmt(json_writer_t *self, const char *prop, + const char *fmt, double val); /* Collections */ void jsonw_start_object(json_writer_t *self); diff --git a/lib/json_writer.c b/lib/json_writer.c index 9fc05e96..6b77d288 100644 --- a/lib/json_writer.c +++ b/lib/json_writer.c @@ -156,7 +156,7 @@ void jsonw_name(json_writer_t *self, const char *name) putc(' ', self->out); } -static void jsonw_printf(json_writer_t *self, const char *fmt, ...) +void jsonw_printf(json_writer_t *self, const char *fmt, ...) { va_list ap; @@ -199,23 +199,38 @@ void jsonw_bool(json_writer_t *self, bool val) jsonw_printf(self, "%s", val ? "true" : "false"); } -#ifdef notused void jsonw_null(json_writer_t *self) { jsonw_printf(self, "null"); } +void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num) +{ + jsonw_printf(self, fmt, num); +} + +#ifdef notused void jsonw_float(json_writer_t *self, double num) { jsonw_printf(self, "%g", num); } #endif +void jsonw_hu(json_writer_t *self, unsigned short num) +{ + jsonw_printf(self, "%hu", num); +} + void jsonw_uint(json_writer_t *self, uint64_t num) { jsonw_printf(self, "%"PRIu64, num); } +void jsonw_lluint(json_writer_t *self, unsigned long long int num) +{ + jsonw_printf(self, "%llu", num); +} + void jsonw_int(json_writer_t *self, int64_t num) { jsonw_printf(self, "%"PRId64, num); @@ -242,25 +257,46 @@ void jsonw_float_field(json_writer_t *self, const char *prop, double val) } #endif +void jsonw_float_field_fmt(json_writer_t *self, + const char *prop, + const char *fmt, + double val) +{ + jsonw_name(self, prop); + jsonw_float_fmt(self, fmt, val); +} + void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num) { jsonw_name(self, prop); jsonw_uint(self, num); } +void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num) +{ + jsonw_name(self, prop); + jsonw_hu(self, num); +} + +void jsonw_lluint_field(json_writer_t *self, + const char *prop, + unsigned long long int num) +{ + jsonw_name(self, prop); + jsonw_lluint(self, num); +} + void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num) { jsonw_name(self, prop); jsonw_int(self, num); } -#ifdef notused void jsonw_null_field(json_writer_t *self, const char *prop) { jsonw_name(self, prop); jsonw_null(self); } -#endif #ifdef TEST int main(int argc, char **argv)