annotate util.c @ 413:14b685cdbd2c

Rename files.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 06:41:07 +0300
parents libnnutil.c@3e64acb433e8
children d015ecbd231d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
394
cabf2233ea39 Update copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 393
diff changeset
4 * (C) Copyright 2008-2012 Tecnic Software productions (TNSP)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
6 #include "util.h"
396
07a46ca075ab Add some new functions for socket / connection input buffer
Matti Hamalainen <ccr@tnsp.org>
parents: 395
diff changeset
7
07a46ca075ab Add some new functions for socket / connection input buffer
Matti Hamalainen <ccr@tnsp.org>
parents: 395
diff changeset
8
304
61884ce9db41 Use th_vputch() and th_vputs() instead of similar routines in libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
9 #define PUSHCHAR(x) th_vputch(&result, &resSize, &resPos, x)
61884ce9db41 Use th_vputch() and th_vputs() instead of similar routines in libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
10 #define PUSHSTR(x) th_vputs(&result, &resSize, &resPos, x)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
12 char *nn_encode_str1(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
14 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
15 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
16 size_t resSize, resPos = 0;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
17
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
18 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
19
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
20 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
21 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
22 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
23
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
24 while (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
25 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
26 switch (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
27 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
28 case 32:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
29 PUSHCHAR('+');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
30 break;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
31
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
32 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
33 if (th_isalnum(*s))
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
34 PUSHCHAR(*s);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
35 else
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
36 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
37 char tmpStr[4];
215
659b8229d015 Silence some warnings on OpenBSD.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
38 snprintf(tmpStr, sizeof(tmpStr), "%2X", (unsigned char) *s);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
39 PUSHCHAR('%');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
40 PUSHSTR(tmpStr);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
41 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
42 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
43 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
44 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
45 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
46 PUSHCHAR(0);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
47
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
48 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
395
74d97581dc46 Rename variable and function, internal use only.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
52 static int nn_get_hexdigit(const int c, const int shift)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
54 int i;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
55
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
56 if (c >= 'A' && c <= 'F')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
57 i = c - 'A' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
58 else if (c >= 'a' && c <= 'f')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
59 i = c - 'a' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
60 else if (c >= '0' && c <= '9')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
61 i = c - '0';
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
62 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
63 return -1;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
64
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
65 return i << shift;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
69 char *nn_decode_str1(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
71 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
72 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
73 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
74 int c;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
75
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
76 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
77
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
78 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
79 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
80 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
81
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
82 while (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
83 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
84 switch (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
85 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
86 case '+':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
87 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
88 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
89 break;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
90
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
91 case '½':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
92 /* Escape these .. */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
93 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
94 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
95 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
96 break;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
97
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
98 case '\r':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
99 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
100 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
101 break;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
102
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
103 case '%':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
104 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
105 if (*s == '%')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
106 PUSHCHAR('%');
395
74d97581dc46 Rename variable and function, internal use only.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
107 else if ((c = nn_get_hexdigit(*s, 4)) >= 0)
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
108 {
395
74d97581dc46 Rename variable and function, internal use only.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
109 int i = nn_get_hexdigit(*(++s), 0);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
110 if (i >= 0)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
111 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
112 PUSHCHAR(c | i);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
113 }
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
114 else
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
115 {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
116 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
117 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
118 }
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
119 }
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
120 else
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
121 {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
122 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
123 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
124 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
125 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
126 break;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
127
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
128 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
129 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
130 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
131 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
132 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
133 PUSHCHAR(0);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
134
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
135 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
139 char *nn_strip_tags(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
141 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
142 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
143 size_t resSize, resPos = 0;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
144
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
145 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
146
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
147 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
148 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
149 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
150
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
151 while (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
152 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
153 if (*s == '<')
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
154 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
155 while (*s && *s != '>') s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
156 if (*s == '>') s++;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
157 }
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
158 else
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
159 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
160 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
161 PUSHCHAR(0);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
162
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
163 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
167 typedef struct
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
168 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
169 char c;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
170 char *ent;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
171 } html_entity_t;
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
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
174 static const html_entity_t HTMLEntities[] =
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
175 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
176 { '<', "&lt;" },
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
177 { '>', "&gt;" },
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
178 };
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 static const int numHTMLEntities = sizeof(HTMLEntities) / sizeof(HTMLEntities[0]);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
181
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
182
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
183 char *nn_encode_str2(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
185 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
186 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
187 size_t resSize, resPos = 0;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
188
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
189 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
190
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
191 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
192 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
193 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
194
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
195 while (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
196 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
197 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
198 BOOL found = FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
199 for (i = 0; i < numHTMLEntities; i++)
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
200 if (HTMLEntities[i].c == *s)
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
201 {
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
202 PUSHSTR(HTMLEntities[i].ent);
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
203 found = TRUE;
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
204 break;
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
205 }
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
206 if (!found) PUSHCHAR(*s);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
207
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
208 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
209 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
210 PUSHCHAR(0);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
211
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
212 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
216 char *nn_decode_str2(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
218 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
219 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
220 size_t resSize, resPos = 0;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
221
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
222 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
223
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
224 resSize = strlen(str);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
225 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
226 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
227
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
228 while (*s)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
229 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
230 if (*s == '&')
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
231 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
232 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
233 BOOL found = FALSE;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
234 for (i = 0; i < numHTMLEntities; i++)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
235 {
95
69f9c46e4a94 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
236 const html_entity_t *ent = &HTMLEntities[i];
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
237 int len = strlen(ent->ent);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
238 if (!strncmp(s, ent->ent, len))
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
239 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
240 PUSHCHAR(ent->c);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
241 s += len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
242 found = TRUE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
243 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
244 }
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 if (!found) PUSHCHAR(*s++);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
247 }
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
248 else
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
249 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
250 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
251 PUSHCHAR(0);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
252
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
253 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
257 char *nn_dbldecode_str(const char *str)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
258 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
259 char *res, *tmp;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
260
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
261 if ((tmp = nn_decode_str1(str)) == NULL)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
262 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
263
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
264 res = nn_decode_str2(tmp);
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
265 th_free(tmp);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
266
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
267 return res;
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
268 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
269
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
270
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
271 char *nn_dblencode_str(const char *str)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
272 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
273 char *res, *tmp;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
274
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
275 if ((tmp = nn_encode_str2(str)) == NULL)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
276 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
277
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
278 res = nn_encode_str1(tmp);
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
279 th_free(tmp);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
280
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
281 return res;
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
282 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
283
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
284
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
285 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
286 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
287 if (buf->len+1 >= buf->size) return -3;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
288
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
289 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
290 return -1;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
291 else if (pos >= buf->len)
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
292 buf->data[buf->len++] = ch;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
293 else
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
294 buf->data[pos] = ch;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
295
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
296 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
297 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
298
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
299
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
300 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
301 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
302 if (buf->len+1 >= buf->size) return -3;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
303
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
304 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
305 return -1;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
306 else if (pos >= buf->len)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
307 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
308 buf->data[buf->len] = ch;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
309 }
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
310 else
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
311 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
312 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
313 buf->data[pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
314 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
315 buf->len++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
316 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
317 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
318
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
319
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
320 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
321 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
322 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
323 return -1;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
324 else if (pos < buf->len)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
325 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
326 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
327 buf->len--;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
328 return 0;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
329 }
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
330 else
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
331 return -2;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
332 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
333
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
334
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
335 void nn_editbuf_clear(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
336 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
337 buf->len = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
338 buf->pos = 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
339 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
340
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
341
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
342 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
343 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
344 nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t));
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
345
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
346 res->data = (char *) th_malloc(n);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
347 res->size = n;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
348
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
349 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
350 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
351
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
352
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
353 void nn_editbuf_free(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
354 {
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
355 if (buf != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
356 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
357 th_free(buf->data);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
358 th_free(buf);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
359 }
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
360 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
361
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
362
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
363 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
364 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
365 nn_editbuf_t *res;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
366
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
367 assert(src != NULL);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
368
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
369 if (src == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
370
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
371 if ((res = nn_editbuf_new(src->size)) == NULL)
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
372 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
373
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
374 memcpy(res->data, src->data, src->size);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
375 res->pos = res->len = src->len;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
376
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
377 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
378 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
379
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
380
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
381 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
382 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
383 char *str;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
384 ssize_t siz;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
385
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
386 if (buf == NULL)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
387 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
388
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
389 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
390 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
391
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
392 if (end < 0)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
393 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
394 siz = buf->len - start + 1;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
395 }
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
396 else if (start <= end)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
397 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
398 siz = end - start + 1;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
399 }
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
400 else
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
401 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
402
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
403 if ((str = th_malloc(siz + 1)) == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
404 return NULL;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
405
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
406 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
407 str[siz] = 0;
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
408
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
409 return str;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
410 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
411
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
412
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
413 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
414 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
415 assert(buf != NULL);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
416
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
417 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
418 buf->pos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
419 else if (pos >= buf->len)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
420 buf->pos = buf->len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
421 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
422 buf->pos = pos;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
423 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
424
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
425
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
426 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
427 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
428 /*
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
429 int n = 0;
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
430 const uint8_t *c = (uint8_t *)name;
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
431 uint8_t hash = 0xff;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
432
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
433 while (*c && n < 4)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
434 {
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
435 hash = (hash << 1) ^ tolower(*c);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
436 c++; n++;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
437 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
438
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
439 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
440 */
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
441 return tolower(name[0]);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
442 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
443
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
444
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
445 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
446 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
447 node->next = *list;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
448 *list = node;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
449 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
450
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
451
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
452 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
453 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
454 int i;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
455
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
456 if (list == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
457
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
458 for (i = 0; i < NN_NUM_BUCKETS; i++)
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
459 if (list->buckets[i] != NULL)
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
460 {
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
461 nn_user_t *curr = list->buckets[i];
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
462 while (curr != NULL)
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
463 {
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
464 if (func(curr) != 0)
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
465 return curr;
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
466 curr = curr->next;
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
467 }
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
468 }
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
469
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
470 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
471 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
472
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
473
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
474 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
475 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
476 uint8_t hash;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
477
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
478 if (list == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
479
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
480 hash = nn_hash_user(name);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
481 if (list->buckets[hash] != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
482 {
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
483 nn_user_t *curr = list->buckets[hash];
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
484 while (curr != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
485 {
372
b9f0bdad6285 Use th_strcasecmp() and th_strncasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 365
diff changeset
486 if (th_strcasecmp(curr->name, name) == 0)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
487 return curr;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
488 curr = curr->next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
489 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
490 }
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
491
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
492 return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
493 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
494
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
495
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
496 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
497 {
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
498 nn_user_t *curr = list;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
499
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
500 while (curr != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
501 {
372
b9f0bdad6285 Use th_strcasecmp() and th_strncasecmp().
Matti Hamalainen <ccr@tnsp.org>
parents: 365
diff changeset
502 if (len <= strlen(curr->name) && th_strncasecmp(curr->name, pattern, len) == 0)
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
503 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
504 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
505 }
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
506 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
507 }
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
508
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
509
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
510 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
511 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
512 uint8_t hash;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
513
108
f323b137ca08 Tab-completion now works with cycling, but bugs with names that have whitespaces.
Matti Hamalainen <ccr@tnsp.org>
parents: 106
diff changeset
514 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
515
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
516 hash = nn_hash_user(pattern);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
517 if (list->buckets[hash] != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
518 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
519 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
520 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
521
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
522 if (current != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
523 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
524 nn_user_t *found = NULL;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
525 while (curr != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
526 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
527 if (th_strcasecmp(curr->name, current) == 0)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
528 {
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
529 if (again)
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
530 return curr;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
531 found = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
532 break;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
533 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
534 curr = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
535 }
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
536
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
537 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
538 return found;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
539 }
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
540
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
541 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
542 return curr;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
543 }
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
544
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
545 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
546 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
547
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
548
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
549 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
550 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
551 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
552 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
553
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
554
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
555 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
556 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
557 uint8_t hash;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
558 nn_user_t *user;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
559
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
560 /* Check arguments */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
561 if (list == NULL || name == NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
562 return -1;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
563
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
564 /* 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
565 if (nn_user_find(list, name) != NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
566 return 1;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
567
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
568 /* No, we'll add it */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
569 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
570 return -3;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
571
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
572 user->name = th_strdup(name);
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
573 if (user->name == NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
574 return -4;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
575
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
576 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
577 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
578
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
579 return 0;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
580 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
581
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
582
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
583 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
584 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
585 uint8_t hash;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
586
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
587 /* Check arguments */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
588 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
589 return -1;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
590
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
591 /* 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
592 hash = nn_hash_user(name);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
593 if (list->buckets[hash] != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
594 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
595 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
596 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
597 prev = NULL;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
598 while (curr != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
599 {
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
600 if (th_strcasecmp(curr->name, name) == 0)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
601 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
602 if (prev)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
603 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
604 else
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
605 list->buckets[hash] = curr->next;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
606
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
607 nn_user_free(curr);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
608
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
609 return 0;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
610 }
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
611 else
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
612 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
613 prev = curr;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
614 curr = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
615 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
616 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
617 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
618
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
619 return 1;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
620 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
621
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
622
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
623 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
624 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
625 nn_user_t *user;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
626
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
627 if (src == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
628
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
629 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
630 return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
631
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
632 /* Copy relevant data */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
633 user->name = th_strdup(src->name);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
634 user->lastspoke = src->lastspoke;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
635 user->joined = src->joined;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
636
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
637 return user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
638 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
639
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
640
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
641 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
642 {
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
643 th_free(user->name);
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
644 th_free(user);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
645 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
646
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
647
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
648 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
649 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
650 nn_user_t *next, *curr = list;
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
651
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
652 while (curr != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
653 {
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
654 next = curr->next;
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
655 nn_user_free(curr);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
656 curr = next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
657 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
658 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
659
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
660 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
661 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
662 int i;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
663 if (hash == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
664 return;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
665
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
666 for (i = 0; i < NN_NUM_BUCKETS; i++)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
667 {
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
668 nn_user_free_list(hash->buckets[i]);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
669 hash->buckets[i] = NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
670 }
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
671
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
672 th_free(hash);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
673 }
285
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
674
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
675
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
676 char *nn_username_encode(char *str)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
677 {
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
678 unsigned char *c = (unsigned char *) str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
679 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
680 for (; *c ; c++)
285
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
681 if (*c == ' ') *c = 255;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
682 return str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
683 }
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
684
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
685
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
686 char *nn_username_decode(char *str)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
687 {
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
688 unsigned char *c = (unsigned char *) str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
689 if (str == NULL) return NULL;
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
690 for (; *c ; c++)
285
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
691 if (*c == 255) *c = ' ';
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
692 return str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
693 }
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
694
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
695
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
696 nn_window_t *nn_window_new(const char *id)
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
697 {
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
698 nn_window_t *res = th_calloc(1, sizeof(nn_window_t));
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
699
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
700 if (res == NULL) return NULL;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
701
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
702 res->data = th_ringbuf_new(NN_BACKBUF_LEN, th_free);
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
703 if (res->data == NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
704 {
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
705 th_free(res);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
706 return NULL;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
707 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
708
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
709 res->id = th_strdup(id);
390
acea18a741e3 Cleanup libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 386
diff changeset
710
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
711 return res;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
712 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
713
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
714
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
715 void nn_window_free(nn_window_t *win)
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
716 {
386
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
717 if (win != NULL)
4523fc0941e8 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 374
diff changeset
718 {
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
719 th_ringbuf_free(win->data);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
720 th_free(win->id);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
721 th_free(win);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
722 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
723 }
330
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
724
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
725
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
726 nn_strtuple_t *nn_strtuple_new(size_t len, char *str)
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
727 {
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
728 nn_strtuple_t *tuple = th_calloc(1, sizeof(nn_strtuple_t));
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
729 tuple->len = len;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
730 tuple->str = str;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
731 return tuple;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
732 }
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
733
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
734
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
735 void nn_strtuple_free(nn_strtuple_t *tuple)
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
736 {
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
737 th_free(tuple->str);
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
738 th_free(tuple);
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
739 }