[Olsr-dev] [PATCH v1 037/115] jsoninfo: put output start/end in the functions table

Ferry Huberts (spam-protected)
Wed Dec 2 21:28:10 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/jsoninfo/src/jsoninfo_printers.c | 16 ++++++++++++++++
 lib/jsoninfo/src/jsoninfo_printers.h |  3 +++
 lib/jsoninfo/src/olsrd_jsoninfo.c    | 16 ++++++----------
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/lib/jsoninfo/src/jsoninfo_printers.c b/lib/jsoninfo/src/jsoninfo_printers.c
index d8f0953..5806b95 100644
--- a/lib/jsoninfo/src/jsoninfo_printers.c
+++ b/lib/jsoninfo/src/jsoninfo_printers.c
@@ -135,6 +135,22 @@ const char * determine_mime_type(unsigned int send_what) {
   return (send_what & SIW_ALL) ? "application/json; charset=utf-8" : "text/plain; charset=utf-8";
 }
 
+void output_start(struct autobuf *abuf) {
+  /* global variables for tracking when to put a comma in for JSON */
+  abuf_json_reset_entry_number_and_depth();
+  abuf_json_mark_output(true, abuf);
+
+  abuf_json_int(abuf, "systemTime", time(NULL));
+  abuf_json_int(abuf, "timeSinceStartup", now_times);
+  if (*uuid)
+    abuf_json_string(abuf, "uuid", uuid);
+}
+
+void output_end(struct autobuf *abuf) {
+  abuf_json_mark_output(false, abuf);
+  abuf_puts(abuf, "\n");
+}
+
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
   struct ipaddr_str buf1;
   struct neighbor_entry *neigh;
diff --git a/lib/jsoninfo/src/jsoninfo_printers.h b/lib/jsoninfo/src/jsoninfo_printers.h
index df0b974..71b3471 100644
--- a/lib/jsoninfo/src/jsoninfo_printers.h
+++ b/lib/jsoninfo/src/jsoninfo_printers.h
@@ -55,6 +55,9 @@ bool isCommand(const char *str, unsigned int siw);
 
 const char * determine_mime_type(unsigned int send_what);
 
+void output_start(struct autobuf *abuf);
+void output_end(struct autobuf *abuf);
+
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
 void ipc_print_links(struct autobuf *abuf);
 void ipc_print_routes(struct autobuf *abuf);
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
index 028366b..4977ff6 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.c
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
@@ -90,6 +90,8 @@ static printer_functions_t printer_functions = { //
         .init = &plugin_init, //
         .is_command = &isCommand, //
         .determine_mime_type = &determine_mime_type, //
+        .output_start = &output_start, //
+        .output_end = &output_end, //
         .neighbors = &ipc_print_neighbors, //
         .links = &ipc_print_links, //
         .routes = &ipc_print_routes, //
@@ -412,14 +414,8 @@ static void send_info(unsigned int send_what, int the_socket) {
 
   // only add if normal format
   if (send_what & SIW_ALL) {
-    /* global variables for tracking when to put a comma in for JSON */
-    abuf_json_reset_entry_number_and_depth();
-    abuf_json_mark_output(true, &abuf);
-
-    abuf_json_int(&abuf, "systemTime", time(NULL));
-    abuf_json_int(&abuf, "timeSinceStartup", now_times);
-    if (*uuid)
-      abuf_json_string(&abuf, "uuid", uuid);
+    if (printer_functions.output_start)
+      (*printer_functions.output_start)(&abuf);
 
     if ((send_what & SIW_LINKS) && printer_functions.links)
       (*printer_functions.links)(&abuf);
@@ -448,8 +444,8 @@ static void send_info(unsigned int send_what, int the_socket) {
     if ((send_what & SIW_PLUGINS) && printer_functions.plugins)
       (*printer_functions.plugins)(&abuf);
 
-    abuf_json_mark_output(false, &abuf);
-    abuf_puts(&abuf, "\n");
+    if (printer_functions.output_end)
+      (*printer_functions.output_end)(&abuf);
   } else if ((send_what & SIW_OLSRD_CONF) && printer_functions.olsrd_conf) {
     /* this outputs the olsrd.conf text directly, not normal format */
     (*printer_functions.olsrd_conf)(&abuf);
-- 
2.5.0




More information about the Olsr-dev mailing list