ustream.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2001-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * FILE NAME : ustream.cpp
  9. *
  10. * Modification History:
  11. *
  12. * Date Name Description
  13. * 06/25/2001 grhoten Move iostream from unistr.h to here
  14. ******************************************************************************
  15. */
  16. #include "unicode/utypes.h"
  17. #if !UCONFIG_NO_CONVERSION
  18. #include "unicode/uobject.h"
  19. #include "unicode/ustream.h"
  20. #include "unicode/ucnv.h"
  21. #include "unicode/uchar.h"
  22. #include "unicode/utf16.h"
  23. #include "ustr_cnv.h"
  24. #include "cmemory.h"
  25. #include <string.h>
  26. // console IO
  27. #define STD_NAMESPACE std::
  28. #define STD_OSTREAM STD_NAMESPACE ostream
  29. #define STD_ISTREAM STD_NAMESPACE istream
  30. U_NAMESPACE_BEGIN
  31. U_IO_API STD_OSTREAM & U_EXPORT2
  32. operator<<(STD_OSTREAM& stream, const UnicodeString& str)
  33. {
  34. if(str.length() > 0) {
  35. char buffer[200];
  36. UConverter *converter;
  37. UErrorCode errorCode = U_ZERO_ERROR;
  38. // use the default converter to convert chunks of text
  39. converter = u_getDefaultConverter(&errorCode);
  40. if(U_SUCCESS(errorCode)) {
  41. const char16_t *us = str.getBuffer();
  42. const char16_t *uLimit = us + str.length();
  43. char *s, *sLimit = buffer + (sizeof(buffer) - 1);
  44. do {
  45. errorCode = U_ZERO_ERROR;
  46. s = buffer;
  47. ucnv_fromUnicode(converter, &s, sLimit, &us, uLimit, nullptr, false, &errorCode);
  48. *s = 0;
  49. // write this chunk
  50. if(s > buffer) {
  51. stream << buffer;
  52. }
  53. } while(errorCode == U_BUFFER_OVERFLOW_ERROR);
  54. u_releaseDefaultConverter(converter);
  55. }
  56. }
  57. /* stream.flush();*/
  58. return stream;
  59. }
  60. U_IO_API STD_ISTREAM & U_EXPORT2
  61. operator>>(STD_ISTREAM& stream, UnicodeString& str)
  62. {
  63. // This is like ICU status checking.
  64. if (stream.fail()) {
  65. return stream;
  66. }
  67. /* ipfx should eat whitespace when ios::skipws is set */
  68. char16_t uBuffer[16];
  69. char buffer[16];
  70. int32_t idx = 0;
  71. UConverter *converter;
  72. UErrorCode errorCode = U_ZERO_ERROR;
  73. // use the default converter to convert chunks of text
  74. converter = u_getDefaultConverter(&errorCode);
  75. if(U_SUCCESS(errorCode)) {
  76. char16_t *us = uBuffer;
  77. const char16_t *uLimit = uBuffer + UPRV_LENGTHOF(uBuffer);
  78. const char *s, *sLimit;
  79. char ch;
  80. char16_t ch32;
  81. UBool initialWhitespace = true;
  82. UBool continueReading = true;
  83. /* We need to consume one byte at a time to see what is considered whitespace. */
  84. while (continueReading) {
  85. ch = stream.get();
  86. if (stream.eof()) {
  87. // The EOF is only set after the get() of an unavailable byte.
  88. if (!initialWhitespace) {
  89. stream.clear(stream.eofbit);
  90. }
  91. continueReading = false;
  92. }
  93. sLimit = &ch + (int)continueReading;
  94. us = uBuffer;
  95. s = &ch;
  96. errorCode = U_ZERO_ERROR;
  97. /*
  98. Since we aren't guaranteed to see the state before this call,
  99. this code won't work on stateful encodings like ISO-2022 or an EBCDIC stateful encoding.
  100. We flush on the last byte to ensure that we output truncated multibyte characters.
  101. */
  102. ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, nullptr, !continueReading, &errorCode);
  103. if(U_FAILURE(errorCode)) {
  104. /* Something really bad happened. setstate() isn't always an available API */
  105. stream.clear(stream.failbit);
  106. goto STOP_READING;
  107. }
  108. /* Was the character consumed? */
  109. if (us != uBuffer) {
  110. /* Reminder: ibm-1390 & JISX0213 can output 2 Unicode code points */
  111. int32_t uBuffSize = static_cast<int32_t>(us-uBuffer);
  112. int32_t uBuffIdx = 0;
  113. while (uBuffIdx < uBuffSize) {
  114. U16_NEXT(uBuffer, uBuffIdx, uBuffSize, ch32);
  115. if (u_isWhitespace(ch32)) {
  116. if (!initialWhitespace) {
  117. buffer[idx++] = ch;
  118. while (idx > 0) {
  119. stream.putback(buffer[--idx]);
  120. }
  121. goto STOP_READING;
  122. }
  123. /* else skip intialWhitespace */
  124. }
  125. else {
  126. if (initialWhitespace) {
  127. /*
  128. When initialWhitespace is true, we haven't appended any
  129. character yet. This is where we truncate the string,
  130. to avoid modifying the string before we know if we can
  131. actually read from the stream.
  132. */
  133. str.truncate(0);
  134. initialWhitespace = false;
  135. }
  136. str.append(ch32);
  137. }
  138. }
  139. idx = 0;
  140. }
  141. else {
  142. buffer[idx++] = ch;
  143. }
  144. }
  145. STOP_READING:
  146. u_releaseDefaultConverter(converter);
  147. }
  148. /* stream.flush();*/
  149. return stream;
  150. }
  151. U_NAMESPACE_END
  152. #endif