1
0

encodedstr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "precomp_wasabi_bfc.h"
  2. // someday, there will be a file here.
  3. #include <bfc/wasabi_std.h>
  4. #include "encodedstr.h"
  5. #include <bfc/string/bfcstring.h>
  6. #include <api/service/svcs/svc_stringconverter.h>
  7. #include <api/memmgr/api_memmgr.h>
  8. EncodedStr::EncodedStr(FOURCC encodingType, void *encodedBuffer, int bufferSize, int deleteIt) {
  9. encoding_type = encodingType;
  10. encoded_buffer = encodedBuffer;
  11. buffer_size = bufferSize;
  12. delete_it = deleteIt;
  13. }
  14. EncodedStr::~EncodedStr() {
  15. if (delete_it && (encoded_buffer != NULL)) {
  16. #ifdef WASABI_COMPILE_MEMMGR
  17. WASABI_API_MEMMGR->sysFree(encoded_buffer);
  18. #else
  19. free(encoded_buffer);
  20. #endif
  21. }
  22. }
  23. void EncodedStr::resetBuffer(FOURCC encodingType, void *encodedBuffer, int bufferSize, int deleteIt) {
  24. // if there's someone already there, toss them.
  25. if (delete_it && (encoded_buffer != NULL)) {
  26. #ifdef WASABI_COMPILE_MEMMGR
  27. WASABI_API_MEMMGR->sysFree(encoded_buffer);
  28. #else
  29. free(encoded_buffer);
  30. #endif
  31. }
  32. encoding_type = encodingType;
  33. encoded_buffer = encodedBuffer;
  34. buffer_size = bufferSize;
  35. delete_it = deleteIt;
  36. }
  37. int EncodedStr::convertToUTF8(String &output_str) {
  38. int retval = 0;
  39. StringConverterEnum myServiceEnum(encoding_type);
  40. svc_stringConverter *myConv = myServiceEnum.getFirst();
  41. if (myConv != NULL) {
  42. void *in_buffer = encoded_buffer;
  43. int size_in_bytes = buffer_size;
  44. if (encoded_buffer != NULL) {
  45. // Preflight
  46. int u8size = myConv->preflightToUTF8(encoding_type, in_buffer, size_in_bytes);
  47. // Alloc
  48. #ifdef WASABI_COMPILE_MEMMGR
  49. char *u8str = reinterpret_cast<char *>(WASABI_API_MEMMGR->sysMalloc(u8size));
  50. #else
  51. char *u8str = reinterpret_cast<char *>(MALLOC(u8size));
  52. #endif
  53. if (u8str != NULL) {
  54. // Convert
  55. retval = myConv->convertToUTF8(encoding_type, in_buffer, size_in_bytes, u8str, u8size);
  56. if (retval < 0) {
  57. // Clear on error.
  58. #ifdef WASABI_COMPILE_MEMMGR
  59. WASABI_API_MEMMGR->sysFree(u8str);
  60. #else
  61. free(encoded_buffer);
  62. #endif
  63. u8str = NULL;
  64. }
  65. } else {
  66. ASSERTPR(u8str != NULL, "Malloc failed in string converter\n");
  67. }
  68. // And call the method to inject the pointer into our string (cleared on error).
  69. output_str.setValue(u8str);
  70. }
  71. // Once we use our service, release our locked instance of it.
  72. myServiceEnum.release(myConv);
  73. } else {
  74. // Clear the string on error.
  75. retval = SvcStrCnv::ERROR_UNAVAILABLE;
  76. output_str.setValue(NULL);
  77. }
  78. return retval;
  79. }
  80. int EncodedStr::convertFromUTF8(FOURCC encodingType, const String &inputStr) {
  81. int retval = 0;
  82. int written = 0;
  83. void *buffer = NULL;
  84. StringConverterEnum myServiceEnum(encodingType);
  85. svc_stringConverter *myConv = myServiceEnum.getFirst();
  86. if (myConv != NULL) {
  87. if (inputStr != NULL) {
  88. const char *val = inputStr.getValue();
  89. int valsize = STRLEN(val) + 1; // count the null char in your size-in-bytes!
  90. // Preflight
  91. int size = myConv->preflightFromUTF8(encodingType, val, valsize);
  92. if (size > 0) {
  93. // Alloc
  94. #ifdef WASABI_COMPILE_MEMMGR
  95. buffer = WASABI_API_MEMMGR->sysMalloc(size);
  96. #else
  97. buffer = MALLOC(size);
  98. #endif
  99. if (buffer != NULL) {
  100. // Convert
  101. written = myConv->convertFromUTF8(encodingType, val, valsize, buffer, size);
  102. if (written > 0) {
  103. retval = written;
  104. } else {
  105. // Clear on error.
  106. #ifdef WASABI_COMPILE_MEMMGR
  107. WASABI_API_MEMMGR->sysFree(buffer);
  108. #else
  109. free(buffer);
  110. #endif
  111. buffer = NULL;
  112. retval = written;
  113. written = 0;
  114. }
  115. } else {
  116. ASSERTPR(buffer != NULL, "Malloc failed in string converter\n");
  117. }
  118. } else {
  119. // Clear on error.
  120. buffer = NULL;
  121. retval = size;
  122. written = 0;
  123. }
  124. }
  125. // Once we use our service, release our locked instance of it.
  126. myServiceEnum.release(myConv);
  127. } else {
  128. // On error locking down a service, all the default values are errors and called through resetBuffer.
  129. retval = SvcStrCnv::ERROR_UNAVAILABLE;
  130. }
  131. resetBuffer(encodingType, buffer, written);
  132. return retval;
  133. }
  134. // This is for debugging.
  135. int EncodedStr::operator ==(const EncodedStr &in_string) {
  136. if (encoding_type == in_string.encoding_type) {
  137. switch (encoding_type) {
  138. case SvcStrCnv::OSNATIVE:
  139. return (STRCMP(reinterpret_cast<char *>(encoded_buffer), reinterpret_cast<char *>(in_string.encoded_buffer)) == 0);
  140. break;
  141. default:
  142. return 0;
  143. break;
  144. }
  145. }
  146. return 0;
  147. }