/** * string.C: String functions. * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2005 Atheme Development Group * Rights to this code are documented in doc/pod/license.pod. */ static char const rcsid[] = "$Id: string.C,v 1.8 2007/09/22 14:27:30 pippijn dead $"; #include "atheme.h" #include char * sstrdup (char const * const s) { char *t; size_t len; if (s == NULL) return NULL; t = salloc (len = strlen (s) + 1); strlcpy (t, s, len); return t; } char * sstrndup (char const *s, int len) { char *t; if (s == NULL) return NULL; t = salloc (len + 1); strlcpy (t, s, len); return t; } void assign (char *dst, char const *src, size_t maxlen) { if (!src) src = ""; size_t len = strlen (src); if (len >= maxlen - 1) { if (maxlen <= 4) { memset (dst, '.', maxlen - 1); dst [maxlen - 1] = 0; } else { memcpy (dst, src, maxlen - 4); memcpy (dst + maxlen - 4, "...", 4); } } else memcpy (dst, src, len + 1); } #ifndef HAVE_STRLCAT /* These functions are taken from Linux. */ size_t strlcat (char *dest, char const *src, size_t count) { size_t dsize = strlen (dest); size_t len = strlen (src); size_t res = dsize + len; dest += dsize; count -= dsize; if (len >= count) len = count - 1; memcpy (dest, src, len); dest[len] = 0; return res; } #endif #ifndef HAVE_STRLCPY size_t strlcpy (char *dest, char const *src, size_t size) { size_t ret = strlen (src); if (size) { size_t len = (ret >= size) ? size - 1 : ret; memcpy (dest, src, len); dest[len] = '\0'; } return ret; } #endif /* removes unwanted chars from a line */ char const * strip (char *line) { char *c = 0; if (line) { if ((c = strchr (line, '\n'))) *c = '\0'; if ((c = strchr (line, '\r'))) *c = '\0'; if ((c = strchr (line, '\1'))) *c = '\0'; } return c; } int dynstr::add (char c) { if (length - pos <= 1) { size_t new_size = std::max (length * 2, pos + 9); length = new_size; m_rep = new char[new_size]; } m_rep[pos] = c; m_rep[++pos] = '\0'; return length; } int dynstr::add (char const *src, size_t len) { if (length - pos <= len) { size_t new_size = std::max (length * 2, pos + len + 8); length = new_size; m_rep = new char[new_size]; } memcpy (m_rep + pos, src, len); pos += len; m_rep[pos] = '\0'; return len; }