ICU-7891 fix u_fflush to flush stateful converters

X-SVN-Rev: 28474
This commit is contained in:
Steven R. Loomis 2010-08-15 02:43:46 +00:00
parent 7d8ae29f34
commit 9bc3d2bfdb
6 changed files with 90 additions and 3 deletions

View file

@ -201,6 +201,7 @@ U_CAPI void U_EXPORT2
u_fflush(UFILE *file)
{
ufile_flush_translit(file);
ufile_flush_io(file);
if (file->fFile) {
fflush(file->fFile);
}

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 1998-2007, International Business Machines
* Copyright (C) 1998-2010, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -121,5 +121,12 @@ ufile_close_translit(UFILE *f);
void
ufile_flush_translit(UFILE *f);
/**
* Flush the IO buffer
* @param f UFile to flush
*/
void
ufile_flush_io(UFILE *f);
#endif

View file

@ -302,9 +302,10 @@ u_fstropen(UChar *stringBuf,
const char *locale);
/**
* Close a UFILE.
* Close a UFILE. Implies u_fflush first.
* @param file The UFILE to close.
* @stable ICU 3.0
* @see u_fflush
*/
U_STABLE void U_EXPORT2
u_fclose(UFILE *file);
@ -344,6 +345,8 @@ u_feof(UFILE *f);
* converter/transliterator state. (That is, a logical break is
* made in the output stream - for example if a different type of
* output is desired.) The underlying OS level file is also flushed.
* Note that for a stateful encoding, the converter may write additional
* bytes to return the stream to default state.
* @param file The UFILE to flush.
* @stable ICU 3.0
*/

View file

@ -1,7 +1,7 @@
/*
******************************************************************************
*
* Copyright (C) 1998-2008, International Business Machines
* Copyright (C) 1998-2010, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
@ -238,6 +238,17 @@ ufile_flush_translit(UFILE *f)
}
void
ufile_flush_io(UFILE *f)
{
if((!f) || (!f->fFile)) {
return; /* skip if no file */
}
u_file_write_flush(NULL, 0, f, TRUE, FALSE);
}
void
ufile_close_translit(UFILE *f)
{

View file

@ -909,6 +909,69 @@ static void TestCodepage(void) {
}
static void TestCodepageFlush(void) {
#if UCONFIG_NO_LEGACY_CONVERSION
log_verbose("Skipping, legacy conversion is disabled.");
#else
UChar utf16String[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
uint8_t inBuf[200];
size_t inLen =0;
const char *enc = "IBM-1388"; /* GBK EBCDIC stateful */
UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", enc);
FILE *myCFile;
int shift = 0;
int i;
if (myFile == NULL) {
log_err("Can't write test file %s\n", STANDARD_TEST_FILE);
return;
}
u_fprintf(myFile, "%S", utf16String);
u_fclose(myFile);
/* now read it back */
myCFile = fopen(STANDARD_TEST_FILE, "rb");
if (myCFile == NULL) {
log_err("Can't read test file.");
return;
}
inLen = fread(inBuf, 1, 200, myCFile);
fclose(myCFile);
if(inLen<=0) {
log_err("Failed during read of test file.");
return;
}
/* check if shift in and out */
for(i=0;i<inLen;i++) {
if(inBuf[i]==0x0E) { /* SO */
shift= 1;
} else if(inBuf[i]==0x0F) { /* SI */
shift= -1;
}
}
if(shift==0) {
log_err("Err: shift was unchanged\n");
} else if(shift==1) {
log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
} else if(shift==-1) {
log_verbose("OK: Shifted in (SI, 0x0F)\n");
}
if(inLen != 12) {
log_err("Expected 12 bytes, read %d\n", inLen);
} else {
log_verbose("OK: read %d bytes\n", inLen);
}
#endif
}
#if !UCONFIG_NO_FORMATTING
static void TestFilePrintCompatibility(void) {
UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
@ -1522,6 +1585,7 @@ addFileTest(TestNode** root) {
addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount");
addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering");
addTest(root, &TestCodepage, "file/TestCodepage");
addTest(root, &TestCodepageFlush, "file/TestCodepageFlush");
addTest(root, &TestFileWriteRetvalUTF16, "file/TestFileWriteRetvalUTF16");
addTest(root, &TestFileWriteRetvalUTF8, "file/TestFileWriteRetvalUTF8");
addTest(root, &TestFileWriteRetvalASCII, "file/TestFileWriteRetvalASCII");

View file

@ -856,6 +856,7 @@ int main(int argc, char* argv[])
/* This should delete any temporary files. */
if (fileToRemove) {
fclose(fileToRemove);
log_verbose("Deleting: %s\n", STANDARD_TEST_FILE);
if (remove(STANDARD_TEST_FILE) != 0) {
/* Maybe someone didn't close the file correctly. */
fprintf(stderr, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE);