enhanced fio.c for DOS

This commit is contained in:
hyung-hwan 2011-05-05 09:11:23 +00:00
parent 574f373cf0
commit 26bae0b0fb
3 changed files with 125 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: fio.h 441 2011-04-22 14:28:43Z hyunghwan.chung $
* $Id: fio.h 452 2011-05-04 15:11:23Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -85,6 +85,8 @@ enum qse_fio_mode_t
/* <os2def.h> => typedef LHANDLE HFILE;
typedef unsigned long LHANDLE; */
typedef unsigned long qse_fio_hnd_t;
#elif defined(__DOS__)
typedef int qse_fio_hnd_t;
#else
typedef int qse_fio_hnd_t;
#endif

View File

@ -1,5 +1,5 @@
/*
* $Id: fio.c 451 2011-05-03 14:00:38Z hyunghwan.chung $
* $Id: fio.c 452 2011-05-04 15:11:23Z hyunghwan.chung $
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -30,6 +30,9 @@
# define INCL_DOSFILEMGR
# define INCL_DOSERRORS
# include <os2.h>
#elif defined(__DOS__)
# include <io.h>
# include <fcntl.h>
#else
# include "syscall.h"
# include <sys/types.h>
@ -90,7 +93,7 @@ qse_fio_t* qse_fio_init (
QSE_MEMSET (fio, 0, QSE_SIZEOF(*fio));
fio->mmgr = mmgr;
/* store the flags for later use though only OS/2 needs
/* store the flags for later use though only OS/2 needs
* this at this moment */
fio->flags = flags;
@ -252,9 +255,57 @@ qse_fio_t* qse_fio_init (
if (ret != NO_ERROR) return QSE_NULL;
}
#elif defined(__DOS__)
/* UNSUPPORTED */
return QSE_NULL;
if (flags & QSE_FIO_HANDLE)
{
handle = *(qse_fio_hnd_t*)path;
}
else
{
int oflags = 0;
int permission = 0;
#ifdef QSE_CHAR_IS_MCHAR
const qse_mchar_t* path_mb = path;
#else
qse_mchar_t path_mb[_MAX_PATH];
if (qse_wcstombs_strict (path,
path_mb, QSE_COUNTOF(path_mb)) == -1) return QSE_NULL;
#endif
if (flags & QSE_FIO_APPEND)
{
if ((flags & QSE_FIO_READ)) oflags |= O_RDWR;
else oflags |= O_WRONLY;
oflags |= O_APPEND;
}
else
{
if ((flags & QSE_FIO_READ) &&
(flags & QSE_FIO_WRITE)) oflags |= O_RDWR;
else if (flags & QSE_FIO_READ) oflags |= O_RDONLY;
else if (flags & QSE_FIO_WRITE) oflags |= O_WRONLY;
}
if (flags & QSE_FIO_CREATE) oflags |= O_CREAT;
if (flags & QSE_FIO_TRUNCATE) oflags |= O_TRUNC;
if (flags & QSE_FIO_EXCLUSIVE) oflags |= O_EXCL;
oflags |= O_BINARY | O_NOINHERIT;
if (mode & QSE_FIO_RUSR) permission |= S_IREAD;
if (mode & QSE_FIO_WUSR) permission |= S_IWRITE;
handle = open (
path_mb,
oflags,
permission
);
if (handle <= -1) return QSE_NULL;
}
#else
if (flags & QSE_FIO_HANDLE)
@ -332,8 +383,7 @@ qse_fio_t* qse_fio_init (
#elif defined(__OS2__)
DosClose (handle);
#elif defined(__DOS__)
/* UNSUPPORTED */
;
close (handle);
#else
QSE_CLOSE (handle);
#endif
@ -356,8 +406,7 @@ void qse_fio_fini (qse_fio_t* fio)
#elif defined(__OS2__)
DosClose (fio->handle);
#elif defined(__DOS__)
/* UNSUPPORTED */
;
close (fio->handle);
#else
QSE_CLOSE (fio->handle);
#endif
@ -427,10 +476,14 @@ qse_fio_off_t qse_fio_seek (
return ((qse_fio_off_t)pos.ulHi << 32) | pos.ulLo;
#elif defined(__DOS__)
static int seek_map[] =
{
SEEK_SET,
SEEK_CUR,
SEEK_END
};
/* UNSUPPORTED */
return -1;
return lseek (fio->handle, offset, seek_map[origin]);
#else
static int seek_map[] =
{
@ -484,8 +537,8 @@ int qse_fio_truncate (qse_fio_t* fio, qse_fio_off_t size)
return (ret == NO_ERROR)? 0: -1;
#elif defined(__DOS__)
/* UNSUPPORTED */
return -1;
return chsize (fio->handle, size);
#else
return QSE_FTRUNCATE (fio->handle, size);
@ -495,19 +548,26 @@ int qse_fio_truncate (qse_fio_t* fio, qse_fio_off_t size)
static qse_ssize_t fio_read (qse_fio_t* fio, void* buf, qse_size_t size)
{
#if defined(_WIN32)
DWORD count;
if (size > QSE_TYPE_MAX(DWORD)) size = QSE_TYPE_MAX(DWORD);
if (ReadFile(fio->handle, buf, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
if (ReadFile (fio->handle,
buf, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
return (qse_ssize_t)count;
#elif defined(__OS2__)
ULONG count;
if (size > QSE_TYPE_MAX(ULONG)) size = QSE_TYPE_MAX(ULONG);
if (DosRead (fio->handle, buf, (ULONG)size, &count) != NO_ERROR) return -1;
if (DosRead (fio->handle,
buf, (ULONG)size, &count) != NO_ERROR) return -1;
return (qse_ssize_t)count;
#elif defined(__DOS__)
/* UNSUPPORTED */
return -1;
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return read (fio->handle, buf, size);
#else
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return QSE_READ (fio->handle, buf, size);
@ -527,20 +587,28 @@ static qse_ssize_t fio_write (qse_fio_t* fio, const void* data, qse_size_t size)
#if defined(_WIN32)
DWORD count;
if (size > QSE_TYPE_MAX(DWORD)) size = QSE_TYPE_MAX(DWORD);
if (WriteFile(fio->handle, data, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
if (WriteFile (fio->handle,
data, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
return (qse_ssize_t)count;
#elif defined(__OS2__)
ULONG count;
if (size > QSE_TYPE_MAX(ULONG)) size = QSE_TYPE_MAX(ULONG);
if (DosWrite(fio->handle, (PVOID)data, (ULONG)size, &count) != NO_ERROR) return -1;
if (DosWrite(fio->handle,
(PVOID)data, (ULONG)size, &count) != NO_ERROR) return -1;
return (qse_ssize_t)count;
#elif defined(__DOS__)
/* UNSUPPORTED */
return -1;
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return write (fio->handle, data, size);
#else
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
return QSE_WRITE (fio->handle, data, size);
#endif
}
@ -667,6 +735,7 @@ static int get_volname_from_handle (
int qse_fio_chmod (qse_fio_t* fio, int mode)
{
#if defined(_WIN32)
int flags = FILE_ATTRIBUTE_NORMAL;
qse_char_t name[MAX_PATH];
@ -678,12 +747,15 @@ int qse_fio_chmod (qse_fio_t* fio, int mode)
if (!(mode & QSE_FIO_WUSR)) flags = FILE_ATTRIBUTE_READONLY;
return (SetFileAttributes (name, flags) == FALSE)? -1: 0;
#elif defined(__OS2__)
int flags = FILE_NORMAL;
FILESTATUS3L stat;
ULONG size = QSE_SIZEOF(stat);
if (DosQueryFileInfo (fio->handle, FIL_STANDARDL, &stat, size) != NO_ERROR) return -1;
if (DosQueryFileInfo (fio->handle,
FIL_STANDARDL, &stat, size) != NO_ERROR) return -1;
if (!(mode & QSE_FIO_WUSR)) flags = FILE_READONLY;
@ -691,8 +763,17 @@ int qse_fio_chmod (qse_fio_t* fio, int mode)
return (DosSetFileInfo (fio->handle, FIL_STANDARDL, &stat, size) != NO_ERROR)? -1: 0;
#elif defined(__DOS__)
/* UNSUPPORTED */
int permission = 0;
if (mode & QSE_FIO_RUSR) permission |= S_IREAD;
if (mode & QSE_FIO_WUSR) permission |= S_IWRITE;
/* TODO: fchmod not available. find a way to do this
return fchmod (fio->handle, permission); */
return -1;
#else
return QSE_FCHMOD (fio->handle, mode);
#endif
@ -701,15 +782,21 @@ int qse_fio_chmod (qse_fio_t* fio, int mode)
int qse_fio_sync (qse_fio_t* fio)
{
#if defined(_WIN32)
return (FlushFileBuffers (fio->handle) == FALSE)? -1: 0;
#elif defined(__OS2__)
return (DosResetBuffer (fio->handle) == NO_ERROR)? 0: -1;
#elif defined(__DOS__)
/* UNSUPPORTED */
return -1;
return fsync (fio->handle);
#else
return QSE_FSYNC (fio->handle);
#endif
}

View File

@ -148,11 +148,11 @@ release/os2/lib/scm/qsescm.tgt
VComponent
34
WRect
3100
1440
2490
146
5700
4240
0
1
0
35
WFileName
@ -264,7 +264,7 @@ WRect
80
5700
4240
0
1
0
56
WFileName
@ -296,7 +296,7 @@ WRect
880
5700
4240
0
1
0
62
WFileName
@ -308,16 +308,16 @@ debug/os2/cmd/awk/qseawk.tgt
VComponent
64
WRect
430
2480
1070
760
5700
4240
0
1
0
65
WFileName
30
debug/dos32/lib/cmn/qsecmn.tgt
0
3
63
7
33