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