You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

175 lines
4.9 KiB

  1. /*
  2. * Wide character string functions
  3. *
  4. * Copyright (C) 2008-2019, 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. #if !defined( _WIDE_STRING_H )
  22. #define _WIDE_STRING_H
  23. #include "common.h"
  24. #include "memory.h"
  25. #include "types.h"
  26. #if defined( HAVE_WCHAR_H ) || defined( WINAPI )
  27. #include <wchar.h>
  28. #endif
  29. #if defined( __cplusplus )
  30. extern "C" {
  31. #endif
  32. /* Intermediate version of the macro required
  33. * for correct evaluation predefined string
  34. */
  35. #define _WIDE_STRING_INTERMEDIATE( string ) \
  36. L ## string
  37. #define _WIDE_STRING( string ) \
  38. _WIDE_STRING_INTERMEDIATE( string )
  39. /* String allocation
  40. */
  41. #define wide_string_allocate( size ) \
  42. (wchar_t *) memory_allocate( sizeof( wchar_t ) * ( size ) )
  43. /* String reallocation
  44. */
  45. #define wide_string_reallocate( string, size ) \
  46. (wchar_t *) memory_reallocate( string, ( sizeof( wchar_t ) * ( size ) ) )
  47. /* String length
  48. */
  49. #if defined( HAVE_WCSLEN ) || defined( WINAPI )
  50. #define wide_string_length( string ) \
  51. wcslen( string )
  52. #endif
  53. /* String compare
  54. */
  55. #if defined( HAVE_WMEMCMP )
  56. #define wide_string_compare( string1, string2, size ) \
  57. wmemcmp( (void *) string1, (void *) string2, size )
  58. #elif defined( HAVE_WCSNCMP ) || defined( WINAPI )
  59. #define wide_string_compare( string1, string2, size ) \
  60. wcsncmp( string1, string2, size )
  61. #endif
  62. /* Caseless string compare
  63. */
  64. #if defined( _MSC_VER ) || ( defined( __BORLANDC__ ) && ( __BORLANDC__ >= 0x0551 ) )
  65. #define wide_string_compare_no_case( string1, string2, size ) \
  66. _wcsnicmp( string1, string2, size )
  67. #elif ( defined( WINAPI ) && !defined( __CYGWIN__ ) ) || defined( HAVE_WCSNICMP )
  68. #define wide_string_compare_no_case( string1, string2, size ) \
  69. wcsnicmp( string1, string2, size )
  70. #elif defined( HAVE_WCSNCASECMP )
  71. #define wide_string_compare_no_case( string1, string2, size ) \
  72. wcsncasecmp( string1, string2, size )
  73. #elif defined( HAVE_WCSCASECMP )
  74. #define wide_string_compare_no_case( string1, string2, size ) \
  75. wcscasecmp( string1, string2 )
  76. #endif
  77. /* String copy
  78. */
  79. #if defined( HAVE_WMEMCPY )
  80. #define wide_string_copy( destination, source, size ) \
  81. (wchar_t *) wmemcpy( (void *) destination, (void *) source, size )
  82. #elif defined( HAVE_WCSNCPY ) || defined( WINAPI )
  83. #define wide_string_copy( destination, source, size ) \
  84. wcsncpy( destination, source, size )
  85. #endif
  86. /* String character search
  87. */
  88. #if defined( HAVE_WMEMCHR )
  89. #define wide_string_search_character( string, character, size ) \
  90. (wchar_t *) wmemchr( (void *) string, (wchar_t) character, size )
  91. #elif defined( HAVE_WCSCHR ) || defined( WINAPI )
  92. #define wide_string_search_character( string, character, size ) \
  93. wcschr( string, (wchar_t) character )
  94. #endif
  95. /* String reverse character search
  96. */
  97. #if defined( HAVE_WMEMRCHR )
  98. #define wide_string_search_character_reverse( string, character, size ) \
  99. (wchar_t *) wmemrchr( (void *) string, (wchar_t) character, size )
  100. #elif defined( HAVE_WCSRCHR ) || defined( WINAPI )
  101. #define wide_string_search_character_reverse( string, character, size ) \
  102. wcsrchr( string, (wchar_t) character )
  103. #endif
  104. /* String sub-string search
  105. */
  106. #if defined( HAVE_WCSSTR ) || defined( WINAPI )
  107. #define wide_string_search_string( string, substring, size ) \
  108. wcsstr( string, substring )
  109. #endif
  110. /* String formatted print (snwprintf)
  111. */
  112. #if defined( _MSC_VER )
  113. #define wide_string_snwprintf( target, size, ... ) \
  114. swprintf_s( target, size, __VA_ARGS__ )
  115. #elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
  116. #define wide_string_snwprintf \
  117. snwprintf
  118. #elif defined( WINAPI )
  119. #define wide_string_snwprintf( target, size, ... ) \
  120. snwprintf( target, size, __VA_ARGS__ )
  121. #elif defined( HAVE_SWPRINTF )
  122. #define wide_string_snwprintf( target, size, ... ) \
  123. swprintf( target, size, __VA_ARGS__ )
  124. #endif
  125. /* Variable arguments formatted print to string function (vsnwprintf)
  126. */
  127. #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
  128. #define wide_string_vsnwprintf \
  129. _vsnwprintf
  130. #elif defined( WINAPI )
  131. #define wide_string_vsnwprintf( string, size, format, ... ) \
  132. _vsnwprintf( string, size, format, __VA_ARGS__ )
  133. #elif defined( HAVE_VSWPRINTF )
  134. #define wide_string_vsnwprintf( string, size, format, ... ) \
  135. vswprintf( string, size, format, __VA_ARGS__ )
  136. #endif
  137. #if defined( __cplusplus )
  138. }
  139. #endif
  140. #endif /* !defined( _WIDE_STRING_H ) */