The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

inspstring.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 /* $Core */
00015 
00016 #include "inspstring.h"
00017 
00018 /*
00019  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00020  * All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright    
00028  *    notice, this list of conditions and the following disclaimer in the  
00029  *    documentation and/or other materials provided with the distribution. 
00030  * 3. The name of the author may not be used to endorse or promote products
00031  *    derived from this software without specific prior written permission.
00032  *
00033  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00034  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00035  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00036  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00037  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00038  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00039  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00040  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00041  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00042  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00043  */
00044  
00045 #ifndef HAS_STRLCPY
00046 CoreExport size_t strlcat(char *dst, const char *src, size_t siz)
00047 {
00048         char *d = dst;
00049         const char *s = src;
00050         size_t n = siz, dlen;
00051 
00052         while (n-- != 0 && *d != '\0')
00053                 d++;
00054 
00055         dlen = d - dst;
00056         n = siz - dlen;
00057 
00058         if (n == 0)
00059                 return(dlen + strlen(s));
00060 
00061         while (*s != '\0')
00062         {
00063                 if (n != 1)
00064                 {
00065                         *d++ = *s;
00066                         n--;
00067                 }
00068 
00069                 s++;
00070         }
00071 
00072         *d = '\0';
00073         return(dlen + (s - src)); /* count does not include NUL */
00074 }
00075 
00076 CoreExport size_t strlcpy(char *dst, const char *src, size_t siz)
00077 {
00078         char *d = dst;
00079         const char *s = src;
00080         size_t n = siz;
00081 
00082         /* Copy as many bytes as will fit */
00083         if (n != 0 && --n != 0)
00084         {
00085                 do
00086                 {
00087                         if ((*d++ = *s++) == 0)
00088                                 break;
00089                 } while (--n != 0);
00090         }
00091 
00092         /* Not enough room in dst, add NUL and traverse rest of src */
00093         if (n == 0)
00094         {
00095                 if (siz != 0)
00096                         *d = '\0'; /* NUL-terminate dst */
00097                 while (*s++);
00098         }
00099 
00100         return(s - src - 1); /* count does not include NUL */
00101 }
00102 #endif
00103 
00104 CoreExport int charlcat(char* x,char y,int z)
00105 {
00106         char* x__n = x;
00107         int v = 0;
00108 
00109         while(*x__n++)
00110                 v++;
00111 
00112         if (v < z - 1)
00113         {
00114                 *--x__n = y;
00115                 *++x__n = 0;
00116         }
00117 
00118         return v;
00119 }
00120 
00121 CoreExport bool charremove(char* mp, char remove)
00122 {
00123         char* mptr = mp;
00124         bool shift_down = false;
00125 
00126         while (*mptr)
00127         {
00128                 if (*mptr == remove)
00129                 shift_down = true;
00130 
00131                 if (shift_down)
00132                         *mptr = *(mptr+1);
00133 
00134                 mptr++;
00135         }
00136 
00137         return shift_down;
00138 }
00139