narrow_string.h 5.22 KB
Newer Older
1 2 3
/*
 * Narrow character string functions
 *
4
 * Copyright (C) 2009-2018, Joachim Metz <joachim.metz@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

22 23
#if !defined( _NARROW_STRING_H )
#define _NARROW_STRING_H
24

25 26 27
#include "common.h"
#include "memory.h"
#include "types.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#if defined( HAVE_GLIB_H )
#include <glib.h>
#endif

#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
#include <stdlib.h>
#endif

#if defined( HAVE_STRING_H ) || defined( WINAPI )
#include <string.h>
#endif

#if defined( __cplusplus )
extern "C" {
#endif

/* String allocation
 */
47
#define narrow_string_allocate( size ) \
48 49 50 51
	(char *) memory_allocate( sizeof( char ) * ( size ) )

/* String reallocation
 */
52
#define narrow_string_reallocate( string, size ) \
53 54 55 56 57
	(char *) memory_reallocate( string, ( sizeof( char ) * ( size ) ) )

/* String length
 */
#if defined( HAVE_STRLEN ) || defined( WINAPI )
58
#define narrow_string_length( string ) \
59 60 61 62 63 64
	strlen( string )
#endif

/* String compare
 */
#if defined( HAVE_MEMCMP ) || defined( WINAPI )
65
#define narrow_string_compare( string1, string2, size ) \
66 67 68
	memcmp( (void *) string1, (void *) string2, size )

#elif defined( HAVE_STRNCMP )
69
#define narrow_string_compare( string1, string2, size ) \
70 71 72 73 74 75
	strncmp( string1, string2, size )
#endif

/* Caseless string compare
 */
#if defined( HAVE_GLIB_H )
76
#define narrow_string_compare_no_case( string1, string2, size ) \
77 78 79
	g_ascii_strncasecmp( string1, string2, size )

#elif defined( _MSC_VER )
80
#define narrow_string_compare_no_case( string1, string2, size ) \
81 82
	_strnicmp( string1, string2, size )

83 84
#elif ( defined( WINAPI ) && !defined( __CYGWIN__ ) ) || defined( HAVE_STRNICMP )
#define narrow_string_compare_no_case( string1, string2, size ) \
85 86 87
	strnicmp( string1, string2, size )

#elif defined( HAVE_STRNCASECMP )
88
#define narrow_string_compare_no_case( string1, string2, size ) \
89 90 91
	strncasecmp( string1, string2, size )

#elif defined( HAVE_STRCASECMP )
92
#define narrow_string_compare_no_case( string1, string2, size ) \
93 94 95 96 97 98
	strcasecmp( string1, string2 )
#endif

/* String copy
 */
#if defined( HAVE_MEMCPY ) || defined( WINAPI )
99
#define narrow_string_copy( destination, source, size ) \
100 101 102
	(char *) memcpy( (void *) destination, (void *) source, size )

#elif defined( HAVE_STRNCPY )
103
#define narrow_string_copy( destination, source, size ) \
104 105 106 107 108 109
	strncpy( destination, source, size )
#endif

/* String character search
 */
#if defined( HAVE_MEMCHR ) || defined( WINAPI )
110
#define narrow_string_search_character( string, character, size ) \
111 112 113
	(char *) memchr( (void *) string, (int) character, size )

#elif defined( HAVE_STRCHR )
114
#define narrow_string_search_character( string, character, size ) \
115 116 117 118 119 120
	strchr( string, (int) character )
#endif

/* String reverse character search
 */
#if defined( HAVE_MEMRCHR ) && ( HAVE_DECL_MEMRCHR == 1 )
121
#define narrow_string_search_character_reverse( string, character, size ) \
122 123 124
	(char *) memrchr( (void *) string, (int) character, size )

#elif defined( HAVE_STRRCHR ) || defined( WINAPI )
125
#define narrow_string_search_character_reverse( string, character, size ) \
126 127 128 129 130 131
	strrchr( string, (int) character )
#endif

/* String sub-string search
 */
#if defined( HAVE_STRSTR ) || defined( WINAPI )
132
#define narrow_string_search_string( string, substring, size ) \
133 134 135 136 137 138
	strstr( string, substring )
#endif

/* String formatted print (snprintf)
 */
#if defined( HAVE_GLIB_H )
139
#define narrow_string_snprintf( target, size, ... ) \
140 141 142
	g_snprintf( target, size, __VA_ARGS__ )

#elif defined( _MSC_VER )
143
#define narrow_string_snprintf( target, size, ... ) \
144 145 146
	sprintf_s( target, size, __VA_ARGS__ )

#elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
147
#define narrow_string_snprintf \
148 149 150
	snprintf

#elif defined( HAVE_SNPRINTF ) || defined( WINAPI )
151
#define narrow_string_snprintf( target, size, ... ) \
152 153 154 155 156 157
	snprintf( target, size, __VA_ARGS__ )
#endif

/* String input conversion (sscanf)
 */
#if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
158
#define narrow_string_sscanf \
159 160 161
	sscanf

#elif defined( HAVE_SSCANF ) || defined( WINAPI )
162
#define narrow_string_sscanf( string, format, ... ) \
163 164 165 166 167 168
	sscanf( string, format, __VA_ARGS__ )
#endif

/* Variable arguments formatted print to string function (vsnprintf)
 */
#if defined( HAVE_GLIB_H )
169
#define narrow_string_vsnprintf( string, size, format, ... ) \
170 171 172
	g_vsnprintf( string, size, format, __VA_ARGS__ )

#elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
173
#define narrow_string_vsnprintf \
174 175 176
	vsnprintf

#elif defined( HAVE_VSNPRINTF ) || defined( WINAPI )
177
#define narrow_string_vsnprintf( string, size, format, ... ) \
178 179 180 181 182 183 184
	vsnprintf( string, size, format, __VA_ARGS__ )
#endif

#if defined( __cplusplus )
}
#endif

185
#endif /* !defined( _NARROW_STRING_H ) */
186