1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2010-01-07 20:36:40 +00:00
|
|
|
* Copyright (C) 1998-2010, 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
|
|
|
*/
|
|
|
|
|
2004-10-28 06:21:47 +00:00
|
|
|
/* define for fileno. */
|
|
|
|
#ifndef _XOPEN_SOURCE
|
2006-08-15 01:08:28 +00:00
|
|
|
#if __STDC_VERSION__ >= 199901L
|
|
|
|
/* It is invalid to compile an XPG3, XPG4, XPG4v2 or XPG5 application using c99 */
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#else
|
2004-10-28 06:21:47 +00:00
|
|
|
#define _XOPEN_SOURCE 4
|
|
|
|
#endif
|
2006-08-15 01:08:28 +00:00
|
|
|
#endif
|
2004-10-28 06:21:47 +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"
|
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
|
|
|
|
2005-03-11 08:39:20 +00:00
|
|
|
#if defined(U_WINDOWS) && !defined(fileno)
|
2004-08-13 17:05:00 +00:00
|
|
|
/* Windows likes to rename Unix-like functions */
|
|
|
|
#define fileno _fileno
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2010-01-07 20:36:40 +00:00
|
|
|
static UFILE*
|
|
|
|
finit_owner(FILE *f,
|
|
|
|
const char *locale,
|
|
|
|
const char *codepage,
|
|
|
|
UBool takeOwnership
|
|
|
|
)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2006-03-28 04:00:58 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UFILE *result;
|
|
|
|
if(f == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = (UFILE*) uprv_malloc(sizeof(UFILE));
|
|
|
|
if(result == 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));
|
2004-08-13 17:05:00 +00:00
|
|
|
result->fFileno = fileno(f);
|
2000-06-05 21:56:47 +00:00
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#ifdef U_WINDOWS
|
2004-08-13 17:05:00 +00:00
|
|
|
if (0 <= result->fFileno && result->fFileno <= 2) {
|
|
|
|
/* stdin, stdout and stderr need to be special cased for Windows 98 */
|
2005-06-14 05:44:35 +00:00
|
|
|
#if _MSC_VER >= 1400
|
2006-08-19 21:27:08 +00:00
|
|
|
result->fFile = &__iob_func()[_fileno(f)];
|
2005-06-14 05:44:35 +00:00
|
|
|
#else
|
2006-08-19 21:27:08 +00:00
|
|
|
result->fFile = &_iob[_fileno(f)];
|
2005-06-14 05:44:35 +00:00
|
|
|
#endif
|
2004-05-28 22:12:25 +00:00
|
|
|
}
|
2004-08-13 17:05:00 +00:00
|
|
|
else
|
2000-01-17 19:04:48 +00:00
|
|
|
#endif
|
2004-08-13 17:05:00 +00:00
|
|
|
{
|
|
|
|
result->fFile = f;
|
|
|
|
}
|
2004-05-28 22:12:25 +00:00
|
|
|
|
2004-04-16 01:08:35 +00:00
|
|
|
result->str.fBuffer = result->fUCBuffer;
|
|
|
|
result->str.fPos = result->fUCBuffer;
|
|
|
|
result->str.fLimit = result->fUCBuffer;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
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 */
|
2004-04-16 01:08:35 +00:00
|
|
|
if(u_locbund_init(&result->str.fBundle, locale) == 0) {
|
2002-02-28 01:42:40 +00:00
|
|
|
/* DO NOT FCLOSE HERE! */
|
|
|
|
uprv_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
2000-10-02 20:04:47 +00:00
|
|
|
|
2004-05-26 19:09:33 +00:00
|
|
|
/* If the codepage is not "" use the ucnv_open default behavior */
|
|
|
|
if(codepage == NULL || *codepage != '\0') {
|
2002-02-28 01:42:40 +00:00
|
|
|
result->fConverter = ucnv_open(codepage, &status);
|
2000-01-28 22:40:27 +00:00
|
|
|
}
|
2004-05-28 22:12:25 +00:00
|
|
|
/* else result->fConverter is already memset'd to NULL. */
|
2003-10-11 02:16:12 +00:00
|
|
|
|
2010-01-07 20:36:40 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
result->fOwnFile = takeOwnership;
|
|
|
|
}
|
|
|
|
else {
|
2003-10-11 02:16:12 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2004-04-16 01:08:35 +00:00
|
|
|
u_locbund_close(&result->str.fBundle);
|
2003-10-11 02:16:12 +00:00
|
|
|
#endif
|
|
|
|
/* DO NOT fclose here!!!!!! */
|
|
|
|
uprv_free(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 20:36:40 +00:00
|
|
|
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_finit(FILE *f,
|
|
|
|
const char *locale,
|
|
|
|
const char *codepage)
|
|
|
|
{
|
|
|
|
return finit_owner(f, locale, codepage, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UFILE* U_EXPORT2
|
|
|
|
u_fadopt(FILE *f,
|
|
|
|
const char *locale,
|
|
|
|
const char *codepage)
|
|
|
|
{
|
|
|
|
return finit_owner(f, locale, codepage, TRUE);
|
|
|
|
}
|
|
|
|
|
2003-08-06 16:18:38 +00:00
|
|
|
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_fopen(const char *filename,
|
|
|
|
const char *perm,
|
|
|
|
const char *locale,
|
|
|
|
const char *codepage)
|
|
|
|
{
|
|
|
|
UFILE *result;
|
|
|
|
FILE *systemFile = fopen(filename, perm);
|
|
|
|
if(systemFile == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-07 20:36:40 +00:00
|
|
|
result = finit_owner(systemFile, locale, codepage, TRUE);
|
2003-08-06 16:18:38 +00:00
|
|
|
|
2010-01-07 20:36:40 +00:00
|
|
|
if (!result) {
|
2004-04-14 23:41:00 +00:00
|
|
|
/* Something bad happened.
|
|
|
|
Maybe the converter couldn't be opened. */
|
|
|
|
fclose(systemFile);
|
|
|
|
}
|
2003-08-06 16:18:38 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-05-10 03:59:44 +00:00
|
|
|
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));
|
2008-01-16 22:24:05 +00:00
|
|
|
/* Null pointer test */
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL; /* Just get out. */
|
|
|
|
}
|
2004-05-10 03:59:44 +00:00
|
|
|
uprv_memset(result, 0, sizeof(UFILE));
|
|
|
|
result->str.fBuffer = stringBuf;
|
|
|
|
result->str.fPos = stringBuf;
|
|
|
|
result->str.fLimit = stringBuf+capacity;
|
2004-05-13 20:59:47 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
/* if locale is 0, use the default */
|
|
|
|
if(u_locbund_init(&result->str.fBundle, locale) == 0) {
|
|
|
|
/* DO NOT FCLOSE HERE! */
|
|
|
|
uprv_free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-10 03:59:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-16 05:47:01 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
u_feof(UFILE *f)
|
|
|
|
{
|
|
|
|
UBool endOfBuffer;
|
|
|
|
if (f == NULL) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit);
|
|
|
|
if (f->fFile != NULL) {
|
|
|
|
return endOfBuffer && feof(f->fFile);
|
|
|
|
}
|
|
|
|
return endOfBuffer;
|
|
|
|
}
|
|
|
|
|
2002-03-13 16:11:04 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
u_fflush(UFILE *file)
|
|
|
|
{
|
2004-04-16 05:47:01 +00:00
|
|
|
ufile_flush_translit(file);
|
2010-08-15 02:43:46 +00:00
|
|
|
ufile_flush_io(file);
|
2004-05-10 03:59:44 +00:00
|
|
|
if (file->fFile) {
|
|
|
|
fflush(file->fFile);
|
|
|
|
}
|
|
|
|
else if (file->str.fPos < file->str.fLimit) {
|
|
|
|
*(file->str.fPos++) = 0;
|
|
|
|
}
|
2004-04-16 05:47:01 +00:00
|
|
|
/* TODO: flush input */
|
2002-03-13 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2003-10-11 02:16:12 +00:00
|
|
|
U_CAPI void
|
|
|
|
u_frewind(UFILE *file)
|
|
|
|
{
|
|
|
|
u_fflush(file);
|
|
|
|
ucnv_reset(file->fConverter);
|
2004-05-10 03:59:44 +00:00
|
|
|
if (file->fFile) {
|
|
|
|
rewind(file->fFile);
|
|
|
|
file->str.fLimit = file->fUCBuffer;
|
|
|
|
file->str.fPos = file->fUCBuffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
file->str.fPos = file->str.fBuffer;
|
|
|
|
}
|
2003-10-11 02:16:12 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-12-02 18:34:34 +00:00
|
|
|
if (file) {
|
|
|
|
u_fflush(file);
|
|
|
|
ufile_close_translit(file);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2009-12-02 18:34:34 +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
|
2009-12-02 18:34:34 +00:00
|
|
|
u_locbund_close(&file->str.fBundle);
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2009-12-02 18:34:34 +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)
|
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
return file->str.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 */
|
2004-05-20 05:34:23 +00:00
|
|
|
u_fsetlocale(UFILE *file,
|
|
|
|
const char *locale)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
u_locbund_close(&file->str.fBundle);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-04-16 01:08:35 +00:00
|
|
|
return u_locbund_init(&file->str.fBundle, locale) == 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;
|
2003-10-11 02:16:12 +00:00
|
|
|
int32_t retVal = -1;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2003-10-11 02:16:12 +00:00
|
|
|
/* We use the normal default codepage for this system, and not the one for the locale. */
|
2004-04-16 01:08:35 +00:00
|
|
|
if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
|
2003-10-11 02:16:12 +00:00
|
|
|
ucnv_close(file->fConverter);
|
|
|
|
file->fConverter = ucnv_open(codepage, &status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
retVal = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal;
|
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
|
|
|
}
|
2003-10-11 02:16:12 +00:00
|
|
|
|