00001 /* 00002 * Copyright (C) 2004-2007, 2014 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 2000, 2001, 2003 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: string.h,v 1.23 2007/09/13 04:48:16 each Exp $ */ 00019 00020 #ifndef ISC_STRING_H 00021 #define ISC_STRING_H 1 00022 00023 /*! \file isc/string.h */ 00024 00025 #include <isc/formatcheck.h> 00026 #include <isc/int.h> 00027 #include <isc/lang.h> 00028 #include <isc/platform.h> 00029 #include <isc/types.h> 00030 00031 #include <string.h> 00032 00033 #ifdef ISC_PLATFORM_HAVESTRINGSH 00034 #include <strings.h> 00035 #endif 00036 00037 #define ISC_STRING_MAGIC 0x5e 00038 00039 ISC_LANG_BEGINDECLS 00040 00041 isc_uint64_t 00042 isc_string_touint64(char *source, char **endp, int base); 00043 /*%< 00044 * Convert the string pointed to by 'source' to isc_uint64_t. 00045 * 00046 * On successful conversion 'endp' points to the first character 00047 * after conversion is complete. 00048 * 00049 * 'base': 0 or 2..36 00050 * 00051 * If base is 0 the base is computed from the string type. 00052 * 00053 * On error 'endp' points to 'source'. 00054 */ 00055 00056 isc_result_t 00057 isc_string_copy(char *target, size_t size, const char *source); 00058 /* 00059 * Copy the string pointed to by 'source' to 'target' which is a 00060 * pointer to a string of at least 'size' bytes. 00061 * 00062 * Requires: 00063 * 'target' is a pointer to a char[] of at least 'size' bytes. 00064 * 'size' an integer > 0. 00065 * 'source' == NULL or points to a NUL terminated string. 00066 * 00067 * Ensures: 00068 * If result == ISC_R_SUCCESS 00069 * 'target' will be a NUL terminated string of no more 00070 * than 'size' bytes (including NUL). 00071 * 00072 * If result == ISC_R_NOSPACE 00073 * 'target' is undefined. 00074 * 00075 * Returns: 00076 * ISC_R_SUCCESS -- 'source' was successfully copied to 'target'. 00077 * ISC_R_NOSPACE -- 'source' could not be copied since 'target' 00078 * is too small. 00079 */ 00080 00081 void 00082 isc_string_copy_truncate(char *target, size_t size, const char *source); 00083 /* 00084 * Copy the string pointed to by 'source' to 'target' which is a 00085 * pointer to a string of at least 'size' bytes. 00086 * 00087 * Requires: 00088 * 'target' is a pointer to a char[] of at least 'size' bytes. 00089 * 'size' an integer > 0. 00090 * 'source' == NULL or points to a NUL terminated string. 00091 * 00092 * Ensures: 00093 * 'target' will be a NUL terminated string of no more 00094 * than 'size' bytes (including NUL). 00095 */ 00096 00097 isc_result_t 00098 isc_string_append(char *target, size_t size, const char *source); 00099 /* 00100 * Append the string pointed to by 'source' to 'target' which is a 00101 * pointer to a NUL terminated string of at least 'size' bytes. 00102 * 00103 * Requires: 00104 * 'target' is a pointer to a NUL terminated char[] of at 00105 * least 'size' bytes. 00106 * 'size' an integer > 0. 00107 * 'source' == NULL or points to a NUL terminated string. 00108 * 00109 * Ensures: 00110 * If result == ISC_R_SUCCESS 00111 * 'target' will be a NUL terminated string of no more 00112 * than 'size' bytes (including NUL). 00113 * 00114 * If result == ISC_R_NOSPACE 00115 * 'target' is undefined. 00116 * 00117 * Returns: 00118 * ISC_R_SUCCESS -- 'source' was successfully appended to 'target'. 00119 * ISC_R_NOSPACE -- 'source' could not be appended since 'target' 00120 * is too small. 00121 */ 00122 00123 void 00124 isc_string_append_truncate(char *target, size_t size, const char *source); 00125 /* 00126 * Append the string pointed to by 'source' to 'target' which is a 00127 * pointer to a NUL terminated string of at least 'size' bytes. 00128 * 00129 * Requires: 00130 * 'target' is a pointer to a NUL terminated char[] of at 00131 * least 'size' bytes. 00132 * 'size' an integer > 0. 00133 * 'source' == NULL or points to a NUL terminated string. 00134 * 00135 * Ensures: 00136 * 'target' will be a NUL terminated string of no more 00137 * than 'size' bytes (including NUL). 00138 */ 00139 00140 isc_result_t 00141 isc_string_printf(char *target, size_t size, const char *format, ...) 00142 ISC_FORMAT_PRINTF(3, 4); 00143 /* 00144 * Print 'format' to 'target' which is a pointer to a string of at least 00145 * 'size' bytes. 00146 * 00147 * Requires: 00148 * 'target' is a pointer to a char[] of at least 'size' bytes. 00149 * 'size' an integer > 0. 00150 * 'format' == NULL or points to a NUL terminated string. 00151 * 00152 * Ensures: 00153 * If result == ISC_R_SUCCESS 00154 * 'target' will be a NUL terminated string of no more 00155 * than 'size' bytes (including NUL). 00156 * 00157 * If result == ISC_R_NOSPACE 00158 * 'target' is undefined. 00159 * 00160 * Returns: 00161 * ISC_R_SUCCESS -- 'format' was successfully printed to 'target'. 00162 * ISC_R_NOSPACE -- 'format' could not be printed to 'target' since it 00163 * is too small. 00164 */ 00165 00166 void 00167 isc_string_printf_truncate(char *target, size_t size, const char *format, ...) 00168 ISC_FORMAT_PRINTF(3, 4); 00169 /* 00170 * Print 'format' to 'target' which is a pointer to a string of at least 00171 * 'size' bytes. 00172 * 00173 * Requires: 00174 * 'target' is a pointer to a char[] of at least 'size' bytes. 00175 * 'size' an integer > 0. 00176 * 'format' == NULL or points to a NUL terminated string. 00177 * 00178 * Ensures: 00179 * 'target' will be a NUL terminated string of no more 00180 * than 'size' bytes (including NUL). 00181 */ 00182 00183 00184 char * 00185 isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source); 00186 /* 00187 * Copy the region pointed to by r to a NUL terminated string 00188 * allocated from the memory context pointed to by mctx. 00189 * 00190 * The result should be deallocated using isc_mem_free() 00191 * 00192 * Requires: 00193 * 'mctx' is a point to a valid memory context. 00194 * 'source' is a pointer to a valid region. 00195 * 00196 * Returns: 00197 * a pointer to a NUL terminated string or 00198 * NULL if memory for the copy could not be allocated 00199 * 00200 */ 00201 00202 char * 00203 isc_string_separate(char **stringp, const char *delim); 00204 00205 #ifdef ISC_PLATFORM_NEEDSTRSEP 00206 #define strsep isc_string_separate 00207 #endif 00208 00209 #ifdef ISC_PLATFORM_NEEDMEMMOVE 00210 #define memmove(a,b,c) bcopy(b,a,c) 00211 #endif 00212 00213 size_t 00214 isc_string_strlcpy(char *dst, const char *src, size_t size); 00215 00216 00217 #ifdef ISC_PLATFORM_NEEDSTRLCPY 00218 #define strlcpy isc_string_strlcpy 00219 #endif 00220 00221 00222 size_t 00223 isc_string_strlcat(char *dst, const char *src, size_t size); 00224 00225 #ifdef ISC_PLATFORM_NEEDSTRLCAT 00226 #define strlcat isc_string_strlcat 00227 #endif 00228 00229 char * 00230 isc_string_strcasestr(const char *big, const char *little); 00231 00232 #ifdef ISC_PLATFORM_NEEDSTRCASESTR 00233 #define strcasestr isc_string_strcasestr 00234 #endif 00235 00236 ISC_LANG_ENDDECLS 00237 00238 #endif /* ISC_STRING_H */