1
0

NDEString.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "NDEString.h"
  2. #include "foundation/error.h"
  3. #include <windows.h>
  4. typedef BOOL (WINAPI *HEAPSETINFORMATION)(HANDLE HeapHandle, HEAP_INFORMATION_CLASS HeapInformationClass, PVOID HeapInformation, SIZE_T HeapInformationLength);
  5. static HANDLE string_heap=0;
  6. static HMODULE kernel=0;
  7. extern "C" void NDE_HeapInit()
  8. {
  9. if (!string_heap)
  10. {
  11. string_heap=HeapCreate(0, 0, 0);
  12. kernel = LoadLibraryW(L"kernel32.dll");
  13. if (kernel)
  14. {
  15. HEAPSETINFORMATION hsi = (HEAPSETINFORMATION)GetProcAddress(kernel, "HeapSetInformation");
  16. if (hsi)
  17. {
  18. ULONG argh = 2;
  19. hsi(string_heap, HeapCompatibilityInformation, &argh, sizeof(ULONG));
  20. }
  21. }
  22. NXStringSetHeap(string_heap);
  23. }
  24. }
  25. extern "C" void NDE_HeapQuit()
  26. {
  27. if (kernel)
  28. FreeLibrary(kernel);
  29. }
  30. nx_string_t ndestring_get_string(wchar_t *str)
  31. {
  32. if (!str)
  33. return 0;
  34. nx_string_t self = (nx_string_t)((uint8_t *)str - sizeof(size_t) - sizeof(size_t));
  35. return self;
  36. }
  37. wchar_t *ndestring_wcsdup(const wchar_t *str)
  38. {
  39. NDE_HeapInit();
  40. if (!str)
  41. return 0;
  42. nx_string_t value;
  43. if (NXStringCreateWithUTF16(&value, str) != NErr_Success)
  44. return 0;
  45. return value->string;
  46. }
  47. wchar_t *ndestring_wcsndup(const wchar_t *str, size_t len)
  48. {
  49. NDE_HeapInit();
  50. if (!str)
  51. return 0;
  52. nx_string_t value;
  53. if (NXStringCreateWithBytes(&value, str, len*2, nx_charset_utf16le) != NErr_Success)
  54. return 0;
  55. return value->string;
  56. }
  57. wchar_t *ndestring_malloc(size_t str_size)
  58. {
  59. NDE_HeapInit();
  60. nx_string_t value=NXStringMalloc((str_size+1)/2 - 1);
  61. if (!value)
  62. return 0;
  63. return value->string;
  64. }
  65. void ndestring_release(wchar_t *str)
  66. {
  67. nx_string_t value = ndestring_get_string(str);
  68. NXStringRelease(value);
  69. }
  70. void ndestring_retain(wchar_t *str)
  71. {
  72. nx_string_t value = ndestring_get_string(str);
  73. NXStringRetain(value);
  74. }