]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ustream.cpp
2 **********************************************************************
3 * Copyright (C) 2001-2006, 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 ******************************************************************************
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"
26 #if U_IOSTREAM_SOURCE >= 198506
28 #if U_IOSTREAM_SOURCE >= 199711
29 #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
);
54 errorCode
= U_ZERO_ERROR
;
56 ucnv_fromUnicode(converter
, &s
, sLimit
, &us
, uLimit
, 0, FALSE
, &errorCode
);
60 stream
.write(buffer
, (int32_t)(s
- buffer
));
62 } while(errorCode
== U_BUFFER_OVERFLOW_ERROR
);
63 u_releaseDefaultConverter(converter
);
71 U_IO_API STD_ISTREAM
& U_EXPORT2
72 operator>>(STD_ISTREAM
& stream
, UnicodeString
& str
)
74 /* ipfx should eat whitespace when ios::skipws is set */
78 UConverter
*converter
;
79 UErrorCode errorCode
= U_ZERO_ERROR
;
82 // use the default converter to convert chunks of text
83 converter
= u_getDefaultConverter(&errorCode
);
84 if(U_SUCCESS(errorCode
)) {
86 const UChar
*uLimit
= uBuffer
+ sizeof(uBuffer
)/sizeof(*uBuffer
);
87 const char *s
, *sLimit
;
90 UBool intialWhitespace
= TRUE
;
92 /* We need to consume one byte at a time to see what is considered whitespace. */
93 while (!stream
.eof()) {
96 errorCode
= U_ZERO_ERROR
;
99 ucnv_toUnicode(converter
, &us
, uLimit
, &s
, sLimit
, 0, FALSE
, &errorCode
);
100 if(U_FAILURE(errorCode
)) {
101 /* Something really bad happened */
104 /* Was the character consumed? */
106 /* Reminder: ibm-1390 & JISX0213 can output 2 Unicode code points */
107 int32_t uBuffSize
= us
-uBuffer
;
108 int32_t uBuffIdx
= 0;
109 while (uBuffIdx
< uBuffSize
) {
110 U16_NEXT(uBuffer
, uBuffIdx
, uBuffSize
, ch32
);
111 if (u_isWhitespace(ch32
)) {
112 if (!intialWhitespace
) {
115 stream
.putback(buffer
[--idx
]);
119 /* else skip intialWhitespace */
123 intialWhitespace
= FALSE
;
133 u_releaseDefaultConverter(converter
);