Audio plugin host https://kx.studio/carla
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.

fstrdefs.h 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //-----------------------------------------------------------------------------
  2. // Project : SDK Core
  3. //
  4. // Category : SDK Core Interfaces
  5. // Filename : pluginterfaces/base/fstrdefs.h
  6. // Created by : Steinberg, 01/2004
  7. // Description : Definitions for handling strings (Unicode / ASCII / Platforms)
  8. //
  9. //-----------------------------------------------------------------------------
  10. // This file is part of a Steinberg SDK. It is subject to the license terms
  11. // in the LICENSE file found in the top-level directory of this distribution
  12. // and at www.steinberg.net/sdklicenses.
  13. // No part of the SDK, including this file, may be copied, modified, propagated,
  14. // or distributed except according to the terms contained in the LICENSE file.
  15. //-----------------------------------------------------------------------------
  16. #pragma once
  17. #include "ftypes.h"
  18. //----------------------------------------------------------------------------
  19. /** string methods defines unicode / ASCII */
  20. //----------------------------------------------------------------------------
  21. // 16 bit string operations
  22. #if SMTG_CPP11 // if c++11 unicode string literals
  23. #define SMTG_CPP11_CAT_PRIVATE_DONT_USE(a,b) a ## b
  24. #if SMTG_OS_WINDOWS
  25. #define STR16(x) SMTG_CPP11_CAT_PRIVATE_DONT_USE(L,x)
  26. #else
  27. #define STR16(x) SMTG_CPP11_CAT_PRIVATE_DONT_USE(u,x)
  28. #endif
  29. #else
  30. #include "conststringtable.h"
  31. #define STR16(x) Steinberg::ConstStringTable::instance ()->getString (x)
  32. #endif
  33. #ifdef UNICODE
  34. #define STR(x) STR16(x)
  35. #define tStrBufferSize(buffer) (sizeof(buffer)/sizeof(Steinberg::tchar))
  36. #else
  37. #define STR(x) x
  38. #define tStrBufferSize(buffer) (sizeof(buffer))
  39. #endif
  40. #define str8BufferSize(buffer) (sizeof(buffer)/sizeof(Steinberg::char8))
  41. #define str16BufferSize(buffer) (sizeof(buffer)/sizeof(Steinberg::char16))
  42. #if SMTG_OS_WINDOWS
  43. #define FORMAT_INT64A "I64d"
  44. #define FORMAT_UINT64A "I64u"
  45. #elif SMTG_OS_MACOS || SMTG_OS_LINUX
  46. #define FORMAT_INT64A "lld"
  47. #define FORMAT_UINT64A "llu"
  48. #define stricmp strcasecmp
  49. #define strnicmp strncasecmp
  50. #endif
  51. #ifdef UNICODE
  52. #define FORMAT_INT64W STR(FORMAT_INT64A)
  53. #define FORMAT_UINT64W STR(FORMAT_UINT64A)
  54. #define FORMAT_INT64 FORMAT_INT64W
  55. #define FORMAT_UINT64 FORMAT_UINT64W
  56. #else
  57. #define FORMAT_INT64 FORMAT_INT64A
  58. #define FORMAT_UINT64 FORMAT_UINT64A
  59. #endif
  60. //----------------------------------------------------------------------------
  61. // newline
  62. //----------------------------------------------------------------------------
  63. #if SMTG_OS_WINDOWS
  64. #define ENDLINE_A "\r\n"
  65. #define ENDLINE_W STR ("\r\n")
  66. #elif SMTG_OS_MACOS
  67. #define ENDLINE_A "\r"
  68. #define ENDLINE_W STR ("\r")
  69. #elif SMTG_OS_LINUX
  70. #define ENDLINE_A "\n"
  71. #define ENDLINE_W STR ("\n")
  72. #endif
  73. #ifdef UNICODE
  74. #define ENDLINE ENDLINE_W
  75. #else
  76. #define ENDLINE ENDLINE_A
  77. #endif
  78. #if SMTG_OS_WINDOWS && !defined(__GNUC__) && defined(_MSC_VER) && (_MSC_VER < 1900)
  79. #define stricmp _stricmp
  80. #define strnicmp _strnicmp
  81. #define snprintf _snprintf
  82. #endif
  83. namespace Steinberg {
  84. //----------------------------------------------------------------------------
  85. static const tchar kEmptyString[] = { 0 };
  86. static const char8 kEmptyString8[] = { 0 };
  87. static const char16 kEmptyString16[] = { 0 };
  88. #ifdef UNICODE
  89. static const tchar kInfiniteSymbol[] = { 0x221E, 0 };
  90. #else
  91. static const tchar* const kInfiniteSymbol = STR ("oo");
  92. #endif
  93. //----------------------------------------------------------------------------
  94. template <class T>
  95. inline int32 _tstrlen (const T* wcs)
  96. {
  97. const T* eos = wcs;
  98. while (*eos++)
  99. ;
  100. return (int32) (eos - wcs - 1);
  101. }
  102. inline int32 tstrlen (const tchar* str) {return _tstrlen (str);}
  103. inline int32 strlen8 (const char8* str) {return _tstrlen (str);}
  104. inline int32 strlen16 (const char16* str) {return _tstrlen (str);}
  105. //----------------------------------------------------------------------------
  106. template <class T>
  107. inline int32 _tstrcmp (const T* src, const T* dst)
  108. {
  109. while (*src == *dst && *dst)
  110. {
  111. src++;
  112. dst++;
  113. }
  114. if (*src == 0 && *dst == 0)
  115. return 0;
  116. else if (*src == 0)
  117. return -1;
  118. else if (*dst == 0)
  119. return 1;
  120. else
  121. return (int32) (*src - *dst);
  122. }
  123. inline int32 tstrcmp (const tchar* src, const tchar* dst) {return _tstrcmp (src, dst);}
  124. inline int32 strcmp8 (const char8* src, const char8* dst) {return _tstrcmp (src, dst);}
  125. inline int32 strcmp16 (const char16* src, const char16* dst) {return _tstrcmp (src, dst);}
  126. template <typename T>
  127. inline int32 strcmpT (const T* first, const T* last);
  128. template <>
  129. inline int32 strcmpT<char8> (const char8* first, const char8* last) { return _tstrcmp (first, last); }
  130. template <>
  131. inline int32 strcmpT<char16> (const char16* first, const char16* last) { return _tstrcmp (first, last); }
  132. //----------------------------------------------------------------------------
  133. template <class T>
  134. inline int32 _tstrncmp (const T* first, const T* last, uint32 count)
  135. {
  136. if (count == 0)
  137. return 0;
  138. while (--count && *first && *first == *last)
  139. {
  140. first++;
  141. last++;
  142. }
  143. if (*first == 0 && *last == 0)
  144. return 0;
  145. else if (*first == 0)
  146. return -1;
  147. else if (*last == 0)
  148. return 1;
  149. else
  150. return (int32) (*first - *last);
  151. }
  152. inline int32 tstrncmp (const tchar* first, const tchar* last, uint32 count) {return _tstrncmp (first, last, count);}
  153. inline int32 strncmp8 (const char8* first, const char8* last, uint32 count) {return _tstrncmp (first, last, count);}
  154. inline int32 strncmp16 (const char16* first, const char16* last, uint32 count) {return _tstrncmp (first, last, count);}
  155. template <typename T>
  156. inline int32 strncmpT (const T* first, const T* last, uint32 count);
  157. template <>
  158. inline int32 strncmpT<char8> (const char8* first, const char8* last, uint32 count) { return _tstrncmp (first, last, count); }
  159. template <>
  160. inline int32 strncmpT<char16> (const char16* first, const char16* last, uint32 count) {return _tstrncmp (first, last, count); }
  161. //----------------------------------------------------------------------------
  162. template <class T>
  163. inline T* _tstrcpy (T* dst, const T* src)
  164. {
  165. T* cp = dst;
  166. while ((*cp++ = *src++) != 0) // copy string
  167. ;
  168. return dst;
  169. }
  170. inline tchar* tstrcpy (tchar* dst, const tchar* src) {return _tstrcpy (dst, src);}
  171. inline char8* strcpy8 (char8* dst, const char8* src) {return _tstrcpy (dst, src);}
  172. inline char16* strcpy16 (char16* dst, const char16* src) {return _tstrcpy (dst, src);}
  173. //----------------------------------------------------------------------------
  174. template <class T>
  175. inline T* _tstrncpy (T* dest, const T* source, uint32 count)
  176. {
  177. T* start = dest;
  178. while (count && (*dest++ = *source++) != 0) // copy string
  179. count--;
  180. if (count) // pad out with zeros
  181. {
  182. while (--count)
  183. *dest++ = 0;
  184. }
  185. return start;
  186. }
  187. inline tchar* tstrncpy (tchar* dest, const tchar* source, uint32 count) {return _tstrncpy (dest, source, count);}
  188. inline char8* strncpy8 (char8* dest, const char8* source, uint32 count) {return _tstrncpy (dest, source, count);}
  189. inline char16* strncpy16 (char16* dest, const char16* source, uint32 count) {return _tstrncpy (dest, source, count);}
  190. //----------------------------------------------------------------------------
  191. template <class T>
  192. inline T* _tstrcat (T* dst, const T* src)
  193. {
  194. T* cp = dst;
  195. while (*cp)
  196. cp++; // find end of dst
  197. while ((*cp++ = *src++) != 0) // Copy src to end of dst
  198. ;
  199. return dst;
  200. }
  201. inline tchar* tstrcat (tchar* dst, const tchar* src) {return _tstrcat (dst, src); }
  202. inline char8* strcat8 (char8* dst, const char8* src) {return _tstrcat (dst, src); }
  203. inline char16* strcat16 (char16* dst, const char16* src) {return _tstrcat (dst, src); }
  204. //----------------------------------------------------------------------------
  205. inline void str8ToStr16 (char16* dst, const char8* src, int32 n = -1)
  206. {
  207. int32 i = 0;
  208. for (;;)
  209. {
  210. if (i == n)
  211. {
  212. dst[i] = 0;
  213. return;
  214. }
  215. #if BYTEORDER == kBigEndian
  216. char8* pChr = (char8*)&dst[i];
  217. pChr[0] = 0;
  218. pChr[1] = src[i];
  219. #else
  220. dst[i] = static_cast<char16> (src[i]);
  221. #endif
  222. if (src[i] == 0)
  223. break;
  224. i++;
  225. }
  226. while (n > i)
  227. {
  228. dst[i] = 0;
  229. i++;
  230. }
  231. }
  232. //------------------------------------------------------------------------
  233. inline bool FIDStringsEqual (FIDString id1, FIDString id2)
  234. {
  235. return (id1 && id2) ? (strcmp8 (id1, id2) == 0) : false;
  236. }
  237. static const uint32 kPrintfBufferSize = 4096;
  238. //------------------------------------------------------------------------
  239. } // namespace Steinberg