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

# User Rev Content
1 pippijn 1.1 /*
2     * Copyright © 2005 Atheme Development Group
3 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
4 pippijn 1.1 *
5     * String functions.
6     */
7    
8 pippijn 1.4 static char const rcsid[] = "$Id: string.C,v 1.3 2007-07-21 13:23:22 pippijn Exp $";
9 pippijn 1.1
10 pippijn 1.4 #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 pippijn 1.1
35 pippijn 1.4 t = alloc<char> (len + 1);
36    
37     strlcpy (t, s, len);
38     return t;
39     }
40 pippijn 1.1
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 pippijn 1.4 dynstr::append (char c)
122 pippijn 1.1 {
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 pippijn 1.4 dynstr::append_nonul (char c)
137 pippijn 1.1 {
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 pippijn 1.4 dynstr::append (char const *src, size_t len)
151 pippijn 1.1 {
152 pippijn 1.4 if (size - pos <= len)
153 pippijn 1.1 {
154 pippijn 1.4 size_t new_size = std::max (size * 2, pos + len + 8);
155     size = new_size;
156 pippijn 1.1 m_rep = new char[new_size];
157     }
158 pippijn 1.4 memcpy (m_rep + pos, src, len);
159     pos += len;
160 pippijn 1.1 m_rep[pos] = '\0';
161    
162 pippijn 1.4 return len;
163 pippijn 1.1 }
164    
165     int
166 pippijn 1.4 dynstr::append_nonul (char const *src, size_t len)
167 pippijn 1.1 {
168 pippijn 1.4 if (size - pos <= len)
169 pippijn 1.1 {
170 pippijn 1.4 size_t new_size = std::max (size * 2, pos + len + 8);
171     size = new_size;
172 pippijn 1.1 m_rep = new char[new_size];
173     }
174 pippijn 1.4 memcpy (m_rep + pos, src, len);
175     pos += len;
176 pippijn 1.1
177 pippijn 1.4 return len;
178 pippijn 1.1 }
179    
180     int
181 pippijn 1.4 dynstr::append_unguarded (char c)
182 pippijn 1.1 {
183     m_rep[pos] = c;
184     m_rep[++pos] = '\0';
185    
186     return size;
187     }
188    
189     int
190 pippijn 1.4 dynstr::append_unguarded_nonul (char c)
191 pippijn 1.1 {
192     m_rep[pos++] = c;
193    
194     return size;
195     }
196    
197     int
198 pippijn 1.4 dynstr::append_unguarded (char const *src, size_t len)
199 pippijn 1.1 {
200 pippijn 1.4 memcpy (m_rep + pos, src, len);
201     pos += len;
202 pippijn 1.1 m_rep[pos] = '\0';
203    
204 pippijn 1.4 return len;
205 pippijn 1.1 }
206    
207     int
208 pippijn 1.4 dynstr::append_unguarded_nonul (char const *src, size_t len)
209 pippijn 1.1 {
210 pippijn 1.4 memcpy (m_rep + pos, src, len);
211     pos += len;
212 pippijn 1.1
213 pippijn 1.4 return len;
214 pippijn 1.1 }