home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
ML
/
FILEIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-21
|
7KB
|
351 lines
/*
* ファイル処理
*
* M.Takatsu 1996.6.20
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <assert.h>
#include <ctype.h>
#ifdef X68K
#include <doslib.h>
#include <direct.h>
#else
#include <dir.h>
#endif
#include "parse.h"
#include "exec.h"
#include "strclass.h"
#include "inlib.h"
#include "err.h"
static int FileClassID;
extern int StringClassID;
static int FuncFullPath( int, int, DataStruct* );
static int FuncDelete( int, int, DataStruct* );
static int FuncChdir( int, int, DataStruct* );
static int FuncFprint( int, int, DataStruct* );
static int fileOpen( int, int, DataStruct* );
static int filePuts( int, int, DataStruct* );
static int fileGets( int, int, DataStruct* );
static int filePutc( int, int, DataStruct* );
static int fileGetc( int, int, DataStruct* );
static int fileClose( int, int, DataStruct* );
typedef enum {FileWrite, FileRead} OpenType;
typedef struct _FileClass {
Object object;
FILE *fp;
OpenType type;
} FileClass;
/* 初期化 */
void FileioInit()
{
FileClassID = ObjectSetClass( "File", MaxFunctions, 0 );
FunctionSet( StringClassID, "fullpath", FuncFullPath );
FunctionSet( StringClassID, "delete", FuncDelete );
FunctionSet( 0, "dir", FuncChdir );
FunctionSet( StringClassID, "fprint", FuncFprint );
FunctionSet( StringClassID, "fileopen", fileOpen );
FunctionSet( FileClassID, "fileputs", filePuts);
FunctionSet( FileClassID, "filegets", fileGets );
FunctionSet( FileClassID, "fileputc", filePutc );
FunctionSet( FileClassID, "filegetc", fileGetc );
FunctionSet( FileClassID, "fileclose", fileClose );
}
#define MAX_PATH 1024
/* 文字列をフルパスに */
static int FuncFullPath( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
StringClass *str, *ret ;
char *p, *q;
char tmp[MAX_PATH];
ArgCheck( "toupper", args, buf, TYPE_OBJECT, TYPE_NOASN );
str = (StringClass*)buf[0].od.ptr ;
p = str->str;
q = tmp;
if (p[0] && p[1] == ':') {
*q++ = toupper(p[0]);
*q++ = ':';
p += 2;
} else {
#ifdef X68K
*q++ = CURDRV() + 'A';
#else
*q++ = getdisk() + 'A';
#endif
*q++ = ':';
}
*q++ = '\\';
if (p[0] == '\\') {
p++;
} else {
#ifdef X68K
CURDIR(toupper(tmp[0]) - 'A' + 1, (unsigned char *)q);
#else
getcurdir(0, q);
#endif
q += strlen(q);
if (q[-1] != '\\') {
*q++ = '\\';
}
}
while (*p) {
if (p[0] == '.' && p[1] == '\\') {
p += 2;
} else if (p[0] == '.' && p[1] == '.' && p[2] == '\\') {
p += 3;
q -= 2;;
while (tmp < q && q[0] != ':' && q[0] != '\\') {
q--;
}
q++;
} else {
while (*p && *p != '\\') {
*q++ = *p++;
}
if (*p) {
*q++ = *p++;
}
}
}
*q = '\0';
ret = StringAlloc(strlen(tmp));
strcpy(ret->str, tmp);
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}
/* ファイル削除 */
static int FuncDelete( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
StringClass *str;
str = (StringClass*)buf[0].od.ptr;
unlink(str->str);
return RETURN_VOID;
}
/* カレントディレクトリ設定/取得 */
static int FuncChdir( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
StringClass *ret;
char tmp[MAX_PATH];
if (args == 0) {
getcwd(tmp, MAX_PATH);
tmp[MAX_PATH-1] = '\0';
ret = StringSet( tmp );
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN;
} else {
ArgCheck( "dir", args, buf, TYPE_OBJECT, TYPE_NOASN );
if ( ObjectCheck( &buf[0], StringClassID ) == FALSE )
{
ExecError( "引数の型が違います。(dir)" );
return RETURN_VOID ;
}
chdir(((StringClass*)buf[0].od.ptr)->str);
return RETURN_VOID;
}
}
/* 表示 */
static int FuncFprint( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int i ;
char *file;
StringClass *str ;
DataStruct *top ;
FILE *fp;
file = ((StringClass*)buf[0].od.ptr)->str;
if ((fp = fopen(file, "a")) == NULL) {
return RETURN_VOID;
}
ident = IdentSearch( IdentFunction, "tostring" );
for( i = 1 ; i < args ; i++ )
{
StackPush( buf+i );
top = StackTop();
CallFunctionLocal( ident, 1, top );
StackRelease( top );
assert( top->type == TYPE_OBJECT );
str = (StringClass*)top->od.ptr ;
fprintf(fp, str->str );
if ( i < args - 1 )
fprintf(fp, " " );
StackPop();
}
fclose(fp);
return RETURN_VOID ;
}
static int fileOpen( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
char *file, *type;
FILE *fp;
FileClass *fc;
file = ((StringClass*)buf[0].od.ptr)->str;
if (args == 1) {
type = "r";
} else {
ArgCheck( "fileopen", args, buf, TYPE_OBJECT, TYPE_OBJECT, TYPE_NOASN );
type = ((StringClass*)buf[1].od.ptr)->str;
}
fp = fopen(file, type);
if (fp == NULL) {
StackPushBoolean(FALSE);
return RETURN_RETURN;
}
fc = (FileClass*)ObjectAlloc(sizeof(FileClass)-sizeof(Object), FileClassID);
if (type[0] == 'r' || type[0] == 'R') {
fc->type = FileRead;
} else {
fc->type = FileWrite;
}
fc->fp = fp;
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)fc ;
return RETURN_RETURN;
}
static int filePuts( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
FileClass *fc;
int i ;
StringClass *str ;
DataStruct *top ;
FILE *fp;
fc = (FileClass*)buf[0].od.ptr;
ident = IdentSearch( IdentFunction, "tostring" );
for( i = 1 ; i < args ; i++ )
{
StackPush( buf+i );
top = StackTop();
CallFunctionLocal( ident, 1, top );
StackRelease( top );
assert( top->type == TYPE_OBJECT );
str = (StringClass*)top->od.ptr ;
fprintf(fc->fp, str->str );
if ( i < args - 1 )
fprintf(fc->fp, " " );
StackPop();
}
StackPushBoolean(ferror(fc->fp) == 0);
return RETURN_RETURN;
}
static int fileGets( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
FileClass *fc;
StringClass *ret;
char str[2048];
fc = (FileClass*)buf[0].od.ptr;
if (fgets(str, 2048, fc->fp) == NULL) {
StackPushBoolean(FALSE);
return RETURN_RETURN;
}
ret = StringSet( str );
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN;
}
static int filePutc( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
FileClass *fc;
ArgCheck( "filegetc", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN );
fc = (FileClass*)buf[0].od.ptr;
if (fc->type == FileWrite) {
fputc(buf[1].id.i, fc->fp);
}
StackPushBoolean(ferror(fc->fp) == 0);
return RETURN_RETURN;
}
static int fileGetc( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int c;
FileClass *fc;
fc = (FileClass*)buf[0].od.ptr;
c = fgetc(fc->fp);
StackPushInt(c);
return RETURN_RETURN;
}
static int fileClose( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int flag;
FileClass *fc;
fc = (FileClass*)buf[0].od.ptr;
flag = ferror(fc->fp);
fclose(fc->fp);
StackPushBoolean(flag == 0);
return RETURN_RETURN;
}