ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/string.C
Revision: 1.8
Committed: Sat Sep 22 14:27:30 2007 UTC (16 years, 7 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +1 -1 lines
State: FILE REMOVED
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 /**
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 * Copyright © 2005 Atheme Development Group
10 * Rights to this code are documented in doc/pod/license.pod.
11 */
12
13 static char const rcsid[] = "$Id: string.C,v 1.7 2007-09-16 18:54:45 pippijn Exp $";
14
15 #include "atheme.h"
16 #include <common/string.h>
17
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 t = salloc<char> (len = strlen (s) + 1);
28
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
41 t = salloc<char> (len + 1);
42
43 strlcpy (t, s, len);
44 return t;
45 }
46
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
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 }