changeset 78:745f670068dc

Add functions to simplify code.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 18 Dec 2008 14:34:44 +0200
parents e8c9d7d13866
children e36df57c5b0f
files libnnchat.c libnnchat.h nnchat.c
diffstat 3 files changed, 46 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Sat Dec 13 11:19:19 2008 +0200
+++ b/libnnchat.c	Thu Dec 18 14:34:44 2008 +0200
@@ -97,7 +97,7 @@
 {
     size_t tmpLen;
     
-    if (!str) return FALSE;
+    if (str == NULL) return FALSE;
     tmpLen = strlen(str);
     
     if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE))
@@ -115,7 +115,7 @@
     char *result;
     size_t resSize, resPos = 0;
     
-    if (!str) return NULL;
+    if (str == NULL) return NULL;
     
     resSize = strlen(str) + SET_ALLOC_SIZE;
     if ((result = th_malloc(resSize)) == NULL)
@@ -170,7 +170,7 @@
     size_t resSize, resPos = 0;
     int c;
     
-    if (!str) return NULL;
+    if (str == NULL) return NULL;
     
     resSize = strlen(str) + SET_ALLOC_SIZE;
     if ((result = th_malloc(resSize)) == NULL)
@@ -231,7 +231,7 @@
     char *result;
     size_t resSize, resPos = 0;
     
-    if (!str) return NULL;
+    if (str == NULL) return NULL;
     
     resSize = strlen(str) + SET_ALLOC_SIZE;
     if ((result = th_malloc(resSize)) == NULL)
@@ -256,7 +256,7 @@
     char *result;
     size_t resSize, resPos = 0;
     
-    if (!str) return NULL;
+    if (str == NULL) return NULL;
     
     resSize = strlen(str) + SET_ALLOC_SIZE;
     if ((result = th_malloc(resSize)) == NULL)
@@ -287,7 +287,7 @@
     char *result;
     size_t resSize, resPos = 0;
     
-    if (!str) return NULL;
+    if (str == NULL) return NULL;
     
     resSize = strlen(str);
     if ((result = th_malloc(resSize)) == NULL)
@@ -317,6 +317,34 @@
 }
 
 
+char *doubleDecodeStr(const char *str)
+{
+    char *res, *tmp;
+    
+    if ((tmp = decodeStr1(str)) == NULL)
+        return NULL;
+    
+    res = decodeStr2(tmp);
+    th_free(tmp);
+    
+    return res;    
+}
+
+
+char *doubleEncodeStr(const char *str)
+{
+    char *res, *tmp;
+    
+    if ((tmp = encodeStr2(str)) == NULL)
+        return NULL;
+    
+    res = encodeStr1(tmp);
+    th_free(tmp);
+    
+    return res;    
+}
+
+
 BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...)
 {
     char tmpBuf[SET_BUFSIZE], tmpBuf2[SET_BUFSIZE + 256];
--- a/libnnchat.h	Sat Dec 13 11:19:19 2008 +0200
+++ b/libnnchat.h	Thu Dec 18 14:34:44 2008 +0200
@@ -47,6 +47,8 @@
 char *      decodeStr2(const char *str);
 char *      stripXMLTags(const char *str);
 
+char *      doubleDecodeStr(const char *str);
+char *      doubleEncodeStr(const char *str);
 
 ringbuf_t * newRingBuf(const size_t size);
 void        freeRingBuf(ringbuf_t *buf);
--- a/nnchat.c	Sat Dec 13 11:19:19 2008 +0200
+++ b/nnchat.c	Thu Dec 18 14:34:44 2008 +0200
@@ -425,7 +425,7 @@
     if (!s) return 1;
     *s = 0;
     
-    p = decodeStr1(str);
+    p = doubleDecodeStr(str);
     if (!p) return -1;
     
     printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p);
@@ -443,7 +443,7 @@
     if (!s) return 1;
     *s = 0;
     
-    p = decodeStr1(str);
+    p = doubleDecodeStr(str);
     if (!p) return -1;
     
     printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p);
@@ -494,7 +494,7 @@
 
 int handleUserInput(const int sock, char *buf, size_t bufLen)
 {
-    char *tmpStr, *tmpStr2, tmpBuf[4096];
+    char *tmpStr, tmpBuf[4096];
     BOOL result;
     
     /* Trim right */
@@ -521,22 +521,16 @@
         snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg .                                                                                                                                                                                                                                                                                                                                                                          .",
             buf+7);
         
-        tmpStr = encodeStr2(tmpBuf);
+        tmpStr = doubleEncodeStr(tmpBuf);
         if (!tmpStr) return -2;
-        tmpStr2 = encodeStr1(tmpStr);
-        if (!tmpStr2) {
-            th_free(tmpStr);
-            return -3;
-        }
         
         result = TRUE;
         for (i = 0; i < 50 && result; i++) {
-            result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
+            result = sendUserMsg(sock, optUserName2, "%s", tmpStr);
             usleep(250);
         }
         
         th_free(tmpStr);
-        th_free(tmpStr2);
         return 0;
     } else if (!strncmp(buf, "/to ", 4)) {
         th_free(setTarget);
@@ -559,17 +553,11 @@
     
     
     /* Send double-encoded */
-    tmpStr = encodeStr2(buf);
+    tmpStr = doubleEncodeStr(buf);
     if (!tmpStr) return -2;
-    tmpStr2 = encodeStr1(tmpStr);
-    if (!tmpStr2) {
-        th_free(tmpStr);
-        return -3;
-    }
     
-    result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
+    result = sendUserMsg(sock, optUserName2, "%s", tmpStr);
     th_free(tmpStr);
-    th_free(tmpStr2);
     if (result)
         return 0;
     else
@@ -619,7 +607,7 @@
     memset(histBuf, 0, sizeof(histBuf));
     
     /* Initialize */
-    th_init("NNChat", "Newbie Nudes chat client", "0.6.6",
+    th_init("NNChat", "Newbie Nudes chat client", "0.6.7",
         "Written and designed by Anonymous Finnish Guy (C) 2008",
         "This software is freeware, use and distribute as you wish.");
     th_verbosityLevel = 0;
@@ -702,8 +690,8 @@
     }
     
     THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite);
-    optUserName2 = encodeStr1(optUserName);
-    tmpStr = encodeStr1(optSite);
+    optUserName2 = doubleEncodeStr(optUserName);
+    tmpStr = doubleEncodeStr(optSite);
     sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword);
     th_free(tmpStr);