1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 1997-1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File FILESTRM.C
|
|
|
|
*
|
|
|
|
* @author Glenn Marcy
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 5/8/98 gm Created
|
|
|
|
* 03/02/99 stephen Reordered params in ungetc to match stdio
|
|
|
|
* Added wopen
|
|
|
|
* 3/29/99 helena Merged Stephen and Bertrand's changes.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filestrm.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_open(const char* filename, const char* mode)
|
|
|
|
{
|
1999-12-08 00:15:46 +00:00
|
|
|
if(filename != NULL && *filename != 0 && mode != NULL && *mode != 0) {
|
|
|
|
FILE *file = fopen(filename, mode);
|
|
|
|
return (FileStream*)file;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
|
|
|
|
{
|
2000-10-04 22:52:22 +00:00
|
|
|
/* TBD: _wfopen is believed to be MS-specific? */
|
|
|
|
#if defined(WIN32) && !defined(__WINDOWS__)
|
1999-08-16 21:50:52 +00:00
|
|
|
FILE* result = _wfopen(filename, mode);
|
|
|
|
return (FileStream*)result;
|
|
|
|
#else
|
|
|
|
size_t fnMbsSize, mdMbsSize;
|
|
|
|
char *fn, *md;
|
|
|
|
FILE *result;
|
|
|
|
|
|
|
|
/* convert from wchar_t to char */
|
|
|
|
fnMbsSize = wcstombs(NULL, filename, ((size_t)-1) >> 1);
|
1999-12-28 23:39:02 +00:00
|
|
|
fn = (char*)uprv_malloc(fnMbsSize+2);
|
1999-08-16 21:50:52 +00:00
|
|
|
wcstombs(fn, filename, fnMbsSize);
|
|
|
|
fn[fnMbsSize] = 0;
|
|
|
|
|
|
|
|
mdMbsSize = wcstombs(NULL, mode, ((size_t)-1) >> 1);
|
1999-12-28 23:39:02 +00:00
|
|
|
md = (char*)uprv_malloc(mdMbsSize+2);
|
1999-08-16 21:50:52 +00:00
|
|
|
wcstombs(md, mode, mdMbsSize);
|
|
|
|
md[mdMbsSize] = 0;
|
|
|
|
|
|
|
|
result = fopen(fn, md);
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_free(fn);
|
|
|
|
uprv_free(md);
|
1999-08-16 21:50:52 +00:00
|
|
|
return (FileStream*)result;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI void
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_close(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
if (fileStream != 0) fclose((FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
U_CAPI UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_file_exists(const char* filename)
|
|
|
|
{
|
|
|
|
FILE* temp = fopen(filename, "r");
|
|
|
|
if (temp) {
|
|
|
|
fclose(temp);
|
|
|
|
return TRUE;
|
|
|
|
} else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static const int32_t kEOF;
|
|
|
|
const int32_t FileStream::kEOF = EOF;*/
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_tmpfile()
|
|
|
|
{
|
|
|
|
FILE* file = tmpfile();
|
|
|
|
return (FileStream*)file;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_read(FileStream* fileStream, void* addr, int32_t len)
|
|
|
|
{
|
|
|
|
return fread(addr, 1, len, (FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_write(FileStream* fileStream, const void* addr, int32_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
return fwrite(addr, 1, len, (FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI void
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_rewind(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
rewind((FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_putc(FileStream* fileStream, int32_t ch)
|
|
|
|
{
|
|
|
|
int32_t c = fputc(ch, (FILE*)fileStream);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_getc(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
int c = fgetc((FILE*)fileStream);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_ungetc(int32_t ch, FileStream* fileStream)
|
|
|
|
{
|
|
|
|
|
|
|
|
int32_t c = ungetc(ch, (FILE*)fileStream);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_peek(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
int32_t c = fgetc((FILE*)fileStream);
|
|
|
|
return ungetc(c, (FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Added by Bertrand A. D. */
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI char *
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_readLine(FileStream* fileStream, char* buffer, int32_t length)
|
|
|
|
{
|
|
|
|
return fgets(buffer, length, (FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_writeLine(FileStream* fileStream, const char* buffer)
|
|
|
|
{
|
|
|
|
return fputs(buffer, (FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_size(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
int32_t savedPos = ftell((FILE*)fileStream);
|
|
|
|
int32_t size = 0;
|
|
|
|
|
|
|
|
/*Changes by Bertrand A. D. doesn't affect the current position
|
|
|
|
goes to the end of the file before ftell*/
|
|
|
|
fseek((FILE*)fileStream, 0, SEEK_END);
|
|
|
|
size = ftell((FILE*)fileStream);
|
|
|
|
fseek((FILE*)fileStream, savedPos, SEEK_SET);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_eof(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
return feof((FILE*)fileStream);
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI int
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_error(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
return (fileStream == 0 || ferror((FILE*)fileStream));
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI void
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_setError(FileStream* fileStream)
|
|
|
|
{
|
|
|
|
/* force the stream to set its error flag*/
|
|
|
|
fseek((FILE*)fileStream, 99999, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_stdin(void)
|
|
|
|
{
|
|
|
|
return (FileStream*)stdin;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_stdout(void)
|
|
|
|
{
|
|
|
|
return (FileStream*)stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
U_CAPI FileStream*
|
1999-08-16 21:50:52 +00:00
|
|
|
T_FileStream_stderr(void)
|
|
|
|
{
|
|
|
|
return (FileStream*)stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|