PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/inspstring.cpp

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