mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-10 07:39:16 +00:00
ICU-3499 Implement u_fstropen
X-SVN-Rev: 15224
This commit is contained in:
parent
2242d9195a
commit
735b7f14f8
4 changed files with 87 additions and 4 deletions
|
@ -114,6 +114,25 @@ u_fopen(const char *filename,
|
|||
return result;
|
||||
}
|
||||
|
||||
U_CAPI UFILE* U_EXPORT2
|
||||
u_fstropen(UChar *stringBuf,
|
||||
int32_t capacity,
|
||||
const char *locale)
|
||||
{
|
||||
UFILE *result;
|
||||
|
||||
if (capacity < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result = (UFILE*) uprv_malloc(sizeof(UFILE));
|
||||
uprv_memset(result, 0, sizeof(UFILE));
|
||||
result->str.fBuffer = stringBuf;
|
||||
result->str.fPos = stringBuf;
|
||||
result->str.fLimit = stringBuf+capacity;
|
||||
return result;
|
||||
}
|
||||
|
||||
U_CAPI UBool U_EXPORT2
|
||||
u_feof(UFILE *f)
|
||||
{
|
||||
|
@ -132,7 +151,12 @@ U_CAPI void U_EXPORT2
|
|||
u_fflush(UFILE *file)
|
||||
{
|
||||
ufile_flush_translit(file);
|
||||
fflush(file->fFile);
|
||||
if (file->fFile) {
|
||||
fflush(file->fFile);
|
||||
}
|
||||
else if (file->str.fPos < file->str.fLimit) {
|
||||
*(file->str.fPos++) = 0;
|
||||
}
|
||||
/* TODO: flush input */
|
||||
}
|
||||
|
||||
|
@ -140,10 +164,15 @@ U_CAPI void
|
|||
u_frewind(UFILE *file)
|
||||
{
|
||||
u_fflush(file);
|
||||
rewind(file->fFile);
|
||||
ucnv_reset(file->fConverter);
|
||||
file->str.fPos = file->fUCBuffer;
|
||||
file->str.fLimit = file->fUCBuffer;
|
||||
if (file->fFile) {
|
||||
rewind(file->fFile);
|
||||
file->str.fLimit = file->fUCBuffer;
|
||||
file->str.fPos = file->fUCBuffer;
|
||||
}
|
||||
else {
|
||||
file->str.fPos = file->str.fBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
||||
|
|
|
@ -162,6 +162,25 @@ u_finit(FILE *f,
|
|||
const char *locale,
|
||||
const char *codepage);
|
||||
|
||||
/**
|
||||
* Create a UFILE that can be used for localized formatting or parsing.
|
||||
* The u_sprintf and u_sscanf functions do not read or write numbers for a
|
||||
* specific locale. The ustdio.h file functions can be used on this UFILE.
|
||||
* The string is usable once u_fclose or u_fflush has been called on the
|
||||
* returned UFILE.
|
||||
* @param stringBuf The string used for reading or writing.
|
||||
* @param count The number of code units available for use in stringBuf
|
||||
* @param locale The locale whose conventions will be used to format
|
||||
* and parse output. If this parameter is NULL, the default locale will
|
||||
* be used.
|
||||
* @return A new UFILE, or NULL if an error occurred.
|
||||
* @draft 3.0
|
||||
*/
|
||||
U_CAPI UFILE* U_EXPORT2
|
||||
u_fstropen(UChar *stringBuf,
|
||||
int32_t capacity,
|
||||
const char *locale);
|
||||
|
||||
/**
|
||||
* Close a UFILE.
|
||||
* @param file The UFILE to close.
|
||||
|
|
|
@ -314,6 +314,17 @@ u_file_write_flush( const UChar *chars,
|
|||
int32_t written = 0;
|
||||
int32_t numConverted = 0;
|
||||
|
||||
if (!f->fFile) {
|
||||
int32_t charsLeft = f->str.fLimit - f->str.fPos;
|
||||
if (flush && charsLeft > count) {
|
||||
count++;
|
||||
}
|
||||
written = ufmt_min(count, charsLeft);
|
||||
u_strncpy(f->str.fPos, chars, written);
|
||||
f->str.fPos += written;
|
||||
return written;
|
||||
}
|
||||
|
||||
if (count < 0) {
|
||||
count = u_strlen(chars);
|
||||
}
|
||||
|
|
|
@ -245,6 +245,29 @@ static void TestString(void) {
|
|||
|
||||
}
|
||||
|
||||
static void TestLocalizedString(void) {
|
||||
UChar testStr[256];
|
||||
UChar uBuffer[256];
|
||||
int32_t numResult = -1;
|
||||
UFILE *strFile = u_fstropen(testStr, sizeof(testStr)/sizeof(testStr[0]), "en_US");
|
||||
|
||||
if (!strFile) {
|
||||
log_err("u_fstropen failed to work\n");
|
||||
return;
|
||||
}
|
||||
u_fprintf(strFile, "%d", 1234);
|
||||
u_frewind(strFile);
|
||||
u_fscanf(strFile, "%d", &numResult);
|
||||
u_fclose(strFile);
|
||||
u_uastrcpy(uBuffer,"1,234");
|
||||
if (u_strcmp(testStr, uBuffer) != 0) {
|
||||
log_err("u_fprintf failed to work on a string\n");
|
||||
}
|
||||
if (numResult != 1234) {
|
||||
log_err("u_fscanf failed to work on a string\n");
|
||||
}
|
||||
}
|
||||
|
||||
#define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
|
||||
u_uastrncpy(testStr, "xxxxxxxxxxxxxx", sizeof(testStr)/sizeof(testStr[0]));\
|
||||
size = u_snprintf(testStr, limit, format, value);\
|
||||
|
@ -638,6 +661,7 @@ static void TestBadScanfFormat(void) {
|
|||
U_CFUNC void
|
||||
addStringTest(TestNode** root) {
|
||||
addTest(root, &TestString, "string/TestString");
|
||||
addTest(root, &TestLocalizedString, "string/TestLocalizedString");
|
||||
addTest(root, &TestSprintfFormat, "string/TestSprintfFormat");
|
||||
addTest(root, &TestSnprintf, "string/TestSnprintf");
|
||||
addTest(root, &TestSScanset, "string/TestSScanset");
|
||||
|
|
Loading…
Add table
Reference in a new issue