ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/string.C
Revision: 1.4
Committed: Tue Aug 28 17:08:12 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +56 -29 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# Content
1 /*
2 * Copyright © 2005 Atheme Development Group
3 * Rights to this code are documented in doc/pod/license.pod.
4 *
5 * String functions.
6 */
7
8 static char const rcsid[] = "$Id: string.C,v 1.3 2007-07-21 13:23:22 pippijn Exp $";
9
10 #include "atheme.h"
11
12 char *
13 sstrdup (char const * const s)
14 {
15 char *t;
16 size_t len;
17
18 if (s == NULL)
19 return NULL;
20
21 t = alloc<char> (len = strlen (s) + 1);
22
23 strlcpy (t, s, len);
24 return t;
25 }
26
27 char *
28 sstrndup (char const *s, int len)
29 {
30 char *t;
31
32 if (s == NULL)
33 return NULL;
34
35 t = alloc<char> (len + 1);
36
37 strlcpy (t, s, len);
38 return t;
39 }
40
41 void
42 assign (char *dst, char const *src, size_t maxlen)
43 {
44 if (!src)
45 src = "";
46
47 size_t len = strlen (src);
48
49 if (len >= maxlen - 1)
50 {
51 if (maxlen <= 4)
52 {
53 memset (dst, '.', maxlen - 1);
54 dst [maxlen - 1] = 0;
55 }
56 else
57 {
58 memcpy (dst, src, maxlen - 4);
59 memcpy (dst + maxlen - 4, "...", 4);
60 }
61 }
62 else
63 memcpy (dst, src, len + 1);
64 }
65
66 #ifndef HAVE_STRLCAT
67 /* These functions are taken from Linux. */
68 size_t
69 strlcat (char *dest, char const *src, size_t count)
70 {
71 size_t dsize = strlen (dest);
72 size_t len = strlen (src);
73 size_t res = dsize + len;
74
75 dest += dsize;
76 count -= dsize;
77 if (len >= count)
78 len = count - 1;
79 memcpy (dest, src, len);
80 dest[len] = 0;
81 return res;
82 }
83 #endif
84
85 #ifndef HAVE_STRLCPY
86 size_t
87 strlcpy (char *dest, char const *src, size_t size)
88 {
89 size_t ret = strlen (src);
90
91 if (size)
92 {
93 size_t len = (ret >= size) ? size - 1 : ret;
94 memcpy (dest, src, len);
95 dest[len] = '\0';
96 }
97 return ret;
98 }
99 #endif
100
101 /* removes unwanted chars from a line */
102 char const *
103 strip (char *line)
104 {
105 char *c = 0;
106
107 if (line)
108 {
109 if ((c = strchr (line, '\n')))
110 *c = '\0';
111 if ((c = strchr (line, '\r')))
112 *c = '\0';
113 if ((c = strchr (line, '\1')))
114 *c = '\0';
115 }
116
117 return c;
118 }
119
120 int
121 dynstr::append (char c)
122 {
123 if (size - pos <= 1)
124 {
125 size_t new_size = std::max (size * 2, pos + 9);
126 size = new_size;
127 m_rep = new char[new_size];
128 }
129 m_rep[pos] = c;
130 m_rep[++pos] = '\0';
131
132 return size;
133 }
134
135 int
136 dynstr::append_nonul (char c)
137 {
138 if (size - pos <= 1)
139 {
140 size_t new_size = std::max (size * 2, pos + 9);
141 size = new_size;
142 m_rep = new char[new_size];
143 }
144 m_rep[pos++] = c;
145
146 return size;
147 }
148
149 int
150 dynstr::append (char const *src, size_t len)
151 {
152 if (size - pos <= len)
153 {
154 size_t new_size = std::max (size * 2, pos + len + 8);
155 size = new_size;
156 m_rep = new char[new_size];
157 }
158 memcpy (m_rep + pos, src, len);
159 pos += len;
160 m_rep[pos] = '\0';
161
162 return len;
163 }
164
165 int
166 dynstr::append_nonul (char const *src, size_t len)
167 {
168 if (size - pos <= len)
169 {
170 size_t new_size = std::max (size * 2, pos + len + 8);
171 size = new_size;
172 m_rep = new char[new_size];
173 }
174 memcpy (m_rep + pos, src, len);
175 pos += len;
176
177 return len;
178 }
179
180 int
181 dynstr::append_unguarded (char c)
182 {
183 m_rep[pos] = c;
184 m_rep[++pos] = '\0';
185
186 return size;
187 }
188
189 int
190 dynstr::append_unguarded_nonul (char c)
191 {
192 m_rep[pos++] = c;
193
194 return size;
195 }
196
197 int
198 dynstr::append_unguarded (char const *src, size_t len)
199 {
200 memcpy (m_rep + pos, src, len);
201 pos += len;
202 m_rep[pos] = '\0';
203
204 return len;
205 }
206
207 int
208 dynstr::append_unguarded_nonul (char const *src, size_t len)
209 {
210 memcpy (m_rep + pos, src, len);
211 pos += len;
212
213 return len;
214 }