strings.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "main.h"
  2. #include "./strings.h"
  3. #include <strsafe.h>
  4. wchar_t *
  5. String_Malloc(size_t size)
  6. {
  7. return (wchar_t *)malloc(sizeof(wchar_t) * size);
  8. }
  9. wchar_t *
  10. String_ReAlloc(wchar_t *string, size_t size)
  11. {
  12. return (wchar_t *)realloc(string, sizeof(wchar_t) * size);
  13. }
  14. void
  15. String_Free(wchar_t *string)
  16. {
  17. if (NULL != string)
  18. free(string);
  19. }
  20. wchar_t *
  21. String_Duplicate(const wchar_t *string)
  22. {
  23. int length;
  24. wchar_t *copy;
  25. if (NULL == string)
  26. return NULL;
  27. length = lstrlenW(string) + 1;
  28. copy = String_Malloc(length);
  29. if (NULL != copy)
  30. CopyMemory(copy, string, sizeof(wchar_t) * length);
  31. return copy;
  32. }
  33. char *
  34. String_ToAnsi(unsigned int codePage, unsigned long flags, const wchar_t *string,
  35. int stringLength, const char *defaultChar, BOOL *usedDefaultChar)
  36. {
  37. char *buffer;
  38. int bufferSize;
  39. if (stringLength < 0)
  40. stringLength = lstrlen(string);
  41. bufferSize = WideCharToMultiByte(codePage, flags, string, stringLength,
  42. NULL, 0, defaultChar, usedDefaultChar);
  43. if (0 == bufferSize)
  44. return NULL;
  45. buffer = AnsiString_Malloc(bufferSize + 1);
  46. if (NULL == buffer)
  47. return NULL;
  48. bufferSize = WideCharToMultiByte(codePage, flags, string, stringLength,
  49. buffer, bufferSize, defaultChar, usedDefaultChar);
  50. if (0 == bufferSize)
  51. {
  52. AnsiString_Free(buffer);
  53. return NULL;
  54. }
  55. buffer[bufferSize] = '\0';
  56. return buffer;
  57. }
  58. size_t
  59. String_CopyTo(wchar_t *destination, const wchar_t *source, size_t size)
  60. {
  61. size_t remaining;
  62. if (FAILED(StringCchCopyExW(destination, size, source, NULL, &remaining, STRSAFE_IGNORE_NULLS)))
  63. return 0;
  64. return (size - remaining);
  65. }
  66. wchar_t *
  67. String_FromWindowEx(HWND hwnd, size_t *lengthOut, BOOL *errorOut)
  68. {
  69. BOOL error;
  70. size_t length;
  71. wchar_t *string;
  72. error = TRUE;
  73. string = NULL;
  74. length = 0;
  75. if (NULL != hwnd)
  76. {
  77. length = GetWindowTextLength(hwnd);
  78. if (0 != length ||
  79. ERROR_SUCCESS == GetLastError())
  80. {
  81. string = String_Malloc(length + 1);
  82. if(NULL != string)
  83. {
  84. if (0 == length)
  85. {
  86. string[0] = L'\0';
  87. error = FALSE;
  88. }
  89. else
  90. {
  91. length = GetWindowText(hwnd, string, (int)length + 1);
  92. if (0 == length && ERROR_SUCCESS != GetLastError())
  93. {
  94. String_Free(string);
  95. string = NULL;
  96. }
  97. else
  98. error = FALSE;
  99. }
  100. }
  101. }
  102. }
  103. if (NULL != lengthOut)
  104. *lengthOut = length;
  105. if (NULL != errorOut)
  106. *errorOut = error;
  107. return string;
  108. }
  109. char *
  110. AnsiString_Malloc(size_t size)
  111. {
  112. return (char*)malloc(sizeof(char) * size);
  113. }
  114. char *
  115. AnsiString_ReAlloc(char *string, size_t size)
  116. {
  117. return (char*)realloc(string, sizeof(char) * size);
  118. }
  119. void
  120. AnsiString_Free(char *string)
  121. {
  122. if (NULL != string)
  123. free(string);
  124. }
  125. char *
  126. AnsiString_Duplicate(const char *string)
  127. {
  128. char *copy;
  129. INT length;
  130. if (NULL == string)
  131. return NULL;
  132. length = lstrlenA(string) + 1;
  133. copy = AnsiString_Malloc(length);
  134. if (NULL != copy)
  135. CopyMemory(copy, string, sizeof(char) * length);
  136. return copy;
  137. }
  138. wchar_t *
  139. AnsiString_ToUnicode(unsigned int codePage, unsigned long flags, const char* string, INT stringLength)
  140. {
  141. wchar_t *buffer;
  142. int buffferSize;
  143. if (NULL == string)
  144. return NULL;
  145. buffferSize = MultiByteToWideChar(codePage, flags, string, stringLength, NULL, 0);
  146. if (0 == buffferSize)
  147. return NULL;
  148. if (stringLength > 0)
  149. buffferSize++;
  150. buffer = String_Malloc(buffferSize);
  151. if (NULL == buffer)
  152. return NULL;
  153. if (0 == MultiByteToWideChar(codePage, flags, string, stringLength, buffer, buffferSize))
  154. {
  155. String_Free(buffer);
  156. return NULL;
  157. }
  158. if (stringLength > 0)
  159. buffer[buffferSize - 1] = L'\0';
  160. return buffer;
  161. }
  162. wchar_t*
  163. ResourceString_Duplicate(const wchar_t *source)
  164. {
  165. return (FALSE != IS_INTRESOURCE(source)) ?
  166. (LPWSTR)source :
  167. String_Duplicate(source);
  168. }
  169. void
  170. ResourceString_Free(wchar_t *string)
  171. {
  172. if (FALSE == IS_INTRESOURCE(string))
  173. String_Free(string);
  174. }
  175. size_t
  176. ResourceString_CopyTo(wchar_t *destination, const wchar_t *source, size_t size)
  177. {
  178. if (NULL == destination)
  179. return 0;
  180. if (NULL == source)
  181. {
  182. destination[0] = L'\0';
  183. return 0;
  184. }
  185. if (FALSE != IS_INTRESOURCE(source))
  186. {
  187. if (NULL == WASABI_API_LNG)
  188. {
  189. destination[0] = L'\0';
  190. return 0;
  191. }
  192. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)source, destination, size);
  193. return lstrlenW(destination);
  194. }
  195. return String_CopyTo(destination, source, size);
  196. }