1 /* 2 * Narrow character string functions 3 * 4 * Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com> 5 * 6 * Refer to AUTHORS for acknowledgements. 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22 #if !defined( _NARROW_STRING_H ) 23 #define _NARROW_STRING_H 24 25 #include "common.h" 26 #include "memory.h" 27 #include "types.h" 28 29 #if defined( HAVE_GLIB_H ) 30 #include <glib.h> 31 #endif 32 33 #if defined( HAVE_STDLIB_H ) || defined( WINAPI ) 34 #include <stdlib.h> 35 #endif 36 37 #if defined( HAVE_STRING_H ) || defined( WINAPI ) 38 #include <string.h> 39 #endif 40 41 #if defined( __cplusplus ) 42 extern "C" { 43 #endif 44 45 /* String allocation 46 */ 47 #define narrow_string_allocate( size ) \ 48 (char *) memory_allocate( sizeof( char ) * ( size ) ) 49 50 /* String reallocation 51 */ 52 #define narrow_string_reallocate( string, size ) \ 53 (char *) memory_reallocate( string, ( sizeof( char ) * ( size ) ) ) 54 55 /* String length 56 */ 57 #if defined( HAVE_STRLEN ) || defined( WINAPI ) 58 #define narrow_string_length( string ) \ 59 strlen( string ) 60 #endif 61 62 /* String compare 63 */ 64 #if defined( HAVE_MEMCMP ) || defined( WINAPI ) 65 #define narrow_string_compare( string1, string2, size ) \ 66 memcmp( (void *) string1, (void *) string2, size ) 67 68 #elif defined( HAVE_STRNCMP ) 69 #define narrow_string_compare( string1, string2, size ) \ 70 strncmp( string1, string2, size ) 71 #endif 72 73 /* Caseless string compare 74 */ 75 #if defined( HAVE_GLIB_H ) 76 #define narrow_string_compare_no_case( string1, string2, size ) \ 77 g_ascii_strncasecmp( string1, string2, size ) 78 79 #elif defined( _MSC_VER ) 80 #define narrow_string_compare_no_case( string1, string2, size ) \ 81 _strnicmp( string1, string2, size ) 82 83 #elif ( defined( WINAPI ) && !defined( __CYGWIN__ ) ) || defined( HAVE_STRNICMP ) 84 #define narrow_string_compare_no_case( string1, string2, size ) \ 85 strnicmp( string1, string2, size ) 86 87 #elif defined( HAVE_STRNCASECMP ) 88 #define narrow_string_compare_no_case( string1, string2, size ) \ 89 strncasecmp( string1, string2, size ) 90 91 #elif defined( HAVE_STRCASECMP ) 92 #define narrow_string_compare_no_case( string1, string2, size ) \ 93 strcasecmp( string1, string2 ) 94 #endif 95 96 /* String copy 97 */ 98 #if defined( HAVE_MEMCPY ) || defined( WINAPI ) 99 #define narrow_string_copy( destination, source, size ) \ 100 (char *) memcpy( (void *) destination, (void *) source, size ) 101 102 #elif defined( HAVE_STRNCPY ) 103 #define narrow_string_copy( destination, source, size ) \ 104 strncpy( destination, source, size ) 105 #endif 106 107 /* String character search 108 */ 109 #if defined( HAVE_MEMCHR ) || defined( WINAPI ) 110 #define narrow_string_search_character( string, character, size ) \ 111 (char *) memchr( (void *) string, (int) character, size ) 112 113 #elif defined( HAVE_STRCHR ) 114 #define narrow_string_search_character( string, character, size ) \ 115 strchr( string, (int) character ) 116 #endif 117 118 /* String reverse character search 119 */ 120 #if defined( HAVE_MEMRCHR ) && ( HAVE_DECL_MEMRCHR == 1 ) 121 #define narrow_string_search_character_reverse( string, character, size ) \ 122 (char *) memrchr( (void *) string, (int) character, size ) 123 124 #elif defined( HAVE_STRRCHR ) || defined( WINAPI ) 125 /* (void)(size) is used to suppress unused variable warnings */ 126 #define narrow_string_search_character_reverse( string, character, size ) \ 127 strrchr( string, (int) character ); (void)(size) 128 #endif 129 130 /* String sub-string search 131 */ 132 #if defined( HAVE_STRSTR ) || defined( WINAPI ) 133 #define narrow_string_search_string( string, substring, size ) \ 134 strstr( string, substring ) 135 #endif 136 137 /* String formatted print (snprintf) 138 */ 139 #if defined( HAVE_GLIB_H ) 140 #define narrow_string_snprintf( target, size, ... ) \ 141 g_snprintf( target, size, __VA_ARGS__ ) 142 143 #elif defined( _MSC_VER ) 144 #define narrow_string_snprintf( target, size, ... ) \ 145 sprintf_s( target, size, __VA_ARGS__ ) 146 147 #elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) 148 #define narrow_string_snprintf \ 149 snprintf 150 151 #elif defined( HAVE_SNPRINTF ) || defined( WINAPI ) 152 #define narrow_string_snprintf( target, size, ... ) \ 153 snprintf( target, size, __VA_ARGS__ ) 154 #endif 155 156 /* String input conversion (sscanf) 157 */ 158 #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) 159 #define narrow_string_sscanf \ 160 sscanf 161 162 #elif defined( HAVE_SSCANF ) || defined( WINAPI ) 163 #define narrow_string_sscanf( string, format, ... ) \ 164 sscanf( string, format, __VA_ARGS__ ) 165 #endif 166 167 /* Variable arguments formatted print to string function (vsnprintf) 168 */ 169 #if defined( HAVE_GLIB_H ) 170 #define narrow_string_vsnprintf( string, size, format, ... ) \ 171 g_vsnprintf( string, size, format, __VA_ARGS__ ) 172 173 #elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 ) 174 #define narrow_string_vsnprintf \ 175 vsnprintf 176 177 #elif defined( HAVE_VSNPRINTF ) || defined( WINAPI ) 178 #define narrow_string_vsnprintf( string, size, format, ... ) \ 179 vsnprintf( string, size, format, __VA_ARGS__ ) 180 #endif 181 182 #if defined( __cplusplus ) 183 } 184 #endif 185 186 #endif /* !defined( _NARROW_STRING_H ) */ 187 188