]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ustream.cpp
2 **********************************************************************
3 * Copyright (C) 2001-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * FILE NAME : ustream.cpp
8 * Modification History:
10 * Date Name Description
11 * 06/25/2001 grhoten Move iostream from unistr.h to here
12 ******************************************************************************
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_CONVERSION
19 #include "unicode/uobject.h"
20 #include "unicode/ustream.h"
21 #include "unicode/ucnv.h"
22 #include "unicode/uchar.h"
23 #include "unicode/utf16.h"
30 #if U_IOSTREAM_SOURCE >= 199711
32 #define STD_NAMESPACE std::
34 #define STD_OSTREAM STD_NAMESPACE ostream
35 #define STD_ISTREAM STD_NAMESPACE istream
39 U_IO_API STD_OSTREAM
& U_EXPORT2
40 operator<<(STD_OSTREAM
& stream
, const UnicodeString
& str
)
42 if(str
.length() > 0) {
44 UConverter
*converter
;
45 UErrorCode errorCode
= U_ZERO_ERROR
;
47 // use the default converter to convert chunks of text
48 converter
= u_getDefaultConverter(&errorCode
);
49 if(U_SUCCESS(errorCode
)) {
50 const UChar
*us
= str
.getBuffer();
51 const UChar
*uLimit
= us
+ str
.length();
52 char *s
, *sLimit
= buffer
+ (sizeof(buffer
) - 1);
54 errorCode
= U_ZERO_ERROR
;
56 ucnv_fromUnicode(converter
, &s
, sLimit
, &us
, uLimit
, 0, FALSE
, &errorCode
);
63 } while(errorCode
== U_BUFFER_OVERFLOW_ERROR
);
64 u_releaseDefaultConverter(converter
);
72 U_IO_API STD_ISTREAM
& U_EXPORT2
73 operator>>(STD_ISTREAM
& stream
, UnicodeString
& str
)
75 // This is like ICU status checking.
80 /* ipfx should eat whitespace when ios::skipws is set */
84 UConverter
*converter
;
85 UErrorCode errorCode
= U_ZERO_ERROR
;
87 // use the default converter to convert chunks of text
88 converter
= u_getDefaultConverter(&errorCode
);
89 if(U_SUCCESS(errorCode
)) {
91 const UChar
*uLimit
= uBuffer
+ UPRV_LENGTHOF(uBuffer
);
92 const char *s
, *sLimit
;
95 UBool initialWhitespace
= TRUE
;
96 UBool continueReading
= TRUE
;
98 /* We need to consume one byte at a time to see what is considered whitespace. */
99 while (continueReading
) {
102 // The EOF is only set after the get() of an unavailable byte.
103 if (!initialWhitespace
) {
104 stream
.clear(stream
.eofbit
);
106 continueReading
= FALSE
;
108 sLimit
= &ch
+ (int)continueReading
;
111 errorCode
= U_ZERO_ERROR
;
113 Since we aren't guaranteed to see the state before this call,
114 this code won't work on stateful encodings like ISO-2022 or an EBCDIC stateful encoding.
115 We flush on the last byte to ensure that we output truncated multibyte characters.
117 ucnv_toUnicode(converter
, &us
, uLimit
, &s
, sLimit
, 0, !continueReading
, &errorCode
);
118 if(U_FAILURE(errorCode
)) {
119 /* Something really bad happened. setstate() isn't always an available API */
120 stream
.clear(stream
.failbit
);
123 /* Was the character consumed? */
125 /* Reminder: ibm-1390 & JISX0213 can output 2 Unicode code points */
126 int32_t uBuffSize
= us
-uBuffer
;
127 int32_t uBuffIdx
= 0;
128 while (uBuffIdx
< uBuffSize
) {
129 U16_NEXT(uBuffer
, uBuffIdx
, uBuffSize
, ch32
);
130 if (u_isWhitespace(ch32
)) {
131 if (!initialWhitespace
) {
134 stream
.putback(buffer
[--idx
]);
138 /* else skip intialWhitespace */
141 if (initialWhitespace
) {
143 When initialWhitespace is TRUE, we haven't appended any
144 character yet. This is where we truncate the string,
145 to avoid modifying the string before we know if we can
146 actually read from the stream.
149 initialWhitespace
= FALSE
;
161 u_releaseDefaultConverter(converter
);