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