]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ustream.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2001-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * FILE NAME : ustream.cpp
10 * Modification History:
12 * Date Name Description
13 * 06/25/2001 grhoten Move iostream from unistr.h to here
14 ******************************************************************************
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_CONVERSION
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"
32 #if U_IOSTREAM_SOURCE >= 199711
34 #define STD_NAMESPACE std::
36 #define STD_OSTREAM STD_NAMESPACE ostream
37 #define STD_ISTREAM STD_NAMESPACE istream
41 U_IO_API STD_OSTREAM
& U_EXPORT2
42 operator<<(STD_OSTREAM
& stream
, const UnicodeString
& str
)
44 if(str
.length() > 0) {
46 UConverter
*converter
;
47 UErrorCode errorCode
= U_ZERO_ERROR
;
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);
56 errorCode
= U_ZERO_ERROR
;
58 ucnv_fromUnicode(converter
, &s
, sLimit
, &us
, uLimit
, 0, FALSE
, &errorCode
);
65 } while(errorCode
== U_BUFFER_OVERFLOW_ERROR
);
66 u_releaseDefaultConverter(converter
);
74 U_IO_API STD_ISTREAM
& U_EXPORT2
75 operator>>(STD_ISTREAM
& stream
, UnicodeString
& str
)
77 // This is like ICU status checking.
82 /* ipfx should eat whitespace when ios::skipws is set */
86 UConverter
*converter
;
87 UErrorCode errorCode
= U_ZERO_ERROR
;
89 // use the default converter to convert chunks of text
90 converter
= u_getDefaultConverter(&errorCode
);
91 if(U_SUCCESS(errorCode
)) {
93 const UChar
*uLimit
= uBuffer
+ UPRV_LENGTHOF(uBuffer
);
94 const char *s
, *sLimit
;
97 UBool initialWhitespace
= TRUE
;
98 UBool continueReading
= TRUE
;
100 /* We need to consume one byte at a time to see what is considered whitespace. */
101 while (continueReading
) {
104 // The EOF is only set after the get() of an unavailable byte.
105 if (!initialWhitespace
) {
106 stream
.clear(stream
.eofbit
);
108 continueReading
= FALSE
;
110 sLimit
= &ch
+ (int)continueReading
;
113 errorCode
= U_ZERO_ERROR
;
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.
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
);
125 /* Was the character consumed? */
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
) {
136 stream
.putback(buffer
[--idx
]);
140 /* else skip intialWhitespace */
143 if (initialWhitespace
) {
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.
151 initialWhitespace
= FALSE
;
163 u_releaseDefaultConverter(converter
);