summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTingPing <tingping@tingping.se>2014-12-12 04:04:39 -0500
committerTingPing <tingping@tingping.se>2014-12-15 15:06:48 -0500
commitc54a0c6db93b3fc2c6d749b79221a8d26d8afbd7 (patch)
tree78ff2c5784a7d9642cdecfce8d2cd0835eda947f
parent46061f4f3cad58acefeab840f9627ba9293568bb (diff)
fishlim: Improve string handling
-rw-r--r--plugins/fishlim/irc.c12
-rw-r--r--plugins/fishlim/plugin_hexchat.c48
2 files changed, 16 insertions, 44 deletions
diff --git a/plugins/fishlim/irc.c b/plugins/fishlim/irc.c
index 3586921b..c80fc1c9 100644
--- a/plugins/fishlim/irc.c
+++ b/plugins/fishlim/irc.c
@@ -22,8 +22,7 @@
 
 */
 
-#include <stdlib.h>
-#include <string.h>
+#include <glib.h>
 #include "irc.h"
 
 /**
@@ -65,7 +64,6 @@ bool irc_parse_message(const char *words[],
  */
 char *irc_prefix_get_nick(const char *prefix) {
     const char *end;
-    char *nick;
     size_t length;
     
     if (!prefix) return NULL;
@@ -76,13 +74,7 @@ char *irc_prefix_get_nick(const char *prefix) {
     
     // Allocate string
     length = end - prefix;
-    nick = malloc(length+1);
-    if (!nick) return NULL;
-    
-    // Copy to string
-    memcpy(nick, prefix, length);
-    nick[length] = '\0';
-    return nick;
+    return g_strndup (prefix, length);
 }
 
 
diff --git a/plugins/fishlim/plugin_hexchat.c b/plugins/fishlim/plugin_hexchat.c
index f200ea8c..8f2536ce 100644
--- a/plugins/fishlim/plugin_hexchat.c
+++ b/plugins/fishlim/plugin_hexchat.c
@@ -55,24 +55,6 @@ gchar *get_config_filename() {
     return g_build_filename(hexchat_get_info(ph, "configdir"), "addon_fishlim.conf", NULL);
 }
 
-/**
- * Appends data to a string. Returns true if there was sufficient memory.
- * Frees *s and returns false if an error occurs.
- */
-static bool append(char **s, size_t *length, const char *data) {
-    size_t datalen = strlen(data);
-    char *extended = realloc(*s, *length + datalen + 1);
-    if (!extended) {
-        free(*s);
-        return false;
-    }
-    memcpy(extended + *length, data, datalen + 1);
-    *s = extended;
-    *length += datalen;
-    return true;
-}
-
-
 /*static int handle_debug(char *word[], char *word_eol[], void *userdata) {
     hexchat_printf(ph, "debug incoming: ");
     for (size_t i = 1; word[i] != NULL && word[i][0] != '\0'; i++) {
@@ -114,12 +96,11 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
     const char *peice;
     char *sender_nick;
     char *decrypted;
-    char *message;
     size_t w;
     size_t ew;
     size_t uw;
-    size_t length;
     char prefix_char = 0;
+    GString *message;
 
     if (!irc_parse_message((const char **)word, &prefix, &command, &w))
         return HEXCHAT_EAT_NONE;
@@ -149,12 +130,12 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
     if (!decrypted) goto decrypt_error;
     
     // Build unecrypted message
-    message = NULL;
-    length = 0;
-    if (!append(&message, &length, "RECV")) goto decrypt_error;
+    message = g_string_sized_new (100); /* TODO: more accurate estimation of size */
+    g_string_append (message, "RECV");
     
     for (uw = 1; uw < HEXCHAT_MAX_WORDS; uw++) {
-        if (word[uw][0] != '\0' && !append(&message, &length, " ")) goto decrypt_error;
+        if (word[uw][0] != '\0')
+            g_string_append_c (message, ' ');
         
         if (uw == ew) {
             // Add the encrypted data
@@ -163,29 +144,28 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
             
             if (ew == w+1) {
                 // Prefix with colon, which gets stripped out otherwise
-                if (!append(&message, &length, ":")) goto decrypt_error;
+                g_string_append_c (message, ':');
             }
             
             if (prefix_char) {
-                char prefix_str[2] = { prefix_char, '\0' };
-                if (!append(&message, &length, prefix_str)) goto decrypt_error;
+                g_string_append_c (message, prefix_char);
             }
             
         } else {
             // Add unencrypted data (for example, a prefix from a bouncer or bot)
             peice = word[uw];
         }
-        
-        if (!append(&message, &length, peice)) goto decrypt_error;
+
+        g_string_append (message, peice);
     }
     free(decrypted);
     
     // Simulate unencrypted message
-    //hexchat_printf(ph, "simulating: %s\n", message);
-    hexchat_command(ph, message);
-    
-    free(message);
-    free(sender_nick);
+    //hexchat_printf(ph, "simulating: %s\n", message->str);
+    hexchat_command(ph, message->str);
+
+    g_string_free (message, TRUE);
+    g_free(sender_nick);
     return HEXCHAT_EAT_HEXCHAT;
   
   decrypt_error: