1 |
pippijn |
1.7 |
/** |
2 |
|
|
* string.C: String functions. |
3 |
|
|
* |
4 |
|
|
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
|
|
* Rights to this code are as documented in COPYING. |
6 |
|
|
* |
7 |
|
|
* |
8 |
|
|
* Portions of this file were derived from sources bearing the following license: |
9 |
pippijn |
1.1 |
* Copyright © 2005 Atheme Development Group |
10 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
11 |
pippijn |
1.1 |
*/ |
12 |
|
|
|
13 |
pippijn |
1.7 |
static char const rcsid[] = "$Id: string.C,v 1.6 2007-09-05 11:23:15 pippijn Exp $"; |
14 |
pippijn |
1.1 |
|
15 |
pippijn |
1.4 |
#include "atheme.h" |
16 |
pippijn |
1.6 |
#include <common/string.h> |
17 |
pippijn |
1.4 |
|
18 |
|
|
char * |
19 |
|
|
sstrdup (char const * const s) |
20 |
|
|
{ |
21 |
|
|
char *t; |
22 |
|
|
size_t len; |
23 |
|
|
|
24 |
|
|
if (s == NULL) |
25 |
|
|
return NULL; |
26 |
|
|
|
27 |
pippijn |
1.5 |
t = salloc<char> (len = strlen (s) + 1); |
28 |
pippijn |
1.4 |
|
29 |
|
|
strlcpy (t, s, len); |
30 |
|
|
return t; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
char * |
34 |
|
|
sstrndup (char const *s, int len) |
35 |
|
|
{ |
36 |
|
|
char *t; |
37 |
|
|
|
38 |
|
|
if (s == NULL) |
39 |
|
|
return NULL; |
40 |
pippijn |
1.1 |
|
41 |
pippijn |
1.5 |
t = salloc<char> (len + 1); |
42 |
pippijn |
1.4 |
|
43 |
|
|
strlcpy (t, s, len); |
44 |
|
|
return t; |
45 |
|
|
} |
46 |
pippijn |
1.1 |
|
47 |
|
|
void |
48 |
|
|
assign (char *dst, char const *src, size_t maxlen) |
49 |
|
|
{ |
50 |
|
|
if (!src) |
51 |
|
|
src = ""; |
52 |
|
|
|
53 |
|
|
size_t len = strlen (src); |
54 |
|
|
|
55 |
|
|
if (len >= maxlen - 1) |
56 |
|
|
{ |
57 |
|
|
if (maxlen <= 4) |
58 |
|
|
{ |
59 |
|
|
memset (dst, '.', maxlen - 1); |
60 |
|
|
dst [maxlen - 1] = 0; |
61 |
|
|
} |
62 |
|
|
else |
63 |
|
|
{ |
64 |
|
|
memcpy (dst, src, maxlen - 4); |
65 |
|
|
memcpy (dst + maxlen - 4, "...", 4); |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
else |
69 |
|
|
memcpy (dst, src, len + 1); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
#ifndef HAVE_STRLCAT |
73 |
|
|
/* These functions are taken from Linux. */ |
74 |
|
|
size_t |
75 |
|
|
strlcat (char *dest, char const *src, size_t count) |
76 |
|
|
{ |
77 |
|
|
size_t dsize = strlen (dest); |
78 |
|
|
size_t len = strlen (src); |
79 |
|
|
size_t res = dsize + len; |
80 |
|
|
|
81 |
|
|
dest += dsize; |
82 |
|
|
count -= dsize; |
83 |
|
|
if (len >= count) |
84 |
|
|
len = count - 1; |
85 |
|
|
memcpy (dest, src, len); |
86 |
|
|
dest[len] = 0; |
87 |
|
|
return res; |
88 |
|
|
} |
89 |
|
|
#endif |
90 |
|
|
|
91 |
|
|
#ifndef HAVE_STRLCPY |
92 |
|
|
size_t |
93 |
|
|
strlcpy (char *dest, char const *src, size_t size) |
94 |
|
|
{ |
95 |
|
|
size_t ret = strlen (src); |
96 |
|
|
|
97 |
|
|
if (size) |
98 |
|
|
{ |
99 |
|
|
size_t len = (ret >= size) ? size - 1 : ret; |
100 |
|
|
memcpy (dest, src, len); |
101 |
|
|
dest[len] = '\0'; |
102 |
|
|
} |
103 |
|
|
return ret; |
104 |
|
|
} |
105 |
|
|
#endif |
106 |
|
|
|
107 |
|
|
/* removes unwanted chars from a line */ |
108 |
|
|
char const * |
109 |
|
|
strip (char *line) |
110 |
|
|
{ |
111 |
|
|
char *c = 0; |
112 |
|
|
|
113 |
|
|
if (line) |
114 |
|
|
{ |
115 |
|
|
if ((c = strchr (line, '\n'))) |
116 |
|
|
*c = '\0'; |
117 |
|
|
if ((c = strchr (line, '\r'))) |
118 |
|
|
*c = '\0'; |
119 |
|
|
if ((c = strchr (line, '\1'))) |
120 |
|
|
*c = '\0'; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
return c; |
124 |
|
|
} |
125 |
pippijn |
1.6 |
|
126 |
|
|
int |
127 |
|
|
dynstr::add (char c) |
128 |
|
|
{ |
129 |
|
|
if (length - pos <= 1) |
130 |
|
|
{ |
131 |
|
|
size_t new_size = std::max (length * 2, pos + 9); |
132 |
|
|
length = new_size; |
133 |
|
|
m_rep = new char[new_size]; |
134 |
|
|
} |
135 |
|
|
m_rep[pos] = c; |
136 |
|
|
m_rep[++pos] = '\0'; |
137 |
|
|
|
138 |
|
|
return length; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
int |
142 |
|
|
dynstr::add (char const *src, size_t len) |
143 |
|
|
{ |
144 |
|
|
if (length - pos <= len) |
145 |
|
|
{ |
146 |
|
|
size_t new_size = std::max (length * 2, pos + len + 8); |
147 |
|
|
length = new_size; |
148 |
|
|
m_rep = new char[new_size]; |
149 |
|
|
} |
150 |
|
|
memcpy (m_rep + pos, src, len); |
151 |
|
|
pos += len; |
152 |
|
|
m_rep[pos] = '\0'; |
153 |
|
|
|
154 |
|
|
return len; |
155 |
|
|
} |