annotate util.c @ 630:46fcab5ff44e

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