1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
|
|
|
Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
|
|
|
|
Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* File object implementation */
|
|
|
|
|
1990-10-26 11:58:11 -03:00
|
|
|
/* XXX This should become a built-in module 'io'. It should support more
|
|
|
|
functionality, better exception handling for invalid calls, etc.
|
1990-12-20 11:06:42 -04:00
|
|
|
(Especially reading on a write-only file or vice versa!)
|
1990-10-26 11:58:11 -03:00
|
|
|
It should also cooperate with posix to support popen(), which should
|
|
|
|
share most code but have a special close function. */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "allobjects.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "errno.h"
|
|
|
|
#ifndef errno
|
|
|
|
extern int errno;
|
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
FILE *f_fp;
|
|
|
|
object *f_name;
|
|
|
|
object *f_mode;
|
|
|
|
/* XXX Should move the 'need space' on printing flag here */
|
|
|
|
} fileobject;
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
getfilefile(f)
|
|
|
|
object *f;
|
|
|
|
{
|
|
|
|
if (!is_fileobject(f)) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((fileobject *)f)->f_fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
newopenfileobject(fp, name, mode)
|
|
|
|
FILE *fp;
|
|
|
|
char *name;
|
|
|
|
char *mode;
|
|
|
|
{
|
|
|
|
fileobject *f = NEWOBJ(fileobject, &Filetype);
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
f->f_fp = NULL;
|
|
|
|
f->f_name = newstringobject(name);
|
|
|
|
f->f_mode = newstringobject(mode);
|
|
|
|
if (f->f_name == NULL || f->f_mode == NULL) {
|
|
|
|
DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
f->f_fp = fp;
|
|
|
|
return (object *) f;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
newfileobject(name, mode)
|
|
|
|
char *name, *mode;
|
|
|
|
{
|
|
|
|
fileobject *f;
|
|
|
|
FILE *fp;
|
|
|
|
f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode);
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
1991-02-13 19:25:27 -04:00
|
|
|
#ifdef THINK_C
|
|
|
|
if (*mode == '*') {
|
|
|
|
FILE *fopenRF();
|
|
|
|
f->f_fp = fopenRF(name, mode+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
f->f_fp = fopen(name, mode);
|
|
|
|
if (f->f_fp == NULL) {
|
|
|
|
err_errno(RuntimeError);
|
1990-10-14 09:07:46 -03:00
|
|
|
DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (object *)f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
1990-12-20 11:06:42 -04:00
|
|
|
file_dealloc(f)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
{
|
|
|
|
if (f->f_fp != NULL)
|
|
|
|
fclose(f->f_fp);
|
|
|
|
if (f->f_name != NULL)
|
|
|
|
DECREF(f->f_name);
|
|
|
|
if (f->f_mode != NULL)
|
|
|
|
DECREF(f->f_mode);
|
|
|
|
free((char *)f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1990-12-20 11:06:42 -04:00
|
|
|
file_print(f, fp, flags)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
FILE *fp;
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
fprintf(fp, "<%s file ", f->f_fp == NULL ? "closed" : "open");
|
|
|
|
printobject(f->f_name, fp, flags);
|
|
|
|
fprintf(fp, ", mode ");
|
|
|
|
printobject(f->f_mode, fp, flags);
|
|
|
|
fprintf(fp, ">");
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_repr(f)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
{
|
|
|
|
char buf[300];
|
1990-12-20 11:06:42 -04:00
|
|
|
/* XXX This differs from file_print if the filename contains
|
1990-10-14 09:07:46 -03:00
|
|
|
quotes or other funny characters. */
|
|
|
|
sprintf(buf, "<%s file '%.256s', mode '%.10s'>",
|
|
|
|
f->f_fp == NULL ? "closed" : "open",
|
|
|
|
getstringvalue(f->f_name),
|
|
|
|
getstringvalue(f->f_mode));
|
|
|
|
return newstringobject(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_close(f, args)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
if (args != NULL) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (f->f_fp != NULL) {
|
|
|
|
fclose(f->f_fp);
|
|
|
|
f->f_fp = NULL;
|
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_read(f, args)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
object *v;
|
|
|
|
if (f->f_fp == NULL) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (args == NULL || !is_intobject(args)) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
n = getintvalue(args);
|
1990-10-26 11:58:11 -03:00
|
|
|
if (n < 0) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v = newsizedstringobject((char *)NULL, n);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
n = fread(getstringvalue(v), 1, n, f->f_fp);
|
|
|
|
/* EOF is reported as an empty string */
|
|
|
|
/* XXX should detect real I/O errors? */
|
|
|
|
resizestring(&v, n);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX Should this be unified with raw_input()? */
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_readline(f, args)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
object *v;
|
|
|
|
if (f->f_fp == NULL) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (args == NULL) {
|
|
|
|
n = 10000; /* XXX should really be unlimited */
|
|
|
|
}
|
|
|
|
else if (is_intobject(args)) {
|
|
|
|
n = getintvalue(args);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (n < 0) {
|
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v = newsizedstringobject((char *)NULL, n);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1991-01-02 09:51:41 -04:00
|
|
|
#ifndef THINK_C_3_0
|
|
|
|
/* XXX Think C 3.0 wrongly reads up to n characters... */
|
1990-12-20 11:06:42 -04:00
|
|
|
n = n+1;
|
|
|
|
#endif
|
|
|
|
if (fgets(getstringvalue(v), n, f->f_fp) == NULL) {
|
1990-10-14 09:07:46 -03:00
|
|
|
/* EOF is reported as an empty string */
|
|
|
|
/* XXX should detect real I/O errors? */
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = strlen(getstringvalue(v));
|
|
|
|
}
|
|
|
|
resizestring(&v, n);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_write(f, args)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int n, n2;
|
|
|
|
if (f->f_fp == NULL) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (args == NULL || !is_stringobject(args)) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
n2 = fwrite(getstringvalue(args), 1, n = getstringsize(args), f->f_fp);
|
|
|
|
if (n2 != n) {
|
|
|
|
if (errno == 0)
|
|
|
|
errno = EIO;
|
1990-12-20 11:06:42 -04:00
|
|
|
err_errno(RuntimeError);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static struct methodlist file_methods[] = {
|
|
|
|
{"write", file_write},
|
|
|
|
{"read", file_read},
|
|
|
|
{"readline", file_readline},
|
|
|
|
{"close", file_close},
|
1990-10-14 09:07:46 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 11:06:42 -04:00
|
|
|
file_getattr(f, name)
|
1990-10-14 09:07:46 -03:00
|
|
|
fileobject *f;
|
|
|
|
char *name;
|
|
|
|
{
|
1990-12-20 11:06:42 -04:00
|
|
|
return findmethod(file_methods, (object *)f, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
typeobject Filetype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"file",
|
|
|
|
sizeof(fileobject),
|
|
|
|
0,
|
1990-12-20 11:06:42 -04:00
|
|
|
file_dealloc, /*tp_dealloc*/
|
|
|
|
file_print, /*tp_print*/
|
|
|
|
file_getattr, /*tp_getattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
1990-12-20 11:06:42 -04:00
|
|
|
file_repr, /*tp_repr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|