1
0

strings.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 *)calloc(size, sizeof(wchar_t));
  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, 0)))
  63. return 0;
  64. return (size - remaining);
  65. }
  66. char *
  67. AnsiString_Malloc(size_t size)
  68. {
  69. return (char*)calloc(size, sizeof(char));
  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. }