source: trunk/essentials/dev-lang/python/Mac/Modules/MacOS.c

Last change on this file was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 14.5 KB
Line 
1/***********************************************************
2Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Macintosh OS-specific interface */
26
27#include "Python.h"
28#include "pymactoolbox.h"
29
30#include <Carbon/Carbon.h>
31#include <ApplicationServices/ApplicationServices.h>
32
33static PyObject *MacOS_Error; /* Exception MacOS.Error */
34
35#define PATHNAMELEN 1024
36
37/* ----------------------------------------------------- */
38
39/* Declarations for objects of type Resource fork */
40
41typedef struct {
42 PyObject_HEAD
43 short fRefNum;
44 int isclosed;
45} rfobject;
46
47static PyTypeObject Rftype;
48
49
50
51/* ---------------------------------------------------------------- */
52
53static void
54do_close(rfobject *self)
55{
56 if (self->isclosed ) return;
57 (void)FSClose(self->fRefNum);
58 self->isclosed = 1;
59}
60
61static char rf_read__doc__[] =
62"Read data from resource fork"
63;
64
65static PyObject *
66rf_read(rfobject *self, PyObject *args)
67{
68 long n;
69 PyObject *v;
70 OSErr err;
71
72 if (self->isclosed) {
73 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
74 return NULL;
75 }
76
77 if (!PyArg_ParseTuple(args, "l", &n))
78 return NULL;
79
80 v = PyString_FromStringAndSize((char *)NULL, n);
81 if (v == NULL)
82 return NULL;
83
84 err = FSRead(self->fRefNum, &n, PyString_AsString(v));
85 if (err && err != eofErr) {
86 PyMac_Error(err);
87 Py_DECREF(v);
88 return NULL;
89 }
90 _PyString_Resize(&v, n);
91 return v;
92}
93
94
95static char rf_write__doc__[] =
96"Write to resource fork"
97;
98
99static PyObject *
100rf_write(rfobject *self, PyObject *args)
101{
102 char *buffer;
103 long size;
104 OSErr err;
105
106 if (self->isclosed) {
107 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
108 return NULL;
109 }
110 if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
111 return NULL;
112 err = FSWrite(self->fRefNum, &size, buffer);
113 if (err) {
114 PyMac_Error(err);
115 return NULL;
116 }
117 Py_INCREF(Py_None);
118 return Py_None;
119}
120
121
122static char rf_seek__doc__[] =
123"Set file position"
124;
125
126static PyObject *
127rf_seek(rfobject *self, PyObject *args)
128{
129 long amount, pos;
130 int whence = SEEK_SET;
131 long eof;
132 OSErr err;
133
134 if (self->isclosed) {
135 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
136 return NULL;
137 }
138 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
139 return NULL;
140
141 if ((err = GetEOF(self->fRefNum, &eof)))
142 goto ioerr;
143
144 switch (whence) {
145 case SEEK_CUR:
146 if ((err = GetFPos(self->fRefNum, &pos)))
147 goto ioerr;
148 break;
149 case SEEK_END:
150 pos = eof;
151 break;
152 case SEEK_SET:
153 pos = 0;
154 break;
155 default:
156 PyErr_BadArgument();
157 return NULL;
158 }
159
160 pos += amount;
161
162 /* Don't bother implementing seek past EOF */
163 if (pos > eof || pos < 0) {
164 PyErr_BadArgument();
165 return NULL;
166 }
167
168 if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
169ioerr:
170 PyMac_Error(err);
171 return NULL;
172 }
173 Py_INCREF(Py_None);
174 return Py_None;
175}
176
177
178static char rf_tell__doc__[] =
179"Get file position"
180;
181
182static PyObject *
183rf_tell(rfobject *self, PyObject *args)
184{
185 long where;
186 OSErr err;
187
188 if (self->isclosed) {
189 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
190 return NULL;
191 }
192 if (!PyArg_ParseTuple(args, ""))
193 return NULL;
194 if ((err = GetFPos(self->fRefNum, &where)) ) {
195 PyMac_Error(err);
196 return NULL;
197 }
198 return PyInt_FromLong(where);
199}
200
201static char rf_close__doc__[] =
202"Close resource fork"
203;
204
205static PyObject *
206rf_close(rfobject *self, PyObject *args)
207{
208 if (!PyArg_ParseTuple(args, ""))
209 return NULL;
210 do_close(self);
211 Py_INCREF(Py_None);
212 return Py_None;
213}
214
215
216static struct PyMethodDef rf_methods[] = {
217 {"read", (PyCFunction)rf_read, 1, rf_read__doc__},
218 {"write", (PyCFunction)rf_write, 1, rf_write__doc__},
219 {"seek", (PyCFunction)rf_seek, 1, rf_seek__doc__},
220 {"tell", (PyCFunction)rf_tell, 1, rf_tell__doc__},
221 {"close", (PyCFunction)rf_close, 1, rf_close__doc__},
222
223 {NULL, NULL} /* sentinel */
224};
225
226/* ---------- */
227
228
229static rfobject *
230newrfobject(void)
231{
232 rfobject *self;
233
234 self = PyObject_NEW(rfobject, &Rftype);
235 if (self == NULL)
236 return NULL;
237 self->isclosed = 1;
238 return self;
239}
240
241
242static void
243rf_dealloc(rfobject *self)
244{
245 do_close(self);
246 PyObject_DEL(self);
247}
248
249static PyObject *
250rf_getattr(rfobject *self, char *name)
251{
252 return Py_FindMethod(rf_methods, (PyObject *)self, name);
253}
254
255static char Rftype__doc__[] =
256"Resource fork file object"
257;
258
259static PyTypeObject Rftype = {
260 PyObject_HEAD_INIT(&PyType_Type)
261 0, /*ob_size*/
262 "MacOS.ResourceFork", /*tp_name*/
263 sizeof(rfobject), /*tp_basicsize*/
264 0, /*tp_itemsize*/
265 /* methods */
266 (destructor)rf_dealloc, /*tp_dealloc*/
267 (printfunc)0, /*tp_print*/
268 (getattrfunc)rf_getattr, /*tp_getattr*/
269 (setattrfunc)0, /*tp_setattr*/
270 (cmpfunc)0, /*tp_compare*/
271 (reprfunc)0, /*tp_repr*/
272 0, /*tp_as_number*/
273 0, /*tp_as_sequence*/