AutoCharNX.h 2.8 KB

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