Mercurial > hg > nnchat
annotate libnnchat.c @ 108:f323b137ca08
Tab-completion now works with cycling, but bugs with names that have whitespaces.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 23 Oct 2010 02:01:00 +0300 |
parents | c587a99e2096 |
children | 741e45592522 |
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 |
102 | 4 * (C) Copyright 2008-2010 Tecnic Software productions (TNSP) |
62
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 |
95 | 15 static const html_entity_t HTMLEntities[] = { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
16 { '<', "<" }, |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
17 { '>', ">" }, |
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 |
95 | 20 static const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0])); |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
31 int nn_get_socket_errno(void) |
90
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
36 const char *nn_get_socket_errstr(int err) |
90
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
43 int nn_get_socket_errno(void) |
90
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
48 const char *nn_get_socket_errstr(int err) |
90
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
55 int nn_open_connection(struct in_addr *addr, const int port) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
83 void nn_close_connection(const int sock) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
95 BOOL nn_network_init(void) |
91
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
110 void nn_network_close(void) |
91
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
118 BOOL nn_send_to_socket(const int sock, char *buf, const size_t bufLen) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
167 char *nn_encode_str1(const char *str) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
221 char *nn_decode_str1(const char *str) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
283 char *nn_strip_tags(const char *str) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
308 char *nn_encode_str2(const char *str) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
339 char *nn_decode_str2(const char *str) |
62
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++) { |
95 | 356 const html_entity_t *ent = &HTMLEntities[i]; |
71
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
375 char *nn_dbldecode_str(const char *str) |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
379 if ((tmp = nn_decode_str1(str)) == NULL) |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
382 res = nn_decode_str2(tmp); |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
389 char *nn_dblencode_str(const char *str) |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
393 if ((tmp = nn_encode_str2(str)) == NULL) |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
396 res = nn_encode_str1(tmp); |
78
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
403 BOOL nn_send_msg(const int sock, const char *user, const char *fmt, ...) |
62
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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
419 return nn_send_to_socket(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
423 nn_ringbuf_t * nn_ringbuf_new(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
435 void nn_ringbuf_free(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
447 void nn_ringbuf_add(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 | 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 | 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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
460 int nn_editbuf_write(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
476 int nn_editbuf_insert(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
494 int nn_editbuf_delete(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
508 void nn_editbuf_clear(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
515 nn_editbuf_t * nn_editbuf_new(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
526 void nn_editbuf_free(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
535 nn_editbuf_t * nn_editbuf_copy(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 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
543 if ((res = nn_editbuf_new(src->size)) == NULL) |
71
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 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
553 char * nn_editbuf_get_string(nn_editbuf_t *buf, ssize_t start, ssize_t end) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
554 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
555 char *str; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
556 ssize_t siz; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
557 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
558 if (buf == NULL) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
559 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
560 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
561 if (start < 0 || end > buf->len || start >= buf->len) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
562 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
563 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
564 if (end < 0) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
565 siz = buf->len - start + 1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
566 } else if (start <= end) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
567 siz = end - start + 1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
568 } else |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
569 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
570 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
571 str = th_malloc(siz + 1); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
572 memcpy(str, buf->data + start, siz); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
573 str[siz] = 0; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
574 return str; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
575 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
576 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
577 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
578 void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos) |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
579 { |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
580 assert(buf != NULL); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
581 |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
582 if (pos < 0) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
583 buf->pos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
584 else if (pos >= buf->len) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
585 buf->pos = buf->len; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
586 else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
587 buf->pos = pos; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
588 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
589 |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
590 |
102 | 591 static uint8_t nn_hash_user(const char *name) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
592 { |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
593 /* |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
594 int n = 0; |
102 | 595 const uint8_t *c = (uint8_t *)name; |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
596 uint8_t hash = 0xff; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
597 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
598 while (*c && n < 4) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
599 hash = (hash << 1) ^ tolower(*c); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
600 c++; n++; |
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 (hash & 0xff); |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
604 */ |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
605 return tolower(name[0]); |
94
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 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
608 |
102 | 609 static void nn_user_insert(nn_user_t **list, nn_user_t *node) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
610 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
611 node->next = *list; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
612 *list = node; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
613 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
614 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
615 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
616 nn_user_t *nn_userhash_foreach(const nn_userhash_t *list, int (*func)(const nn_user_t *)) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
617 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
618 int i; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
619 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
620 if (list == NULL) return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
621 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
622 for (i = 0; i < NN_NUM_BUCKETS; i++) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
623 if (list->buckets[i] != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
624 nn_user_t *curr = list->buckets[i]; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
625 while (curr != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
626 if (func(curr) != 0) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
627 return curr; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
628 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
629 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
630 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
631 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
632 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
633 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
634 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
635 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
636 nn_user_t *nn_user_find(const nn_userhash_t *list, const char *name) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
637 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
638 uint8_t hash; |
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 if (list == NULL) return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
641 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
642 hash = nn_hash_user(name); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
643 if (list->buckets[hash] != NULL) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
644 nn_user_t *curr = list->buckets[hash]; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
645 while (curr != NULL) { |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
646 if (strcasecmp(curr->name, name) == 0) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
647 return curr; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
648 curr = curr->next; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
649 } |
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 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
652 return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
653 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
654 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
655 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
656 nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current) |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
657 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
658 uint8_t hash; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
659 |
108
f323b137ca08
Tab-completion now works with cycling, but bugs with names that have whitespaces.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
660 if (list == NULL || pattern == NULL) return NULL; |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
661 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
662 hash = nn_hash_user(pattern); |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
663 if (list->buckets[hash] != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
664 nn_user_t *curr = list->buckets[hash]; |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
665 size_t len = strlen(pattern); |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
666 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
667 if (current != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
668 nn_user_t *found = NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
669 while (curr != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
670 if (strcasecmp(curr->name, current) == 0) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
671 found = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
672 break; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
673 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
674 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
675 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
676 if (found != NULL) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
677 curr = found; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
678 else |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
679 curr = list->buckets[hash]; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
680 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
681 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
682 while (curr != NULL) { |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
683 if (len <= strlen(curr->name) && strncasecmp(curr->name, pattern, len) == 0) |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
684 return curr; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
685 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
686 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
687 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
688 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
689 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
690 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
691 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
692 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
693 nn_userhash_t *nn_userhash_new(void) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
694 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
695 return th_calloc(1, sizeof(nn_userhash_t)); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
696 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
697 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
698 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
699 int nn_userhash_insert(nn_userhash_t *list, const char *name) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
700 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
701 uint8_t hash; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
702 nn_user_t *user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
703 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
704 /* Check arguments */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
705 if (list == NULL || name == NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
706 return -1; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
707 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
708 /* Check if username is already there */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
709 if (nn_user_find(list, name) != NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
710 return 1; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
711 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
712 /* No, we'll add it */ |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
713 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
|
714 return -3; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
715 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
716 user->name = th_strdup(name); |
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
717 if (user->name == NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
718 return -4; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
719 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
720 hash = nn_hash_user(name); |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
721 nn_user_insert(&(list->buckets[hash]), user); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
722 |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
723 return 0; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
724 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
725 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
726 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
727 int nn_userhash_delete(nn_userhash_t *list, const char *name) |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
728 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
729 uint8_t hash; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
730 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
731 /* Check arguments */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
732 if (list == NULL || name == NULL) |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
733 return -1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
734 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
735 /* Check if username is already there */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
736 hash = nn_hash_user(name); |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
737 if (list->buckets[hash] != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
738 nn_user_t *curr, *prev; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
739 curr = list->buckets[hash]; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
740 prev = NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
741 while (curr != NULL) { |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
742 if (strcasecmp(curr->name, name) == 0) { |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
743 if (prev) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
744 prev->next = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
745 else |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
746 list->buckets[hash] = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
747 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
748 nn_user_free(curr); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
749 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
750 return 0; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
751 } else { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
752 prev = curr; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
753 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
754 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
755 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
756 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
757 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
758 return 1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
759 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
760 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
761 |
102 | 762 nn_user_t *nn_user_copy(const nn_user_t *src) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
763 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
764 nn_user_t *user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
765 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
766 if (src == NULL) return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
767 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
768 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
|
769 return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
770 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
771 /* Copy relevant data */ |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
772 user->name = th_strdup(src->name); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
773 user->lastspoke = src->lastspoke; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
774 user->joined = src->joined; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
775 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
776 return user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
777 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
778 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
779 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
780 void nn_user_free(nn_user_t *user) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
781 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
782 th_free(user->name); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
783 th_free(user); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
784 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
785 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
786 |
102 | 787 void nn_user_free_list(nn_user_t *list) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
788 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
789 nn_user_t *next, *curr = list; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
790 while (curr != NULL) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
791 next = curr->next; |
102 | 792 nn_user_free(curr); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
793 curr = next; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
794 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
795 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
796 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
797 void nn_userhash_free(nn_userhash_t *hash) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
798 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
799 int i; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
800 if (hash == NULL) |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
801 return; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
802 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
803 for (i = 0; i < NN_NUM_BUCKETS; i++) { |
102 | 804 nn_user_free_list(hash->buckets[i]); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
805 hash->buckets[i] = NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
806 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
807 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
808 th_free(hash); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
809 } |