]> git.saurik.com Git - apple/icu.git/blame - icuSources/io/ustream.cpp
ICU-59173.0.1.tar.gz
[apple/icu.git] / icuSources / io / ustream.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955
A
3/*
4**********************************************************************
2ca993e8 5* Copyright (C) 2001-2016, International Business Machines
374ca955
A
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
374ca955 17#include "unicode/utypes.h"
b331163b
A
18
19#if !UCONFIG_NO_CONVERSION
20
374ca955
A
21#include "unicode/uobject.h"
22#include "unicode/ustream.h"
23#include "unicode/ucnv.h"
24#include "unicode/uchar.h"
4388f060 25#include "unicode/utf16.h"
374ca955 26#include "ustr_cnv.h"
729e4ab9 27#include "cmemory.h"
374ca955
A
28#include <string.h>
29
30// console IO
31
374ca955 32#if U_IOSTREAM_SOURCE >= 199711
4388f060 33
374ca955 34#define STD_NAMESPACE std::
374ca955
A
35
36#define STD_OSTREAM STD_NAMESPACE ostream
37#define STD_ISTREAM STD_NAMESPACE istream
38
39U_NAMESPACE_BEGIN
40
41U_IO_API STD_OSTREAM & U_EXPORT2
42operator<<(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();
729e4ab9 54 char *s, *sLimit = buffer + (sizeof(buffer) - 1);
374ca955
A
55 do {
56 errorCode = U_ZERO_ERROR;
57 s = buffer;
58 ucnv_fromUnicode(converter, &s, sLimit, &us, uLimit, 0, FALSE, &errorCode);
729e4ab9 59 *s = 0;
374ca955
A
60
61 // write this chunk
62 if(s > buffer) {
729e4ab9 63 stream << buffer;
374ca955
A
64 }
65 } while(errorCode == U_BUFFER_OVERFLOW_ERROR);
66 u_releaseDefaultConverter(converter);
67 }
68 }
69
70/* stream.flush();*/
71 return stream;
72}
73
74U_IO_API STD_ISTREAM & U_EXPORT2
75operator>>(STD_ISTREAM& stream, UnicodeString& str)
76{
46f4442e
A
77 // This is like ICU status checking.
78 if (stream.fail()) {
79 return stream;
80 }
81
374ca955
A
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
374ca955
A
89 // use the default converter to convert chunks of text
90 converter = u_getDefaultConverter(&errorCode);
91 if(U_SUCCESS(errorCode)) {
92 UChar *us = uBuffer;
2ca993e8 93 const UChar *uLimit = uBuffer + UPRV_LENGTHOF(uBuffer);
374ca955
A
94 const char *s, *sLimit;
95 char ch;
96 UChar ch32;
46f4442e
A
97 UBool initialWhitespace = TRUE;
98 UBool continueReading = TRUE;
374ca955
A
99
100 /* We need to consume one byte at a time to see what is considered whitespace. */
46f4442e 101 while (continueReading) {
374ca955 102 ch = stream.get();
46f4442e
A
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;
374ca955
A
111 us = uBuffer;
112 s = &ch;
46f4442e
A
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);
374ca955 120 if(U_FAILURE(errorCode)) {
46f4442e
A
121 /* Something really bad happened. setstate() isn't always an available API */
122 stream.clear(stream.failbit);
123 goto STOP_READING;
374ca955
A
124 }
125 /* Was the character consumed? */
126 if (us != uBuffer) {
73c04bcf
A
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)) {
46f4442e 133 if (!initialWhitespace) {
73c04bcf
A
134 buffer[idx++] = ch;
135 while (idx > 0) {
136 stream.putback(buffer[--idx]);
137 }
138 goto STOP_READING;
374ca955 139 }
73c04bcf
A
140 /* else skip intialWhitespace */
141 }
142 else {
46f4442e
A
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 }
73c04bcf 153 str.append(ch32);
374ca955 154 }
374ca955
A
155 }
156 idx = 0;
157 }
158 else {
159 buffer[idx++] = ch;
160 }
161 }
73c04bcf 162STOP_READING:
374ca955
A
163 u_releaseDefaultConverter(converter);
164 }
165
166/* stream.flush();*/
167 return stream;
168}
169
170U_NAMESPACE_END
171
172#endif
b331163b 173#endif