[mw-devel] [Git][arthur/mw][master] Only use the variadic json_addstring when its needed

Justin Mitchell arthur at sucs.org
Tue Jan 12 18:03:39 GMT 2016


Justin Mitchell pushed to branch master at Justin Mitchell / mw


Commits:
a9c50f14 by Justin Mitchell at 2016-01-12T18:02:51Z
Only use the variadic json_addstring when its needed

- - - - -


3 changed files:

- src/server/actions.c
- src/socket.c
- src/socket.h


Changes:

=====================================
src/server/actions.c
=====================================
--- a/src/server/actions.c
+++ b/src/server/actions.c
@@ -90,7 +90,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 		json_addstring(ej, "type", "mrod");
 
 		if (success) {
-			json_addstring(ej, "text", "%s has just dropped the Magic Roundabout of Death on %s",
+			json_vaddstring(ej, "text", "%s has just dropped the Magic Roundabout of Death on %s",
 			               attacker_name, victim_name);
 			json_addstring(ej, "verbose", "You hear a boing in the distance, closely followed by the distant wail of a hurdy-gurdy, which is suddenly terminated by a blood curdling scream, a C major chord explosion and a few curious springing noises...");
 
@@ -109,7 +109,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 			ipcmsg_destroy(update);
 
 		} else {
-			json_addstring(ej, "text", "%s just tried to MROD %s and failed",
+			json_vaddstring(ej, "text", "%s just tried to MROD %s and failed",
 			               attacker_name, victim_name);
 		}
 		ipcmsg_json_encode(event, ej);
@@ -131,7 +131,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 		json_addstring(ej, "type", "zod");
 
 		if (success) {
-			json_addstring(ej, "text", "%s has just sent the Zebedee of Death to %s",
+			json_vaddstring(ej, "text", "%s has just sent the Zebedee of Death to %s",
 			               attacker_name, victim_name);
 			json_addstring(ej, "verbose", "You hear a boing in the distance");
 
@@ -150,7 +150,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 			msg_attach_to_username(update, victim_name);
 			ipcmsg_destroy(update);
 		} else {
-			json_addstring(ej, "text", "%s just tried to ZoD %s and failed",
+			json_vaddstring(ej, "text", "%s just tried to ZoD %s and failed",
 			               attacker_name, victim_name);
 		}
 		ipcmsg_json_encode(event, ej);
@@ -184,7 +184,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 		json_addstring(ej, "type", "gag");
 
 		if (success) {
-			json_addstring(ej, "text", "%s has just gagged %s with %s",
+			json_vaddstring(ej, "text", "%s has just gagged %s with %s",
 			               attacker_name, victim_name, gag_type(gtnum));
 
 			// change users mode
@@ -196,7 +196,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 			msg_attach_to_username(update, victim_name);
 			ipcmsg_destroy(update);
 		} else {
-			json_addstring(ej, "text", "%s just tried to gag %s with %s and failed",
+			json_vaddstring(ej, "text", "%s just tried to gag %s with %s and failed",
 			               attacker_name, victim_name, gag_type(gtnum));
 		}
 		ipcmsg_json_encode(event, ej);
@@ -216,7 +216,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 		json_addstring(ej, "type", "ungag");
 
 		if (success) {
-			json_addstring(ej, "text", "%s has just ungagged %s", attacker_name, victim_name);
+			json_vaddstring(ej, "text", "%s has just ungagged %s", attacker_name, victim_name);
 
 			// change users mode
 			_autofree char *buff=NULL;
@@ -227,7 +227,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
 			msg_attach_to_username(update, victim_name);
 			ipcmsg_destroy(update);
 		} else {
-			json_addstring(ej, "text", "%s just tried to ungag %s and failed",
+			json_vaddstring(ej, "text", "%s just tried to ungag %s and failed",
 			               attacker_name, victim_name);
 		}
 		ipcmsg_json_encode(event, ej);


=====================================
src/socket.c
=====================================
--- a/src/socket.c
+++ b/src/socket.c
@@ -300,25 +300,31 @@ json_t * json_init(ipc_message_t * msg)
 
 /** add/replace a string value in the base object */
 
-int json_addstring(json_t * js, const char * key, const char * value, ...) __attribute__((format (printf, 3, 4)));
+int json_vaddstring(json_t * js, const char * key, const char * value, ...) __attribute__((format (printf, 3, 4)));
 
-int json_addstring(json_t * js, const char * key, const char * value, ...)
+int json_vaddstring(json_t * js, const char * key, const char * value, ...)
 {
-	json_t * tmp;
 	va_list va;
 	va_start(va, value);
 	_autofree char *text = NULL;
 	vasprintf(&text, value, va);
 	va_end(va);
 
+	return json_addstring(js, key, text);
+}
+
+int json_addstring(json_t * js, const char * key, const char * value)
+{
+	json_t * tmp;
+
 	tmp = json_object_get(js, key);
 	if (tmp == NULL) {
-		tmp = json_string(text);
+		tmp = json_string(value);
 		if (tmp == NULL) return -1;
 		json_object_set_new(js, key, tmp);
 	} else {
 		if (!json_is_string(tmp)) return -1;
-		json_string_set(tmp, text);
+		json_string_set(tmp, value);
 	}
 	return 0;
 }


=====================================
src/socket.h
=====================================
--- a/src/socket.h
+++ b/src/socket.h
@@ -78,7 +78,8 @@ ipc_message_t *read_socket(ipc_connection_t *conn, int doread);
 json_t *ipcmsg_json_decode(ipc_message_t *msg);
 int ipcmsg_json_encode(ipc_message_t *msg, json_t *js);
 json_t *json_init(ipc_message_t *msg);
-int json_addstring(json_t *js, const char *key, const char *value, ...);
+int json_vaddstring(json_t *js, const char *key, const char *value, ...);
+int json_addstring(json_t *js, const char *key, const char *value);
 int json_addint(json_t *js, const char *key, int value);
 int json_addobject(json_t *js, const char *key, json_t * obj);
 const char *json_getstring(json_t *js, const char *key);



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/a9c50f1486d90461b2b9f6c2ff367ede6704d247
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20160112/03074d19/attachment-0001.html>


More information about the mw-devel mailing list