AutoCharNX.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include "nx/nxstring.h"
  3. #include "nx/nxuri.h"
  4. #include "foundation/error.h"
  5. template <nx_charset_t charset>
  6. class AutoCharNX
  7. {
  8. public:
  9. AutoCharNX()
  10. {
  11. Init();
  12. }
  13. AutoCharNX(size_t bytes)
  14. {
  15. Init();
  16. ptr = (char *)malloc(bytes);
  17. malloc_size = bytes;
  18. }
  19. AutoCharNX(nx_string_t string)
  20. {
  21. Init();
  22. Set(string);
  23. }
  24. AutoCharNX(nx_uri_t filename)
  25. {
  26. Init();
  27. Set(filename);
  28. }
  29. ~AutoCharNX()
  30. {
  31. if (owned)
  32. free(ptr);
  33. if (reference_string)
  34. NXStringRelease(reference_string);
  35. }
  36. int Set(nx_string_t string)
  37. {
  38. if (reference_string == string)
  39. return NErr_Success;
  40. if (reference_string)
  41. NXStringRelease(reference_string);
  42. reference_string=0;
  43. size_t byte_count=0;
  44. int ret = NXStringGetBytesSize(&byte_count, string, charset, nx_string_get_bytes_size_null_terminate);
  45. if(ret == NErr_DirectPointer)
  46. {
  47. if (owned)
  48. {
  49. free(ptr);
  50. ptr=0;
  51. length=0;
  52. malloc_size=0;
  53. }
  54. ret = NXStringGetBytesDirect((const void **)&ptr, &length, string, charset, nx_string_get_bytes_size_null_terminate);
  55. reference_string = NXStringRetain(string);
  56. owned=false;
  57. }
  58. else if (ret == NErr_Success)
  59. {
  60. if (owned)
  61. {
  62. if (byte_count > malloc_size)
  63. {
  64. ptr = (char *)realloc(ptr, byte_count);
  65. malloc_size = byte_count;
  66. }
  67. }
  68. else
  69. {
  70. /* not owned. need to allocate */
  71. ptr = (char *)malloc(byte_count);
  72. malloc_size = byte_count;
  73. owned=true;
  74. }
  75. if (ptr)
  76. {
  77. ret = NXStringGetBytes(&length, string, ptr, byte_count, charset, nx_string_get_bytes_size_null_terminate);
  78. }
  79. else
  80. {
  81. return NErr_OutOfMemory;
  82. }
  83. }
  84. else
  85. {
  86. Clear();
  87. }
  88. return ret;
  89. }
  90. int Set(nx_uri_t filename)
  91. {
  92. int ret;
  93. nx_string_t string;
  94. ret = NXURIGetNXString(&string, filename);
  95. if (ret == NErr_Success)
  96. {
  97. ret = Set(string);
  98. NXStringRelease(string);
  99. }
  100. else
  101. {
  102. Clear();
  103. // failed! we need to clean up
  104. }
  105. return ret;
  106. }
  107. operator const char *() const
  108. {
  109. if (length)
  110. return ptr;
  111. else
  112. return 0;
  113. }
  114. /* this one will never return a NULL, always a valid string */
  115. const char *GetValidString() const
  116. {
  117. if (length)
  118. return ptr;
  119. else
  120. return "";
  121. }
  122. /* the Clear function clears the string but doesn't deallocate memory */
  123. void Clear()
  124. {
  125. if (!owned)
  126. ptr=0;
  127. length=0;
  128. if (reference_string)
  129. NXStringRelease(reference_string);
  130. reference_string=0;
  131. }
  132. private:
  133. void Init()
  134. {
  135. ptr=0;
  136. length=0;
  137. owned=false;
  138. reference_string=0;
  139. malloc_size=0;
  140. }
  141. char *ptr;
  142. size_t length;
  143. size_t malloc_size;
  144. bool owned;
  145. nx_string_t reference_string;
  146. };
  147. typedef AutoCharNX<nx_charset_utf8> AutoCharUTF8;
  148. #define AutoCharPrintfUTF8(x) (AutoCharUTF8(x).GetValidString())
  149. class AutoCharNative
  150. {
  151. public:
  152. };
  153. class AutoFilename
  154. {
  155. public:
  156. };