ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/string.C
Revision: 1.5
Committed: Thu Aug 30 19:56:26 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -99 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

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.5 static char const rcsid[] = "$Id: string.C,v 1.4 2007-08-28 17:08:12 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 pippijn 1.5 t = salloc<char> (len = strlen (s) + 1);
22 pippijn 1.4
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.5 t = salloc<char> (len + 1);
36 pippijn 1.4
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     }