strings.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. char *
  67. AnsiString_Malloc(size_t size)
  68. {
  69. return (char*)malloc(sizeof(char) * size);
  70. }
  71. char *
  72. AnsiString_ReAlloc(char *string, size_t size)
  73. {
  74. return (char*)realloc(string, sizeof(char) * size);
  75. }
  76. void
  77. AnsiString_Free(char *string)
  78. {
  79. if (NULL != string)
  80. free(string);
  81. }
  82. char *
  83. AnsiString_Duplicate(const char *string)
  84. {
  85. char *copy;
  86. INT length;
  87. if (NULL == string)
  88. return NULL;
  89. length = lstrlenA(string) + 1;
  90. copy = AnsiString_Malloc(length);
  91. if (NULL != copy)
  92. CopyMemory(copy, string, sizeof(char) * length);
  93. return copy;
  94. }
  95. wchar_t *
  96. AnsiString_ToUnicode(unsigned int codePage, unsigned long flags, const char* string, INT stringLength)
  97. {
  98. wchar_t *buffer;
  99. int buffferSize;
  100. if (NULL == string)
  101. return NULL;
  102. buffferSize = MultiByteToWideChar(codePage, flags, string, stringLength, NULL, 0);
  103. if (0 == buffferSize)
  104. return NULL;
  105. if (stringLength > 0)
  106. buffferSize++;
  107. buffer = String_Malloc(buffferSize);
  108. if (NULL == buffer)
  109. return NULL;
  110. if (0 == MultiByteToWideChar(codePage, flags, string, stringLength, buffer, buffferSize))
  111. {
  112. String_Free(buffer);
  113. return NULL;
  114. }
  115. if (stringLength > 0)
  116. buffer[buffferSize - 1] = L'\0';
  117. return buffer;
  118. }
  119. wchar_t*
  120. ResourceString_Duplicate(const wchar_t *source)
  121. {
  122. return (FALSE != IS_INTRESOURCE(source)) ?
  123. (LPWSTR)source :
  124. String_Duplicate(source);
  125. }
  126. void
  127. ResourceString_Free(wchar_t *string)
  128. {
  129. if (FALSE == IS_INTRESOURCE(string))
  130. String_Free(string);
  131. }
  132. size_t
  133. ResourceString_CopyTo(wchar_t *destination, const wchar_t *source, size_t size)
  134. {
  135. if (NULL == destination)
  136. return 0;
  137. if (NULL == source)
  138. {
  139. destination[0] = L'\0';
  140. return 0;
  141. }
  142. if (FALSE != IS_INTRESOURCE(source))
  143. {
  144. if (NULL == WASABI_API_LNG)
  145. {
  146. destination[0] = L'\0';
  147. return 0;
  148. }
  149. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)source, destination, size);
  150. return lstrlenW(destination);
  151. }
  152. return String_CopyTo(destination, source, size);
  153. }