tcharize a couple of routines

svn path=/trunk/; revision=13791
This commit is contained in:
Gunnar Dalsnes 2005-03-01 22:22:56 +00:00
parent 926067bd87
commit f1a3bf3cef
5 changed files with 29 additions and 107 deletions

View file

@ -274,7 +274,9 @@ STDIO_OBJECTS = \
stdio/fileno.o \
stdio/flsbuf.o \
stdio/fopen.o \
stdio/wfopen.o \
stdio/fprintf.o \
stdio/fwprintf.o \
stdio/fputc.o \
stdio/fputchar.o \
stdio/fputs.o \

View file

@ -30,13 +30,14 @@
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <tchar.h>
#include <internal/file.h>
//might change fopen(file,mode) -> fsopen(file,mode,_SH_DENYNO);
FILE* fopen(const char *file, const char *mode)
FILE* _tfopen(const _TCHAR *file, const _TCHAR *mode)
{
FILE *f;
int fd, rw, oflags = 0;
@ -50,28 +51,28 @@ FILE* fopen(const char *file, const char *mode)
if (f == NULL)
return NULL;
rw = (strchr(mode, '+') == NULL) ? 0 : 1;
if (strchr(mode, 'a'))
rw = (_tcschr(mode, '+') == NULL) ? 0 : 1;
if (_tcschr(mode, 'a'))
oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
if (strchr(mode, 'r'))
if (_tcschr(mode, 'r'))
oflags = rw ? O_RDWR : O_RDONLY;
if (strchr(mode, 'w'))
if (_tcschr(mode, 'w'))
oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
if (strchr(mode, 't'))
if (_tcschr(mode, 't'))
oflags |= O_TEXT;
else if (strchr(mode, 'b'))
else if (_tcschr(mode, 'b'))
oflags |= O_BINARY;
else
oflags |= (_fmode& (O_TEXT|O_BINARY));
fd = _open(file, oflags, 0);
fd = _topen(file, oflags, 0);
if (fd < 0)
return NULL;
// msvcrt ensures that writes will end up at the end of file in append mode
// we just move the file pointer to the end of file initially
if (strchr(mode, 'a'))
if (_tcschr(mode, 'a'))
_lseek(fd, 0, SEEK_END);
f->_cnt = 0;
@ -79,75 +80,14 @@ FILE* fopen(const char *file, const char *mode)
f->_bufsiz = 0;
if (rw)
f->_flag = _IOREAD | _IOWRT;
else if (strchr(mode, 'r'))
else if (_tcschr(mode, 'r'))
f->_flag = _IOREAD;
else
f->_flag = _IOWRT;
if (strchr(mode, 't'))
if (_tcschr(mode, 't'))
f->_flag |= _IOTEXT;
else if (strchr(mode, 'b'))
f->_flag |= _IOBINARY;
else if (_fmode& O_BINARY)
f->_flag |= _IOBINARY;
f->_base = f->_ptr = NULL;
return f;
}
/*
* @implemented
*/
FILE* _wfopen(const wchar_t *file, const wchar_t *mode)
{
FILE *f;
int fd, rw, oflags = 0;
if (file == 0)
return 0;
if (mode == 0)
return 0;
f = __alloc_file();
if (f == NULL)
return NULL;
rw = (wcschr(mode, L'+') == NULL) ? 0 : 1;
if (wcschr(mode, L'a'))
oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
if (wcschr(mode, L'r'))
oflags = rw ? O_RDWR : O_RDONLY;
if (wcschr(mode, L'w'))
oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
if (wcschr(mode, L't'))
oflags |= O_TEXT;
else if (wcschr(mode, L'b'))
oflags |= O_BINARY;
else
oflags |= (_fmode& (O_TEXT|O_BINARY));
fd = _wopen(file, oflags, 0);
if (fd < 0)
return NULL;
// msvcrt ensures that writes will end up at the end of file in append mode
// we just move the file pointer to the end of file initially
if (wcschr(mode, 'a'))
_lseek(fd, 0, SEEK_END);
f->_cnt = 0;
f->_file = fd;
f->_bufsiz = 0;
if (rw)
f->_flag = _IOREAD | _IOWRT;
else if (wcschr(mode, L'r'))
f->_flag = _IOREAD;
else
f->_flag = _IOWRT;
if (wcschr(mode, L't'))
f->_flag |= _IOTEXT;
else if (wcschr(mode, L'b'))
else if (_tcschr(mode, 'b'))
f->_flag |= _IOBINARY;
else if (_fmode& O_BINARY)
f->_flag |= _IOBINARY;

View file

@ -1,45 +1,17 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <wchar.h>
#include <tchar.h>
#include <internal/file.h>
/*
* @implemented
*/
int
fprintf(register FILE *iop, const char *fmt, ...)
_ftprintf(register FILE *iop, const _TCHAR *fmt, ...)
{
int len;
char localbuf[BUFSIZ];
va_list a=0;
va_start( a, fmt );
if (iop->_flag & _IONBF)
{
iop->_flag &= ~_IONBF;
iop->_ptr = iop->_base = localbuf;
iop->_bufsiz = BUFSIZ;
len = vfprintf(iop,fmt,a);
fflush(iop);
iop->_flag |= _IONBF;
iop->_base = NULL;
iop->_bufsiz = 0;
iop->_cnt = 0;
}
else
len = vfprintf(iop, fmt, a);
return ferror(iop) ? EOF : len;
}
/*
* @implemented
*/
int
fwprintf(register FILE *iop, const wchar_t *fmt, ...)
{
int len;
wchar_t localbuf[BUFSIZ];
_TCHAR localbuf[BUFSIZ];
va_list a=0;
@ -49,7 +21,7 @@ fwprintf(register FILE *iop, const wchar_t *fmt, ...)
iop->_flag &= ~_IONBF;
iop->_ptr = iop->_base = (char *)localbuf;
iop->_bufsiz = BUFSIZ;
len = vfwprintf(iop,fmt,a);
len = _vftprintf(iop,fmt,a);
fflush(iop);
iop->_flag |= _IONBF;
iop->_base = NULL;
@ -57,6 +29,6 @@ fwprintf(register FILE *iop, const wchar_t *fmt, ...)
iop->_cnt = 0;
}
else
len = vfwprintf(iop, fmt, a);
return ferror(iop) ? EOF : len;
len = _vftprintf(iop, fmt, a);
return ferror(iop) ? -1 : len;
}

View file

@ -0,0 +1,4 @@
#define UNICODE
#define _UNICODE
#include "fprintf.c"

View file

@ -0,0 +1,4 @@
#define UNICODE
#define _UNICODE
#include "fopen.c"