]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
IRC: factor out message parsing
authorJānis Rūcis <parasti@gmail.com>
Tue, 30 Mar 2010 15:33:46 +0000 (18:33 +0300)
committerJānis Rūcis <parasti@gmail.com>
Sun, 27 Jun 2010 12:23:20 +0000 (15:23 +0300)
irc.c

diff --git a/irc.c b/irc.c
index 85c4ccd2b36edce541cd2ad2e93931de8685cc8c..b2b41a7b8c75893cbd8218df5d7b8d007280e9d5 100644 (file)
--- a/irc.c
+++ b/irc.c
@@ -123,14 +123,39 @@ typedef struct ircmessage_s
 }
 ircmessage_t;
 
-static void IRC_ProcessMessage(const char *line)
+static ircmessage_t *IRC_AllocMessage(void)
+{
+       ircmessage_t *msg;
+
+       if ((msg = Z_Malloc(sizeof (*msg))))
+               memset(msg, 0, sizeof (*msg));
+
+       return msg;
+}
+
+static void IRC_FreeMessage(ircmessage_t *msg)
+{
+       if (msg)
+       {
+               if (msg->prefix) Z_Free(msg->prefix);
+               if (msg->command) Z_Free(msg->command);
+               if (msg->args) Z_Free(msg->args);
+
+               Z_Free(msg);
+       }
+}
+
+static ircmessage_t *IRC_ParseMessage(const char *line)
 {
        const int line_len = strlen(line);
        const char *line_end = line + line_len;
-       ircmessage_t msg;
+       ircmessage_t *msg;
        int len;
 
-       memset(&msg, 0, sizeof (msg));
+       msg = IRC_AllocMessage();
+
+       if (!msg)
+               return NULL;
 
        if (line[0] == ':')
        {
@@ -138,24 +163,29 @@ static void IRC_ProcessMessage(const char *line)
 
                len = strcspn(line, " ");
 
+               /* TODO, is this really needed? */
+
                if (line + len == line_end)
-                       return;
+               {
+                       IRC_FreeMessage(msg);
+                       return NULL;
+               }
 
-               msg.prefix = Z_Malloc(len + 1);
-               memcpy(msg.prefix, line, len);
-               msg.prefix[len] = 0;
+               msg->prefix = Z_Malloc(len + 1);
+               memcpy(msg->prefix, line, len);
+               msg->prefix[len] = 0;
 
                line += len;
                line += strspn(line, " ");
        }
        else
-               msg.prefix = NULL;
+               msg->prefix = NULL;
 
        len = strcspn(line, " ");
 
-       msg.command = Z_Malloc(len + 1);
-       memcpy(msg.command, line, len);
-       msg.command[len] = 0;
+       msg->command = Z_Malloc(len + 1);
+       memcpy(msg->command, line, len);
+       msg->command[len] = 0;
 
        if (line + len != line_end)
        {
@@ -164,29 +194,31 @@ static void IRC_ProcessMessage(const char *line)
 
                len = line_end - line;
 
-               msg.args = Z_Malloc(len + 1);
-               memcpy(msg.args, line, len);
-               msg.args[len] = 0;
+               msg->args = Z_Malloc(len + 1);
+               memcpy(msg->args, line, len);
+               msg->args[len] = 0;
        }
        else
-               msg.args = NULL;
-
-       Con_Printf("[IRC] prefix:  %s\n"
-                          "[IRC] command: %s\n"
-                          "[IRC] args:    %s\n",
-                          msg.prefix ? msg.prefix : "",
-                          msg.command,
-                          msg.args ? msg.args : "");
-
-       /* Free stuff. */
+               msg->args = NULL;
 
-       if (msg.prefix)
-               Z_Free(msg.prefix);
+       return msg;
+}
 
-       Z_Free(msg.command);
+static void IRC_ProcessMessage(const char *line)
+{
+       ircmessage_t *msg;
 
-       if (msg.args)
-               Z_Free(msg.args);
+       if ((msg = IRC_ParseMessage(line)))
+       {
+               Con_Printf("[IRC] prefix:  %s\n"
+                                  "[IRC] command: %s\n"
+                                  "[IRC] args:    %s\n",
+                                  msg->prefix ? msg->prefix : "",
+                                  msg->command,
+                                  msg->args ? msg->args : "");
+
+               IRC_FreeMessage(msg);
+       }
 }
 
 static void IRC_ProcessAllMessages(void)