]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspstring.cpp
d8df39e1883f9d4fd95b77a20c22a27fa395a0b0
[user/henk/code/inspircd.git] / src / inspstring.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspstring.h"
15 #include <string.h>
16
17 /*
18  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright    
27  *    notice, this list of conditions and the following disclaimer in the  
28  *    documentation and/or other materials provided with the distribution. 
29  * 3. The name of the author may not be used to endorse or promote products
30  *    derived from this software without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
33  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
34  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
35  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  */
43  
44 #ifndef HAS_STRLCPY
45 size_t strlcat(char *dst, const char *src, size_t siz)
46 {
47   char *d = dst;
48   const char *s = src;
49   size_t n = siz, dlen;
50
51   while (n-- != 0 && *d != '\0')
52     d++;
53
54   dlen = d - dst;
55   n    = siz - dlen;
56
57   if (n == 0)
58     return(dlen + strlen(s));
59
60   while (*s != '\0')
61   {
62     if (n != 1)
63     {
64       *d++ = *s;
65       n--;
66     }
67
68     s++;
69   }
70
71   *d = '\0';
72   return(dlen + (s - src)); /* count does not include NUL */
73 }
74
75 size_t strlcpy(char *dst, const char *src, size_t siz)
76 {
77   char *d = dst;
78   const char *s = src;
79   size_t n = siz;
80
81   /* Copy as many bytes as will fit */
82   if (n != 0 && --n != 0)
83   {
84     do
85     {
86       if ((*d++ = *s++) == 0)
87         break;
88     } while (--n != 0);
89   }
90
91   /* Not enough room in dst, add NUL and traverse rest of src */
92   if (n == 0)
93   {
94     if (siz != 0)
95       *d = '\0'; /* NUL-terminate dst */
96     while (*s++)
97       ;
98   }
99
100   return(s - src - 1); /* count does not include NUL */
101 }
102 #endif
103
104 int charlcat(char* x,char y,int z)
105 {
106         char* x__n = x;
107         int v = 0;
108
109         while(*x__n++)
110                 v++;
111
112         if (v < z - 1)
113         {
114                 *--x__n = y;
115                 *++x__n = 0;
116         }
117
118         return v;
119 }
120
121 bool charremove(char* mp, char remove)
122 {
123         char* mptr = mp;
124         bool shift_down = false;
125
126         while (*mptr)
127         {
128                 if (*mptr == remove)
129                 shift_down = true;
130
131                 if (shift_down)
132                         *mptr = *(mptr+1);
133
134                 mptr++;
135         }
136
137         return shift_down;
138 }
139