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