ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/string.C
Revision: 1.2
Committed: Sat Jul 21 01:29:13 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
- moved to new documentation system
- fixed small build error

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