2001-08-30 01:58:44 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2011-07-05 23:42:32 +00:00
|
|
|
* Copyright (C) 2001-2011, International Business Machines
|
2001-08-30 01:58:44 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* FILE NAME : ustream.cpp
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 06/25/2001 grhoten Move iostream from unistr.h to here
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
2001-08-30 01:58:44 +00:00
|
|
|
#include "unicode/ustream.h"
|
|
|
|
#include "unicode/ucnv.h"
|
2002-02-25 21:46:41 +00:00
|
|
|
#include "unicode/uchar.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2004-08-27 17:56:01 +00:00
|
|
|
#include "ustr_cnv.h"
|
2010-01-26 16:40:30 +00:00
|
|
|
#include "cmemory.h"
|
2002-02-23 00:34:39 +00:00
|
|
|
#include <string.h>
|
2001-08-30 01:58:44 +00:00
|
|
|
|
|
|
|
// console IO
|
|
|
|
|
|
|
|
#if U_IOSTREAM_SOURCE >= 199711
|
2011-07-05 23:42:32 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
#define STD_NAMESPACE std::
|
2001-08-30 01:58:44 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
#define STD_OSTREAM STD_NAMESPACE ostream
|
|
|
|
#define STD_ISTREAM STD_NAMESPACE istream
|
2001-08-30 01:58:44 +00:00
|
|
|
|
2004-03-15 05:50:27 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
U_IO_API STD_OSTREAM & U_EXPORT2
|
2002-02-23 00:34:39 +00:00
|
|
|
operator<<(STD_OSTREAM& stream, const UnicodeString& str)
|
2001-08-30 01:58:44 +00:00
|
|
|
{
|
2002-02-23 00:34:39 +00:00
|
|
|
if(str.length() > 0) {
|
2001-08-30 01:58:44 +00:00
|
|
|
char buffer[200];
|
|
|
|
UConverter *converter;
|
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// use the default converter to convert chunks of text
|
|
|
|
converter = u_getDefaultConverter(&errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
2002-02-23 00:34:39 +00:00
|
|
|
const UChar *us = str.getBuffer();
|
|
|
|
const UChar *uLimit = us + str.length();
|
2010-03-25 04:44:04 +00:00
|
|
|
char *s, *sLimit = buffer + (sizeof(buffer) - 1);
|
2001-08-30 01:58:44 +00:00
|
|
|
do {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
|
|
|
s = buffer;
|
|
|
|
ucnv_fromUnicode(converter, &s, sLimit, &us, uLimit, 0, FALSE, &errorCode);
|
2010-03-25 04:44:04 +00:00
|
|
|
*s = 0;
|
2001-08-30 01:58:44 +00:00
|
|
|
|
|
|
|
// write this chunk
|
|
|
|
if(s > buffer) {
|
2010-01-26 16:40:30 +00:00
|
|
|
stream << buffer;
|
2001-08-30 01:58:44 +00:00
|
|
|
}
|
|
|
|
} while(errorCode == U_BUFFER_OVERFLOW_ERROR);
|
|
|
|
u_releaseDefaultConverter(converter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* stream.flush();*/
|
2001-08-30 01:58:44 +00:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2004-03-15 05:50:27 +00:00
|
|
|
U_IO_API STD_ISTREAM & U_EXPORT2
|
2002-02-23 00:34:39 +00:00
|
|
|
operator>>(STD_ISTREAM& stream, UnicodeString& str)
|
|
|
|
{
|
2007-09-06 08:23:24 +00:00
|
|
|
// This is like ICU status checking.
|
|
|
|
if (stream.fail()) {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* ipfx should eat whitespace when ios::skipws is set */
|
2002-02-25 21:46:41 +00:00
|
|
|
UChar uBuffer[16];
|
2002-02-26 21:23:21 +00:00
|
|
|
char buffer[16];
|
|
|
|
int32_t idx = 0;
|
2002-02-23 00:34:39 +00:00
|
|
|
UConverter *converter;
|
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// use the default converter to convert chunks of text
|
|
|
|
converter = u_getDefaultConverter(&errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
UChar *us = uBuffer;
|
|
|
|
const UChar *uLimit = uBuffer + sizeof(uBuffer)/sizeof(*uBuffer);
|
|
|
|
const char *s, *sLimit;
|
|
|
|
char ch;
|
2002-02-26 21:23:21 +00:00
|
|
|
UChar ch32;
|
2007-09-05 23:13:02 +00:00
|
|
|
UBool initialWhitespace = TRUE;
|
2007-08-30 23:13:07 +00:00
|
|
|
UBool continueReading = TRUE;
|
2002-02-23 00:34:39 +00:00
|
|
|
|
2002-02-25 21:46:41 +00:00
|
|
|
/* We need to consume one byte at a time to see what is considered whitespace. */
|
2007-08-30 23:13:07 +00:00
|
|
|
while (continueReading) {
|
2002-02-25 21:46:41 +00:00
|
|
|
ch = stream.get();
|
2007-08-30 23:13:07 +00:00
|
|
|
if (stream.eof()) {
|
|
|
|
// The EOF is only set after the get() of an unavailable byte.
|
2007-09-05 23:13:02 +00:00
|
|
|
if (!initialWhitespace) {
|
2007-09-06 16:58:11 +00:00
|
|
|
stream.clear(stream.eofbit);
|
2007-09-05 23:13:02 +00:00
|
|
|
}
|
2007-08-30 23:13:07 +00:00
|
|
|
continueReading = FALSE;
|
|
|
|
}
|
|
|
|
sLimit = &ch + (int)continueReading;
|
2002-02-25 21:46:41 +00:00
|
|
|
us = uBuffer;
|
|
|
|
s = &ch;
|
2007-08-30 23:13:07 +00:00
|
|
|
errorCode = U_ZERO_ERROR;
|
2007-09-06 08:23:24 +00:00
|
|
|
/*
|
|
|
|
Since we aren't guaranteed to see the state before this call,
|
|
|
|
this code won't work on stateful encodings like ISO-2022 or an EBCDIC stateful encoding.
|
|
|
|
We flush on the last byte to ensure that we output truncated multibyte characters.
|
|
|
|
*/
|
2007-08-30 23:13:07 +00:00
|
|
|
ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, !continueReading, &errorCode);
|
2002-02-25 21:46:41 +00:00
|
|
|
if(U_FAILURE(errorCode)) {
|
2007-09-06 19:21:55 +00:00
|
|
|
/* Something really bad happened. setstate() isn't always an available API */
|
|
|
|
stream.clear(stream.failbit);
|
2007-08-30 23:13:07 +00:00
|
|
|
goto STOP_READING;
|
2002-02-23 00:34:39 +00:00
|
|
|
}
|
2002-02-26 21:23:21 +00:00
|
|
|
/* Was the character consumed? */
|
2002-02-25 21:46:41 +00:00
|
|
|
if (us != uBuffer) {
|
2006-01-30 07:46:00 +00:00
|
|
|
/* Reminder: ibm-1390 & JISX0213 can output 2 Unicode code points */
|
|
|
|
int32_t uBuffSize = us-uBuffer;
|
|
|
|
int32_t uBuffIdx = 0;
|
|
|
|
while (uBuffIdx < uBuffSize) {
|
|
|
|
U16_NEXT(uBuffer, uBuffIdx, uBuffSize, ch32);
|
|
|
|
if (u_isWhitespace(ch32)) {
|
2007-09-05 23:13:02 +00:00
|
|
|
if (!initialWhitespace) {
|
2006-01-30 07:46:00 +00:00
|
|
|
buffer[idx++] = ch;
|
|
|
|
while (idx > 0) {
|
|
|
|
stream.putback(buffer[--idx]);
|
|
|
|
}
|
|
|
|
goto STOP_READING;
|
2002-02-26 21:23:21 +00:00
|
|
|
}
|
2006-01-30 07:46:00 +00:00
|
|
|
/* else skip intialWhitespace */
|
|
|
|
}
|
|
|
|
else {
|
2007-09-06 08:23:24 +00:00
|
|
|
if (initialWhitespace) {
|
|
|
|
/*
|
|
|
|
When initialWhitespace is TRUE, we haven't appended any
|
|
|
|
character yet. This is where we truncate the string,
|
|
|
|
to avoid modifying the string before we know if we can
|
|
|
|
actually read from the stream.
|
|
|
|
*/
|
|
|
|
str.truncate(0);
|
|
|
|
initialWhitespace = FALSE;
|
|
|
|
}
|
2006-01-30 07:46:00 +00:00
|
|
|
str.append(ch32);
|
2002-02-25 21:46:41 +00:00
|
|
|
}
|
2002-02-23 00:34:39 +00:00
|
|
|
}
|
2002-02-26 21:23:21 +00:00
|
|
|
idx = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer[idx++] = ch;
|
2002-02-23 00:34:39 +00:00
|
|
|
}
|
2002-02-25 21:46:41 +00:00
|
|
|
}
|
2006-01-30 07:46:00 +00:00
|
|
|
STOP_READING:
|
2002-02-23 00:34:39 +00:00
|
|
|
u_releaseDefaultConverter(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stream.flush();*/
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2004-03-15 05:50:27 +00:00
|
|
|
U_NAMESPACE_END
|
2002-02-23 00:34:39 +00:00
|
|
|
|
2001-08-30 01:58:44 +00:00
|
|
|
#endif
|