changeset 431:a9b20b31cae1

More code cleanups, this time using clang with excessive warnings enabled. Also re-added util.h, that was missing. Oops.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 20:57:06 +0300
parents aaadf6cea6be
children 966c521e0954
files main.c util.c util.h
diffstat 3 files changed, 146 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Thu May 24 14:10:47 2012 +0300
+++ b/main.c	Thu May 24 20:57:06 2012 +0300
@@ -841,19 +841,19 @@
 typedef struct
 {
     char *cmd;
-    ssize_t len;
+    size_t len;
     int (*handler)(nn_conn_t *);
 } nn_protocolcmd_t;
 
 
 static nn_protocolcmd_t protoCmds[] =
 {
-    { "<USER>",         -1, nnproto_parse_user },
-    { "<LOGIN_",        -1, nnproto_parse_login },
-    { "<DELETE_USER>",  -1, nnproto_parse_delete_user },
-    { "<ADD_USER>",     -1, nnproto_parse_add_user },
-    { "<NUMCLIENTS>",   -1, nnproto_parse_num_clients },
-    { "<BOOT />",       -1, nnproto_parse_boot },
+    { "<USER>",         0, nnproto_parse_user },
+    { "<LOGIN_",        0, nnproto_parse_login },
+    { "<DELETE_USER>",  0, nnproto_parse_delete_user },
+    { "<ADD_USER>",     0, nnproto_parse_add_user },
+    { "<NUMCLIENTS>",   0, nnproto_parse_num_clients },
+    { "<BOOT />",       0, nnproto_parse_boot },
 };
 
 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]);
@@ -964,7 +964,7 @@
     {
         // Just list whomever is in ignore now
         qlist_t *user = setIgnoreList;
-        ssize_t nuser = th_llist_length(setIgnoreList);
+        size_t nuser = th_llist_length(setIgnoreList);
         char *result = th_strdup_printf("Users ignored (%d): ", nuser);
         while (user != NULL)
         {
@@ -1149,7 +1149,7 @@
 {
     char *cmd;
     int flags;
-    ssize_t len;
+    size_t len;
     int (*handler)(nn_conn_t *, char *buf);
 } nn_usercmd_t;
 
@@ -1157,24 +1157,24 @@
 static nn_usercmd_t userCmds[] =
 {
     // Server side commands, we just implement completion
-    { "me",       CMDARG_STRING,   -1, NULL },
-    { "status",   CMDARG_STRING,   -1, NULL },
-    { "list",     CMDARG_NONE,     -1, nncmd_list_all_users },
+    { "me",       CMDARG_STRING,   0, NULL },
+    { "status",   CMDARG_STRING,   0, NULL },
+    { "list",     CMDARG_NONE,     0, nncmd_list_all_users },
 
     // List internal username list
-    { "who",      CMDARG_NONE,     -1, nncmd_names },
-    { "names",    CMDARG_NONE,     -1, nncmd_names },
+    { "who",      CMDARG_NONE,     0, nncmd_names },
+    { "names",    CMDARG_NONE,     0, nncmd_names },
 
-    { "w",        CMDARG_NICK,     -1, nncmd_open_profile },
-    { "profile",  CMDARG_NICK,     -1, nncmd_open_profile },
+    { "w",        CMDARG_NICK,     0, nncmd_open_profile },
+    { "profile",  CMDARG_NICK,     0, nncmd_open_profile },
 
-    { "query",    CMDARG_NICK,     -1, nncmd_open_query },
-    { "close",    CMDARG_OPTIONAL, -1, nncmd_close_query },
-    { "win",      CMDARG_OPTIONAL, -1, nncmd_window_info },
+    { "query",    CMDARG_NICK,     0, nncmd_open_query },
+    { "close",    CMDARG_OPTIONAL, 0, nncmd_close_query },
+    { "win",      CMDARG_OPTIONAL, 0, nncmd_window_info },
 
-    { "ignore",   CMDARG_NICK,     -1, nncmd_ignore },
-    { "color",    CMDARG_STRING,   -1, nncmd_set_color },
-    { "save",     CMDARG_NONE,     -1, nncmd_save_config },
+    { "ignore",   CMDARG_NICK,     0, nncmd_ignore },
+    { "color",    CMDARG_STRING,   0, nncmd_set_color },
+    { "save",     CMDARG_NONE,     0, nncmd_save_config },
 };
 
 static const int nuserCmds = sizeof(userCmds) / sizeof(userCmds[0]);
@@ -1274,7 +1274,7 @@
     {
         if (currWin->id != NULL)
         {
-            char *tmp, *msg = th_strdup_printf("/prv -to %s -msg %s", currWin->id, buf);
+            char *msg = th_strdup_printf("/prv -to %s -msg %s", currWin->id, buf);
             if (msg == NULL) return -3;
             tmp = nn_dblencode_str(msg);
             if (tmp == NULL)
@@ -1345,7 +1345,7 @@
          newPattern = FALSE, hasSpace = FALSE, isCommand;
     char *str = buf->data;
     int mode = 0;
-    ssize_t endPos, startPos = buf->pos;
+    size_t endPos, startPos = buf->pos;
 
     // previous word
     if (startPos >= 2 && str[startPos - 1] == ' ' && str[startPos - 2] != ' ')
@@ -1448,7 +1448,7 @@
 
         if (user)
         {
-            int i;
+            size_t i;
             char *c = user->name;
             if (optDebug)
                 printMsg(currWin, "match='%s' / prev='%s'\n", user->name, previous);
@@ -1562,7 +1562,7 @@
 char *promptRequester(WINDOW *win, const char *info, BOOL allowEmpty)
 {
     char tmpBuf[512], *ptr;
-    ssize_t pos;
+    size_t pos;
     int curVis = curs_set(1);
 
     echo();
--- a/util.c	Thu May 24 14:10:47 2012 +0300
+++ b/util.c	Thu May 24 20:57:06 2012 +0300
@@ -109,7 +109,7 @@
                 int i = nn_get_hexdigit(*(++s), 0);
                 if (i >= 0)
                 {
-                    PUSHCHAR(c | i);
+                    PUSHCHAR((char) (c | i));
                 }
                 else
                 {
@@ -234,7 +234,7 @@
             for (i = 0; i < numHTMLEntities; i++)
             {
                 const html_entity_t *ent = &HTMLEntities[i];
-                int len = strlen(ent->ent);
+                size_t len = strlen(ent->ent);
                 if (!strncmp(s, ent->ent, len))
                 {
                     PUSHCHAR(ent->c);
@@ -282,13 +282,11 @@
 }
 
 
-int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch)
+int nn_editbuf_write(nn_editbuf_t *buf, size_t pos, char ch)
 {
     if (buf->len+1 >= buf->size) return -3;
 
-    if (pos < 0)
-        return -1;
-    else if (pos >= buf->len)
+    if (pos >= buf->len)
         buf->data[buf->len++] = ch;
     else
         buf->data[pos] = ch;
@@ -297,13 +295,11 @@
 }
 
 
-int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch)
+int nn_editbuf_insert(nn_editbuf_t *buf, size_t pos, char ch)
 {
     if (buf->len+1 >= buf->size) return -3;
 
-    if (pos < 0)
-        return -1;
-    else if (pos >= buf->len)
+    if (pos >= buf->len)
     {
         buf->data[buf->len] = ch;
     }
@@ -317,11 +313,9 @@
 }
 
 
-int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos)
+int nn_editbuf_delete(nn_editbuf_t *buf, size_t pos)
 {
-    if (pos < 0)
-        return -1;
-    else if (pos < buf->len)
+    if (pos < buf->len)
     {
         memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos);
         buf->len--;
@@ -339,7 +333,7 @@
 }
 
 
-nn_editbuf_t * nn_editbuf_new(ssize_t n)
+nn_editbuf_t * nn_editbuf_new(size_t n)
 {
     nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t));
 
@@ -378,25 +372,19 @@
 }
 
 
-char * nn_editbuf_get_string(nn_editbuf_t *buf, ssize_t start, ssize_t end)
+char * nn_editbuf_get_string(nn_editbuf_t *buf, size_t start, size_t end)
 {
     char *str;
-    ssize_t siz;
+    size_t siz;
 
     if (buf == NULL)
         return NULL;
 
-    if (start < 0 || end > buf->len || start >= buf->len)
+    if (end > buf->len || start >= buf->len)
         return NULL;
 
-    if (end < 0)
-    {
-        siz = buf->len - start + 1;
-    }
-    else if (start <= end)
-    {
+    if (start <= end)
         siz = end - start + 1;
-    }
     else
         return NULL;
 
@@ -410,13 +398,11 @@
 }
 
 
-void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos)
+void nn_editbuf_setpos(nn_editbuf_t *buf, size_t pos)
 {
     assert(buf != NULL);
 
-    if (pos < 0)
-        buf->pos = 0;
-    else if (pos >= buf->len)
+    if (pos >= buf->len)
         buf->pos = buf->len;
     else
         buf->pos = pos;
@@ -438,7 +424,7 @@
     
     return (hash & 0xff);
 */
-    return tolower(name[0]);
+    return (uint8_t) tolower(name[0]);
 }
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util.h	Thu May 24 20:57:06 2012 +0300
@@ -0,0 +1,104 @@
+/*
+ * NNChat - Custom chat client for NewbieNudes.com chatrooms
+ * Written by Matti 'ccr' Hämäläinen
+ * (C) Copyright 2008-2012 Tecnic Software productions (TNSP)
+ */
+#ifndef LIBNNUTIL_H
+#define LIBNNUTIL_H
+
+#include <stdio.h>
+#include <unistd.h>
+#include "th_types.h"
+#include "th_string.h"
+
+
+#define NN_TMPBUF_SIZE    (4096)
+#define NN_ALLOC_SIZE     (128)
+#define NN_NUM_BUCKETS    (256)
+#define NN_BACKBUF_LEN    (512)       // Backbuffer size (in lines)
+
+
+typedef struct _nn_user_t
+{
+    char *name;
+    time_t lastspoke, joined;
+    struct _nn_user_t *next;
+} nn_user_t;
+
+
+typedef struct
+{
+    nn_user_t *buckets[NN_NUM_BUCKETS];
+} nn_userhash_t;
+
+
+nn_userhash_t *nn_userhash_new(void);
+nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *));
+nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current, BOOL again);
+int         nn_userhash_insert(nn_userhash_t *, const char *name);
+int         nn_userhash_delete(nn_userhash_t *, const char *name);
+void        nn_userhash_free(nn_userhash_t *);
+void        nn_user_free(nn_user_t *);
+void        nn_user_free_list(nn_user_t *);
+nn_user_t * nn_user_copy(const nn_user_t *src);
+nn_user_t * nn_user_find(const nn_userhash_t *list, const char *name);
+
+
+char *      nn_encode_str1(const char *str);
+char *      nn_decode_str1(const char *str);
+char *      nn_encode_str2(const char *str);
+char *      nn_decode_str2(const char *str);
+char *      nn_strip_tags(const char *str);
+char *      nn_dbldecode_str(const char *str);
+char *      nn_dblencode_str(const char *str);
+
+char *      nn_username_encode(char *str);
+char *      nn_username_decode(char *str);
+
+
+typedef struct
+{
+    size_t pos, len, size;
+    char *data;
+} nn_editbuf_t;
+
+int         nn_editbuf_write(nn_editbuf_t *buf, size_t pos, char ch);
+int         nn_editbuf_insert(nn_editbuf_t *buf, size_t pos, char ch);
+int         nn_editbuf_delete(nn_editbuf_t *buf, size_t pos);
+void        nn_editbuf_clear(nn_editbuf_t *buf);
+nn_editbuf_t * nn_editbuf_new(size_t n);
+void        nn_editbuf_free(nn_editbuf_t *buf);
+nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src);
+void        nn_editbuf_setpos(nn_editbuf_t *buf, size_t pos);
+char *      nn_editbuf_get_string(nn_editbuf_t *buf, size_t start, size_t end);
+
+
+typedef struct
+{
+    qringbuf_t *data;   // "Backbuffer" data for this window
+    int pos;            // Current position in the window, 0 = real time
+    BOOL dirty;
+
+    char *id;           // Chatter ID, NULL = main window
+    int num;		// Window number
+
+    char *buf;
+    size_t len, bufsize;
+    size_t chlen;
+} nn_window_t;
+
+nn_window_t *nn_window_new(const char *);
+void        nn_window_free(nn_window_t *);
+
+
+typedef struct
+{
+    size_t len;
+    char *str;
+} nn_strtuple_t;
+
+
+nn_strtuple_t *nn_strtuple_new(size_t, char *);
+void nn_strtuple_free(nn_strtuple_t *);
+
+#endif