3 |
STRING *string_new(void) |
STRING *string_new(void) |
4 |
{ |
{ |
5 |
STRING *ns = mallocwrapper(sizeof(STRING) + MINLEN + 1); |
STRING *ns = mallocwrapper(sizeof(STRING) + MINLEN + 1); |
6 |
|
|
7 |
STRLENGTH(ns) = 0; |
STRLENGTH(ns) = 0; |
8 |
STRTOCHARN(ns, 0) = 0; |
STRTOCHARN(ns, 0) = 0; |
9 |
return(ns); |
return (ns); |
10 |
} |
} |
11 |
|
|
12 |
void string_cat(STRING **s, char *catstr, int len) |
void string_cat(STRING ** s, |
13 |
|
char *catstr, |
14 |
|
int len) |
15 |
{ |
{ |
16 |
if (len == -1) |
if (len == -1) |
17 |
len = strlen(catstr); |
len = strlen(catstr); |
18 |
|
|
19 |
if (len + STRLENGTH(*s) >= MINLEN) |
if (len + STRLENGTH(*s) >= MINLEN) |
20 |
reallocwrapper(len + STRCURSIZE(*s), (void *)s); |
reallocwrapper(len + STRCURSIZE(*s), (void *) s); |
21 |
|
|
22 |
memcpy(STRTOCHAR(*s) + STRLENGTH(*s), catstr, len); |
memcpy(STRTOCHAR(*s) + STRLENGTH(*s), catstr, len); |
23 |
STRLENGTH(*s) += len; |
STRLENGTH(*s) += len; |
24 |
STRTOCHARN(*s, (*s)->strlen) = 0; |
STRTOCHARN(*s, (*s)->strlen) = 0; |
25 |
} |
} |
26 |
|
|
27 |
void string_clear(STRING **s) |
void string_clear(STRING ** s) |
28 |
{ |
{ |
29 |
if (STRLENGTH(*s) > MINLEN) |
if (STRLENGTH(*s) > MINLEN) |
30 |
reallocwrapper(sizeof(STRING) + MINLEN + 1, (void *)s); |
reallocwrapper(sizeof(STRING) + MINLEN + 1, (void *) s); |
31 |
|
|
32 |
STRLENGTH(*s) = 0; |
STRLENGTH(*s) = 0; |
33 |
STRTOCHARN(*s, 0) = 0; |
STRTOCHARN(*s, 0) = 0; |
34 |
} |
} |
35 |
|
|
36 |
void string_dropfront(STRING **s, int nchars) |
void string_dropfront(STRING ** s, |
37 |
|
int nchars) |
38 |
{ |
{ |
39 |
int oldlen = STRLENGTH(*s); |
int oldlen = STRLENGTH(*s); |
40 |
|
|
41 |
if ((*s)->strlen < nchars) |
if ((*s)->strlen < nchars) |
42 |
nchars = (*s)->strlen; |
nchars = (*s)->strlen; |
43 |
|
|
44 |
memmove(STRTOCHAR(*s), STRTOCHAR(*s) + nchars, STRLENGTH(*s) - nchars); |
memmove(STRTOCHAR(*s), STRTOCHAR(*s) + nchars, STRLENGTH(*s) - nchars); |
45 |
STRLENGTH(*s) -= nchars; |
STRLENGTH(*s) -= nchars; |
46 |
STRTOCHARN(*s, STRLENGTH(*s)) = 0; |
STRTOCHARN(*s, STRLENGTH(*s)) = 0; |
47 |
|
|
48 |
if (oldlen >= MINLEN) |
if (oldlen >= MINLEN) |
49 |
{ |
{ |
50 |
if (STRLENGTH(*s) >= MINLEN) |
if (STRLENGTH(*s) >= MINLEN) |
51 |
reallocwrapper(sizeof(STRING) + STRLENGTH(*s) + 1, (void *)s); |
reallocwrapper(sizeof(STRING) + STRLENGTH(*s) + 1, (void *) s); |
52 |
else |
else |
53 |
reallocwrapper(sizeof(STRING) + MINLEN + 1, (void *)s); |
reallocwrapper(sizeof(STRING) + MINLEN + 1, (void *) s); |
54 |
} |
} |
55 |
} |
} |
56 |
|
|
57 |
void string_catvprintf(STRING **s, char *format, va_list ap) |
void string_catvprintf(STRING ** s, |
58 |
|
char *format, |
59 |
|
va_list ap) |
60 |
{ |
{ |
61 |
int len; |
int len; |
62 |
|
|
63 |
len = vsnprintf(NULL, 0, format, ap); |
len = vsnprintf(NULL, 0, format, ap); |
64 |
|
|
65 |
if (STRLENGTH(*s) + len >= MINLEN) |
if (STRLENGTH(*s) + len >= MINLEN) |
66 |
reallocwrapper(len + STRCURSIZE(*s), (void *)s); |
reallocwrapper(len + STRCURSIZE(*s), (void *) s); |
67 |
|
|
68 |
vsnprintf(STRTOCHAR(*s) + STRLENGTH(*s), len+1, format, ap); |
vsnprintf(STRTOCHAR(*s) + STRLENGTH(*s), len + 1, format, ap); |
69 |
|
|
70 |
STRLENGTH(*s) += len; |
STRLENGTH(*s) += len; |
71 |
} |
} |
72 |
|
|
73 |
void string_catprintf(STRING **s, char *format, ...) |
void string_catprintf(STRING ** s, |
74 |
|
char *format, |
75 |
|
...) |
76 |
{ |
{ |
77 |
va_list printfargs; |
va_list printfargs; |
78 |
|
|
79 |
va_start(printfargs, format); |
va_start(printfargs, format); |
80 |
string_catvprintf(s, format, printfargs); |
string_catvprintf(s, format, printfargs); |
81 |
va_end(printfargs); |
va_end(printfargs); |
82 |
} |
} |
83 |
|
|
84 |
void string_filterbadchars(STRING **s, int start) |
void string_filterbadchars(STRING ** s, |
85 |
|
int start) |
86 |
{ |
{ |
87 |
int count, slen; |
int count, slen; |
88 |
unsigned char *pos1, *pos2; |
unsigned char *pos1, *pos2; |
89 |
|
|
90 |
if (start > STRLENGTH(*s)) |
if (start > STRLENGTH(*s)) |
91 |
return; |
return; |
92 |
|
|
93 |
pos1 = pos2 = STRTOCHAR(*s) + start; |
pos1 = pos2 = STRTOCHAR(*s) + start; |
94 |
slen = STRLENGTH(*s); |
slen = STRLENGTH(*s); |
95 |
for(count = start; count < slen; count++) |
for (count = start; count < slen; count++) |
96 |
{ |
{ |
97 |
/* be very aggressive. Only printable charaters! */ |
/* |
98 |
|
* be very aggressive. Only printable charaters! |
99 |
|
*/ |
100 |
if ((*pos1 >= 32) && (*pos1 <= 126)) |
if ((*pos1 >= 32) && (*pos1 <= 126)) |
101 |
{ |
{ |
102 |
*pos2 = *pos1; |
*pos2 = *pos1; |
104 |
} |
} |
105 |
else |
else |
106 |
{ |
{ |
107 |
switch(*pos1) |
switch (*pos1) |
108 |
{ |
{ |
109 |
case '\n': |
case '\n': |
110 |
case '\r': |
case '\r': |
111 |
case '\t': |
case '\t': |
112 |
*pos2 = *pos1; |
*pos2 = *pos1; |
113 |
pos2++; |
pos2++; |
114 |
break; |
break; |
115 |
default: |
default: |
116 |
STRLENGTH(*s) -= 1; |
STRLENGTH(*s) -= 1; |
117 |
break; |
break; |
118 |
} |
} |
119 |
} |
} |
120 |
pos1++; |
pos1++; |