annotate libnnchat.c @ 94:6e47426efb6a

Add preliminary userlist data handling functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 07 May 2009 06:14:21 +0300
parents acfc4b4bc180
children 69f9c46e4a94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2008 Tecnic Software productions (TNSP)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #include "libnnchat.h"
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 typedef struct {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
10 char c;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
11 char *ent;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 } html_entity_t;
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 html_entity_t HTMLEntities[] = {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
16 { '<', "&lt;" },
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
17 { '>', "&gt;" },
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 };
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0]));
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
23 #ifdef __WIN32
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
24 const char *hstrerror(int err)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
25 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
26 static char buf[64];
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
27 snprintf(buf, sizeof(buf), "Error #%d", err);
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
28 return buf;
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
29 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
30
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
31 int getSocketErrno(void)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
32 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
33 return WSAGetLastError();
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
34 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
35
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
36 const char *getSocketErrStr(int err)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
37 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
38 static char buf[64];
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
39 snprintf(buf, sizeof(buf), "Error #%d", err);
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
40 return buf;
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
41 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
42 #else
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
43 int getSocketErrno(void)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
44 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
45 return errno;
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
46 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
47
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
48 const char *getSocketErrStr(int err)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
49 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
50 return strerror(err);
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
51 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
52 #endif
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
53
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
54
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 int openConnection(struct in_addr *addr, const int port)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
57 struct sockaddr_in tmpAddr;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
58 int sock = -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
59
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
60 tmpAddr.sin_family = AF_INET;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
61 tmpAddr.sin_port = htons(port);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
62 tmpAddr.sin_addr = *addr;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
64 THMSG(1, "Connecting to %s:%d ...\n",
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
65 inet_ntoa(tmpAddr.sin_addr), port);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
67 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
68 THERR("Could not open socket: %s\n", strerror(errno));
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
69 return -2;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
70 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
72 THMSG(2, "Using socket %d.\n", sock);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
73
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
74 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
75 THERR("Could not connect: %s\n", strerror(errno));
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
76 return -5;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
77 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
78
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
79 return sock;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 void closeConnection(const int sock)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
85 if (sock >= 0) {
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
86 #ifdef __WIN32
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
87 closesocket(sock);
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
88 #else
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
89 close(sock);
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
90 #endif
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
91 }
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
91
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
95 BOOL initNetwork(void)
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
96 {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
97 #ifdef __WIN32
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
98 /* Initialize WinSock, if needed */
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
99 WSADATA wsaData;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
100 int err = WSAStartup(0x0101, &wsaData);
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
101 if (err != 0) {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
102 THERR("Could not initialize WinSock library (err=%d).\n", err);
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
103 return FALSE;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
104 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
105 #endif
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
106 return TRUE;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
107 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
108
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
109
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
110 void closeNetwork(void)
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
111 {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
112 #ifdef __WIN32
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
113 WSACleanup();
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
114 #endif
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
115 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
116
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
117
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 BOOL sendToSocket(const int sock, char *buf, const size_t bufLen)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
120 size_t bufLeft = bufLen;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
121 char *bufPtr = buf;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
122
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
123 while (bufLeft > 0) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
124 ssize_t bufSent;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
125 bufSent = send(sock, bufPtr, bufLeft, 0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
126 if (bufSent < 0) return FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
127 bufLeft -= bufSent;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
128 bufPtr += bufSent;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
129 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
130 return TRUE;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 BOOL bufRealloc(char **buf, size_t *size, size_t add)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
136 return ((*buf = th_realloc(*buf, *size + add)) != NULL);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
142 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE))
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
143 return FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
144
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
145 (*buf)[*pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
146 (*pos)++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
147 return TRUE;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
153 size_t tmpLen;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
154
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
155 if (str == NULL) return FALSE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
156 tmpLen = strlen(str);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
157
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
158 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE))
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
159 return FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
160
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
161 strcpy(*buf + *pos, str);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
162 (*pos) += tmpLen;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
163 return TRUE;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 char *encodeStr1(const char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
169 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
170 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
171 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
172
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
173 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
174
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
175 resSize = strlen(str) + SET_ALLOC_SIZE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
176 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
177 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
178
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
179 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
180 switch (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
181 case 32:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
182 PUSHCHAR('+');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
183 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
184
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
185 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
186 if (th_isalnum(*s))
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
187 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
188 else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
189 char tmpStr[4];
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
190 sprintf(tmpStr, "%2X", (unsigned char) *s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
191 PUSHCHAR('%');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
192 PUSHSTR(tmpStr);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
193 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
194 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
195 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
196 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
197 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
198 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
199
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
200 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 static int getHexDigit(const int c, const int shift)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
206 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
207
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
208 if (c >= 'A' && c <= 'F')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
209 i = c - 'A' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
210 else if (c >= 'a' && c <= 'f')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
211 i = c - 'a' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
212 else if (c >= '0' && c <= '9')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
213 i = c - '0';
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
214 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
215 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
216
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
217 return i << shift;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 char *decodeStr1(const char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
223 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
224 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
225 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
226 int c;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
227
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
228 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
229
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
230 resSize = strlen(str) + SET_ALLOC_SIZE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
231 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
232 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
233
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
234 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
235 switch (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
236 case '+':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
237 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
238 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
239 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
240
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
241 case '½':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
242 /* Escape these .. */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
243 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
244 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
245 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
246 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
247
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
248 case '\r':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
249 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
250 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
251 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
252
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
253 case '%':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
254 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
255 if (*s == '%')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
256 PUSHCHAR('%');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
257 else if ((c = getHexDigit(*s, 4)) >= 0) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
258 int i = getHexDigit(*(++s), 0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
259 if (i >= 0) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
260 PUSHCHAR(c | i);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
261 } else {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
262 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
263 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
264 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
265 } else {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
266 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
267 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
268 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
269 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
270 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
271
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
272 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
273 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
274 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
275 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
276 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
277 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
278
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
279 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 char *stripXMLTags(const char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
285 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
286 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
287 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
288
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
289 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
290
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
291 resSize = strlen(str) + SET_ALLOC_SIZE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
292 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
293 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
294
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
295 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
296 if (*s == '<') {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
297 while (*s && *s != '>') s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
298 if (*s == '>') s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
299 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
300 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
301 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
302 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
303
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
304 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 char *encodeStr2(const char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
310 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
311 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
312 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
313
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
314 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
315
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
316 resSize = strlen(str) + SET_ALLOC_SIZE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
317 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
318 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
319
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
320 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
321 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
322 BOOL found = FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
323 for (i = 0; i < numHTMLEntities; i++)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
324 if (HTMLEntities[i].c == *s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
325 PUSHSTR(HTMLEntities[i].ent);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
326 found = TRUE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
327 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
328 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
329 if (!found) PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
330
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
331 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
332 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
333 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
334
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
335 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 char *decodeStr2(const char *str)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
341 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
342 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
343 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
344
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
345 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
346
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
347 resSize = strlen(str);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
348 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
349 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
350
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
351 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
352 if (*s == '&') {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
353 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
354 BOOL found = FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
355 for (i = 0; i < numHTMLEntities; i++) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
356 html_entity_t *ent = &HTMLEntities[i];
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
357 int len = strlen(ent->ent);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
358 if (!strncmp(s, ent->ent, len)) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
359 PUSHCHAR(ent->c);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
360 s += len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
361 found = TRUE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
362 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
363 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
364 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
365 if (!found) PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
366 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
367 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
368 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
369 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
370
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
371 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
375 char *doubleDecodeStr(const char *str)
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
376 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
377 char *res, *tmp;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
378
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
379 if ((tmp = decodeStr1(str)) == NULL)
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
380 return NULL;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
381
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
382 res = decodeStr2(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
383 th_free(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
384
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
385 return res;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
386 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
387
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
388
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
389 char *doubleEncodeStr(const char *str)
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
390 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
391 char *res, *tmp;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
392
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
393 if ((tmp = encodeStr2(str)) == NULL)
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
394 return NULL;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
395
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
396 res = encodeStr1(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
397 th_free(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
398
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
399 return res;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
400 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
401
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
402
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
405 char tmpBuf[SET_BUFSIZE], tmpBuf2[SET_BUFSIZE + 256];
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
406 int n;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
407 va_list ap;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
408
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
409 va_start(ap, fmt);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
410 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
411 va_end(ap);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
412
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
413 if (n < 0) return FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
414
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
415 snprintf(tmpBuf2, sizeof(tmpBuf2),
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
416 "<USER>%s</USER><MESSAGE>%s</MESSAGE>",
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
417 user, tmpBuf);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
418
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
419 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
422
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
423 nn_ringbuf_t * newRingBuf(const size_t size)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
424 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
425 nn_ringbuf_t *res = th_calloc(1, sizeof(nn_ringbuf_t));
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
426
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
427 res->data = (char **) th_malloc(size * sizeof(char *));
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
428 res->size = size;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
429 res->n = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
430
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
431 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
432 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
433
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
434
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
435 void freeRingBuf(nn_ringbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
436 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
437 size_t i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
438
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
439 for (i = 0; i < buf->n; i++)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
440 th_free(buf->data[i]);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
441
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
442 th_free(buf->data);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
443 th_free(buf);
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
444 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
445
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
446
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
447 void addRingBuf(nn_ringbuf_t *buf, const char *str)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
448 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
449 if (buf->n < buf->size) {
79
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
450 buf->data[buf->n] = th_strdup(str);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
451 buf->n++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
452 } else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
453 th_free(buf->data[0]);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
454 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
79
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
455 buf->data[buf->size - 1] = th_strdup(str);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
456 }
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
457 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
458
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
459
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
460 int writeBuf(nn_editbuf_t *buf, ssize_t pos, int ch)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
461 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
462 /* Check arguments */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
463 if (buf->len+1 >= buf->size) return -3;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
464
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
465 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
466 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
467 else if (pos >= buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
468 buf->data[buf->len++] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
469 } else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
470 buf->data[pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
471 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
472 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
473 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
474
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
475
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
476 int insertBuf(nn_editbuf_t *buf, ssize_t pos, int ch)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
477 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
478 /* Check arguments */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
479 if (buf->len+1 >= buf->size) return -3;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
480
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
481 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
482 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
483 else if (pos >= buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
484 buf->data[buf->len] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
485 } else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
486 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
487 buf->data[pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
488 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
489 buf->len++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
490 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
491 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
492
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
493
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
494 int deleteBuf(nn_editbuf_t *buf, ssize_t pos)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
495 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
496 /* Check arguments */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
497 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
498 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
499 else if (pos < buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
500 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
501 buf->len--;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
502 return 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
503 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
504 return -2;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
505 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
506
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
507
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
508 void clearBuf(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
509 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
510 buf->len = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
511 buf->pos = 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
512 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
513
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
514
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
515 nn_editbuf_t * newBuf(ssize_t n)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
516 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
517 nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t));
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
518
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
519 res->data = (char *) th_malloc(n);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
520 res->size = n;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
521
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
522 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
523 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
524
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
525
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
526 void freeBuf(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
527 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
528 if (buf) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
529 th_free(buf->data);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
530 th_free(buf);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
531 }
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
532 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
533
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
534
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
535 nn_editbuf_t * copyBuf(nn_editbuf_t *src)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
536 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
537 nn_editbuf_t *res;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
538
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
539 assert(src != NULL);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
540
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
541 if (src == NULL) return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
542
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
543 if ((res = newBuf(src->size)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
544 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
545
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
546 memcpy(res->data, src->data, src->size);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
547 res->pos = res->len = src->len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
548
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
549 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
550 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
551
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
552
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
553 void setBufPos(nn_editbuf_t *buf, ssize_t pos)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
554 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
555 /* Check arguments */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
556 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
557 buf->pos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
558 else if (pos >= buf->len)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
559 buf->pos = buf->len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
560 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
561 buf->pos = pos;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
562 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
563
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
564
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
565 static uint8_t hashUserName(char *name)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
566 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
567 int n = 0;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
568 uint8_t *c = (uint8_t *)name;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
569 uint8_t hash = 0xff;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
570
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
571 while (*c && n < 4) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
572 hash = (hash << 1) ^ tolower(*c);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
573 c++; n++;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
574 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
575
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
576 return (hash & 0xff);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
577 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
578
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
579
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
580 static void insertUser(nn_user_t **list, nn_user_t *node)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
581 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
582 node->next = *list;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
583 *list = node;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
584 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
585
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
586
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
587 nn_user_t *findUserEnc(nn_userhash_t *list, char *encname)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
588 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
589 uint8_t hash;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
590
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
591 if (list == NULL) return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
592
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
593 hash = hashUserName(encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
594 if (list->buckets[hash] != NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
595 nn_user_t *curr = list->buckets[hash];
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
596 while (curr != NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
597 if (strcasecmp(curr->encname, encname) == 0)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
598 return curr;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
599 curr = curr->next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
600 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
601 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
602
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
603 return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
604 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
605
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
606
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
607 int addUserToHash(nn_userhash_t **list, char *encname)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
608 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
609 uint8_t hash;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
610 nn_user_t *user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
611
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
612 /* Check arguments */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
613 if (list == NULL || encname == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
614 return -1;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
615
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
616 /* Check if list data exists */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
617 if (*list == NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
618 *list = th_calloc(1, sizeof(nn_userhash_t));
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
619 if (*list == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
620 return -2;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
621 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
622
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
623 /* Check if username is already there */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
624 if (findUserEnc(*list, encname) != NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
625 return 1;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
626
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
627 /* No, we'll add it */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
628 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
629 return -3;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
630
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
631 user->encname = th_strdup(encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
632 user->name = doubleDecodeStr(encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
633 if (user->encname == NULL || user->name == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
634 return -4;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
635
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
636 hash = hashUserName(encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
637 insertUser(&((*list)->buckets[hash]), user);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
638 return 0;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
639 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
640
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
641
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
642 nn_user_t *copyUser(nn_user_t *src)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
643 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
644 nn_user_t *user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
645
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
646 if (src == NULL) return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
647
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
648 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
649 return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
650
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
651 /* Copy relevant data */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
652 user->encname = th_strdup(src->encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
653 user->name = th_strdup(src->name);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
654 user->lastspoke = src->lastspoke;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
655 user->joined = src->joined;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
656
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
657 return user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
658 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
659
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
660
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
661 static void freeUser(nn_user_t *user)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
662 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
663 th_free(user->encname);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
664 th_free(user->name);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
665 th_free(user);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
666 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
667
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
668
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
669 void freeUserList(nn_user_t *list)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
670 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
671 nn_user_t *next, *curr = list;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
672 while (curr != NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
673 next = curr->next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
674 freeUser(curr);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
675 curr = next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
676 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
677 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
678
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
679 void freeUserHash(nn_userhash_t *hash)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
680 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
681 int i;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
682 if (hash == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
683 return;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
684
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
685 for (i = 0; i < NN_NUM_BUCKETS; i++) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
686 freeUserList(hash->buckets[i]);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
687 hash->buckets[i] = NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
688 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
689
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
690 th_free(hash);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
691 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
692