1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:07:38 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
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 12:07:46 +00:00
|
|
|
/* File object implementation */
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "allobjects.h"
|
1992-01-27 16:53:23 +00:00
|
|
|
#include "modsupport.h"
|
1994-08-01 11:34:53 +00:00
|
|
|
#include "structmember.h"
|
1992-08-05 19:58:53 +00:00
|
|
|
#include "ceval.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1995-02-19 15:55:19 +00:00
|
|
|
#ifdef THINK_C
|
|
|
|
#define HAVE_FOPENRF
|
|
|
|
#endif
|
1995-04-23 22:12:47 +00:00
|
|
|
#ifdef __MWERKS__
|
|
|
|
/* Mwerks fopen() doesn't always set errno */
|
|
|
|
#define NO_FOPEN_ERRNO
|
|
|
|
#endif
|
1995-02-19 15:55:19 +00:00
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
#define BUF(v) GETSTRINGVALUE((stringobject *)v)
|
|
|
|
|
1993-07-05 10:31:29 +00:00
|
|
|
#include <errno.h>
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
FILE *f_fp;
|
|
|
|
object *f_name;
|
|
|
|
object *f_mode;
|
1991-06-04 19:37:39 +00:00
|
|
|
int (*f_close) PROTO((FILE *));
|
1991-04-04 10:44:06 +00:00
|
|
|
int f_softspace; /* Flag used by 'print' command */
|
1990-10-14 12:07:46 +00:00
|
|
|
} fileobject;
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
getfilefile(f)
|
|
|
|
object *f;
|
|
|
|
{
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL || !is_fileobject(f))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1992-09-25 21:59:05 +00:00
|
|
|
else
|
|
|
|
return ((fileobject *)f)->f_fp;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1993-10-18 17:06:59 +00:00
|
|
|
object *
|
|
|
|
getfilename(f)
|
|
|
|
object *f;
|
|
|
|
{
|
|
|
|
if (f == NULL || !is_fileobject(f))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return ((fileobject *)f)->f_name;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
object *
|
1991-06-04 19:37:39 +00:00
|
|
|
newopenfileobject(fp, name, mode, close)
|
1990-10-14 12:07:46 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *name;
|
|
|
|
char *mode;
|
1991-06-04 19:37:39 +00:00
|
|
|
int (*close) FPROTO((FILE *));
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
|
|
|
fileobject *f = NEWOBJ(fileobject, &Filetype);
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
f->f_fp = NULL;
|
|
|
|
f->f_name = newstringobject(name);
|
|
|
|
f->f_mode = newstringobject(mode);
|
1991-06-04 19:37:39 +00:00
|
|
|
f->f_close = close;
|
1991-04-04 10:44:06 +00:00
|
|
|
f->f_softspace = 0;
|
1990-10-14 12:07:46 +00:00
|
|
|
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;
|
|
|
|
{
|
1991-06-04 19:37:39 +00:00
|
|
|
extern int fclose PROTO((FILE *));
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
1991-06-04 19:37:39 +00:00
|
|
|
f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FOPENRF
|
1991-02-13 23:25:27 +00:00
|
|
|
if (*mode == '*') {
|
|
|
|
FILE *fopenRF();
|
|
|
|
f->f_fp = fopenRF(name, mode+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
1992-08-05 19:58:53 +00:00
|
|
|
{
|
|
|
|
BGN_SAVE
|
|
|
|
f->f_fp = fopen(name, mode);
|
|
|
|
END_SAVE
|
|
|
|
}
|
1991-02-13 23:25:27 +00:00
|
|
|
if (f->f_fp == NULL) {
|
1995-04-23 22:12:47 +00:00
|
|
|
#ifdef NO_FOPEN_ERRNO
|
|
|
|
if ( errno == 0 ) {
|
|
|
|
err_setstr(IOError, "Cannot open file");
|
|
|
|
DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1991-12-10 14:00:03 +00:00
|
|
|
err_errno(IOError);
|
1990-10-14 12:07:46 +00:00
|
|
|
DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (object *)f;
|
|
|
|
}
|
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
void
|
|
|
|
setfilebufsize(f, bufsize)
|
|
|
|
object *f;
|
|
|
|
int bufsize;
|
|
|
|
{
|
|
|
|
if (bufsize >= 0) {
|
|
|
|
#ifdef HAVE_SETVBUF
|
|
|
|
int type;
|
|
|
|
switch (bufsize) {
|
|
|
|
case 0:
|
|
|
|
type = _IONBF;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
type = _IOLBF;
|
|
|
|
bufsize = BUFSIZ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = _IOFBF;
|
|
|
|
}
|
|
|
|
setvbuf(((fileobject *)f)->f_fp, (char *)NULL, type, bufsize);
|
|
|
|
#endif /* HAVE_SETVBUF */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
static object *
|
|
|
|
err_closed()
|
|
|
|
{
|
|
|
|
err_setstr(ValueError, "I/O operation on closed file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
1990-12-20 15:06:42 +00:00
|
|
|
file_dealloc(f)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
if (f->f_fp != NULL && f->f_close != NULL) {
|
|
|
|
BGN_SAVE
|
1991-06-04 19:37:39 +00:00
|
|
|
(*f->f_close)(f->f_fp);
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f->f_name != NULL)
|
|
|
|
DECREF(f->f_name);
|
|
|
|
if (f->f_mode != NULL)
|
|
|
|
DECREF(f->f_mode);
|
|
|
|
free((char *)f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_repr(f)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
{
|
|
|
|
char buf[300];
|
1993-01-10 18:33:56 +00:00
|
|
|
sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
|
1990-10-14 12:07:46 +00:00
|
|
|
f->f_fp == NULL ? "closed" : "open",
|
|
|
|
getstringvalue(f->f_name),
|
1993-01-10 18:33:56 +00:00
|
|
|
getstringvalue(f->f_mode),
|
|
|
|
(long)f);
|
1990-10-14 12:07:46 +00:00
|
|
|
return newstringobject(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_close(f, args)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1991-06-04 19:37:39 +00:00
|
|
|
int sts = 0;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (!getnoarg(args))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
if (f->f_fp != NULL) {
|
1992-08-05 19:58:53 +00:00
|
|
|
if (f->f_close != NULL) {
|
|
|
|
BGN_SAVE
|
|
|
|
errno = 0;
|
1991-06-04 19:37:39 +00:00
|
|
|
sts = (*f->f_close)(f->f_fp);
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
f->f_fp = NULL;
|
|
|
|
}
|
1992-03-04 16:39:24 +00:00
|
|
|
if (sts == EOF)
|
1991-12-10 14:00:03 +00:00
|
|
|
return err_errno(IOError);
|
1991-06-04 19:37:39 +00:00
|
|
|
if (sts != 0)
|
|
|
|
return newintobject((long)sts);
|
1990-10-14 12:07:46 +00:00
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_seek(f, args)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1991-03-06 13:06:18 +00:00
|
|
|
long offset;
|
1992-07-06 14:19:26 +00:00
|
|
|
int whence;
|
1992-08-05 19:58:53 +00:00
|
|
|
int ret;
|
1991-03-06 13:06:18 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
whence = 0;
|
|
|
|
if (!getargs(args, "l", &offset)) {
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(li)", &offset, &whence))
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
ret = fseek(f->f_fp, offset, whence);
|
|
|
|
END_SAVE
|
|
|
|
if (ret != 0) {
|
1992-03-04 16:39:24 +00:00
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
|
|
|
static object *
|
|
|
|
file_truncate(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
long newsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!getargs(args, "l", &newsize)) {
|
|
|
|
err_clear();
|
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
BGN_SAVE
|
|
|
|
errno = 0;
|
|
|
|
newsize = ftell(f->f_fp); /* default to current position*/
|
|
|
|
END_SAVE
|
|
|
|
if (newsize == -1L) {
|
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BGN_SAVE
|
|
|
|
errno = 0;
|
|
|
|
ret = fflush(f->f_fp);
|
|
|
|
END_SAVE
|
|
|
|
if (ret == 0) {
|
|
|
|
BGN_SAVE
|
|
|
|
errno = 0;
|
|
|
|
ret = ftruncate(fileno(f->f_fp), newsize);
|
|
|
|
END_SAVE
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_FTRUNCATE */
|
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
static object *
|
|
|
|
file_tell(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
long offset;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!getnoarg(args))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
|
|
|
offset = ftell(f->f_fp);
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
if (offset == -1L) {
|
1992-03-04 16:39:24 +00:00
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
return newintobject(offset);
|
|
|
|
}
|
|
|
|
|
1992-06-23 09:07:03 +00:00
|
|
|
static object *
|
|
|
|
file_fileno(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1992-06-23 09:07:03 +00:00
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
|
|
|
return newintobject((long) fileno(f->f_fp));
|
|
|
|
}
|
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
static object *
|
|
|
|
file_flush(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
int res;
|
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!getnoarg(args))
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
res = fflush(f->f_fp);
|
|
|
|
END_SAVE
|
|
|
|
if (res != 0) {
|
1992-03-04 16:39:24 +00:00
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1991-06-04 19:37:39 +00:00
|
|
|
static object *
|
|
|
|
file_isatty(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
long res;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!getnoarg(args))
|
1991-06-04 19:37:39 +00:00
|
|
|
return NULL;
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
|
|
|
res = isatty((int)fileno(f->f_fp));
|
|
|
|
END_SAVE
|
|
|
|
return newintobject(res);
|
1991-06-04 19:37:39 +00:00
|
|
|
}
|
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
static object *
|
|
|
|
file_read(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int n, n1, n2, n3;
|
|
|
|
object *v;
|
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1994-08-01 11:34:53 +00:00
|
|
|
if (args == NULL)
|
1995-07-10 23:32:26 +00:00
|
|
|
n = -1;
|
1994-08-01 11:34:53 +00:00
|
|
|
else {
|
|
|
|
if (!getargs(args, "i", &n))
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1995-07-10 23:32:26 +00:00
|
|
|
n2 = n >= 0 ? n : BUFSIZ;
|
1991-03-06 13:06:18 +00:00
|
|
|
v = newsizedstringobject((char *)NULL, n2);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
n1 = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
for (;;) {
|
|
|
|
n3 = fread(BUF(v)+n1, 1, n2-n1, f->f_fp);
|
|
|
|
/* XXX Error check? */
|
|
|
|
if (n3 == 0)
|
|
|
|
break;
|
|
|
|
n1 += n3;
|
|
|
|
if (n1 == n)
|
|
|
|
break;
|
1995-07-10 23:32:26 +00:00
|
|
|
if (n < 0) {
|
1991-03-06 13:06:18 +00:00
|
|
|
n2 = n1 + BUFSIZ;
|
1992-08-05 19:58:53 +00:00
|
|
|
RET_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
if (resizestring(&v, n2) < 0)
|
|
|
|
return NULL;
|
1992-08-05 19:58:53 +00:00
|
|
|
RES_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
if (n1 != n2)
|
|
|
|
resizestring(&v, n1);
|
1990-10-14 12:07:46 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1991-04-04 15:21:57 +00:00
|
|
|
/* Internal routine to get a line.
|
|
|
|
Size argument interpretation:
|
|
|
|
> 0: max length;
|
|
|
|
= 0: read arbitrary line;
|
|
|
|
< 0: strip trailing '\n', raise EOFError if EOF reached immediately
|
1991-03-06 13:06:18 +00:00
|
|
|
*/
|
|
|
|
|
1993-06-17 12:35:49 +00:00
|
|
|
static object *
|
1991-04-04 15:21:57 +00:00
|
|
|
getline(f, n)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
1991-04-04 15:21:57 +00:00
|
|
|
int n;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-03-06 13:06:18 +00:00
|
|
|
register FILE *fp;
|
|
|
|
register int c;
|
|
|
|
register char *buf, *end;
|
1991-04-04 15:21:57 +00:00
|
|
|
int n1, n2;
|
1990-10-14 12:07:46 +00:00
|
|
|
object *v;
|
1991-04-04 15:21:57 +00:00
|
|
|
|
1992-07-31 12:42:38 +00:00
|
|
|
fp = f->f_fp;
|
1991-04-04 15:21:57 +00:00
|
|
|
n2 = n > 0 ? n : 100;
|
1991-03-06 13:06:18 +00:00
|
|
|
v = newsizedstringobject((char *)NULL, n2);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
buf = BUF(v);
|
|
|
|
end = buf + n2;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
for (;;) {
|
1991-04-04 15:21:57 +00:00
|
|
|
if ((c = getc(fp)) == EOF) {
|
1991-06-03 10:54:55 +00:00
|
|
|
clearerr(fp);
|
1994-08-01 11:34:53 +00:00
|
|
|
if (sigcheck()) {
|
1992-08-05 19:58:53 +00:00
|
|
|
RET_SAVE
|
1991-04-04 15:21:57 +00:00
|
|
|
DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (n < 0 && buf == BUF(v)) {
|
1992-08-05 19:58:53 +00:00
|
|
|
RET_SAVE
|
1991-04-04 15:21:57 +00:00
|
|
|
DECREF(v);
|
1991-12-24 13:26:41 +00:00
|
|
|
err_setstr(EOFError,
|
|
|
|
"EOF when reading a line");
|
1991-04-04 15:21:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-03-06 13:06:18 +00:00
|
|
|
break;
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
if ((*buf++ = c) == '\n') {
|
|
|
|
if (n < 0)
|
|
|
|
buf--;
|
|
|
|
break;
|
|
|
|
}
|
1991-03-06 13:06:18 +00:00
|
|
|
if (buf == end) {
|
1991-04-04 15:21:57 +00:00
|
|
|
if (n > 0)
|
1991-03-06 13:06:18 +00:00
|
|
|
break;
|
|
|
|
n1 = n2;
|
|
|
|
n2 += 1000;
|
1992-08-05 19:58:53 +00:00
|
|
|
RET_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
if (resizestring(&v, n2) < 0)
|
|
|
|
return NULL;
|
1992-08-05 19:58:53 +00:00
|
|
|
RES_SAVE
|
1991-03-06 13:06:18 +00:00
|
|
|
buf = BUF(v) + n1;
|
|
|
|
end = BUF(v) + n2;
|
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
n1 = buf - BUF(v);
|
|
|
|
if (n1 != n2)
|
|
|
|
resizestring(&v, n1);
|
1990-10-14 12:07:46 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1991-04-04 15:21:57 +00:00
|
|
|
/* External C interface */
|
|
|
|
|
|
|
|
object *
|
|
|
|
filegetline(f, n)
|
|
|
|
object *f;
|
|
|
|
int n;
|
|
|
|
{
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL) {
|
1991-04-04 15:21:57 +00:00
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
if (!is_fileobject(f)) {
|
|
|
|
object *reader;
|
|
|
|
object *args;
|
|
|
|
object *result;
|
|
|
|
reader = getattr(f, "readline");
|
|
|
|
if (reader == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (n <= 0)
|
|
|
|
args = mkvalue("()");
|
|
|
|
else
|
|
|
|
args = mkvalue("(i)", n);
|
|
|
|
if (args == NULL) {
|
|
|
|
DECREF(reader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = call_object(reader, args);
|
|
|
|
DECREF(reader);
|
|
|
|
DECREF(args);
|
|
|
|
if (result != NULL && !is_stringobject(result)) {
|
|
|
|
DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"object.readline() returned non-string");
|
|
|
|
}
|
|
|
|
if (n < 0 && result != NULL) {
|
|
|
|
char *s = getstringvalue(result);
|
|
|
|
int len = getstringsize(result);
|
|
|
|
if (len == 0) {
|
|
|
|
DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
err_setstr(EOFError,
|
|
|
|
"EOF when reading a line");
|
|
|
|
}
|
|
|
|
else if (s[len-1] == '\n') {
|
|
|
|
if (result->ob_refcnt == 1)
|
|
|
|
resizestring(&result, len-1);
|
|
|
|
else {
|
|
|
|
object *v;
|
1992-12-22 14:24:04 +00:00
|
|
|
v = newsizedstringobject(s, len-1);
|
1992-09-25 21:59:05 +00:00
|
|
|
DECREF(result);
|
|
|
|
result = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1992-07-06 14:19:26 +00:00
|
|
|
if (((fileobject*)f)->f_fp == NULL)
|
|
|
|
return err_closed();
|
1991-04-04 15:21:57 +00:00
|
|
|
return getline((fileobject *)f, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Python method */
|
|
|
|
|
|
|
|
static object *
|
|
|
|
file_readline(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1991-04-04 15:21:57 +00:00
|
|
|
if (args == NULL)
|
|
|
|
n = 0; /* Unlimited */
|
|
|
|
else {
|
|
|
|
if (!getintarg(args, &n))
|
|
|
|
return NULL;
|
1995-07-10 23:32:26 +00:00
|
|
|
if (n == 0)
|
|
|
|
return newstringobject("");
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
|
1992-03-27 17:23:38 +00:00
|
|
|
return getline(f, n);
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
static object *
|
|
|
|
file_readlines(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *list;
|
|
|
|
object *line;
|
1991-04-04 15:21:57 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1991-04-04 15:21:57 +00:00
|
|
|
if (!getnoarg(args))
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
if ((list = newlistobject(0)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (;;) {
|
1991-04-04 15:21:57 +00:00
|
|
|
line = getline(f, 0);
|
1991-03-06 13:06:18 +00:00
|
|
|
if (line != NULL && getstringsize(line) == 0) {
|
|
|
|
DECREF(line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line == NULL || addlistitem(list, line) != 0) {
|
|
|
|
DECREF(list);
|
|
|
|
XDECREF(line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
DECREF(line);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
static object *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_write(f, args)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
1992-07-06 14:19:26 +00:00
|
|
|
char *s;
|
1990-10-14 12:07:46 +00:00
|
|
|
int n, n2;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!getargs(args, "s#", &s, &n))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-04-04 10:44:06 +00:00
|
|
|
f->f_softspace = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1990-10-14 12:07:46 +00:00
|
|
|
errno = 0;
|
1992-07-06 14:19:26 +00:00
|
|
|
n2 = fwrite(s, 1, n, f->f_fp);
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1990-10-14 12:07:46 +00:00
|
|
|
if (n2 != n) {
|
1991-12-10 14:00:03 +00:00
|
|
|
err_errno(IOError);
|
1992-03-04 16:39:24 +00:00
|
|
|
clearerr(f->f_fp);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1993-10-25 09:59:04 +00:00
|
|
|
static object *
|
|
|
|
file_writelines(f, args)
|
|
|
|
fileobject *f;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (args == NULL || !is_listobject(args)) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"writelines() requires list of strings");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
n = getlistsize(args);
|
|
|
|
f->f_softspace = 0;
|
|
|
|
BGN_SAVE
|
|
|
|
errno = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *line = getlistitem(args, i);
|
|
|
|
int len;
|
|
|
|
int nwritten;
|
|
|
|
if (!is_stringobject(line)) {
|
|
|
|
RET_SAVE
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"writelines() requires list of strings");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
len = getstringsize(line);
|
|
|
|
nwritten = fwrite(getstringvalue(line), 1, len, f->f_fp);
|
|
|
|
if (nwritten != len) {
|
|
|
|
RET_SAVE
|
|
|
|
err_errno(IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_SAVE
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static struct methodlist file_methods[] = {
|
1995-02-19 15:55:19 +00:00
|
|
|
{"close", (method)file_close, 0},
|
|
|
|
{"flush", (method)file_flush, 0},
|
|
|
|
{"fileno", (method)file_fileno, 0},
|
|
|
|
{"isatty", (method)file_isatty, 0},
|
|
|
|
{"read", (method)file_read, 0},
|
|
|
|
{"readline", (method)file_readline, 0},
|
|
|
|
{"readlines", (method)file_readlines, 0},
|
|
|
|
{"seek", (method)file_seek, 0},
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
1995-02-19 15:55:19 +00:00
|
|
|
{"truncate", (method)file_truncate, 0},
|
1995-01-02 19:07:15 +00:00
|
|
|
#endif
|
1995-02-19 15:55:19 +00:00
|
|
|
{"tell", (method)file_tell, 0},
|
|
|
|
{"write", (method)file_write, 0},
|
|
|
|
{"writelines", (method)file_writelines, 0},
|
1990-10-14 12:07:46 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
#define OFF(x) offsetof(fileobject, x)
|
|
|
|
|
|
|
|
static struct memberlist file_memberlist[] = {
|
|
|
|
{"softspace", T_INT, OFF(f_softspace)},
|
|
|
|
{"mode", T_OBJECT, OFF(f_mode), RO},
|
|
|
|
{"name", T_OBJECT, OFF(f_name), RO},
|
|
|
|
/* getattr(f, "closed") is implemented without this table */
|
|
|
|
{"closed", T_INT, 0, RO},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
static object *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_getattr(f, name)
|
1990-10-14 12:07:46 +00:00
|
|
|
fileobject *f;
|
|
|
|
char *name;
|
|
|
|
{
|
1994-08-01 11:34:53 +00:00
|
|
|
object *res;
|
|
|
|
|
|
|
|
res = findmethod(file_methods, (object *)f, name);
|
|
|
|
if (res != NULL)
|
|
|
|
return res;
|
|
|
|
err_clear();
|
|
|
|
if (strcmp(name, "closed") == 0)
|
|
|
|
return newintobject((long)(f->f_fp == 0));
|
|
|
|
return getmember((char *)f, file_memberlist, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_setattr(f, name, v)
|
|
|
|
fileobject *f;
|
|
|
|
char *name;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL) {
|
|
|
|
err_setstr(AttributeError, "can't delete file attributes");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return setmember((char *)f, file_memberlist, name, v);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typeobject Filetype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"file",
|
|
|
|
sizeof(fileobject),
|
|
|
|
0,
|
1994-08-01 11:34:53 +00:00
|
|
|
(destructor)file_dealloc, /*tp_dealloc*/
|
1992-09-17 17:54:56 +00:00
|
|
|
0, /*tp_print*/
|
1994-08-01 11:34:53 +00:00
|
|
|
(getattrfunc)file_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)file_setattr, /*tp_setattr*/
|
1990-10-14 12:07:46 +00:00
|
|
|
0, /*tp_compare*/
|
1994-08-01 11:34:53 +00:00
|
|
|
(reprfunc)file_repr, /*tp_repr*/
|
1990-10-14 12:07:46 +00:00
|
|
|
};
|
1991-04-04 10:44:06 +00:00
|
|
|
|
|
|
|
/* Interface for the 'soft space' between print items. */
|
|
|
|
|
|
|
|
int
|
|
|
|
softspace(f, newflag)
|
|
|
|
object *f;
|
|
|
|
int newflag;
|
|
|
|
{
|
|
|
|
int oldflag = 0;
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
1993-11-01 16:26:16 +00:00
|
|
|
else if (is_fileobject(f)) {
|
1991-04-04 10:44:06 +00:00
|
|
|
oldflag = ((fileobject *)f)->f_softspace;
|
|
|
|
((fileobject *)f)->f_softspace = newflag;
|
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
else {
|
|
|
|
object *v;
|
|
|
|
v = getattr(f, "softspace");
|
|
|
|
if (v == NULL)
|
|
|
|
err_clear();
|
|
|
|
else {
|
|
|
|
if (is_intobject(v))
|
|
|
|
oldflag = getintvalue(v);
|
|
|
|
DECREF(v);
|
|
|
|
}
|
|
|
|
v = newintobject((long)newflag);
|
|
|
|
if (v == NULL)
|
|
|
|
err_clear();
|
|
|
|
else {
|
|
|
|
if (setattr(f, "softspace", v) != 0)
|
|
|
|
err_clear();
|
|
|
|
DECREF(v);
|
|
|
|
}
|
|
|
|
}
|
1991-04-04 10:44:06 +00:00
|
|
|
return oldflag;
|
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
|
|
|
|
/* Interfaces to write objects/strings to file-like objects */
|
|
|
|
|
|
|
|
int
|
|
|
|
writeobject(v, f, flags)
|
|
|
|
object *v;
|
|
|
|
object *f;
|
|
|
|
int flags;
|
|
|
|
{
|
1995-07-10 23:32:26 +00:00
|
|
|
object *writer, *value, *args, *result;
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
err_setstr(TypeError, "writeobject with NULL file");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (is_fileobject(f)) {
|
|
|
|
FILE *fp = getfilefile(f);
|
|
|
|
if (fp == NULL) {
|
|
|
|
err_closed();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return printobject(v, fp, flags);
|
|
|
|
}
|
|
|
|
writer = getattr(f, "write");
|
|
|
|
if (writer == NULL)
|
|
|
|
return -1;
|
1993-11-05 10:22:19 +00:00
|
|
|
if (flags & PRINT_RAW)
|
|
|
|
value = strobject(v);
|
|
|
|
else
|
1992-09-25 21:59:05 +00:00
|
|
|
value = reprobject(v);
|
1993-11-05 10:22:19 +00:00
|
|
|
if (value == NULL) {
|
|
|
|
DECREF(writer);
|
|
|
|
return -1;
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
1995-07-10 23:32:26 +00:00
|
|
|
args = mkvalue("(O)", value);
|
|
|
|
if (value == NULL) {
|
|
|
|
DECREF(value);
|
|
|
|
DECREF(writer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result = call_object(writer, args);
|
|
|
|
DECREF(args);
|
1992-09-25 21:59:05 +00:00
|
|
|
DECREF(value);
|
1995-07-10 23:32:26 +00:00
|
|
|
DECREF(writer);
|
1992-09-25 21:59:05 +00:00
|
|
|
if (result == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
writestring(s, f)
|
|
|
|
char *s;
|
|
|
|
object *f;
|
|
|
|
{
|
|
|
|
if (f == NULL) {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
else if (is_fileobject(f)) {
|
|
|
|
FILE *fp = getfilefile(f);
|
|
|
|
if (fp != NULL)
|
|
|
|
fputs(s, fp);
|
|
|
|
}
|
1994-08-01 11:34:53 +00:00
|
|
|
else if (!err_occurred()) {
|
1992-09-25 21:59:05 +00:00
|
|
|
object *v = newstringobject(s);
|
|
|
|
if (v == NULL) {
|
|
|
|
err_clear();
|
|
|
|
}
|
|
|
|
else {
|
1993-01-21 16:07:51 +00:00
|
|
|
if (writeobject(v, f, PRINT_RAW) != 0)
|
1992-09-25 21:59:05 +00:00
|
|
|
err_clear();
|
|
|
|
DECREF(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|