scuffed-code/icu4c/source/common/filestrm.c

214 lines
4.4 KiB
C
Raw Normal View History

1999-08-16 21:50:52 +00:00
/*
******************************************************************************
*
* Copyright (C) 1997-2001, 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.
*
******************************************************************************
1999-08-16 21:50:52 +00:00
*/
#include "filestrm.h"
#include "cmemory.h"
#include <stdio.h>
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_open(const char* filename, const char* mode)
{
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
}
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_wopen(const wchar_t* filename, const wchar_t* mode)
{
/* 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);
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);
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);
uprv_free(fn);
uprv_free(md);
1999-08-16 21:50:52 +00:00
return (FileStream*)result;
#endif
}
U_CAPI void
1999-08-16 21:50:52 +00:00
T_FileStream_close(FileStream* fileStream)
{
if (fileStream != 0) fclose((FILE*)fileStream);
}
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;*/
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_tmpfile()
{
FILE* file = tmpfile();
return (FileStream*)file;
}
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);
}
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);
}
U_CAPI void
1999-08-16 21:50:52 +00:00
T_FileStream_rewind(FileStream* fileStream)
{
rewind((FILE*)fileStream);
}
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;
}
U_CAPI int
1999-08-16 21:50:52 +00:00
T_FileStream_getc(FileStream* fileStream)
{
int c = fgetc((FILE*)fileStream);
return c;
}
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;
}
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. */
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);
}
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);
}
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;
}
U_CAPI int
1999-08-16 21:50:52 +00:00
T_FileStream_eof(FileStream* fileStream)
{
return feof((FILE*)fileStream);
}
U_CAPI int
1999-08-16 21:50:52 +00:00
T_FileStream_error(FileStream* fileStream)
{
return (fileStream == 0 || ferror((FILE*)fileStream));
}
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);
}
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_stdin(void)
{
return (FileStream*)stdin;
}
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_stdout(void)
{
return (FileStream*)stdout;
}
U_CAPI FileStream*
1999-08-16 21:50:52 +00:00
T_FileStream_stderr(void)
{
return (FileStream*)stderr;
}