1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 22:07:51 +00:00
|
|
|
* Copyright (C) 1998-2001, International Business Machines
|
2000-01-13 23:54:23 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File ufile.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 11/19/98 stephen Creation.
|
|
|
|
* 03/12/99 stephen Modified for new C API.
|
|
|
|
* 06/16/99 stephen Changed T_LocaleBundle to u_locbund
|
|
|
|
* 07/19/99 stephen Fixed to use ucnv's default codepage.
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2001-09-28 22:53:22 +00:00
|
|
|
#include "locmap.h"
|
2000-01-05 19:40:01 +00:00
|
|
|
#include "unicode/ustdio.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ufile.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/uloc.h"
|
1999-10-15 01:27:37 +00:00
|
|
|
#include "loccache.h"
|
2000-05-15 18:53:13 +00:00
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/ucnv.h"
|
2001-09-28 22:53:22 +00:00
|
|
|
#include "cstring.h"
|
2002-02-28 01:42:40 +00:00
|
|
|
#include "cmemory.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
2001-02-06 21:56:47 +00:00
|
|
|
static UBool hasICUData(const char *cp) {
|
2000-05-15 18:53:13 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UConverter *cnv = NULL;
|
2000-10-02 20:04:47 +00:00
|
|
|
#if 0
|
2000-05-15 18:53:13 +00:00
|
|
|
UResourceBundle *r = NULL;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2000-05-15 18:53:13 +00:00
|
|
|
r = ures_open(NULL, NULL, &status);
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
ures_close(r);
|
|
|
|
}
|
2000-10-02 20:04:47 +00:00
|
|
|
#endif
|
|
|
|
cnv = ucnv_open(cp, &status);
|
2000-05-15 18:53:13 +00:00
|
|
|
if(cnv == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
ucnv_close(cnv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fopen(const char *filename,
|
2002-02-28 01:42:40 +00:00
|
|
|
const char *perm,
|
|
|
|
const char *locale,
|
|
|
|
const char *codepage)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-15 00:28:52 +00:00
|
|
|
UFILE *result;
|
|
|
|
FILE *systemFile = fopen(filename, perm);
|
|
|
|
if(systemFile == 0) {
|
2002-02-28 01:42:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-15 00:28:52 +00:00
|
|
|
result = u_finit(systemFile, locale, codepage);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2002-03-22 00:01:46 +00:00
|
|
|
if (result) {
|
|
|
|
result->fOwnFile = TRUE;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_finit(FILE *f,
|
2002-02-28 01:42:40 +00:00
|
|
|
const char *locale,
|
|
|
|
const char *codepage)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-03-15 00:28:52 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UBool useSysCP = (UBool)(locale == NULL && codepage == NULL);
|
2002-02-28 01:42:40 +00:00
|
|
|
UFILE *result = (UFILE*) uprv_malloc(sizeof(UFILE));
|
2002-03-15 00:28:52 +00:00
|
|
|
if(result == NULL || f == NULL) {
|
2002-02-28 01:42:40 +00:00
|
|
|
return 0;
|
2002-03-15 00:28:52 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
uprv_memset(result, 0, sizeof(UFILE));
|
2000-06-05 21:56:47 +00:00
|
|
|
|
2000-01-17 19:04:48 +00:00
|
|
|
#ifdef WIN32
|
2002-02-28 01:42:40 +00:00
|
|
|
result->fFile = &_iob[_fileno(f)];
|
2000-01-17 19:04:48 +00:00
|
|
|
#else
|
2002-02-28 01:42:40 +00:00
|
|
|
result->fFile = f;
|
2000-01-17 19:04:48 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
result->fUCPos = result->fUCBuffer;
|
|
|
|
result->fUCLimit = result->fUCBuffer;
|
|
|
|
|
|
|
|
if(hasICUData(codepage)) {
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2002-02-28 01:42:40 +00:00
|
|
|
/* if locale is 0, use the default */
|
|
|
|
if(locale == 0) {
|
|
|
|
locale = uloc_getDefault();
|
|
|
|
}
|
2000-10-02 20:04:47 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
result->fBundle = u_loccache_get(locale);
|
|
|
|
if(result->fBundle == 0) {
|
|
|
|
/* DO NOT FCLOSE HERE! */
|
|
|
|
uprv_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
} else {
|
|
|
|
/* bootstrap mode */
|
|
|
|
return result;
|
|
|
|
}
|
2000-10-02 20:04:47 +00:00
|
|
|
|
2002-03-22 00:01:46 +00:00
|
|
|
/* if the codepage is NULL, use the default for the locale */
|
|
|
|
if(codepage == NULL) {
|
|
|
|
if(!useSysCP) { /* if both locale and codepage are NULL, use the system default codepage */
|
|
|
|
codepage = uprv_defaultCodePageForLocale(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the codepage is still NULL, the default codepage will be used */
|
|
|
|
result->fConverter = ucnv_open(codepage, &status);
|
|
|
|
if(U_FAILURE(status) || result->fConverter == NULL) {
|
|
|
|
/* DO NOT fclose here!!!!!! */
|
|
|
|
uprv_free(result);
|
|
|
|
return 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
} else if (*codepage != '\0') {
|
|
|
|
result->fConverter = ucnv_open(codepage, &status);
|
2002-03-22 00:01:46 +00:00
|
|
|
if(U_FAILURE(status) || result->fConverter == NULL) {
|
2002-02-28 01:42:40 +00:00
|
|
|
/* DO NOT fclose here!!!!!! */
|
|
|
|
uprv_free(result);
|
2000-01-28 22:40:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-03-13 16:11:04 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
u_fflush(UFILE *file)
|
|
|
|
{
|
|
|
|
ufile_flush_translit(file);
|
|
|
|
fflush(file->fFile);
|
|
|
|
/* TODO: flush input */
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fclose(UFILE *file)
|
|
|
|
{
|
2002-03-13 16:11:04 +00:00
|
|
|
u_fflush(file);
|
|
|
|
ufile_close_translit(file);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
if(file->fOwnFile)
|
|
|
|
fclose(file->fFile);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2002-02-28 01:42:40 +00:00
|
|
|
if(file->fOwnBundle)
|
|
|
|
u_locbund_delete(file->fBundle);
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
ucnv_close(file->fConverter);
|
|
|
|
uprv_free(file);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fgetfile( UFILE *f)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
return f->fFile;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fgetlocale( UFILE *file)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
return file->fBundle->fLocale;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fsetlocale(const char *locale,
|
2002-02-28 01:42:40 +00:00
|
|
|
UFILE *file)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
if(file->fOwnBundle)
|
|
|
|
u_locbund_delete(file->fBundle);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
file->fBundle = u_loccache_get(locale);
|
|
|
|
file->fOwnBundle = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
return file->fBundle == 0 ? -1 : 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fgetcodepage(UFILE *file)
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-03-22 00:01:46 +00:00
|
|
|
const char *codepage = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-22 00:01:46 +00:00
|
|
|
if (file->fConverter) {
|
|
|
|
codepage = ucnv_getName(file->fConverter, &status);
|
|
|
|
if(U_FAILURE(status))
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
return codepage;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_fsetcodepage( const char *codepage,
|
2002-02-28 01:42:40 +00:00
|
|
|
UFILE *file)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2002-02-28 01:42:40 +00:00
|
|
|
/* if the codepage is 0, use the default for the locale */
|
|
|
|
if(codepage == 0) {
|
|
|
|
codepage = uprv_defaultCodePageForLocale(file->fBundle->fLocale);
|
|
|
|
|
|
|
|
/* if the codepage is still 0, fall back on the default codepage */
|
|
|
|
}
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
ucnv_close(file->fConverter);
|
|
|
|
file->fConverter = ucnv_open(codepage, &status);
|
|
|
|
if(U_FAILURE(status))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-10-15 01:27:37 +00:00
|
|
|
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fgetConverter(UFILE *file)
|
1999-10-15 01:27:37 +00:00
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
return file->fConverter;
|
1999-10-15 01:27:37 +00:00
|
|
|
}
|