summaryrefslogtreecommitdiffstats
path: root/Objects/fileobject.c
blob: 09690c65f8172134a3eea022ab6310dff86ea39e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* File object implementation */

/* XXX This should become a built-in module 'io'.  It should support more
   functionality, better exception handling for invalid calls, etc.
   (Especially reading on a write-only file or vice versa!)
   It should also cooperate with posix to support popen(), which should
   share most code but have a special close function. */

#include "allobjects.h"

#include "errno.h"
#ifndef errno
extern int errno;
#endif

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)) {
		err_badcall();
		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;
	if ((f->f_fp = fopen(name, mode)) == NULL) {
		DECREF(f);
		err_errno(RuntimeError); /* XXX Should use another error */
		return NULL;
	}
	return (object *)f;
}

/* Methods */

static void
file_dealloc(f)
	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
file_print(f, fp, flags)
	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 *
file_repr(f)
	fileobject *f;
{
	char buf[300];
	/* XXX This differs from file_print if the filename contains
	   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 *
file_close(f, args)
	fileobject *f;
	object *args;
{
	if (args != NULL) {
		err_badarg();
		return NULL;
	}
	if (f->f_fp != NULL) {
		fclose(f->f_fp);
		f->f_fp = NULL;
	}
	INCREF(None);
	return None;
}

static object *
file_read(f, args)
	fileobject *f;
	object *args;
{
	int n;
	object *v;
	if (f->f_fp == NULL) {
		err_badarg();
		return NULL;
	}
	if (args == NULL || !is_intobject(args)) {
		err_badarg();
		return NULL;
	}
	n = getintvalue(args);
	if (n < 0) {
		err_badarg();
		return NULL;
	}
	v = newsizedstringobject((char *)NULL, n);
	if (v == NULL)
		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 *
file_readline(f, args)
	fileobject *f;
	object *args;
{
	int n;
	object *v;
	if (f->f_fp == NULL) {
		err_badarg();
		return NULL;
	}
	if (args == NULL) {
		n = 10000; /* XXX should really be unlimited */
	}
	else if (is_intobject(args)) {
		n = getintvalue(args);
		if (n < 0) {
			err_badarg();
			return NULL;
		}
	}
	else {
		err_badarg();
		return NULL;
	}
	v = newsizedstringobject((char *)NULL, n);
	if (v == NULL)
		return NULL;
#ifndef THINK_C
	/* XXX Think C reads n characters, others read n-1 characters... */
	n = n+1;
#endif
	if (fgets(getstringvalue(v), n, f->f_fp) == NULL) {
		/* 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 *
file_write(f, args)
	fileobject *f;
	object *args;
{
	int n, n2;
	if (f->f_fp == NULL) {
		err_badarg();
		return NULL;
	}
	if (args == NULL || !is_stringobject(args)) {
		err_badarg();
		return NULL;
	}
	errno = 0;
	n2 = fwrite(getstringvalue(args), 1, n = getstringsize(args), f->f_fp);
	if (n2 != n) {
		if (errno == 0)
			errno = EIO;
		err_errno(RuntimeError);
		return NULL;
	}
	INCREF(None);
	return None;
}

static struct methodlist file_methods[] = {
	{"write",	file_write},
	{"read",	file_read},
	{"readline",	file_readline},
	{"close",	file_close},
	{NULL,		NULL}		/* sentinel */
};

static object *
file_getattr(f, name)
	fileobject *f;
	char *name;
{
	return findmethod(file_methods, (object *)f, name);
}

typeobject Filetype = {
	OB_HEAD_INIT(&Typetype)
	0,
	"file",
	sizeof(fileobject),
	0,
	file_dealloc,	/*tp_dealloc*/
	file_print,	/*tp_print*/
	file_getattr,	/*tp_getattr*/
	0,		/*tp_setattr*/
	0,		/*tp_compare*/
	file_repr,	/*tp_repr*/
};