annotate dlib/dlib.c @ 2104:3e7e5395f0bc

non-ASCII keybindings Alexander Voigt has kindly done some testing, and it seems that this makes bindings to most keys on a German keyboard possible -- except those that need AltGr don't work yet.
author corvid <corvid@lavabit.com>
date Thu, 23 Jun 2011 19:24:11 +0000
parents c478e5a7bed6
children
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: dlib.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 12
diff changeset
4 * Copyright (C) 2006-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 /* Memory allocation, Simple dynamic strings, Lists (simple and sorted),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * and a few utility functions
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 * TODO: vsnprintf() is in C99, maybe a simple replacement if necessary.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include <stdarg.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #include "dlib.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
30 static bool_t dLib_show_msg = TRUE;
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
31
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
32 /* dlib msgs go to stderr to avoid problems with filter dpis */
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
33 #define DLIB_MSG(...) \
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
34 D_STMT_START { \
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
35 if (dLib_show_msg) \
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
36 fprintf(stderr, __VA_ARGS__); \
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
37 } D_STMT_END
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
38
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 *- Memory --------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 void *dMalloc (size_t size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 void *value = malloc (size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 if (value == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 return value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 void *dRealloc (void *mem, size_t size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 void *value = realloc (mem, size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 if (value == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 return value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 void *dMalloc0 (size_t size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 void *value = dMalloc (size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 memset (value, 0, size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 return value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 void dFree (void *mem)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 if (mem)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 free(mem);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 *- strings (char *) ----------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 char *dStrdup(const char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 if (s) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 int len = strlen(s)+1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 char *ns = dNew(char, len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 memcpy(ns, s, len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 return ns;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 char *dStrndup(const char *s, size_t sz)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 if (s) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 char *ns = dNew(char, sz+1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 memcpy(ns, s, sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 ns[sz] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 return ns;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 * Concatenate a NULL-terminated list of strings
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 char *dStrconcat(const char *s1, ...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 va_list args;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 char *s, *ns = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 if (s1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 Dstr *dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 va_start(args, s1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 for (s = (char*)s1; s; s = va_arg(args, char*))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 dStr_append(dstr, s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 va_end(args);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 ns = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 dStr_free(dstr, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 return ns;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 * Remove leading and trailing whitespace
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 char *dStrstrip(char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 char *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 if (s && *s) {
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1105
diff changeset
127 for (p = s; dIsspace(*p); ++p);
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1105
diff changeset
128 for (len = strlen(p); len && dIsspace(p[len-1]); --len);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 if (p > s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 memmove(s, p, len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 s[len] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 return s;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 * Return a new string of length 'len' filled with 'c' characters
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 char *dStrnfill(size_t len, char c)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 char *ret = dNew(char, len+1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 for (ret[len] = 0; len > 0; ret[--len] = c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 * strsep() implementation
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 char *dStrsep(char **orig, const char *delim)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 char *str, *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 if (!(str = *orig))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 p = strpbrk(str, delim);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 if (p) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 *p++ = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 *orig = p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 *orig = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 return str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 * Case insensitive strstr
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 char *dStristr(const char *haystack, const char *needle)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 int i, j;
1247
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
172 char *ret = NULL;
1249
ebeb6294ee46 Added a couple extra sanity checks to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
173
1247
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
174 if (haystack && needle) {
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
175 for (i = 0, j = 0; haystack[i] && needle[j]; ++i)
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
176 if (tolower(haystack[i]) == tolower(needle[j])) {
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
177 ++j;
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
178 } else if (j) {
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
179 i -= j;
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
180 j = 0;
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
181 }
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
182 if (!needle[j]) /* Got all */
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
183 ret = (char *)(haystack + i - j);
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
184 }
d8ad0a9ad8cb dStristr check for NULL args
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
185 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 *- dStr ----------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 * Private allocator
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 static void dStr_resize(Dstr *ds, int n_sz, int keep)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 if (n_sz >= 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 if (keep && n_sz > ds->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 ds->str = (Dstr_char_t*) dRealloc (ds->str, n_sz*sizeof(Dstr_char_t));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 ds->sz = n_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 } else {
226
d0488cd8ac6b - Some s/free/dFree/ in dlib.
jcid
parents: 167
diff changeset
203 dFree(ds->str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 ds->str = dNew(Dstr_char_t, n_sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 ds->sz = n_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 ds->len = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 ds->str[0] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 * Create a new string with a given size.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 * Initialized to ""
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 Dstr *dStr_sized_new (int sz)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 {
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 318
diff changeset
218 Dstr *ds;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 if (sz < 2)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 sz = 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 318
diff changeset
222 ds = dNew(Dstr, 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 ds->str = NULL;
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
224 dStr_resize(ds, sz + 1, 0); /* (sz + 1) for the extra '\0' */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 return ds;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 * Return memory if there's too much allocated
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 void dStr_fit (Dstr *ds)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 dStr_resize(ds, ds->len + 1, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 * Insert a C string, at a given position, into a Dstr (providing length).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 * Note: It also works with embedded nil characters.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 int n_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 if (ds && s && l && pos_0 >= 0 && pos_0 <= ds->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 for (n_sz = ds->sz; ds->len + l >= n_sz; n_sz *= 2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 if (n_sz > ds->sz) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 dStr_resize(ds, n_sz, (ds->len > 0) ? 1 : 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 if (pos_0 < ds->len)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 memmove(ds->str+pos_0+l, ds->str+pos_0, ds->len-pos_0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 memcpy(ds->str+pos_0, s, l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 ds->len += l;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 ds->str[ds->len] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 * Insert a C string, at a given position, into a Dstr
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 void dStr_insert (Dstr *ds, int pos_0, const char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 if (s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 dStr_insert_l(ds, pos_0, s, strlen(s));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 * Append a C string to a Dstr (providing length).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 * Note: It also works with embedded nil characters.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 void dStr_append_l (Dstr *ds, const char *s, int l)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 dStr_insert_l(ds, ds->len, s, l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 * Append a C string to a Dstr.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 void dStr_append (Dstr *ds, const char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 dStr_append_l(ds, s, strlen(s));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284 * Create a new string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 * Initialized to 's' or empty if 's == NULL'
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
287 Dstr *dStr_new (const char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
288 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
289 Dstr *ds = dStr_sized_new(0);
318
ae03b7f50fc7 - Minor optimization in dlib's dStr_new().
jcid
parents: 226
diff changeset
290 if (s && *s)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 dStr_append(ds, s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292 return ds;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296 * Free a dillo string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297 * if 'all' free everything, else free the structure only.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299 void dStr_free (Dstr *ds, int all)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 if (ds) {
226
d0488cd8ac6b - Some s/free/dFree/ in dlib.
jcid
parents: 167
diff changeset
302 if (all)
d0488cd8ac6b - Some s/free/dFree/ in dlib.
jcid
parents: 167
diff changeset
303 dFree(ds->str);
d0488cd8ac6b - Some s/free/dFree/ in dlib.
jcid
parents: 167
diff changeset
304 dFree(ds);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309 * Append one character.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 void dStr_append_c (Dstr *ds, int c)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313 char cs[2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 if (ds) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316 if (ds->sz > ds->len + 1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 ds->str[ds->len++] = (Dstr_char_t)c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 ds->str[ds->len] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 cs[0] = (Dstr_char_t)c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321 cs[1] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 dStr_append_l (ds, cs, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328 * Truncate a Dstr to be 'len' bytes long.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 void dStr_truncate (Dstr *ds, int len)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332 if (ds && len < ds->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 ds->str[len] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 ds->len = len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
336 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 * Erase a substring.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 void dStr_erase (Dstr *ds, int pos_0, int len)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 if (ds && pos_0 >= 0 && len > 0 && pos_0 + len <= ds->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344 memmove(ds->str + pos_0, ds->str + pos_0 + len, ds->len - pos_0 - len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345 ds->len -= len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 ds->str[ds->len] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 * vsprintf-like function that appends.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 * Used by: dStr_vsprintf(), dStr_sprintf() and dStr_sprintfa().
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354 void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
356 int n, n_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358 if (ds && format) {
4
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
359 va_list argp2; /* Needed in case of looping on non-32bit arch */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360 while (1) {
4
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
361 va_copy(argp2, argp);
12
55c4fdb2b9d6 Fix a typing bug in patch...
jcid
parents: 4
diff changeset
362 n = vsnprintf(ds->str + ds->len, ds->sz - ds->len, format, argp2);
4
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
363 va_end(argp2);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 if (n > -1 && n < ds->sz - ds->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365 ds->len += n; /* Success! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 } else if (n > -1) { /* glibc >= 2.1 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 n_sz = ds->len + n + 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 } else { /* old glibc */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 n_sz = ds->sz * 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 dStr_resize(ds, n_sz, (ds->len > 0) ? 1 : 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378 * vsprintf-like function.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 void dStr_vsprintf (Dstr *ds, const char *format, va_list argp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382 if (ds) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 dStr_truncate(ds, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384 dStr_vsprintfa(ds, format, argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389 * Printf-like function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
391 void dStr_sprintf (Dstr *ds, const char *format, ...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 va_list argp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395 if (ds && format) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 va_start(argp, format);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 dStr_vsprintf(ds, format, argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 va_end(argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
403 * Printf-like function that appends.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
404 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405 void dStr_sprintfa (Dstr *ds, const char *format, ...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
407 va_list argp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
408
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
409 if (ds && format) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410 va_start(argp, format);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 dStr_vsprintfa(ds, format, argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
412 va_end(argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
413 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
414 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
415
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
416 /*
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
417 * Compare two dStrs.
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
418 */
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
419 int dStr_cmp(Dstr *ds1, Dstr *ds2)
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
420 {
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
421 int ret = 0;
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
422
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
423 if (ds1 && ds2)
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
424 ret = memcmp(ds1->str, ds2->str, MIN(ds1->len+1, ds2->len+1));
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
425 return ret;
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
426 }
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
427
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
428 /*
163
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
429 * Return a pointer to the first occurrence of needle in haystack.
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
430 */
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
431 char *dStr_memmem(Dstr *haystack, Dstr *needle)
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
432 {
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
433 int i;
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
434
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
435 if (needle && haystack) {
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
436 if (needle->len == 0)
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
437 return haystack->str;
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
438
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
439 for (i = 0; i <= (haystack->len - needle->len); i++) {
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
440 if (haystack->str[i] == needle->str[0] &&
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
441 !memcmp(haystack->str + i, needle->str, needle->len))
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
442 return haystack->str + i;
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
443 }
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
444 }
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
445 return NULL;
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
446 }
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
447
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 157
diff changeset
448 /*
167
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
449 * Return a printable representation of the provided Dstr, limited to a length
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
450 * of roughly maxlen.
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
451 *
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
452 * This is NOT threadsafe.
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
453 */
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
454 const char *dStr_printable(Dstr *in, int maxlen)
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
455 {
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
456 int i;
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
457 static const char *const HEX = "0123456789ABCDEF";
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
458 static Dstr *out = NULL;
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
459
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
460 if (in == NULL)
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
461 return NULL;
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
462
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
463 if (out)
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
464 dStr_truncate(out, 0);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
465 else
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
466 out = dStr_sized_new(in->len);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
467
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
468 for (i = 0; (i < in->len) && (out->len < maxlen); ++i) {
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
469 if (isprint(in->str[i]) || (in->str[i] == '\n')) {
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
470 dStr_append_c(out, in->str[i]);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
471 } else {
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
472 dStr_append_l(out, "\\x", 2);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
473 dStr_append_c(out, HEX[(in->str[i] >> 4) & 15]);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
474 dStr_append_c(out, HEX[in->str[i] & 15]);
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
475 }
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
476 }
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
477 if (out->len >= maxlen)
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
478 dStr_append(out, "...");
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
479 return out->str;
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
480 }
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
481
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
482 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
483 *- dList ---------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
484 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
485
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
486 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
487 * Create a new empty list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
488 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
489 Dlist *dList_new(int size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
490 {
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 318
diff changeset
491 Dlist *l;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
492 if (size <= 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
493 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
494
430
b296fd9d8752 - Allowed compilation with older machines by removing a few C99isms.
jcid
parents: 318
diff changeset
495 l = dNew(Dlist, 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
496 l->len = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
497 l->sz = size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
498 l->list = dNew(void*, l->sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
499 return l;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
500 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
501
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
502 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
503 * Free a list (not its elements)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
504 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
505 void dList_free (Dlist *lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
506 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
507 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
508 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
509
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
510 dFree(lp->list);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
511 dFree(lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
512 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
513
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
514 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
515 * Insert an element at a given position [0 based]
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
516 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
517 void dList_insert_pos (Dlist *lp, void *data, int pos0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
518 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
519 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
520
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
521 if (!lp || pos0 < 0 || pos0 > lp->len)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
522 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
523
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
524 if (lp->sz == lp->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
525 lp->sz *= 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
526 lp->list = (void**) dRealloc (lp->list, lp->sz*sizeof(void*));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
527 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
528 ++lp->len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
529
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
530 for (i = lp->len - 1; i > pos0; --i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
531 lp->list[i] = lp->list[i - 1];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
532 lp->list[pos0] = data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
533 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
534
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
535 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
536 * Append a data item to the list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
537 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
538 void dList_append (Dlist *lp, void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
539 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
540 dList_insert_pos(lp, data, lp->len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
541 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
542
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
543 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
544 * Prepend a data item to the list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
545 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
546 void dList_prepend (Dlist *lp, void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
547 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
548 dList_insert_pos(lp, data, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
549 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
550
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
551 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
552 * For completing the ADT.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
553 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
554 int dList_length (Dlist *lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
555 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
556 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
557 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
558 return lp->len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
559 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
560
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
561 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
562 * Remove a data item without preserving order.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
563 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
564 void dList_remove_fast (Dlist *lp, const void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
565 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
566 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
567
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
568 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
569 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
570
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
571 for (i = 0; i < lp->len; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
572 if (lp->list[i] == data) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
573 lp->list[i] = lp->list[--lp->len];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
574 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
575 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
576 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
577 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
578
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
579 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
580 * Remove a data item preserving order.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
581 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
582 void dList_remove (Dlist *lp, const void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
583 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
584 int i, j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
585
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
586 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
587 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
588
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
589 for (i = 0; i < lp->len; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
590 if (lp->list[i] == data) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
591 --lp->len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
592 for (j = i; j < lp->len; ++j)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
593 lp->list[j] = lp->list[j + 1];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
594 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
595 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
596 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
597 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
598
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
599 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
600 * Return the nth data item,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
601 * NULL when not found or 'n0' is out of range
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
602 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
603 void *dList_nth_data (Dlist *lp, int n0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
604 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
605 if (!lp || n0 < 0 || n0 >= lp->len)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
606 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
607 return lp->list[n0];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
608 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
609
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
610 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
611 * Return the found data item, or NULL if not present.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
612 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
613 void *dList_find (Dlist *lp, const void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
614 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
615 int i = dList_find_idx(lp, data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
616 return (i >= 0) ? lp->list[i] : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
617 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
618
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
619 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
620 * Search a data item.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
621 * Return value: its index if found, -1 if not present.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
622 * (this is useful for a list of integers, for finding number zero).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
623 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
624 int dList_find_idx (Dlist *lp, const void *data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
625 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
626 int i, ret = -1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
627
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
628 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
629 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
630
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
631 for (i = 0; i < lp->len; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
632 if (lp->list[i] == data) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
633 ret = i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
634 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
635 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
636 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
637 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
638 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
639
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
640 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
641 * Search a data item using a custom function.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
642 * func() is given the list item and the user data as parameters.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
643 * Return: data item when found, NULL otherwise.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
644 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
645 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
646 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
647 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
648 void *ret = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
649
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
650 if (!lp)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
651 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
652
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
653 for (i = 0; i < lp->len; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
654 if (func(lp->list[i], data) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
655 ret = lp->list[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
656 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
657 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
658 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
659 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
660 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
661
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
662 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
663 * QuickSort implementation.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
664 * This allows for a simple compare function for all the ADT.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
665 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
666 static void QuickSort(void **left, void **right, dCompareFunc compare)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
667 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
668 void **p = left, **q = right, **t = left;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
669
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
670 while (1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
671 while (p != t && compare(*p, *t) < 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
672 ++p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
673 while (q != t && compare(*q, *t) > 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
674 --q;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
675 if (p > q)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
676 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
677 if (p < q) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
678 void *tmp = *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
679 *p = *q;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
680 *q = tmp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
681 if (t == p)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
682 t = q;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
683 else if (t == q)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
684 t = p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
685 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
686 if (++p > --q)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
687 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
688 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
689
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
690 if (left < q)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
691 QuickSort(left, q, compare);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
692 if (p < right)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
693 QuickSort(p, right, compare);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
694 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
695
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
696 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
697 * Sort the list using a custom function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
698 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
699 void dList_sort (Dlist *lp, dCompareFunc func)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
700 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
701 if (lp && lp->len > 1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
702 QuickSort(lp->list, lp->list + lp->len - 1, func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
703 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
704 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
705
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
706 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
707 * Insert an element into a sorted list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
708 * The comparison function receives two list elements.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
709 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
710 void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
711 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
712 int i, st, min, max;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
713
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
714 if (lp) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
715 min = st = i = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
716 max = lp->len - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
717 while (min <= max) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
718 i = (min + max) / 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
719 st = func(lp->list[i], data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
720 if (st < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
721 min = i + 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
722 } else if (st > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
723 max = i - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
724 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
725 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
726 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
727 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
728 dList_insert_pos(lp, data, (st >= 0) ? i : i+1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
729 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
730 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
731
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
732 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
733 * Search a sorted list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
734 * Return the found data item, or NULL if not present.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
735 * func() is given the list item and the user data as parameters.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
736 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
737 void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
738 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
739 int i, st, min, max;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
740 void *ret = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
741
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
742 if (lp && lp->len) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
743 min = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
744 max = lp->len - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
745 while (min <= max) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
746 i = (min + max) / 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
747 st = func(lp->list[i], data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
748 if (st < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
749 min = i + 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
750 } else if (st > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
751 max = i - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
752 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
753 ret = lp->list[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
754 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
755 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
756 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
757 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
758
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
759 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
760 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
761
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
762 /*
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
763 *- Parse function ------------------------------------------------------------
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
764 */
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
765
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
766 /*
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
767 * Take a dillo rc line and return 'name' and 'value' pointers to it.
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
768 * Notes:
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
769 * - line is modified!
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
770 * - it skips blank lines and lines starting with '#'
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
771 *
1053
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
772 * Return value: 1 on blank line or comment, 0 on successful value/pair,
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
773 * -1 otherwise.
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
774 */
1053
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
775 int dParser_parse_rc_line(char **line, char **name, char **value)
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
776 {
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
777 char *eq, *p;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
778 int len, ret = -1;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
779
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
780 dReturn_val_if_fail(*line, ret);
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
781
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
782 *name = NULL;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
783 *value = NULL;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
784 dStrstrip(*line);
1053
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
785 if (!*line[0] || *line[0] == '#') {
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
786 /* blank line or comment */
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
787 ret = 1;
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 928
diff changeset
788 } else if ((eq = strchr(*line, '='))) {
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
789 /* get name */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1105
diff changeset
790 for (p = *line; *p && *p != '=' && !dIsspace(*p); ++p);
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
791 *p = 0;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
792 *name = *line;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
793
452
6d57529fdeec - Allowed the rc parser to skip whitespace around the equal sign.
jcid
parents: 430
diff changeset
794 /* skip whitespace */
6d57529fdeec - Allowed the rc parser to skip whitespace around the equal sign.
jcid
parents: 430
diff changeset
795 if (p < eq)
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1105
diff changeset
796 for (++p; dIsspace(*p); ++p);
452
6d57529fdeec - Allowed the rc parser to skip whitespace around the equal sign.
jcid
parents: 430
diff changeset
797
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
798 /* get value */
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
799 if (p == eq) {
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1105
diff changeset
800 for (++p; dIsspace(*p); ++p);
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
801 len = strlen(p);
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
802 if (len >= 2 && *p == '"' && p[len-1] == '"') {
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
803 p[len-1] = 0;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
804 ++p;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
805 }
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
806 *value = p;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
807 ret = 0;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
808 }
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
809 }
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
810
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
811 return ret;
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
812 }
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
813
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 35
diff changeset
814 /*
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
815 *- Dlib messages -------------------------------------------------------------
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
816 */
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
817 void dLib_show_messages(bool_t show)
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
818 {
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
819 dLib_show_msg = show;
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
820 }
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
821
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
822 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
823 *- Misc utility functions ----------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
824 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
825
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
826 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
827 * Return the current working directory in a new string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
828 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
829 char *dGetcwd ()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
830 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
831 size_t size = 128;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
832
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
833 while (1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
834 char *buffer = dNew(char, size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
835 if (getcwd (buffer, size) == buffer)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
836 return buffer;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
837 dFree (buffer);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
838 if (errno != ERANGE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
839 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
840 size *= 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
841 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
842 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
843
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
844 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
845 * Return the home directory in a static string (don't free)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
846 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
847 char *dGethomedir ()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
848 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
849 static char *homedir = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
850
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
851 if (!homedir) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
852 if (getenv("HOME")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
853 homedir = dStrdup(getenv("HOME"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
854
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
855 } else if (getenv("HOMEDRIVE") && getenv("HOMEPATH")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
856 homedir = dStrconcat(getenv("HOMEDRIVE"), getenv("HOMEPATH"), NULL);
1105
8152083008f4 unset HOME defaults to /
corvid <corvid@lavabit.com>
parents: 1104
diff changeset
857 } else {
8152083008f4 unset HOME defaults to /
corvid <corvid@lavabit.com>
parents: 1104
diff changeset
858 DLIB_MSG("dGethomedir: $HOME not set, using '/'.\n");
8152083008f4 unset HOME defaults to /
corvid <corvid@lavabit.com>
parents: 1104
diff changeset
859 homedir = dStrdup("/");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
860 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
861 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
862 return homedir;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
863 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
864
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
865 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
866 * Get a line from a FILE stream.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
867 * It handles backslash as line-continues character.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
868 * Return value: read line on success, NULL on EOF.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
869 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
870 char *dGetline (FILE *stream)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
871 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
872 int ch;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
873 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
874 char *line;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
875
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
876 dReturn_val_if_fail (stream, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
877
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
878 dstr = dStr_sized_new(64);
928
7771cf243ca6 's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents: 452
diff changeset
879 while ((ch = fgetc(stream)) != EOF) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
880 if (ch == '\\') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
881 /* continue with the next line */
928
7771cf243ca6 's/if(/if (/g' 's/for(/for (/g' 's/while(/while (/g', and indentation.
Jorge Arellano Cid <jcid@dillo.org>
parents: 452
diff changeset
882 while ((ch = fgetc(stream)) != EOF && ch != '\n') ;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
883 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
884 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
885 dStr_append_c(dstr, ch);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
886 if (ch == '\n')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
887 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
888 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
889
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
890 line = (dstr->len) ? dstr->str : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
891 dStr_free(dstr, (line) ? 0 : 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
892 return line;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
893 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
894