• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_string.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   String and text processing routines for libts
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "libts.h"   /* MAGIC_EDITING_TAG */
00025 
00026 #include <assert.h>
00027 #include <stdarg.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #define INK_MAX_STRING_ARRAY_SIZE 128
00032 
00033 /*-------------------------------------------------------------------------
00034   -------------------------------------------------------------------------*/
00035 char *
00036 ink_memcpy_until_char(char *dst, char *src, unsigned int n, unsigned char c)
00037 {
00038   unsigned int i = 0;
00039   for (; ((i < n) && (((unsigned char) src[i]) != c)); i++)
00040     dst[i] = src[i];
00041   return &src[i];
00042 }
00043 
00044 /*---------------------------------------------------------------------------*
00045 
00046   char *ink_strncpy(char *dest, char *src, int n)
00047 
00048   This routine is a safer version of strncpy which always NUL terminates
00049   the destination string.  Note that this routine has the SAME semantics
00050   as strncpy, such as copying exactly n bytes, padding dest with NULs
00051   is necessary.  Use ink_string_copy for a non-padding version.
00052 
00053  *---------------------------------------------------------------------------*/
00054 
00055 char *
00056 ink_strncpy(char *dest, const char *src, int n)
00057 {
00058   if (likely(src && dest)) {
00059     if (n > 1)
00060       strncpy(dest, src, (n - 1));
00061     if (n > 0)
00062       dest[n - 1] = '\0';
00063   }
00064 
00065   return (dest);
00066 }                               /* End ink_strncpy */
00067 
00068 /*---------------------------------------------------------------------------*
00069 
00070   char *ink_strncat(char *dest, char *src, int n)
00071 
00072   This routine is a safer version of strncat which always NUL terminates
00073   the destination string.  Note that this routine has the SAME semantics
00074   as strncat, such as concatinating exactly n bytes, padding dest with NULs
00075   is necessary.  Use ink_string_copy for a non-padding version.
00076 
00077  *---------------------------------------------------------------------------*/
00078 
00079 char *
00080 ink_strncat(char *dest, const char *src, int n)
00081 {
00082   if (likely(src && dest)) {
00083     if (n > 1)
00084       strncat(dest, src, (n - 1));
00085     if (n > 0)
00086       dest[n - 1] = '\0';
00087   }
00088 
00089   return (dest);
00090 }                               /* End ink_strncat */
00091 
00092 /*---------------------------------------------------------------------------*
00093 
00094   char *ink_string_concatenate_strings(char *dest, ...)
00095 
00096   This routine concatenates a variable number of strings into the buffer
00097   <dest>, returning the pointer to <dest>.  The sequence of strings must end
00098   with NULL.
00099 
00100  *---------------------------------------------------------------------------*/
00101 
00102 char *
00103 ink_string_concatenate_strings(char *dest, ...)
00104 {
00105   va_list ap;
00106   char *s, *d;
00107 
00108   va_start(ap, dest);
00109 
00110   d = dest;
00111 
00112   while (1) {
00113     s = va_arg(ap, char *);
00114     if (s == NULL)
00115       break;
00116 
00117     while (*s)
00118       *d++ = *s++;
00119   }
00120   *d++ = '\0';
00121   va_end(ap);
00122   return (dest);
00123 }                               /* End ink_string_concatenate_strings */
00124 
00125 
00126 /*---------------------------------------------------------------------------*
00127 
00128   char *ink_string_concatenate_strings_n(char *dest, int n, ...)
00129 
00130   This routine concatenates a variable number of strings into the buffer
00131   <dest>, returning the pointer to <dest>.  The sequence of strings must end
00132   with NULL.  A NUL will always be placed after <dest>, and no more than
00133   <n> - 1 characters will ever be written to <dest>.
00134 
00135  *---------------------------------------------------------------------------*/
00136 
00137 char *
00138 ink_string_concatenate_strings_n(char *dest, int n, ...)
00139 {
00140   va_list ap;
00141   char *s, *d;
00142 
00143   va_start(ap, n);
00144 
00145   d = dest;
00146 
00147   while (n > 1) {
00148     s = va_arg(ap, char *);
00149     if (s == NULL)
00150       break;
00151     while (*s && (n > 1)) {
00152       *d++ = *s++;
00153       n--;
00154     }
00155   }
00156   if (n >= 1)
00157     *d = '\0';
00158   va_end(ap);
00159   return (dest);
00160 }                               /* End ink_string_concatenate_strings_n */
00161 
00162 
00163 /*---------------------------------------------------------------------------*
00164 
00165   char *ink_string_append(char *dest, char *src, int n)
00166 
00167   This routine appends <src> to the end of <dest>, but it insures the
00168   string pointed to by <dest> never grows beyond <n> characters, including
00169   the terminating NUL.  A NUL is always written if n > 0.
00170 
00171  *---------------------------------------------------------------------------*/
00172 
00173 char *
00174 ink_string_append(char *dest, char *src, int n)
00175 {
00176   char *d, *s, *last_valid_char;
00177 
00178   ink_assert(src != NULL);
00179   ink_assert(dest != NULL);
00180   ink_assert(n >= 0);
00181 
00182   if (n == 0)
00183     return (dest);
00184 
00185   last_valid_char = dest + n - 1;
00186 
00187   /* Scan For End Of Dest */
00188 
00189   for (d = dest; (d <= last_valid_char) && (*d != '\0'); d++);
00190 
00191   /* If At End Of String, NUL Terminate & Exit */
00192 
00193   if (d > last_valid_char) {
00194     dest[n - 1] = '\0';
00195     return (dest);
00196   }
00197 
00198   /* Append src To String */
00199 
00200   s = src;
00201   while ((d < last_valid_char) && (*s != '\0'))
00202     *d++ = *s++;
00203 
00204   /* If At End Of String, NUL Terminate & Exit */
00205 
00206   if (d > last_valid_char)
00207     dest[n - 1] = '\0';
00208   else
00209     *d = '\0';
00210 
00211   return (dest);
00212 }                               /* End ink_string_append */
00213 
00214 
00215 #if !HAVE_STRLCPY
00216 size_t
00217 ink_strlcpy(char *dst, const char *src, size_t siz)
00218 {
00219   char *d = dst;
00220   const char *s = src;
00221   size_t n = siz;
00222 
00223   /* Copy as many bytes as will fit */
00224   if (n != 0) {
00225     while (--n != 0) {
00226       if ((*d++ = *s++) == '\0')
00227         break;
00228     }
00229   }
00230 
00231   /* Not enough room in dst, add NUL and traverse rest of src */
00232   if (n == 0) {
00233     if (siz != 0)
00234                             *d = '\0';      /* NUL-terminate dst */
00235     while (*s++)
00236       ;
00237   }
00238 
00239   return (s - src - 1);   /* count does not include NUL */
00240 }
00241 #endif
00242 
00243 #if !HAVE_STRLCAT
00244 size_t
00245 ink_strlcat(char *dst, const char *src, size_t siz)
00246 {
00247   char *d = dst;
00248   const char *s = src;
00249   size_t n = siz;
00250   size_t dlen;
00251 
00252   /* Find the end of dst and adjust bytes left but don't go past end */
00253   while (n-- != 0 && *d != '\0')
00254     d++;
00255   dlen = d - dst;
00256   n = siz - dlen;
00257 
00258   if (n == 0)
00259     return (dlen + strlen(s));
00260   while (*s != '\0') {
00261     if (n != 1) {
00262       *d++ = *s;
00263       n--;
00264     }
00265     s++;
00266   }
00267   *d = '\0';
00268 
00269   return (dlen + (s - src));  /* count does not include NUL */
00270 }
00271 #endif
00272 

Generated by  doxygen 1.7.1