Mercurial > hg > nnchat
annotate libnnchat.c @ 218:0e60593006c9
Change ShellExecute return value's variable type to HINSTANCE as it should be.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 21 Nov 2010 04:22:06 +0200 |
parents | 659b8229d015 |
children | 1211b740fbda |
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 |
90
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
9 #ifdef __WIN32 |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
10 const char *hstrerror(int err) |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
11 { |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
12 static char buf[64]; |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
13 snprintf(buf, sizeof(buf), "Error #%d", err); |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
14 return buf; |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
15 } |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
16 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
17 int nn_get_socket_errno(void) |
90
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
18 { |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
19 return WSAGetLastError(); |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
20 } |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
21 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
22 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
|
23 { |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
24 static char buf[64]; |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
25 switch (err) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
26 case WSAEADDRINUSE: return "Address already in use"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
27 case WSAECONNABORTED: return "Software caused connection abort"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
28 case WSAECONNREFUSED: return "Connection refused"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
29 case WSAECONNRESET: return "Connection reset by peer"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
30 case WSAEHOSTUNREACH: return "No route to host"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
31 case WSAENETDOWN: return "Network is down"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
32 case WSAETIMEDOUT: return "Connection timed out"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
33 case WSAHOST_NOT_FOUND: return "Host not found"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
34 case WSAVERNOTSUPPORTED: return "Wrong WinSock DLL version"; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
35 default: |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
36 snprintf(buf, sizeof(buf), "Error #%d", err); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
37 return buf; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
38 break; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
39 } |
90
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
40 } |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
41 #else |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
42 int nn_get_socket_errno(void) |
90
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
43 { |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
44 return errno; |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
45 } |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
46 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
47 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
|
48 { |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
49 return strerror(err); |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
50 } |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
51 #endif |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
52 |
1e0bf7b4fd41
Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
53 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
54 void nn_conn_err(nn_conn_t *conn, const char *fmt, ...) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 { |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
56 conn->err = TRUE; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
57 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
58 if (conn->errfunc) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
59 va_list ap; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
60 va_start(ap, fmt); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
61 conn->errfunc(conn, fmt, ap); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
62 va_end(ap); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
63 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
64 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
65 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
66 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
67 static void nn_conn_msg(nn_conn_t *conn, const char *fmt, ...) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
68 { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
69 if (conn->msgfunc) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
70 va_list ap; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
71 va_start(ap, fmt); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
72 conn->msgfunc(conn, fmt, ap); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
73 va_end(ap); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
74 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
75 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
76 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
77 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
78 nn_conn_t *nn_conn_open(struct in_addr *addr, const int port) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
79 { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
80 nn_conn_t *conn = th_calloc(1, sizeof(nn_conn_t)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
81 struct sockaddr_in dest; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
82 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
83 if (conn == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
84 return NULL; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
85 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
86 dest.sin_family = AF_INET; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
87 dest.sin_port = htons(port); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
88 dest.sin_addr = *addr; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
90 nn_conn_msg(conn, "Connecting to %s:%d ...\n", |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
91 inet_ntoa(dest.sin_addr), port); |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
93 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
94 nn_conn_err(conn, "Could not open socket: %s\n", strerror(errno)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
95 conn->status = NN_CONN_CLOSED; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
96 return conn; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
97 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
98 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
99 nn_conn_msg(conn, "Using socket %d.\n", conn->socket); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
100 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
101 if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
102 nn_conn_err(conn, "Could not connect: %s\n", strerror(errno)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
103 conn->status = NN_CONN_CLOSED; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
104 return conn; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
105 } |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
106 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
107 conn->port = port; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
108 conn->address = *addr; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
109 FD_ZERO(&(conn->sockfds)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
110 FD_SET(conn->socket, &(conn->sockfds)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
111 conn->status = NN_CONN_OPEN; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
112 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
113 return conn; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
117 void nn_conn_close(nn_conn_t *conn) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 { |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
119 if (conn == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
120 return; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
121 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
122 if (conn->socket >= 0) { |
64
6a3a917303e4
Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
123 #ifdef __WIN32 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
124 closesocket(conn->socket); |
64
6a3a917303e4
Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
125 #else |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
126 close(conn->socket); |
64
6a3a917303e4
Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
127 #endif |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
128 conn->socket = -1; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
129 } |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
130 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
131 conn->status = NN_CONN_CLOSED; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
132 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
133 th_free(conn); |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
137 BOOL nn_conn_send_buf(nn_conn_t *conn, const char *buf, const size_t len) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
138 { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
139 size_t bufLeft = len; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
140 const char *bufPtr = buf; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
141 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
142 while (bufLeft > 0) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
143 ssize_t bufSent; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
144 bufSent = send(conn->socket, bufPtr, bufLeft, 0); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
145 if (bufSent < 0) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
146 nn_conn_err(conn, "nn_conn_send_buf() failed: %s", strerror(errno)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
147 return FALSE; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
148 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
149 bufLeft -= bufSent; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
150 bufPtr += bufSent; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
151 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
152 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
153 return TRUE; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
154 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
155 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
156 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
157 BOOL nn_conn_pull(nn_conn_t *conn) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
158 { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
159 int result; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
160 struct timeval socktv; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
161 fd_set tmpfds; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
162 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
163 if (conn == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
164 return -1; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
165 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
166 conn->ptr = conn->buf; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
167 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
168 /* Check for incoming data */ |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
169 socktv.tv_sec = 0; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
170 socktv.tv_usec = NN_DELAY_USEC; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
171 tmpfds = conn->sockfds; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
172 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
173 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
174 int err = nn_get_socket_errno(); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
175 if (err != EINTR) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
176 nn_conn_err(conn, "Error occured in select(%d, sockfds): %d, %s\n", |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
177 socket, err, nn_get_socket_errstr(err)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
178 return -1; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
179 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
180 } else |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
181 if (FD_ISSET(conn->socket, &tmpfds)) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
182 conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE, 0); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
183 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
184 if (conn->got < 0) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
185 int res = nn_get_socket_errno(); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
186 nn_conn_err(conn, "Error in recv: %d, %s\n", res, nn_get_socket_errstr(res)); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
187 return -2; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
188 } else if (conn->got == 0) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
189 nn_conn_err(conn, "Server closed connection.\n"); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
190 conn->status = NN_CONN_CLOSED; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
191 return -3; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
192 } else { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
193 /* Handle protocol data */ |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
194 conn->buf[conn->got] = 0; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
195 return 0; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
196 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
197 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
198 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
199 return 1; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
200 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
201 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
202 BOOL nn_conn_check(nn_conn_t *conn) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
203 { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
204 if (conn == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
205 return FALSE; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
206 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
207 return conn->err == 0 && conn->status == NN_CONN_OPEN; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
208 } |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
209 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
210 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
211 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
212 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
|
213 { |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
214 #ifdef __WIN32 |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
215 /* 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
|
216 WSADATA wsaData; |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 return FALSE; |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
221 } |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
222 #endif |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
223 return TRUE; |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
224 } |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
225 |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
226 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
227 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
|
228 { |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
229 #ifdef __WIN32 |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
230 WSACleanup(); |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
231 #endif |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
232 } |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
233 |
acfc4b4bc180
Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents:
90
diff
changeset
|
234 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
235 static BOOL bufRealloc(char **buf, size_t *size, size_t add) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
237 return ((*buf = th_realloc(*buf, *size + add)) != NULL); |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x) |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
241 static BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 { |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
243 if (*pos >= *size && !bufRealloc(buf, size, NN_ALLOC_SIZE)) |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
244 return FALSE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
245 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
246 (*buf)[*pos] = ch; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
247 (*pos)++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
248 return TRUE; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x) |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
252 static BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
254 size_t tmpLen; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
255 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
256 if (str == NULL) return FALSE; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
257 tmpLen = strlen(str); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
258 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
259 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + NN_ALLOC_SIZE)) |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
260 return FALSE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
261 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
262 strcpy(*buf + *pos, str); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
263 (*pos) += tmpLen; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
264 return TRUE; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
268 char *nn_encode_str1(const char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
270 const char *s = str; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
271 char *result; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
272 size_t resSize, resPos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
273 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
274 if (str == NULL) return NULL; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
275 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
276 resSize = strlen(str) + NN_ALLOC_SIZE; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
277 if ((result = th_malloc(resSize)) == NULL) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
278 return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
279 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
280 while (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
281 switch (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
282 case 32: |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
283 PUSHCHAR('+'); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
284 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
285 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
286 default: |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
287 if (th_isalnum(*s)) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
288 PUSHCHAR(*s); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
289 else { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
290 char tmpStr[4]; |
215
659b8229d015
Silence some warnings on OpenBSD.
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
291 snprintf(tmpStr, sizeof(tmpStr), "%2X", (unsigned char) *s); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
292 PUSHCHAR('%'); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
293 PUSHSTR(tmpStr); |
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 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
296 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
297 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
298 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
299 PUSHCHAR(0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
300 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
301 return result; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
302 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 static int getHexDigit(const int c, const int shift) |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
307 int i; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
308 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
309 if (c >= 'A' && c <= 'F') |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
310 i = c - 'A' + 10; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
311 else if (c >= 'a' && c <= 'f') |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
312 i = c - 'a' + 10; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
313 else if (c >= '0' && c <= '9') |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
314 i = c - '0'; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
315 else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
316 return -1; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
317 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
318 return i << shift; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
319 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
322 char *nn_decode_str1(const char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
324 const char *s = str; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
325 char *result; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
326 size_t resSize, resPos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
327 int c; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
328 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
329 if (str == NULL) return NULL; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
330 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
331 resSize = strlen(str) + NN_ALLOC_SIZE; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
332 if ((result = th_malloc(resSize)) == NULL) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
333 return NULL; |
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 while (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
336 switch (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
337 case '+': |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
338 PUSHCHAR(' '); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
339 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
340 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
341 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
342 case '½': |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
343 /* Escape these .. */ |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
344 PUSHCHAR('½'); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
345 PUSHCHAR('½'); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
346 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
347 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
348 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
349 case '\r': |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
350 PUSHCHAR(' '); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
351 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
352 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
353 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
354 case '%': |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
355 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
356 if (*s == '%') |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
357 PUSHCHAR('%'); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
358 else if ((c = getHexDigit(*s, 4)) >= 0) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
359 int i = getHexDigit(*(++s), 0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
360 if (i >= 0) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
361 PUSHCHAR(c | i); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
362 } else { |
72
fe5fc76c0806
Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
363 PUSHCHAR('§'); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
364 PUSHCHAR(*s); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
365 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
366 } else { |
72
fe5fc76c0806
Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
367 PUSHCHAR('§'); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
368 PUSHCHAR(*s); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
369 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
370 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
371 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
372 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
373 default: |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
374 PUSHCHAR(*s); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
375 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
376 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
377 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
378 PUSHCHAR(0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
379 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
380 return result; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
381 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
384 char *nn_strip_tags(const char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
386 const char *s = str; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
387 char *result; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
388 size_t resSize, resPos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
389 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
390 if (str == NULL) return NULL; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
391 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
392 resSize = strlen(str) + NN_ALLOC_SIZE; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
393 if ((result = th_malloc(resSize)) == NULL) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
394 return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
395 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
396 while (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
397 if (*s == '<') { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
398 while (*s && *s != '>') s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
399 if (*s == '>') s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
400 } else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
401 PUSHCHAR(*s++); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
402 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
403 PUSHCHAR(0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
404 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
405 return result; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
406 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
409 typedef struct { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
410 char c; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
411 char *ent; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
412 } html_entity_t; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
413 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
414 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
415 static const html_entity_t HTMLEntities[] = { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
416 { '<', "<" }, |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
417 { '>', ">" }, |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
418 }; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
419 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
420 static const int numHTMLEntities = sizeof(HTMLEntities) / sizeof(HTMLEntities[0]); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
421 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
422 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
423 char *nn_encode_str2(const char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
425 const char *s = str; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
426 char *result; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
427 size_t resSize, resPos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
428 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
429 if (str == NULL) return NULL; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
430 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
431 resSize = strlen(str) + NN_ALLOC_SIZE; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
432 if ((result = th_malloc(resSize)) == NULL) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
433 return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
434 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
435 while (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
436 int i; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
437 BOOL found = FALSE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
438 for (i = 0; i < numHTMLEntities; i++) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
439 if (HTMLEntities[i].c == *s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
440 PUSHSTR(HTMLEntities[i].ent); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
441 found = TRUE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
442 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
443 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
444 if (!found) PUSHCHAR(*s); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
445 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
446 s++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
447 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
448 PUSHCHAR(0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
449 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
450 return result; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
451 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
453 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
454 char *nn_decode_str2(const char *str) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
455 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
456 const char *s = str; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
457 char *result; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
458 size_t resSize, resPos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
459 |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
460 if (str == NULL) return NULL; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
461 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
462 resSize = strlen(str); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
463 if ((result = th_malloc(resSize)) == NULL) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
464 return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
465 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
466 while (*s) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
467 if (*s == '&') { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
468 int i; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
469 BOOL found = FALSE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
470 for (i = 0; i < numHTMLEntities; i++) { |
95 | 471 const html_entity_t *ent = &HTMLEntities[i]; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
472 int len = strlen(ent->ent); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
473 if (!strncmp(s, ent->ent, len)) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
474 PUSHCHAR(ent->c); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
475 s += len; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
476 found = TRUE; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
477 break; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
478 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
479 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
480 if (!found) PUSHCHAR(*s++); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
481 } else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
482 PUSHCHAR(*s++); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
483 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
484 PUSHCHAR(0); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
485 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
486 return result; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
487 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
488 |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
489 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
490 char *nn_dbldecode_str(const char *str) |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
491 { |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
492 char *res, *tmp; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
493 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
494 if ((tmp = nn_decode_str1(str)) == NULL) |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
495 return NULL; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
496 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
497 res = nn_decode_str2(tmp); |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
498 th_free(tmp); |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
499 |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
500 return res; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
501 } |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
502 |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
503 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
504 char *nn_dblencode_str(const char *str) |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
505 { |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
506 char *res, *tmp; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
507 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
508 if ((tmp = nn_encode_str2(str)) == NULL) |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
509 return NULL; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
510 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
511 res = nn_encode_str1(tmp); |
78
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
512 th_free(tmp); |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
513 |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
514 return res; |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
515 } |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
516 |
745f670068dc
Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
517 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
518 BOOL nn_conn_send_msg(nn_conn_t *conn, const char *user, const char *fmt, ...) |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
519 { |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
520 char *tmp, *msg; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
521 va_list ap; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
522 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
523 va_start(ap, fmt); |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
524 tmp = th_strdup_vprintf(fmt, ap); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
525 va_end(ap); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
526 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
527 if (tmp == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
528 return FALSE; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
529 |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
530 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, tmp); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
531 th_free(tmp); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
532 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
533 if (msg != NULL) { |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
534 BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
535 th_free(msg); |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
536 return ret; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
537 } else |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
538 return FALSE; |
62
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
539 } |
ff5d74f0d428
Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
541 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
542 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
|
543 { |
89
c2d916b340bf
Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
79
diff
changeset
|
544 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
|
545 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
546 res->data = (char **) th_malloc(size * sizeof(char *)); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
547 res->size = size; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
548 res->n = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
549 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
550 return res; |
65
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 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
553 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
554 void nn_ringbuf_free(nn_ringbuf_t *buf) |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
555 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
556 size_t i; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
557 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
558 for (i = 0; i < buf->n; i++) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
559 th_free(buf->data[i]); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
560 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
561 th_free(buf->data); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
562 th_free(buf); |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
563 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
564 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
565 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
566 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
|
567 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
568 if (buf->n < buf->size) { |
79 | 569 buf->data[buf->n] = th_strdup(str); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
570 buf->n++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
571 } else { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
572 th_free(buf->data[0]); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
573 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); |
79 | 574 buf->data[buf->size - 1] = th_strdup(str); |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
575 } |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
576 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
577 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
578 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
579 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
|
580 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
581 if (buf->len+1 >= buf->size) return -3; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
582 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
583 if (pos < 0) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
584 return -1; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
585 else if (pos >= buf->len) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
586 buf->data[buf->len++] = ch; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
587 } else { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
588 buf->data[pos] = ch; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
589 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
590 return 0; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
591 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
592 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
593 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
594 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
|
595 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
596 if (buf->len+1 >= buf->size) return -3; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
597 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
598 if (pos < 0) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
599 return -1; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
600 else if (pos >= buf->len) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
601 buf->data[buf->len] = ch; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
602 } else { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
603 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
|
604 buf->data[pos] = ch; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
605 } |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
606 buf->len++; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
607 return 0; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
608 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
609 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
610 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
611 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
|
612 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
613 if (pos < 0) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
614 return -1; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
615 else if (pos < buf->len) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
616 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
|
617 buf->len--; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
618 return 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
619 } else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
620 return -2; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
621 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
622 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
623 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
624 void nn_editbuf_clear(nn_editbuf_t *buf) |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
625 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
626 buf->len = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
627 buf->pos = 0; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
628 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
629 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
630 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
631 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
|
632 { |
89
c2d916b340bf
Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
79
diff
changeset
|
633 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
|
634 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
635 res->data = (char *) th_malloc(n); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
636 res->size = n; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
637 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
638 return res; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
639 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
640 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
641 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
642 void nn_editbuf_free(nn_editbuf_t *buf) |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
643 { |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
644 if (buf) { |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
645 th_free(buf->data); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
646 th_free(buf); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
647 } |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
648 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
649 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
650 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
651 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
|
652 { |
89
c2d916b340bf
Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
79
diff
changeset
|
653 nn_editbuf_t *res; |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
654 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
655 assert(src != NULL); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
656 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
657 if (src == NULL) return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
658 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
659 if ((res = nn_editbuf_new(src->size)) == NULL) |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
660 return NULL; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
661 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
662 memcpy(res->data, src->data, src->size); |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
663 res->pos = res->len = src->len; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
664 |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
665 return res; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
666 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
667 |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
668 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
669 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
|
670 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
671 char *str; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
672 ssize_t siz; |
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 if (buf == NULL) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
675 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
676 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
677 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
|
678 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
679 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
680 if (end < 0) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
681 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
|
682 } 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
|
683 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
|
684 } else |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
685 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
686 |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
687 if ((str = th_malloc(siz + 1)) == NULL) |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
688 return NULL; |
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
689 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
690 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
|
691 str[siz] = 0; |
168
2e4850ece456
Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
167
diff
changeset
|
692 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
693 return str; |
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 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
696 |
97
218efd2f0641
Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
697 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
|
698 { |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
699 assert(buf != NULL); |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
700 |
71
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
701 if (pos < 0) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
702 buf->pos = 0; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
703 else if (pos >= buf->len) |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
704 buf->pos = buf->len; |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
705 else |
3a23c2adfb78
Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
706 buf->pos = pos; |
65
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
707 } |
e763ef5cfd53
Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
708 |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
709 |
102 | 710 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
|
711 { |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
712 /* |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
713 int n = 0; |
102 | 714 const uint8_t *c = (uint8_t *)name; |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
715 uint8_t hash = 0xff; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
716 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
717 while (*c && n < 4) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
718 hash = (hash << 1) ^ tolower(*c); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
719 c++; n++; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
720 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
721 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
722 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
|
723 */ |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
724 return tolower(name[0]); |
94
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 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
727 |
102 | 728 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
|
729 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
730 node->next = *list; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
731 *list = node; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
732 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
733 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
734 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
735 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
|
736 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
737 int i; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
738 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
739 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
|
740 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
741 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
|
742 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
|
743 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
|
744 while (curr != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
745 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
|
746 return curr; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
747 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
748 } |
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 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
751 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
752 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
753 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
754 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
755 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
|
756 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
757 uint8_t hash; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
758 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
759 if (list == NULL) return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
760 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
761 hash = nn_hash_user(name); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
762 if (list->buckets[hash] != NULL) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
763 nn_user_t *curr = list->buckets[hash]; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
764 while (curr != NULL) { |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
765 if (strcasecmp(curr->name, name) == 0) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
766 return curr; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
767 curr = curr->next; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
768 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
769 } |
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 return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
772 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
773 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
774 |
164
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
775 static nn_user_t *nn_user_match_do(nn_user_t *list, const char *pattern, size_t len) |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
776 { |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
777 nn_user_t *curr = list; |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
778 while (curr != NULL) { |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
779 if (len <= strlen(curr->name) && strncasecmp(curr->name, pattern, len) == 0) |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
780 return curr; |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
781 curr = curr->next; |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
782 } |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
783 return NULL; |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
784 } |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
785 |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
786 |
116
741e45592522
Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
787 nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current, BOOL again) |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
788 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
789 uint8_t hash; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
790 |
108
f323b137ca08
Tab-completion now works with cycling, but bugs with names that have whitespaces.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
791 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
|
792 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
793 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
|
794 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
|
795 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
|
796 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
|
797 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
798 if (current != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
799 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
|
800 while (curr != NULL) { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
801 if (strcasecmp(curr->name, current) == 0) { |
116
741e45592522
Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
802 if (again) |
741e45592522
Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
803 return curr; |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
804 found = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
805 break; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
806 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
807 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
808 } |
164
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
809 |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
810 if (found != NULL && (found = nn_user_match_do(found, pattern, len)) != NULL) |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
811 return found; |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
812 } |
116
741e45592522
Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents:
108
diff
changeset
|
813 |
164
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
814 if ((curr = nn_user_match_do(list->buckets[hash], pattern, len)) != NULL) |
701c54a45466
Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
815 return curr; |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
816 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
817 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
818 return NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
819 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
820 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
821 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
822 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
|
823 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
824 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
|
825 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
826 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
827 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
828 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
|
829 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
830 uint8_t hash; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
831 nn_user_t *user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
832 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
833 /* Check arguments */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
834 if (list == NULL || name == NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
835 return -1; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
836 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
837 /* 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
|
838 if (nn_user_find(list, name) != NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
839 return 1; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
840 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
841 /* No, we'll add it */ |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
842 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
|
843 return -3; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
844 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
845 user->name = th_strdup(name); |
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
846 if (user->name == NULL) |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
847 return -4; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
848 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
849 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
|
850 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
|
851 |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
852 return 0; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
853 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
854 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
855 |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
856 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
|
857 { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
858 uint8_t hash; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
859 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
860 /* Check arguments */ |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
861 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
|
862 return -1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
863 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
864 /* 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
|
865 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
|
866 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
|
867 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
|
868 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
|
869 prev = NULL; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
870 while (curr != NULL) { |
106
c587a99e2096
Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
871 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
|
872 if (prev) |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
873 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
|
874 else |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
875 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
|
876 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
877 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
|
878 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
879 return 0; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
880 } else { |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
881 prev = curr; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
882 curr = curr->next; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
883 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
884 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
885 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
886 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
887 return 1; |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
888 } |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
889 |
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
890 |
102 | 891 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
|
892 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
893 nn_user_t *user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
894 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
895 if (src == NULL) return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
896 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
897 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
|
898 return NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
899 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
900 /* Copy relevant data */ |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
901 user->name = th_strdup(src->name); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
902 user->lastspoke = src->lastspoke; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
903 user->joined = src->joined; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
904 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
905 return user; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
906 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
907 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
908 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
909 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
|
910 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
911 th_free(user->name); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
912 th_free(user); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
913 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
914 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
915 |
102 | 916 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
|
917 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
918 nn_user_t *next, *curr = list; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
919 while (curr != NULL) { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
920 next = curr->next; |
102 | 921 nn_user_free(curr); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
922 curr = next; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
923 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
924 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
925 |
103
eaa524e153f9
Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
926 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
|
927 { |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
928 int i; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
929 if (hash == NULL) |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
930 return; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
931 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
932 for (i = 0; i < NN_NUM_BUCKETS; i++) { |
102 | 933 nn_user_free_list(hash->buckets[i]); |
94
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
934 hash->buckets[i] = NULL; |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
935 } |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
936 |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
937 th_free(hash); |
6e47426efb6a
Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
91
diff
changeset
|
938 } |