<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40284 >

 This patch adds function send_chat_printf() for handling building of
messages. Used where appropriate (saves about 50 lines of code)


 - ML

diff -Nurd -X.diff_ignore freeciv/client/chatline_common.c 
freeciv/client/chatline_common.c
--- freeciv/client/chatline_common.c    2008-03-08 16:13:03.000000000 +0200
+++ freeciv/client/chatline_common.c    2008-06-16 00:58:37.000000000 +0300
@@ -67,6 +67,24 @@
   dsend_packet_chat_msg_req(&client.conn, message);
 }
 
+/**************************************************************************
+  Send the message as a chat to the server. Message is constructed
+  in printf style.
+**************************************************************************/
+void send_chat_printf(const char *format, ...)
+{
+  char msg[50];
+  int maxlen = sizeof(msg);
+
+  va_list ap;
+  va_start(ap, format);
+  my_vsnprintf(msg, maxlen, format, ap);
+  msg[maxlen] = '\0'; /* Make sure there is always ending zero */
+  send_chat(msg);
+  va_end(ap);
+}
+
+
 static int frozen_level = 0;
 
 /**************************************************************************
diff -Nurd -X.diff_ignore freeciv/client/chatline_common.h 
freeciv/client/chatline_common.h
--- freeciv/client/chatline_common.h    2008-02-04 08:53:36.000000000 +0200
+++ freeciv/client/chatline_common.h    2008-06-16 00:58:37.000000000 +0300
@@ -16,6 +16,8 @@
 #include "shared.h"            /* bool type */
 
 void send_chat(const char *message);
+void send_chat_printf(const char *format, ...)
+  fc__attribute((__format__ (__printf__, 1, 2)));
 
 void chatline_common_init(void);
 void chatline_common_done(void);
diff -Nurd -X.diff_ignore freeciv/client/connectdlg_common.c 
freeciv/client/connectdlg_common.c
--- freeciv/client/connectdlg_common.c  2008-03-08 16:13:03.000000000 +0200
+++ freeciv/client/connectdlg_common.c  2008-06-16 01:38:46.000000000 +0300
@@ -183,7 +183,7 @@
 #if !defined(HAVE_WORKING_FORK) && !defined(WIN32_NATIVE)
   /* Can't do much without fork */
   return FALSE;
-#else
+#else /* HAVE_WORKING_FORK || WIN32_NATIVE */
   char buf[512];
   int connect_tries = 0;
 # ifdef WIN32_NATIVE
@@ -196,7 +196,7 @@
   char cmdline3[512];
   char logcmdline[512];
   char scriptcmdline[512];
-# endif
+# endif /* WIN32_NATIVE */
 
   /* only one server (forked from this client) shall be running at a time */
   /* This also resets client_has_hack. */
@@ -275,7 +275,7 @@
      * Calling exit here is dangerous due to X11 problems (async replies) */ 
     _exit(1);
   } 
-# else
+# else /* HAVE_WORKING_FORK */
 #  ifdef WIN32_NATIVE
   if (logfile) {
     loghandle = CreateFile(logfile, GENERIC_WRITE,
@@ -326,8 +326,8 @@
   
   server_process = pi.hProcess;
 
-#  endif
-# endif
+#  endif /* WIN32_NATIVE */
+# endif /* HAVE_WORKING_FORK */
  
   /* a reasonable number of tries */ 
   while (connect_to_server(user_name, "localhost", internal_server_port, 
@@ -338,8 +338,8 @@
     if (waitpid(server_pid, NULL, WNOHANG) != 0) {
       break;
     }
-#endif
-#endif
+#endif /* WIN32_NATIVE */
+#endif /* HAVE_WORKING_FORK */
     if (connect_tries++ > NUMBER_OF_TRIES) {
       break;
     }
@@ -373,16 +373,15 @@
    * has sufficient permissions to do so (it doesn't have HACK access yet) it
    * is safe enough.  Note that if you load a savegame the topology will be
    * set but then overwritten during the load. */
-  my_snprintf(buf, sizeof(buf), "/set topology %d",
-             (TF_WRAPX
-              | ((tileset_is_isometric(tileset)
-                  && tileset_hex_height(tileset) == 0) ? TF_ISO : 0)
-              | ((tileset_hex_width(tileset) != 0
-                  || tileset_hex_height(tileset) != 0) ? TF_HEX : 0)));
-  send_chat(buf);
+  send_chat_printf("/set topology %d",
+                   (TF_WRAPX
+                    | ((tileset_is_isometric(tileset)
+                        && tileset_hex_height(tileset) == 0) ? TF_ISO : 0)
+                    | ((tileset_hex_width(tileset) != 0
+                        || tileset_hex_height(tileset) != 0) ? TF_HEX : 0)));
 
   return TRUE;
-#endif
+#endif /* HAVE_WORKING_FORK || WIN32_NATIVE */
 }
 
 /*************************************************************************
@@ -488,13 +487,9 @@
 *****************************************************************/ 
 void send_start_saved_game(void)
 {   
-  char buf[MAX_LEN_MSG];
-
   send_chat("/set timeout 0");
   send_chat("/set autotoggle 1");
-  my_snprintf(buf, sizeof(buf), "/take \"%s\" \"%s\"",
-             user_name, leader_name);
-  send_chat(buf);
+  send_chat_printf("/take \"%s\" \"%s\"", user_name, leader_name);
   send_chat("/start");
 }
 
@@ -503,15 +498,11 @@
 *****************************************************************/ 
 void send_save_game(char *filename)
 {   
-  char message[MAX_LEN_MSG];
-
   if (filename) {
-    my_snprintf(message, MAX_LEN_MSG, "/save %s", filename);
+    send_chat_printf("/save %s", filename);
   } else {
-    my_snprintf(message, MAX_LEN_MSG, "/save");
+    send_chat("/save");
   }
-
-  send_chat(message);
 }
 
 /**************************************************************************
diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/connectdlg.c 
freeciv/client/gui-gtk-2.0/connectdlg.c
--- freeciv/client/gui-gtk-2.0/connectdlg.c     2008-03-08 16:12:56.000000000 
+0200
+++ freeciv/client/gui-gtk-2.0/connectdlg.c     2008-06-16 01:35:49.000000000 
+0300
@@ -76,10 +76,7 @@
     if (is_save) {
       send_save_game(filename);
     } else {
-      char message[MAX_LEN_MSG];
-
-      my_snprintf(message, sizeof(message), "/load %s", filename);
-      send_chat(message);
+      send_chat_printf("/load %s", filename);
     }
 
     g_free(filename);
diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/pages.c 
freeciv/client/gui-gtk-2.0/pages.c
--- freeciv/client/gui-gtk-2.0/pages.c  2008-06-09 18:58:57.000000000 +0300
+++ freeciv/client/gui-gtk-2.0/pages.c  2008-06-16 01:08:04.000000000 +0300
@@ -929,7 +929,6 @@
 static void ai_skill_callback(GtkWidget *w, gpointer data)
 {
   const char *name;
-  char buf[512];
   enum ai_level level = GPOINTER_TO_UINT(data);
 
   if (level == AI_LEVEL_LAST) {
@@ -938,8 +937,7 @@
 
   name = ai_level_cmd(level);
 
-  my_snprintf(buf, sizeof(buf), "/%s", name);
-  send_chat(buf);
+  send_chat_printf("/%s", name);
 }
 
 /* HACK: sometimes when creating the ruleset combo the value is set without
@@ -966,12 +964,9 @@
 static bool send_new_aifill_to_server = TRUE;
 static void ai_fill_callback(GtkWidget *w, gpointer data)
 {
-  char buf[512];
-
   if (send_new_aifill_to_server) {
-    my_snprintf(buf, sizeof(buf), "/set aifill %d",
-      gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(w)));
-    send_chat(buf);
+    send_chat_printf("/set aifill %d",
+                     gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(w)));
   }
 }
 
@@ -1009,11 +1004,7 @@
       /* Make sure player reverts to AI control. This is much more neat,
        * and hides the ugly double username in the name list because
        * the player username equals the connection username. */
-      char buf[512];
-
-      my_snprintf(buf, sizeof(buf), "/aitoggle \"%s\"",
-                  player_name(client.conn.playing));
-      send_chat(buf);
+      send_chat_printf("/aitoggle \"%s\"", player_name(client.conn.playing));
     }
     send_chat("/detach");
     send_chat("/observe");
@@ -1045,13 +1036,11 @@
 static void conn_menu_team_chosen(GtkMenuItem *menuitem, gpointer data)
 {
   struct team *pteam = data;
-  char buf[1024];
 
   if (pteam != conn_menu_player->team) {
-    my_snprintf(buf, sizeof(buf), "/team \"%s\" \"%s\"",
-               player_name(conn_menu_player),
-               team_rule_name(pteam));
-    send_chat(buf);
+    send_chat_printf("/team \"%s\" \"%s\"",
+                     player_name(conn_menu_player),
+                     team_rule_name(pteam));
   }
 }
 
@@ -1080,15 +1069,12 @@
 ****************************************************************************/
 static void conn_menu_player_command(GtkMenuItem *menuitem, gpointer data)
 {
-  char buf[1024];
   char *command = data;
 
   assert(command != NULL);
   assert(conn_menu_player != NULL);
 
-  my_snprintf(buf, sizeof(buf), "/%s \"%s\"", command, 
-              player_name(conn_menu_player));
-  send_chat(buf);
+  send_chat_printf("/%s \"%s\"", command, player_name(conn_menu_player));
 }
 
 /****************************************************************************
@@ -1096,17 +1082,11 @@
 ****************************************************************************/
 static void conn_menu_player_take(GtkMenuItem *menuitem, gpointer data)
 {
-  char buf[1024];
-
   if (conn_menu_player->ai.control) {
     /* See comment on detach command for why */
-    my_snprintf(buf, sizeof(buf), "/aitoggle \"%s\"",
-                player_name(conn_menu_player));
-    send_chat(buf);
+    send_chat_printf("/aitoggle \"%s\"", player_name(conn_menu_player));
   }
-  my_snprintf(buf, sizeof(buf), "/take \"%s\"",
-              player_name(conn_menu_player));
-  send_chat(buf);
+  send_chat_printf("/take \"%s\"", player_name(conn_menu_player));
 }
 
 /****************************************************************************
@@ -1115,15 +1095,12 @@
 ****************************************************************************/
 static void conn_menu_connection_command(GtkMenuItem *menuitem, gpointer data)
 {
-  char buf[1024];
   const char *command = data;
 
   assert(conn_menu_conn != NULL);
   assert(command != NULL);
 
-  my_snprintf(buf, sizeof(buf), "/%s \"%s\"",
-              command, conn_menu_conn->username);
-  send_chat(buf);
+  send_chat_printf("/%s \"%s\"", command, conn_menu_conn->username);
 }
 
 /**************************************************************************
@@ -1649,10 +1626,7 @@
   gtk_tree_model_get(GTK_TREE_MODEL(load_store), &it, 1, &filename, -1);
 
   if (is_server_running()) {
-    char message[MAX_LEN_MSG];
-
-    my_snprintf(message, sizeof(message), "/load %s", filename);
-    send_chat(message);
+    send_chat_printf("/load %s", filename);
   }
 }
 
@@ -1828,10 +1802,7 @@
   gtk_tree_model_get(GTK_TREE_MODEL(scenario_store), &it, 1, &filename, -1);
 
   if (is_server_running()) {
-    char message[MAX_LEN_MSG];
-
-    my_snprintf(message, sizeof(message), "/load %s", filename);
-    send_chat(message);
+    send_chat_printf("/load %s", filename);
   }
 }
 
diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/plrdlg.c 
freeciv/client/gui-gtk-2.0/plrdlg.c
--- freeciv/client/gui-gtk-2.0/plrdlg.c 2008-03-08 16:12:56.000000000 +0200
+++ freeciv/client/gui-gtk-2.0/plrdlg.c 2008-06-16 01:09:26.000000000 +0300
@@ -832,13 +832,10 @@
 
   if (gtk_tree_selection_get_selected(players_selection, &model, &it)) {
     gint plrno;
-    char buf[512];
 
     gtk_tree_model_get(model, &it, ncolumns - 1, &plrno, -1);
 
-    my_snprintf(buf, sizeof(buf), "/aitoggle \"%s\"",
-                player_name(player_by_number(plrno)));
-    send_chat(buf);
+    send_chat_printf("/aitoggle \"%s\"", player_name(player_by_number(plrno)));
   }
 }
 
@@ -852,14 +849,12 @@
 
   if (gtk_tree_selection_get_selected(players_selection, &model, &it)) {
     gint plrno;
-    char buf[512];
 
     gtk_tree_model_get(model, &it, ncolumns - 1, &plrno, -1);
 
-    my_snprintf(buf, sizeof(buf), "/%s %s",
-                ai_level_cmd(GPOINTER_TO_UINT(data)),
-                player_name(player_by_number(plrno)));
-    send_chat(buf);
+    send_chat_printf("/%s %s",
+                     ai_level_cmd(GPOINTER_TO_UINT(data)),
+                     player_name(player_by_number(plrno)));
   }
 }
 
diff -Nurd -X.diff_ignore freeciv/client/gui-sdl/chatline.c 
freeciv/client/gui-sdl/chatline.c
--- freeciv/client/gui-sdl/chatline.c   2008-03-08 16:13:02.000000000 +0200
+++ freeciv/client/gui-sdl/chatline.c   2008-06-16 01:22:33.000000000 +0300
@@ -129,10 +129,7 @@
     char *filename = (char*)pWidget->data.ptr;
 
     if (is_server_running()) {
-      char message[MAX_LEN_MSG];
-
-      my_snprintf(message, sizeof(message), "/load %s", filename);
-      send_chat(message);
+      send_chat_printf("/load %s", filename);
       
       if (get_client_page() == PAGE_LOAD) {
         set_client_page(PAGE_START);
diff -Nurd -X.diff_ignore freeciv/client/gui-win32/connectdlg.c 
freeciv/client/gui-win32/connectdlg.c
--- freeciv/client/gui-win32/connectdlg.c       2008-03-08 16:12:59.000000000 
+0200
+++ freeciv/client/gui-win32/connectdlg.c       2008-06-16 01:44:31.000000000 
+0300
@@ -746,8 +746,6 @@
     }
   } else {
     if (GetOpenFileName(&ofn)) {
-      char cmd[MAX_LEN_MSG];
-
       if (current_filename) {
        free(current_filename);
       }
@@ -757,8 +755,7 @@
 
       current_filename = mystrdup(ofn.lpstrFile);
 
-      my_snprintf(cmd, sizeof(cmd), "/load %s", ofn.lpstrFile);
-      send_chat(cmd);
+      send_chat_printf("/load %s", ofn.lpstrFile);
     } else {
       SetCurrentDirectory(dirname);
     }
@@ -776,15 +773,12 @@
 }
 
 /**************************************************************************
-
+  Send parameters for new game to server.
 **************************************************************************/
 static void set_new_game_params(HWND win)
 {
-  int aifill;
-  int aiskill;
-
   char buf[512];
-  char aifill_str[MAX_LEN_MSG - MAX_LEN_USERNAME + 1];
+  int aiskill;
 
   if (!is_server_running()) {
     client_start_server();
@@ -792,18 +786,15 @@
 
   aiskill = ComboBox_GetCurSel(GetDlgItem(newgame_dlg,
                                          ID_NEWGAMEDLG_AISKILL));
-
-  my_snprintf(buf, sizeof(buf), "/%s", ai_level_cmd(aiskill));
-  send_chat(buf);
+  send_chat_printf("/%s", ai_level_cmd(aiskill));
 
 #if 0 
   send_chat("/set autotoggle 1");
 #endif
+
   Edit_GetText(GetDlgItem(newgame_dlg, ID_NEWGAMEDLG_AIFILL), buf, 512);
-  aifill = atoi(buf);
 
-  my_snprintf(aifill_str, sizeof(aifill_str), "/set aifill %d", aifill);
-  send_chat(aifill_str);
+  send_chat_printf("/set aifill %d", atoi(buf));
 
   really_close_connection_dialog();
 
diff -Nurd -X.diff_ignore freeciv/client/gui-win32/repodlgs.c 
freeciv/client/gui-win32/repodlgs.c
--- freeciv/client/gui-win32/repodlgs.c 2008-03-08 16:12:59.000000000 +0200
+++ freeciv/client/gui-win32/repodlgs.c 2008-06-16 01:21:36.000000000 +0300
@@ -829,10 +829,7 @@
            int val = Button_GetState(GetDlgItem(tab_wnds[tab],
                                      ID_OPTIONS_BASE + i)) == BST_CHECKED;
            if (val != o->val) {
-             char buffer[MAX_LEN_MSG];
-             my_snprintf(buffer, MAX_LEN_MSG, "/set %s %d",
-                         o->name, val);
-             send_chat(buffer);
+             send_chat_printf("/set %s %d", o->name, val);
            }
          } else if (SSET_INT == o->stype) {
            char buf[512];
@@ -841,20 +838,14 @@
                                    512);
            val = atoi(buf);
            if (val != o->val) {
-             char buffer[MAX_LEN_MSG];
-             my_snprintf(buffer, MAX_LEN_MSG, "/set %s %d",
-                         o->name, val);
-             send_chat(buffer);
-           }       
+              send_chat_printf("/set %s %d", o->name, val);
+           }
          } else {
            char strval[512];
            Edit_GetText(GetDlgItem(tab_wnds[tab], ID_OPTIONS_BASE + i),
                         strval, 512);
            if (strcmp(strval, o->strval) != 0) {
-             char buffer[MAX_LEN_MSG];
-             my_snprintf(buffer, MAX_LEN_MSG, "/set %s %s",
-                         o->name, strval);
-             send_chat(buffer);
+              send_chat_printf("/set %s %s", o->name, strval);
            }
          }
        }
diff -Nurd -X.diff_ignore freeciv/client/gui-xaw/repodlgs.c 
freeciv/client/gui-xaw/repodlgs.c
--- freeciv/client/gui-xaw/repodlgs.c   2008-03-08 16:12:58.000000000 +0200
+++ freeciv/client/gui-xaw/repodlgs.c   2008-06-16 01:12:10.000000000 +0300
@@ -1485,33 +1485,29 @@
 
       if (setting_class_is_changeable(o->sclass)
          && o->is_visible) {
-       char buffer[MAX_LEN_MSG];
 
        switch (o->stype) {
        case SSET_BOOL:
          old_b = o->val ? True: False;
          XtVaGetValues(settable_options_widgets[i], XtNstate, &b, NULL);
          if (b != old_b) {
-           my_snprintf(buffer, MAX_LEN_MSG, "/set %s %s",
-                       o->name, b ? "1" : "0");
-           send_chat(buffer);
+           send_chat_printf("/set %s %s",
+                             o->name, b ? "1" : "0");
          }
          break;
        case SSET_INT:
          XtVaGetValues(settable_options_widgets[i], XtNstring, &dp, NULL);
          sscanf(dp, "%d", &val);
          if (val != o->val) {
-           my_snprintf(buffer, MAX_LEN_MSG, "/set %s %d",
-                       o->name, val);
-           send_chat(buffer);
+           send_chat_printf("/set %s %d",
+                             o->name, val);
          }
          break;
        case SSET_STRING:
          XtVaGetValues(settable_options_widgets[i], XtNstring, &dp, NULL);
          if (strcmp(o->strval, dp)) {
-           my_snprintf(buffer, MAX_LEN_MSG, "/set %s %s",
-                       o->name, (char *)dp);
-           send_chat(buffer);
+           send_chat_printf("/set %s %s",
+                             o->name, (char *)dp);
          }
          break;
        }
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to