jsoninfo: put output start/end in the functions table
authorFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 2 Dec 2015 10:40:11 +0000 (11:40 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 2 Dec 2015 17:41:14 +0000 (18:41 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/jsoninfo/src/jsoninfo_printers.c
lib/jsoninfo/src/jsoninfo_printers.h
lib/jsoninfo/src/olsrd_jsoninfo.c

index d8f0953..5806b95 100644 (file)
@@ -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";
 }
 
   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;
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
   struct ipaddr_str buf1;
   struct neighbor_entry *neigh;
index df0b974..71b3471 100644 (file)
@@ -55,6 +55,9 @@ bool isCommand(const char *str, unsigned int siw);
 
 const char * determine_mime_type(unsigned int send_what);
 
 
 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);
 void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
 void ipc_print_links(struct autobuf *abuf);
 void ipc_print_routes(struct autobuf *abuf);
index 028366b..4977ff6 100644 (file)
@@ -90,6 +90,8 @@ static printer_functions_t printer_functions = { //
         .init = &plugin_init, //
         .is_command = &isCommand, //
         .determine_mime_type = &determine_mime_type, //
         .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, //
         .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) {
 
   // 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);
 
     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);
 
     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);
   } 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);