Improved unicode fileio support.

svn path=/trunk/; revision=3822
This commit is contained in:
Robert Dickenson 2002-12-05 15:31:26 +00:00
parent 2aacb78c3e
commit f1592aa69f
29 changed files with 718 additions and 578 deletions

View file

@ -49,8 +49,9 @@
extern BOOL verbose_flagged;
extern BOOL status_flagged;
extern TCHAR test_buffer[TEST_BUFFER_SIZE];
//extern TCHAR test_buffer[TEST_BUFFER_SIZE];
static TCHAR test_buffer[TEST_BUFFER_SIZE];
static TCHAR dos_data[] = _T("line1: this is a bunch of readable text.\r\n")\
_T("line2: some more printable text and punctuation !@#$%^&*()\r\n")\
@ -111,31 +112,33 @@ static BOOL verify_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_d
_tprintf(_T("ERROR: (%s) Can't open file for reading\n"), file_name);
_tprintf(_T("ERROR: ferror returned %d\n"), ferror(file));
return FALSE;
} else if (verbose_flagged) {
} else if (status_flagged) {
_tprintf(_T("STATUS: (%s) opened file for reading\n"), file_name);
}
#ifndef _NO_NEW_DEPENDS_
while (_fgetts(test_buffer, TEST_BUFFER_SIZE, file)) {
int length = _tcslen(test_buffer);
int req_len = _tcschr(file_data+offset, _T('\n')) - (file_data+offset) + 1;
++line_num;
if (verbose_flagged) {
if (length > req_len) {
_tprintf(_T("ERROR: read excess bytes from line %d, length %d, but expected %d\n"), line_num, length, req_len);
error_flagged = TRUE;
break;
}
if (status_flagged) {
_tprintf(_T("STATUS: Verifying %d bytes read from line %d\n"), length, line_num);
}
if (_tcsncmp(test_buffer, file_data+offset, length) == 0) {
if (_tcsncmp(test_buffer, file_data+offset, length - 1) == 0) {
result = TRUE;
} else {
if (verbose_flagged) {
if (status_flagged) {
int i;
_tprintf(_T("WARNING: (%s) failed to verify file\n"), file_name);
//_tprintf(_T("expected: \"%s\"\n"), file_data+offset);
//_tprintf(_T(" found: \"%s\"\n"), test_buffer);
_tprintf(_T("expected: "));
for (i = 0; i < length, i < 10; i++) {
_tprintf(_T("0x%02x "), file_data+offset+i);
for (i = 0; i < length; i++) {
if (file_data[offset+i] != test_buffer[i]) {
_tprintf(_T("line %d, offset %d expected: 0x%04x found: 0x%04x\n"), line_num, i, (int)file_data[offset+i], (int)test_buffer[i]);
}
_tprintf(_T("\n found: "));
for (i = 0; i < length, i < 10; i++) {
_tprintf(_T("0x%02x "), test_buffer+i);
}
_tprintf(_T("\n"));
} else {
@ -196,7 +199,6 @@ static int check_file_size(TCHAR* file_name, TCHAR* file_mode, int expected)
//_tprintf(_T("STATUS: (%s) checking for %d bytes in %s mode\n"), file_name, expected, _tcschr(file_mode, _T('b')) ? _T("binary") : _T("text"));
_tprintf(_T("STATUS: (%s) checking for %d bytes with mode %s\n"), file_name, expected, file_mode);
}
file = _tfopen(file_name, file_mode);
if (file == NULL) {
_tprintf(_T("ERROR: (%s) failed to open file for reading\n"), file_name);

View file

@ -32,14 +32,10 @@
#define VERSION 1
#define TEST_BUFFER_SIZE 200
#ifdef UNICODE
#define TARGET "UNICODE"
w_char_t test_buffer[TEST_BUFFER_SIZE];
#else
#define TARGET "MBCS"
char test_buffer[TEST_BUFFER_SIZE*2];
#endif
BOOL verbose_flagged = 0;
@ -145,10 +141,18 @@ int __cdecl main(int argc, char* argv[])
printf("finished\n");
return result;
}
/*
ANSI:
all tests passing
UNICODE:
writing binary files short one byte. ie. odd number file lengths.
reading text files returns one extra byte.
*/
#ifndef __GNUC__
char* args[] = { "fileio.exe", "0", "ansi", "verbose"};
char* args[] = { "fileio.exe", "0", "unicode", "verbose"};
char* args1[] = { "fileio.exe", "1", "unicode" };
char* args2[] = { "fileio.exe", "2", "unicode" };

View file

@ -1,4 +1,4 @@
# $Id: Makefile,v 1.28 2002/11/29 15:59:02 robd Exp $
# $Id: Makefile,v 1.29 2002/12/05 15:30:43 robd Exp $
PATH_TO_TOP = ../..
@ -266,7 +266,8 @@ PROCESS_OBJECTS = \
process/process.o \
process/procid.o \
process/thread.o \
process/threadid.o
process/threadid.o \
process/threadx.o
SEARCH_OBJECTS = \
search/lfind.o \

View file

@ -1,4 +1,4 @@
/* $Id: open.c,v 1.12 2002/11/24 18:42:22 robd Exp $
/* $Id: open.c,v 1.13 2002/12/05 15:30:44 robd Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
@ -29,6 +29,8 @@
#define NDEBUG
#include <msvcrt/msvcrtdbg.h>
//#define _OLD_BUILD_
#define STD_AUX_HANDLE 3
#define STD_PRINTER_HANDLE 4
@ -76,7 +78,6 @@ int _open(const char* _path, int _oflag,...)
else if ((_oflag & S_IWRITE) == S_IWRITE) {
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
}
/*
*
* _O_BINARY Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.)
@ -84,13 +85,21 @@ int _open(const char* _path, int _oflag,...)
*
* _O_APPEND Moves file pointer to end of file before every write operation.
*/
#ifdef _OLD_BUILD_
if ((_oflag & _O_RDWR) == _O_RDWR)
dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ;
else if ((_oflag & O_RDONLY) == O_RDONLY)
dwDesiredAccess |= GENERIC_READ;
else if ((_oflag & _O_WRONLY) == _O_WRONLY)
dwDesiredAccess |= GENERIC_WRITE ;
#else
if ((_oflag & _O_WRONLY) == _O_WRONLY )
dwDesiredAccess |= GENERIC_WRITE ;
else if ((_oflag & _O_RDWR) == _O_RDWR )
dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ;
else //if ((_oflag & O_RDONLY) == O_RDONLY)
dwDesiredAccess |= GENERIC_READ;
#endif
if (( _oflag & S_IREAD ) == S_IREAD)
dwShareMode |= FILE_SHARE_READ;
@ -117,22 +126,16 @@ int _open(const char* _path, int _oflag,...)
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
if ((_oflag & _O_SEQUENTIAL) == _O_SEQUENTIAL)
dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
if (( _oflag & _O_TEMPORARY ) == _O_TEMPORARY )
{
if ((_oflag & _O_TEMPORARY) == _O_TEMPORARY) {
dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n");
}
if (( _oflag & _O_SHORT_LIVED ) == _O_SHORT_LIVED )
{
if ((_oflag & _O_SHORT_LIVED) == _O_SHORT_LIVED) {
dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n");
}
if (_oflag & _O_NOINHERIT)
sa.bInheritHandle = FALSE;
hFile = CreateFileA(_path,
dwDesiredAccess,
dwShareMode,
@ -140,32 +143,26 @@ int _open(const char* _path, int _oflag,...)
dwCreationDistribution,
dwFlagsAndAttributes,
NULL);
if (hFile == (HANDLE)-1)
{
if (hFile == (HANDLE)-1) {
dwLastError = GetLastError();
if (dwLastError == ERROR_ALREADY_EXISTS)
{
if (dwLastError == ERROR_ALREADY_EXISTS) {
DPRINT("ERROR_ALREADY_EXISTS\n");
__set_errno(EEXIST);
}
else
{
} else {
DPRINT("%x\n", dwLastError);
__set_errno(ENOFILE);
}
return -1;
}
DPRINT("OK\n");
if (!(_oflag & (_O_TEXT|_O_BINARY)))
{
if (!(_oflag & (_O_TEXT|_O_BINARY))) {
_oflag |= _fmode;
}
return __fileno_alloc(hFile,_oflag);
}
int
__fileno_alloc(HANDLE hFile, int mode)
int __fileno_alloc(HANDLE hFile, int mode)
{
int i;
/* Check for bogus values */
@ -183,14 +180,12 @@ __fileno_alloc(HANDLE hFile, int mode)
/* See if we need to expand the tables. Check this BEFORE it might fail,
so that when we hit the count'th request, we've already up'd it. */
if ( i == maxfno)
{
if (i == maxfno) {
int oldcount = maxfno;
fileno_modes_type* old_fileno_modes = fileno_modes;
maxfno += 255;
fileno_modes = (fileno_modes_type*)malloc(maxfno * sizeof(fileno_modes_type));
if ( old_fileno_modes != NULL )
{
if (old_fileno_modes != NULL) {
memcpy(fileno_modes, old_fileno_modes, oldcount * sizeof(fileno_modes_type));
free(old_fileno_modes);
}
@ -206,8 +201,7 @@ __fileno_alloc(HANDLE hFile, int mode)
void* filehnd(int fileno)
{
if ( fileno < 0 || fileno>= maxfno || fileno_modes[fileno].fd == -1)
{
if (fileno < 0 || fileno >= maxfno || fileno_modes[fileno].fd == -1) {
return (void*)-1;
}
return fileno_modes[fileno].hFile;
@ -216,12 +210,10 @@ void *filehnd(int fileno)
int __fileno_setmode(int _fd, int _newmode)
{
int m;
if ( _fd < 0 || _fd >= maxfno )
{
if (_fd < 0 || _fd >= maxfno) {
__set_errno(EBADF);
return -1;
}
m = fileno_modes[_fd].mode;
fileno_modes[_fd].mode = _newmode;
return m;
@ -229,8 +221,7 @@ int __fileno_setmode(int _fd, int _newmode)
int __fileno_getmode(int _fd)
{
if ( _fd < 0 || _fd >= maxfno )
{
if (_fd < 0 || _fd >= maxfno) {
__set_errno(EBADF);
return -1;
}
@ -240,12 +231,10 @@ int __fileno_getmode(int _fd)
int __fileno_close(int _fd)
{
if ( _fd < 0 || _fd >= maxfno )
{
if (_fd < 0 || _fd >= maxfno) {
__set_errno(EBADF);
return -1;
}
fileno_modes[_fd].fd = -1;
fileno_modes[_fd].hFile = (HANDLE)-1;
return 0;
@ -265,20 +254,17 @@ int __fileno_dup2( int handle1, int handle2 )
{
HANDLE hProcess;
BOOL result;
if (handle1 >= maxfno || handle1 < 0 || handle2 >= maxfno || handle2 < 0 )
{
if (handle1 >= maxfno || handle1 < 0 || handle2 >= maxfno || handle2 < 0) {
__set_errno(EBADF);
return -1;
}
if (fileno_modes[handle1].fd == -1)
{
if (fileno_modes[handle1].fd == -1) {
__set_errno(EBADF);
return -1;
}
if (handle1 == handle2)
return handle1;
if (fileno_modes[handle2].fd != -1)
{
if (fileno_modes[handle2].fd != -1) {
_close(handle2);
}
hProcess = GetCurrentProcess();
@ -289,12 +275,10 @@ int __fileno_dup2( int handle1, int handle2 )
0,
TRUE,
DUPLICATE_SAME_ACCESS);
if (result)
{
if (result) {
fileno_modes[handle2].fd = handle2;
fileno_modes[handle2].mode = fileno_modes[handle1].mode;
switch (handle2)
{
switch (handle2) {
case 0:
SetStdHandle(STD_INPUT_HANDLE, fileno_modes[handle2].hFile);
break;
@ -312,9 +296,7 @@ int __fileno_dup2( int handle1, int handle2 )
break;
}
return handle1;
}
else
{
} else {
__set_errno(EMFILE); // Is this the correct error no.?
return -1;
}
@ -331,9 +313,7 @@ BOOL __fileno_init(void)
STARTUPINFO StInfo;
GetStartupInfoA(&StInfo);
if (StInfo.lpReserved2 && StInfo.cbReserved2 >= sizeof(ULONG))
{
if (StInfo.lpReserved2 && StInfo.cbReserved2 >= sizeof(ULONG)) {
count = *(ULONG*)StInfo.lpReserved2;
/*
if (sizeof(ULONG) + count * (sizeof(HANDLE) + sizeof(char)) != StInfo.cbReserved2)
@ -347,10 +327,11 @@ BOOL __fileno_init(void)
maxfno += 255;
{
#ifdef _OLD_BUILD_
// why was this here ???? - robd.
//int result;
//result = malloc(50);
int result;
result = malloc(50);
#endif
}
//fileno_modes = (fileno_modes_type*)malloc(sizeof(fileno_modes_type) * maxfno);
fileno_modes = malloc(sizeof(fileno_modes_type) * maxfno);
@ -358,15 +339,11 @@ BOOL __fileno_init(void)
return FALSE;
}
memset(fileno_modes, -1, sizeof(fileno_modes_type) * maxfno);
if (count)
{
if (count) {
pFile = (HANDLE*)(StInfo.lpReserved2 + sizeof(ULONG) + count * sizeof(char));
pmode = (char*)(StInfo.lpReserved2 + sizeof(ULONG));
for (i = 0; i < count; i++)
{
if (*pFile != INVALID_HANDLE_VALUE)
{
for (i = 0; i < count; i++) {
if (*pFile != INVALID_HANDLE_VALUE) {
fileno_modes[i].fd = i;
fileno_modes[i].mode = ((*pmode << 8) & (_O_TEXT|_O_BINARY)) | (*pmode & _O_ACCMODE);
fileno_modes[i].hFile = *pFile;
@ -375,33 +352,27 @@ BOOL __fileno_init(void)
pmode++;
}
}
if (fileno_modes[0].fd == -1)
{
if (fileno_modes[0].fd == -1) {
fileno_modes[0].fd = 0;
fileno_modes[0].hFile = GetStdHandle(STD_INPUT_HANDLE);
fileno_modes[0].mode = _O_RDONLY|_O_TEXT;
}
if (fileno_modes[1].fd == -1)
{
if (fileno_modes[1].fd == -1) {
fileno_modes[1].fd = 1;
fileno_modes[1].hFile = GetStdHandle(STD_OUTPUT_HANDLE);
fileno_modes[1].mode = _O_WRONLY|_O_TEXT;
}
if (fileno_modes[2].fd == -1)
{
if (fileno_modes[2].fd == -1) {
fileno_modes[2].fd = 2;
fileno_modes[2].hFile = GetStdHandle(STD_ERROR_HANDLE);
fileno_modes[2].mode = _O_WRONLY|_O_TEXT;
}
if (fileno_modes[3].fd == -1)
{
if (fileno_modes[3].fd == -1) {
fileno_modes[3].fd = 3;
fileno_modes[3].hFile = GetStdHandle(STD_AUX_HANDLE);
fileno_modes[3].mode = _O_WRONLY|_O_TEXT;
}
if (fileno_modes[4].fd == -1)
{
if (fileno_modes[4].fd == -1) {
fileno_modes[4].fd = 4;
fileno_modes[4].hFile = GetStdHandle(STD_PRINTER_HANDLE);
fileno_modes[4].mode = _O_WRONLY|_O_TEXT;

View file

@ -1,4 +1,4 @@
/* $Id: wopen.c,v 1.1 2002/11/24 18:42:22 robd Exp $
/* $Id: wopen.c,v 1.2 2002/12/05 15:30:44 robd Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
@ -62,6 +62,7 @@ int _wopen(const wchar_t* _path, int _oflag, ...)
*
* _O_APPEND Moves file pointer to end of file before every write operation.
*/
#if 0
if ((_oflag & _O_RDWR) == _O_RDWR)
dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
FILE_WRITE_DATA | FILE_READ_ATTRIBUTES |
@ -72,6 +73,18 @@ int _wopen(const wchar_t* _path, int _oflag, ...)
else if ((_oflag & _O_WRONLY) == _O_WRONLY)
dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
#else
if ((_oflag & _O_WRONLY) == _O_WRONLY)
dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA |
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES;
else if ((_oflag & _O_RDWR) == _O_RDWR)
dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA |
FILE_WRITE_DATA | FILE_READ_ATTRIBUTES |
FILE_WRITE_ATTRIBUTES;
else //if ((_oflag & O_RDONLY) == O_RDONLY)
dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
FILE_WRITE_ATTRIBUTES;
#endif
if ((_oflag & S_IREAD) == S_IREAD)
dwShareMode |= FILE_SHARE_READ;

View file

@ -16,7 +16,22 @@
#include <msvcrt/msvcrtdbg.h>
#define BUFSIZE 4096
/*
void ReportLastError(void)
{
DWORD error = GetLastError();
if (error != ERROR_SUCCESS) {
PTSTR msg;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL)) {
printf("ReportLastError() %d - %s\n", error, msg);
} else {
printf("ReportLastError() %d - unknown error\n", error);
}
LocalFree(msg);
}
}
*/
size_t _write(int _fd, const void* _buf, size_t _nbyte)
{
char *tmp, *in, *out;
@ -25,32 +40,26 @@ size_t _write(int _fd, const void *_buf, size_t _nbyte)
DWORD wbyte;
DPRINT("_write(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte);
if (__fileno_getmode(_fd) & O_TEXT)
{
if (__fileno_getmode(_fd) & O_TEXT) {
result = _nbyte;
tmp = (char*) malloc(BUFSIZE);
if (tmp == NULL)
{
if (tmp == NULL) {
return -1;
}
count = BUFSIZE;
out = tmp;
in = (char*) _buf;
while (_nbyte--)
{
if (*in == 0x0a)
{
while (_nbyte--) {
if (*in == 0x0a) {
*out++ = 0x0d;
count--;
if (count == 0)
{
if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE, &wbyte, NULL))
{
if (count == 0) {
if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE, &wbyte, NULL)) {
//ReportLastError();
result = -1;
break;
}
if (wbyte < BUFSIZE)
{
if (wbyte < BUFSIZE) {
result = in - (char*)_buf;
break;
}
@ -60,15 +69,15 @@ size_t _write(int _fd, const void *_buf, size_t _nbyte)
}
*out++ = *in++;
count--;
if (count == 0 || _nbyte == 0)
{
if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE - count, &wbyte, NULL))
{
if (count == 0 || _nbyte == 0) {
int tmp_len_debug = strlen(tmp);
if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE - count, &wbyte, NULL)) {
//ReportLastError();
result = -1;
tmp_len_debug = 0;
break;
}
if (wbyte < (BUFSIZE - count))
{
if (wbyte < (BUFSIZE - count)) {
result = in - (char*)_buf;
break;
}
@ -78,32 +87,9 @@ size_t _write(int _fd, const void *_buf, size_t _nbyte)
}
free(tmp);
return result;
}
else
{
if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL))
{
DWORD error = GetLastError();
if (error != ERROR_SUCCESS) {
PTSTR msg;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
printf("_write(fd %d, buf %x, nbyte %d) - %s\n", _fd, _buf, _nbyte, msg);
else
printf("_write(fd %d, buf %x, nbyte %d) - unknown error: %d\n", _fd, _buf, _nbyte, msg, error);
LocalFree(msg);
}
/*
DWORD FormatMessage(
DWORD dwFlags, // source and processing options
LPCVOID lpSource, // message source
DWORD dwMessageId, // message identifier
DWORD dwLanguageId, // language identifier
LPTSTR lpBuffer, // message buffer
DWORD nSize, // maximum size of message buffer
va_list *Arguments // array of message inserts
);
*/
} else {
if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL)) {
//ReportLastError();
return -1;
}
return wbyte;

View file

@ -1,7 +1,7 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/crtdll/mbstring/mbbtype.c
* FILE: lib/msvcrt/mbstring/mbbtype.c
* PURPOSE: Determines the type of a multibyte character
* PROGRAMER: Boudewijn Dekker
* UPDATE HISTORY:
@ -23,9 +23,7 @@ int _mbbtype(unsigned char c , int type)
return _MBC_ILLEGAL;
else
return 0;
}
else {
} else {
if (( c >= 0x20 && c <= 0x7E ) || ( c >= 0xA1 && c <= 0xDF )) {
return _MBC_SINGLE;
}
@ -36,10 +34,7 @@ int _mbbtype(unsigned char c , int type)
return _MBC_ILLEGAL;
else
return 0;
}
return 0;
}

View file

@ -1,4 +1,4 @@
/* $Id: crtmain.c,v 1.2 2002/11/25 17:41:39 robd Exp $
/* $Id: crtmain.c,v 1.3 2002/12/05 15:30:44 robd Exp $
*
* ReactOS MSVCRT.DLL Compatibility Library
*/
@ -17,8 +17,6 @@ int _fltused;
/* FUNCTIONS **************************************************************/
/*
*/
int
STDCALL
_except_handler3(void)

View file

@ -13,44 +13,34 @@
int _readcnv(int fn, void* buf, size_t siz);
int
_filbuf(FILE *f)
int _filbuf(FILE* f)
{
int size;
char c;
if ( !OPEN4READING(f)) {
__set_errno (EINVAL);
return EOF;
}
if (f->_flag&(_IOSTRG|_IOEOF))
return EOF;
f->_flag &= ~_IOUNGETC;
if (f->_base == NULL && (f->_flag & _IONBF) == 0) {
size = 4096;
if ((f->_base = malloc(size+1)) == NULL)
{
if ((f->_base = malloc(size+1)) == NULL) {
// error ENOMEM
f->_flag |= _IONBF;
f->_flag &= ~(_IOFBF|_IOLBF);
}
else
{
} else {
f->_flag |= _IOMYBUF;
f->_bufsiz = size;
}
}
if (f->_flag&_IONBF)
f->_base = &c;
// fush stdout before reading from stdin
// flush stdout before reading from stdin
if (f == stdin) {
if (stdout->_flag&_IOLBF)
fflush(stdout);
@ -99,6 +89,7 @@ _filbuf(FILE *f)
}
f->_cnt--;
return *f->_ptr++ & 0377;
}

View file

@ -1,4 +1,4 @@
/* $Id: getc.c,v 1.5 2002/11/24 18:42:24 robd Exp $
/* $Id: getc.c,v 1.6 2002/12/05 15:30:44 robd Exp $
*
* ReactOS msvcrt library
*
@ -38,19 +38,17 @@
int getc(FILE *fp)
{
int c = -1;
// check for invalid stream
// check for invalid stream
if ( !__validfp (fp) ) {
__set_errno(EINVAL);
return EOF;
}
// check for read access on stream
if ( !OPEN4READING(fp) ) {
__set_errno(EINVAL);
return EOF;
}
if(fp->_cnt > 0) {
fp->_cnt--;
c = (int)*fp->_ptr++;
@ -69,25 +67,22 @@ wint_t getwc(FILE *fp)
__set_errno(EINVAL);
return WEOF;
}
// check for read access on stream
//#define OPEN4READING(f) ((((f)->_flag & _IOREAD) == _IOREAD ) )
if (!OPEN4READING(fp)) {
__set_errno(EINVAL);
return WEOF;
}
// might check on multi bytes if text mode
if (fp->_flag & _IOBINARY) {
if (fp->_cnt > 0) {
if (fp->_cnt > 1) {
fp->_cnt -= sizeof(wchar_t);
c = (wint_t)*((wchar_t*)(fp->_ptr))++;
} else {
c = _filwbuf(fp);
}
} else {
#if 0
BOOL get_bytes = 0;
int mb_cnt = 0;
int found_cr = 0;
@ -95,41 +90,19 @@ wint_t getwc(FILE *fp)
char mbchar[MB_CUR_MAX];
do {
if (fp->_cnt > 0) {
fp->_cnt--;
mbchar[mb_cnt] = *fp->_ptr++;
} else {
mbchar[mb_cnt] = _filbuf(fp);
}
/*
// convert cr/lf pairs into a single lf.
if (mbchar[mb_cnt] == '\r') {
//found_cr = 1;
if (fp->_cnt > 0) {
fp->_cnt--;
mbchar[mb_cnt+1] = *fp->_ptr++;
} else {
mbchar[mb_cnt+1] = _filbuf(fp);
}
if (mbchar[mb_cnt+1] == '\n') {
mbchar[mb_cnt] = '\n';
} else {
++mb_cnt;
}
}
*/
if (isleadbyte(mbchar[mb_cnt])) {
get_bytes = 1;
} else {
get_bytes = 0;
}
if (_ismbblead(mbchar[mb_cnt])) {
}
++mb_cnt;
//}
} while (get_bytes);
@ -140,9 +113,15 @@ wint_t getwc(FILE *fp)
fp->_flag |= _IOERR;
return WEOF;
}
#else
if (fp->_cnt > 0) {
fp->_cnt--;
c = *fp->_ptr++;
} else {
c = _filbuf(fp);
}
#endif
}
return c;
}
// MultiByteToWideChar
// WideCharToMultiByte

View file

@ -20,10 +20,8 @@
#include <msvcrt/stdio.h>
#include <msvcrt/wchar.h>
/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
printf (const char *format, ...)
int printf(const char* format, ...)
{
va_list arg;
int done;
@ -34,8 +32,7 @@ printf (const char *format, ...)
return done;
}
int
wprintf (const wchar_t *format, ...)
int wprintf(const wchar_t* format, ...)
{
va_list arg;
int done;
@ -45,10 +42,3 @@ wprintf (const wchar_t *format, ...)
va_end(arg);
return done;
}
#ifdef USE_IN_LIBIO
# undef _IO_printf
/* This is for libg++. */
strong_alias (printf, _IO_printf);
#endif

View file

@ -1,4 +1,4 @@
/* $Id: putc.c,v 1.5 2002/11/24 18:42:24 robd Exp $
/* $Id: putc.c,v 1.6 2002/12/05 15:30:44 robd Exp $
*
* ReactOS msvcrt library
*
@ -64,10 +64,6 @@ int putc(int c, FILE *fp)
//wint_t putwc(wint_t c, FILE* fp)
int putwc(wint_t c, FILE* fp)
{
int i;
int mb_cnt;
char mbchar[MB_CUR_MAX_CONST];
// valid stream macro should check that fp is dword aligned
if (!__validfp(fp)) {
__set_errno(EINVAL);
@ -90,10 +86,26 @@ int putwc(wint_t c, FILE *fp)
++((wchar_t*)(fp->_ptr));
return (wint_t)c;
} else {
#if 1
wint_t result;
result = _flsbuf((int)(c >> 8), fp);
if (result == EOF)
return WEOF;
result = _flsbuf(c, fp);
if (result == EOF)
return WEOF;
return result;
#else
return _flswbuf(c, fp);
#endif
}
} else {
#if 0
int i;
int mb_cnt;
char mbchar[MB_CUR_MAX_CONST];
// Convert wide character to the corresponding multibyte character.
mb_cnt = wctomb(mbchar, (wchar_t)c);
if (mb_cnt == -1) {
@ -105,19 +117,6 @@ int putwc(wint_t c, FILE *fp)
}
for (i = 0; i < mb_cnt; i++) {
fp->_flag |= _IODIRTY;
#if 0
// convert lf's into a cr/lf pair.
if (mbchar[i] == '\n') {
if (fp->_cnt > 0) {
fp->_cnt--;
*(fp)->_ptr++ = (unsigned char)'\r';
} else {
if (_flsbuf((unsigned char)'\r', fp) == EOF) {
return WEOF;
}
}
}
#endif
if (fp->_cnt > 0) {
fp->_cnt--;
*(fp)->_ptr++ = (unsigned char)mbchar[i];
@ -127,6 +126,17 @@ int putwc(wint_t c, FILE *fp)
}
}
}
#else
fp->_flag |= _IODIRTY;
if (fp->_cnt > 0) {
fp->_cnt--;
*(fp)->_ptr++ = (unsigned char)c;
} else {
if (_flsbuf(c, fp) == EOF) {
return WEOF;
}
}
#endif
return c;
}
return WEOF;

View file

@ -11,11 +11,10 @@ int _isinf(double x);
int
__vfprintf (FILE *fp, const char *fmt0, va_list args);
int __vfprintf(FILE*, const char*, va_list);
int
vfprintf(FILE *f, const char *fmt, va_list ap)
int vfprintf(FILE* f, const char* fmt, va_list ap)
{
int len;
char localbuf[BUFSIZ];
@ -23,8 +22,7 @@ vfprintf(FILE *f, const char *fmt, va_list ap)
#if 0
__fileno_lock(fileno(f));
#endif
if (f->_flag & _IONBF)
{
if (f->_flag & _IONBF) {
f->_flag &= ~_IONBF;
f->_ptr = f->_base = localbuf;
f->_bufsiz = BUFSIZ;
@ -34,9 +32,9 @@ vfprintf(FILE *f, const char *fmt, va_list ap)
f->_base = NULL;
f->_bufsiz = 0;
f->_cnt = 0;
}
else
} else {
len = __vfprintf(f,fmt, ap);
}
#if 0
__fileno_unlock(fileno(f));
#endif

View file

@ -21,27 +21,57 @@ Cambridge, MA 02139, USA. */
#include <msvcrt/stdio.h>
#include <msvcrt/wchar.h>
#undef vprintf
#undef vwprintf
/* Write formatted output to stdout according to the
format string FORMAT, using the argument list in ARG. */
int
vprintf (format, arg)
const char *format;
va_list arg;
//#define _USE_VARARG_
#ifndef _USE_VARARG_
int vprintf(const char* format, va_list arg)
{
int ret = vfprintf (stdout, format, arg);
int ret;
ret = vfprintf(stdout, format, arg);
fflush(stdout);
return ret;
}
int
vwprintf (format, arg)
const wchar_t *format;
va_list arg;
int vwprintf(const wchar_t* format, va_list arg)
{
int ret = vfwprintf (stdout, format, arg);
int ret;
ret = vfwprintf(stdout, format, arg);
fflush(stdout);
return ret;
}
#else
int vprintf(const char* format, ...)
{
va_list arg;
int ret;
va_start(arg, format);
ret = vfprintf(stdout, format, arg);
va_end(arg);
fflush(stdout);
return ret;
}
int vwprintf(const wchar_t* format, ...)
{
va_list arg;
int ret;
va_start(arg, format);
ret = vfwprintf(stdout, format, arg);
va_end(arg);
fflush(stdout);
return ret;
}
#endif

View file

@ -1,37 +1,118 @@
#include <windows.h>
#include <msvcrt/stdlib.h>
#if 1
size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count)
{
size_t size;
int i;
printf("\nmbstowcs(%p, %p, %d) called.\n\n", wcstr, mbstr, count);
if (count <= 0 || !mbstr)
return 0;
if (!*mbstr)
return 0;
if (wcstr == NULL) {
// return required size for the converted string
return strlen(mbstr); // TODO: fixme
}
for (size = 0, i = 0; i < count; size++) {
int result;
////int mbtowc( wchar_t *wchar, const char *mbchar, size_t count )
//// result = mbtowc(wcstr + size, mbstr + i, count - i);
// result = mbtowc(wcstr + size, mbstr + i, 1);
/////////////////////////////////////////
if (mbstr[i] == 0) {
result = 0;
} else {
wcstr[size] = mbstr[i];
result = 1;
}
/////////////////////////////////////////
if (result == -1) {
return -1;
} else if (result == 0) {
wcstr[size] = L'\0';
break;
} else {
i += result;
}
}
return size;
}
#else
#if 1
//int mbtowc(wchar_t *dst, const char *str, size_t n)
size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count)
{
size_t len;
if (count <= 0 || !mbstr)
return 0;
len = MultiByteToWideChar(CP_ACP, 0, mbstr, count, wcstr, (wcstr == NULL) ? 0 : count);
if (!len) {
DWORD err = GetLastError();
switch (err) {
case ERROR_INSUFFICIENT_BUFFER:
break;
case ERROR_INVALID_FLAGS:
break;
case ERROR_INVALID_PARAMETER:
break;
case ERROR_NO_UNICODE_TRANSLATION:
break;
default:
return 1;
}
return -1;
}
/* return the number of bytes from src that have been used */
if (!*mbstr)
return 0;
// if (count >= 2 && isleadbyte(*mbstr) && mbstr[1])
// return 2;
return len;
}
#else
size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count)
{
size_t Size;
return (size_t)Size;
size_t size;
int i;
if (wcstr == NULL) {
// return required size for the converted string
return strlen(mbstr); // TODO: fixme
}
/*
NTSTATUS Status;
ULONG Size;
ULONG Length;
Length = strlen (mbstr);
if (wcstr == NULL)
{
RtlMultiByteToUnicodeSize (&Size,
(char *)mbstr,
Length);
return (size_t)Size;
}
Status = RtlMultiByteToUnicodeN (wcstr,
count,
&Size,
(char *)mbstr,
Length);
if (!NT_SUCCESS(Status))
return -1;
*/
for (size = 0, i = 0; i < count; size++) {
int result;
//int mbtowc( wchar_t *wchar, const char *mbchar, size_t count )
//{
// return 0;
//}
// result = mbtowc(wcstr + size, mbstr + i, count - i);
result = mbtowc(wcstr + size, mbstr + i, 1);
if (result == -1) {
return -1;
} else if (result == 0) {
wcstr[size] = L'\0';
break;
} else {
i += result;
}
}
return (size_t)size;
}
#endif
#endif

View file

@ -1,12 +1,52 @@
#include <windows.h>
#include <msvcrt/stdlib.h>
#include <msvcrt/ctype.h>
//int mbtowc(wchar_t* wcDest, const char* mbConvert, size_t size)
int mbtowc(wchar_t *wchar, const char *mbchar, size_t count)
#if 1
int mbtowc(wchar_t *dst, const char *str, size_t n)
{
*wchar = (wchar_t)*mbchar;
return 1;
// printf("\t\t\tmbtowc(%p, %p, %d) called.\n", dst, str, n);
// WideCharToMultiByte
// MultiByteToWideChar
if (n <= 0 || !str)
return 0;
*dst = *str;
if (!*str)
return 0;
return 1;
}
#else
int mbtowc(wchar_t *dst, const char *str, size_t n)
{
if (n <= 0 || !str)
return 0;
if (!MultiByteToWideChar(CP_ACP, 0, str, n, dst, (dst == NULL) ? 0 : 1)) {
DWORD err = GetLastError();
switch (err) {
case ERROR_INSUFFICIENT_BUFFER:
break;
case ERROR_INVALID_FLAGS:
break;
case ERROR_INVALID_PARAMETER:
break;
case ERROR_NO_UNICODE_TRANSLATION:
break;
default:
return 1;
}
return -1;
}
/* return the number of bytes from src that have been used */
if (!*str)
return 0;
if (n >= 2 && isleadbyte(*str) && str[1])
return 2;
return 1;
}
#endif

View file

@ -29,7 +29,7 @@
static const wchar_t encoding_mask[] =
{
(wchar_t)~0x7ff, (wchar_t)~0xffff, (wchar_t)~0x1fffff, (wchar_t)~0x3ffffff
~0x7ff, ~0xffff, ~0x1fffff, ~0x3ffffff
};
static const unsigned char encoding_byte[] =

View file

@ -16,11 +16,43 @@
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <windows.h>
#include <msvcrt/stdlib.h>
#include <msvcrt/ctype.h>
#include <msvcrt/wchar.h>
#include <msvcrt/errno.h>
#include <msvcrt/internal/file.h>
int
STDCALL
WideCharToMultiByte(
UINT CodePage,
DWORD dwFlags,
LPCWSTR lpWideCharStr,
int cchWideChar,
LPSTR lpMultiByteStr,
int cchMultiByte,
LPCSTR lpDefaultChar,
LPBOOL lpUsedDefaultChar);
int wctomb(char* dst, wchar_t ch)
{
#if 0
return WideCharToMultiByte(CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL);
#else
if (dst == NULL) {
return 1;
}
*dst = ch;
return 1;
#endif
}
#if 0
#ifndef EILSEQ
#define EILSEQ EINVAL
#endif
@ -110,3 +142,5 @@ size_t __wcrtomb(char *s, wchar_t wc)
}
return written;
}
#endif

View file

@ -1,4 +1,4 @@
/* $Id: witow.c,v 1.1 2002/11/24 18:42:25 robd Exp $
/* $Id: witow.c,v 1.2 2002/12/05 15:30:45 robd Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
@ -26,8 +26,7 @@ wchar_t* _i64tow(__int64 value, wchar_t* string, int radix)
int sign;
wchar_t* sp;
if (radix > 36 || radix <= 1)
{
if (radix > 36 || radix <= 1) {
__set_errno(EDOM);
return 0;
}
@ -37,8 +36,7 @@ wchar_t* _i64tow(__int64 value, wchar_t* string, int radix)
v = -value;
else
v = (unsigned)value;
while (v || tp == tmp)
{
while (v || tp == tmp) {
i = v % radix;
v = v / radix;
if (i < 10)
@ -67,14 +65,12 @@ wchar_t* _ui64tow(unsigned __int64 value, wchar_t* string, int radix)
unsigned long v = value;
wchar_t* sp;
if (radix > 36 || radix <= 1)
{
if (radix > 36 || radix <= 1) {
__set_errno(EDOM);
return 0;
}
while (v || tp == tmp)
{
while (v || tp == tmp) {
i = v % radix;
v = v / radix;
if (i < 10)

View file

@ -1,6 +1,7 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <msvcrt/string.h>
#pragma function(memcmp)
int memcmp(const void *s1, const void *s2, size_t n)
{

View file

@ -1,5 +1,6 @@
#include <msvcrt/string.h>
#pragma function(memcpy)
/* This is the most reliable way to avoid incompatibilities
in available built-in functions on various systems. */

View file

@ -1,5 +1,6 @@
#include <msvcrt/string.h>
#pragma function(memset)
void* memset(void* src, int val, size_t count)
{

View file

@ -1,6 +1,7 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <msvcrt/string.h>
#pragma function(strcat)
char* strcat(char* s, const char* append)
{

View file

@ -1,6 +1,7 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <msvcrt/string.h>
#pragma function(strcmp)
int strcmp(const char* s1, const char* s2)
{

View file

@ -1,6 +1,7 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <msvcrt/string.h>
#pragma function(strlen)
size_t strlen(const char* str)
{

View file

@ -245,8 +245,7 @@ settzname(void)
}
}
static char *
tzdir(void)
static char* tzdir(void)
{
static char dir[80]={0}, *cp;
if (dir[0] == 0)
@ -266,8 +265,7 @@ tzdir(void)
return dir;
}
static int
tzload(const char *name, struct state * CPP_CONST sp)
static int tzload(const char* name, struct state* CPP_CONST sp)
{
const char * p;
int i;

View file

@ -1,9 +1,10 @@
#include <msvcrt/wchar.h>
#if 0
int wcsncmp(const wchar_t* cs, const wchar_t* ct, size_t count)
{
while ((*cs) == (*ct) && count > 0)
{
while ((*cs) == (*ct) && count > 0) {
if (*cs == 0)
return 0;
cs++;
@ -11,6 +12,22 @@ int wcsncmp(const wchar_t * cs,const wchar_t * ct,size_t count)
count--;
}
return (*cs) - (*ct);
}
#else
int wcsncmp(const wchar_t* cs, const wchar_t* ct, size_t count)
{
if (count == 0)
return 0;
do {
if (*cs != *ct++)
//return *(unsigned const char *)cs - *(unsigned const char *)--ct;
return (*cs) - (*(--ct));
if (*cs++ == 0)
break;
} while (--count != 0);
return 0;
}
#endif