blob: 0db402227458d865de183b097f14d2a1732b4fc2 [file] [log] [blame]
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossumefd97671995-01-26 22:56:16 +00003The Netherlands.
Jack Jansen84fa5ec1995-01-18 14:04:40 +00004
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
Jack Jansenf5c20571997-01-30 15:48:07 +000025#include "Python.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000026#include "macglue.h"
27
Guido van Rossumbecdbec1995-02-14 01:27:24 +000028#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000029#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000030#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000031#include <StandardFile.h>
32#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000033#include <LowMem.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000034
Jack Jansen924ca851996-09-20 15:25:16 +000035#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000036
Guido van Rossuma42960c1995-01-19 00:03:47 +000037#ifdef THINK_C
38#define FileFilterUPP FileFilterProcPtr
39#endif
40
Jack Jansenf5c20571997-01-30 15:48:07 +000041static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000042
43/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000044/* Declarations for objects of type Alias */
45
46typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000047 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000048 AliasHandle alias;
49} mfsaobject;
50
Jack Jansenf5c20571997-01-30 15:48:07 +000051staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000052
53#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
54
55/* ---------------------------------------------------------------- */
56/* Declarations for objects of type FSSpec */
57
58typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000059 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000060 FSSpec fsspec;
61} mfssobject;
62
Jack Jansenf5c20571997-01-30 15:48:07 +000063staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000064
65#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
66
Guido van Rossumefd97671995-01-26 22:56:16 +000067
Jack Jansen3d185931995-08-07 14:04:10 +000068/* ---------------------------------------------------------------- */
69/* Declarations for objects of type FInfo */
70
71typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000072 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000073 FInfo finfo;
74} mfsiobject;
75
Jack Jansenf5c20571997-01-30 15:48:07 +000076staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +000077
78#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
79
80
Guido van Rossumefd97671995-01-26 22:56:16 +000081mfssobject *newmfssobject(FSSpec *fss); /* Forward */
82
Jack Jansen17ba43f1995-01-26 16:22:07 +000083/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +000084
Jack Jansenf5c20571997-01-30 15:48:07 +000085static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +000086mfsa_Resolve(self, args)
87 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +000088 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000089{
Jack Jansen17ba43f1995-01-26 16:22:07 +000090 FSSpec from, *fromp, result;
91 Boolean changed;
92 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000093
Jack Jansen17ba43f1995-01-26 16:22:07 +000094 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +000095 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +000096 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +000097 if (from.name[0] )
98 fromp = &from;
99 else
100 fromp = NULL;
101 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000102 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000103 PyErr_Mac(ErrorObject, err);
104 return NULL;
105 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000106 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000107}
108
Jack Jansenf5c20571997-01-30 15:48:07 +0000109static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000110mfsa_GetInfo(self, args)
111 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000112 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000113{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000114 Str63 value;
115 int i;
116 OSErr err;
117
Jack Jansenf5c20571997-01-30 15:48:07 +0000118 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000119 return NULL;
120 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
121 if ( err ) {
122 PyErr_Mac(ErrorObject, err);
123 return 0;
124 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000125 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000126}
127
Jack Jansenf5c20571997-01-30 15:48:07 +0000128static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000129mfsa_Update(self, args)
130 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000131 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000132{
133 FSSpec target, fromfile, *fromfilep;
134 OSErr err;
135 Boolean changed;
136
137 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000139 PyMac_GetFSSpec, &fromfile))
140 return NULL;
141 if ( fromfile.name[0] )
142 fromfilep = &fromfile;
143 else
144 fromfilep = NULL;
145 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
146 if ( err ) {
147 PyErr_Mac(ErrorObject, err);
148 return 0;
149 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000151}
152
Jack Jansenf5c20571997-01-30 15:48:07 +0000153static struct PyMethodDef mfsa_methods[] = {
154 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
155 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
156 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000157
158 {NULL, NULL} /* sentinel */
159};
160
161/* ---------- */
162
Jack Jansenf5c20571997-01-30 15:48:07 +0000163static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000164mfsa_getattr(self, name)
165 mfsaobject *self;
166 char *name;
167{
Jack Jansenc889b761995-02-13 12:00:46 +0000168 if ( strcmp(name, "data") == 0 ) {
169 int size;
170 PyObject *rv;
171
172 size = GetHandleSize((Handle)self->alias);
173 HLock((Handle)self->alias);
174 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
175 HUnlock((Handle)self->alias);
176 return rv;
177 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000179}
180
181mfsaobject *
182newmfsaobject(alias)
183 AliasHandle alias;
184{
185 mfsaobject *self;
186
Jack Jansenf5c20571997-01-30 15:48:07 +0000187 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000188 if (self == NULL)
189 return NULL;
190 self->alias = alias;
191 return self;
192}
193
194
195static void
196mfsa_dealloc(self)
197 mfsaobject *self;
198{
199#if 0
200 if ( self->alias ) {
201 should we do something here?
202 }
203#endif
204
Jack Jansenf5c20571997-01-30 15:48:07 +0000205 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000206}
207
Jack Jansenf5c20571997-01-30 15:48:07 +0000208statichere PyTypeObject Mfsatype = {
209 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000210 0, /*ob_size*/
211 "Alias", /*tp_name*/
212 sizeof(mfsaobject), /*tp_basicsize*/
213 0, /*tp_itemsize*/
214 /* methods */
215 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000216 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000217 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000218 (setattrfunc)0, /*tp_setattr*/
219 (cmpfunc)0, /*tp_compare*/
220 (reprfunc)0, /*tp_repr*/
221 0, /*tp_as_number*/
222 0, /*tp_as_sequence*/
223 0, /*tp_as_mapping*/
224 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000225};
226
227/* End of code for Alias objects */
228/* -------------------------------------------------------- */
229
Jack Jansen3d185931995-08-07 14:04:10 +0000230/* ---------------------------------------------------------------- */
231
Jack Jansenf5c20571997-01-30 15:48:07 +0000232static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000233
234 {NULL, NULL} /* sentinel */
235};
236
237/* ---------- */
238
239static mfsiobject *
240newmfsiobject()
241{
242 mfsiobject *self;
243
Jack Jansenf5c20571997-01-30 15:48:07 +0000244 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000245 if (self == NULL)
246 return NULL;
247 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
248 return self;
249}
250
251static void
252mfsi_dealloc(self)
253 mfsiobject *self;
254{
Jack Jansenf5c20571997-01-30 15:48:07 +0000255 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000256}
257
Jack Jansenf5c20571997-01-30 15:48:07 +0000258static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000259mfsi_getattr(self, name)
260 mfsiobject *self;
261 char *name;
262{
Jack Jansen3d185931995-08-07 14:04:10 +0000263 if ( strcmp(name, "Type") == 0 )
264 return PyMac_BuildOSType(self->finfo.fdType);
265 else if ( strcmp(name, "Creator") == 0 )
266 return PyMac_BuildOSType(self->finfo.fdCreator);
267 else if ( strcmp(name, "Flags") == 0 )
268 return Py_BuildValue("i", (int)self->finfo.fdFlags);
269 else if ( strcmp(name, "Location") == 0 )
270 return PyMac_BuildPoint(self->finfo.fdLocation);
271 else if ( strcmp(name, "Fldr") == 0 )
272 return Py_BuildValue("i", (int)self->finfo.fdFldr);
273 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000274 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000275}
276
277
278static int
279mfsi_setattr(self, name, v)
280 mfsiobject *self;
281 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000282 PyObject *v;
Jack Jansen3d185931995-08-07 14:04:10 +0000283{
284 int rv;
285 int i;
286
287 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000289 return -1;
290 }
291 if ( strcmp(name, "Type") == 0 )
292 rv = PyMac_GetOSType(v, &self->finfo.fdType);
293 else if ( strcmp(name, "Creator") == 0 )
294 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
295 else if ( strcmp(name, "Flags") == 0 ) {
296 rv = PyArg_Parse(v, "i", &i);
297 self->finfo.fdFlags = (short)i;
298 } else if ( strcmp(name, "Location") == 0 )
299 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
300 else if ( strcmp(name, "Fldr") == 0 ) {
301 rv = PyArg_Parse(v, "i", &i);
302 self->finfo.fdFldr = (short)i;
303 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000304 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000305 return -1;
306 }
307 if (rv)
308 return 0;
309 return -1;
310}
311
312
Jack Jansenf5c20571997-01-30 15:48:07 +0000313static PyTypeObject Mfsitype = {
314 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000315 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000316 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000317 sizeof(mfsiobject), /*tp_basicsize*/
318 0, /*tp_itemsize*/
319 /* methods */
320 (destructor)mfsi_dealloc, /*tp_dealloc*/
321 (printfunc)0, /*tp_print*/
322 (getattrfunc)mfsi_getattr, /*tp_getattr*/
323 (setattrfunc)mfsi_setattr, /*tp_setattr*/
324 (cmpfunc)0, /*tp_compare*/
325 (reprfunc)0, /*tp_repr*/
326 0, /*tp_as_number*/
327 0, /*tp_as_sequence*/
328 0, /*tp_as_mapping*/
329 (hashfunc)0, /*tp_hash*/
330};
331
332/* End of code for FInfo object objects */
333/* -------------------------------------------------------- */
334
335
Jack Jansen17ba43f1995-01-26 16:22:07 +0000336/*
337** Helper routine for other modules: return an FSSpec * if the
338** object is a python fsspec object, else NULL
339*/
340FSSpec *
341mfs_GetFSSpecFSSpec(self)
Jack Jansenf5c20571997-01-30 15:48:07 +0000342 PyObject *self;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000343{
344 if ( is_mfssobject(self) )
345 return &((mfssobject *)self)->fsspec;
346 return NULL;
347}
348
Jack Jansen0bdf9791996-09-15 22:11:25 +0000349/*
350** Two generally useful routines
351*/
352static OSErr
353PyMac_GetFileDates(fss, crdat, mddat, bkdat)
354 FSSpec *fss;
355 unsigned long *crdat, *mddat, *bkdat;
356{
357 CInfoPBRec pb;
358 OSErr error;
359
360 pb.dirInfo.ioNamePtr = fss->name;
361 pb.dirInfo.ioFDirIndex = 0;
362 pb.dirInfo.ioVRefNum = fss->vRefNum;
363 pb.dirInfo.ioDrDirID = fss->parID;
364 error = PBGetCatInfoSync(&pb);
365 if ( error ) return error;
366 *crdat = pb.hFileInfo.ioFlCrDat;
367 *mddat = pb.hFileInfo.ioFlMdDat;
368 *bkdat = pb.hFileInfo.ioFlBkDat;
369 return 0;
370}
371
372static OSErr
373PyMac_SetFileDates(fss, crdat, mddat, bkdat)
374 FSSpec *fss;
375 unsigned long crdat, mddat, bkdat;
376{
377 CInfoPBRec pb;
378 OSErr error;
379
380 pb.dirInfo.ioNamePtr = fss->name;
381 pb.dirInfo.ioFDirIndex = 0;
382 pb.dirInfo.ioVRefNum = fss->vRefNum;
383 pb.dirInfo.ioDrDirID = fss->parID;
384 error = PBGetCatInfoSync(&pb);
385 if ( error ) return error;
386 pb.dirInfo.ioNamePtr = fss->name;
387 pb.dirInfo.ioFDirIndex = 0;
388 pb.dirInfo.ioVRefNum = fss->vRefNum;
389 pb.dirInfo.ioDrDirID = fss->parID;
390 pb.hFileInfo.ioFlCrDat = crdat;
391 pb.hFileInfo.ioFlMdDat = mddat;
392 pb.hFileInfo.ioFlBkDat = bkdat;
393 error = PBSetCatInfoSync(&pb);
394 return error;
395}
396
Jack Jansenf5c20571997-01-30 15:48:07 +0000397static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000398mfss_as_pathname(self, args)
399 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000400 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000401{
402 char strbuf[257];
403 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000404
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000406 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000407 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000408 if ( err ) {
409 PyErr_Mac(ErrorObject, err);
410 return NULL;
411 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000412 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000413}
414
Jack Jansenf5c20571997-01-30 15:48:07 +0000415static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000416mfss_as_tuple(self, args)
417 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000418 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000419{
Jack Jansenf5c20571997-01-30 15:48:07 +0000420 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000421 return NULL;
422 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
423 &self->fsspec.name[1], self->fsspec.name[0]);
424}
425
Jack Jansenf5c20571997-01-30 15:48:07 +0000426static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000427mfss_NewAlias(self, args)
428 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000429 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000430{
431 FSSpec src, *srcp;
432 OSErr err;
433 AliasHandle alias;
434
435 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000436 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000437 return NULL;
438 if ( src.name[0] )
439 srcp = &src;
440 else
441 srcp = NULL;
442 err = NewAlias(srcp, &self->fsspec, &alias);
443 if ( err ) {
444 PyErr_Mac(ErrorObject, err);
445 return NULL;
446 }
447
Jack Jansenf5c20571997-01-30 15:48:07 +0000448 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000449}
450
Jack Jansenf5c20571997-01-30 15:48:07 +0000451static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000452mfss_NewAliasMinimal(self, args)
453 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000454 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000455{
456 OSErr err;
457 AliasHandle alias;
458
Jack Jansenf5c20571997-01-30 15:48:07 +0000459 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000460 return NULL;
461 err = NewAliasMinimal(&self->fsspec, &alias);
462 if ( err ) {
463 PyErr_Mac(ErrorObject, err);
464 return NULL;
465 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000466 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000467}
468
Jack Jansen3d185931995-08-07 14:04:10 +0000469/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000470static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000471mfss_GetCreatorType(self, args)
472 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000473 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000474{
475 OSErr err;
476 FInfo info;
477
Jack Jansenf5c20571997-01-30 15:48:07 +0000478 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000479 return NULL;
480 err = FSpGetFInfo(&self->fsspec, &info);
481 if ( err ) {
482 PyErr_Mac(ErrorObject, err);
483 return NULL;
484 }
485 return Py_BuildValue("(O&O&)",
486 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
487}
488
Jack Jansenf5c20571997-01-30 15:48:07 +0000489static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000490mfss_SetCreatorType(self, args)
491 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000492 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000493{
494 OSErr err;
495 OSType creator, type;
496 FInfo info;
497
Jack Jansenf5c20571997-01-30 15:48:07 +0000498 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000499 return NULL;
500 err = FSpGetFInfo(&self->fsspec, &info);
501 if ( err ) {
502 PyErr_Mac(ErrorObject, err);
503 return NULL;
504 }
505 info.fdType = type;
506 info.fdCreator = creator;
507 err = FSpSetFInfo(&self->fsspec, &info);
508 if ( err ) {
509 PyErr_Mac(ErrorObject, err);
510 return NULL;
511 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000512 Py_INCREF(Py_None);
513 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000514}
515
Jack Jansenf5c20571997-01-30 15:48:07 +0000516static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000517mfss_GetFInfo(self, args)
518 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000519 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000520{
521 OSErr err;
522 mfsiobject *fip;
523
524
Jack Jansenf5c20571997-01-30 15:48:07 +0000525 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000526 return NULL;
527 if ( (fip=newmfsiobject()) == NULL )
528 return NULL;
529 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
530 if ( err ) {
531 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000532 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000533 return NULL;
534 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000535 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000536}
537
Jack Jansenf5c20571997-01-30 15:48:07 +0000538static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000539mfss_SetFInfo(self, args)
540 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000541 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000542{
543 OSErr err;
544 mfsiobject *fip;
545
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000547 return NULL;
548 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
549 if ( err ) {
550 PyErr_Mac(ErrorObject, err);
551 return NULL;
552 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000553 Py_INCREF(Py_None);
554 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000555}
556
Jack Jansenf5c20571997-01-30 15:48:07 +0000557static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000558mfss_GetDates(self, args)
559 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000560 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000561{
562 OSErr err;
563 unsigned long crdat, mddat, bkdat;
564
Jack Jansenf5c20571997-01-30 15:48:07 +0000565 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000566 return NULL;
567 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
568 if ( err ) {
569 PyErr_Mac(ErrorObject, err);
570 return NULL;
571 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000572 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000573}
574
Jack Jansenf5c20571997-01-30 15:48:07 +0000575static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000576mfss_SetDates(self, args)
577 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000578 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000579{
580 OSErr err;
581 double crdat, mddat, bkdat;
582
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000584 return NULL;
585 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
586 (unsigned long)mddat, (unsigned long)bkdat);
587 if ( err ) {
588 PyErr_Mac(ErrorObject, err);
589 return NULL;
590 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000591 Py_INCREF(Py_None);
592 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000593}
594
Jack Jansenf5c20571997-01-30 15:48:07 +0000595static struct PyMethodDef mfss_methods[] = {
596 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
597 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
598 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
599 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
600 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
601 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
602 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
603 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
604 {"GetDates", (PyCFunction)mfss_GetDates, 1},
605 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000606
607 {NULL, NULL} /* sentinel */
608};
609
610/* ---------- */
611
Jack Jansenf5c20571997-01-30 15:48:07 +0000612static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000613mfss_getattr(self, name)
614 mfssobject *self;
615 char *name;
616{
Jack Jansenc889b761995-02-13 12:00:46 +0000617 if ( strcmp(name, "data") == 0)
618 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000619 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000620}
621
622mfssobject *
623newmfssobject(fss)
624 FSSpec *fss;
625{
626 mfssobject *self;
627
Jack Jansenf5c20571997-01-30 15:48:07 +0000628 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000629 if (self == NULL)
630 return NULL;
631 self->fsspec = *fss;
632 return self;
633}
634
635static void
636mfss_dealloc(self)
637 mfssobject *self;
638{
Jack Jansenf5c20571997-01-30 15:48:07 +0000639 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000640}
641
Jack Jansenf5c20571997-01-30 15:48:07 +0000642static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000643mfss_repr(self)
644 mfssobject *self;
645{
646 char buf[512];
647
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000648 sprintf(buf, "FSSpec((%d, %d, '%.*s'))",
649 self->fsspec.vRefNum,
650 self->fsspec.parID,
651 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000652 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000653}
654
655static int
656mfss_compare(v, w)
657 mfssobject *v, *w;
658{
659 int minlen;
660 int res;
661
662 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
663 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
664 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
665 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
666 minlen = v->fsspec.name[0];
667 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
668 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
669 if ( res ) return res;
670 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
671 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
672 return res;
673}
674
Jack Jansenf5c20571997-01-30 15:48:07 +0000675statichere PyTypeObject Mfsstype = {
676 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000677 0, /*ob_size*/
678 "FSSpec", /*tp_name*/
679 sizeof(mfssobject), /*tp_basicsize*/
680 0, /*tp_itemsize*/
681 /* methods */
682 (destructor)mfss_dealloc, /*tp_dealloc*/
683 (printfunc)0, /*tp_print*/
684 (getattrfunc)mfss_getattr, /*tp_getattr*/
685 (setattrfunc)0, /*tp_setattr*/
686 (cmpfunc)mfss_compare, /*tp_compare*/
687 (reprfunc)mfss_repr, /*tp_repr*/
688 0, /*tp_as_number*/
689 0, /*tp_as_sequence*/
690 0, /*tp_as_mapping*/
691 (hashfunc)0, /*tp_hash*/
692};
693
694/* End of code for FSSpec objects */
695/* -------------------------------------------------------- */
696
Jack Jansenf5c20571997-01-30 15:48:07 +0000697static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000698mfs_ResolveAliasFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000699 PyObject *self; /* Not used */
700 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000701{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000702 FSSpec fss;
703 Boolean chain = 1, isfolder, wasaliased;
704 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000705
Jack Jansenf5c20571997-01-30 15:48:07 +0000706 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000707 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000708 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
709 if ( err ) {
710 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000711 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000712 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000713 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000714}
715
Jack Jansenf5c20571997-01-30 15:48:07 +0000716static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000717mfs_StandardGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000718 PyObject *self; /* Not used */
719 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000720{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000721 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000722 short numtypes;
723 StandardFileReply reply;
724
725 list[0] = list[1] = list[2] = list[3] = 0;
726 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000727 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000728 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
729 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000730 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000731 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000732 numtypes++;
733 }
Jack Jansenc2632861995-06-03 21:15:50 +0000734 if ( numtypes == 0 )
735 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000736 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000737 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000738}
739
Jack Jansenf5c20571997-01-30 15:48:07 +0000740static PyObject *
Jack Jansend5d5f461995-08-14 12:22:56 +0000741mfs_PromptGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000742 PyObject *self; /* Not used */
743 PyObject *args;
Jack Jansend5d5f461995-08-14 12:22:56 +0000744{
745 SFTypeList list;
746 short numtypes;
747 StandardFileReply reply;
748 char *prompt = NULL;
749
750 list[0] = list[1] = list[2] = list[3] = 0;
751 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000752 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000753 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
754 PyMac_GetOSType, &list[3]) )
755 return NULL;
756 while ( numtypes < 4 && list[numtypes] ) {
757 numtypes++;
758 }
759 if ( numtypes == 0 )
760 numtypes = -1;
761 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000762 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000763}
764
Jack Jansenf5c20571997-01-30 15:48:07 +0000765static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000766mfs_StandardPutFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000767 PyObject *self; /* Not used */
768 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000769{
770 Str255 prompt, dft;
771 StandardFileReply reply;
772
773 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000774 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000775 return NULL;
776 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000777 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000778}
779
Jack Jansend99d2831996-07-22 15:26:01 +0000780/*
781** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000782static PyObject *
Jack Jansend99d2831996-07-22 15:26:01 +0000783mfs_SetFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000784 PyObject *self;
785 PyObject *args;
Jack Jansend99d2831996-07-22 15:26:01 +0000786{
787 FSSpec spec;
788 FSSpec ospec;
789 short orefnum;
790 long oparid;
791
792 /* Get old values */
793 orefnum = -LMGetSFSaveDisk();
794 oparid = LMGetCurDirStore();
795 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
796
797 /* Go to working directory by default */
798 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000799 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000800 return NULL;
801 /* Set standard-file working directory */
802 LMSetSFSaveDisk(-spec.vRefNum);
803 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000804 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000805}
806
Jack Jansenf5c20571997-01-30 15:48:07 +0000807static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000808mfs_FSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000809 PyObject *self; /* Not used */
810 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000811{
812 FSSpec fss;
813
Jack Jansenf5c20571997-01-30 15:48:07 +0000814 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000815 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000816 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000817}
818
Jack Jansenf5c20571997-01-30 15:48:07 +0000819static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +0000820mfs_RawFSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000821 PyObject *self; /* Not used */
822 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +0000823{
824 FSSpec *fssp;
825 int size;
826
Jack Janseneeccca91997-05-07 15:46:31 +0000827 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000828 return NULL;
829 if ( size != sizeof(FSSpec) ) {
830 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
831 return NULL;
832 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000833 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000834}
835
Jack Jansenf5c20571997-01-30 15:48:07 +0000836static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +0000837mfs_RawAlias(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000838 PyObject *self; /* Not used */
839 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +0000840{
841 char *dataptr;
842 Handle h;
843 int size;
844
Jack Janseneeccca91997-05-07 15:46:31 +0000845 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000846 return NULL;
847 h = NewHandle(size);
848 if ( h == NULL ) {
849 PyErr_NoMemory();
850 return NULL;
851 }
852 HLock(h);
853 memcpy((char *)*h, dataptr, size);
854 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +0000855 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +0000856}
857
Jack Jansenf5c20571997-01-30 15:48:07 +0000858static PyObject *
Jack Jansen81f51c71995-02-20 15:45:25 +0000859mfs_GetDirectory(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000860 PyObject *self; /* Not used */
861 PyObject *args;
Jack Jansen81f51c71995-02-20 15:45:25 +0000862{
863 FSSpec fsdir;
864 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +0000865 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +0000866
Jack Jansenf5c20571997-01-30 15:48:07 +0000867 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +0000868 return NULL;
869
Jack Jansend5d5f461995-08-14 12:22:56 +0000870 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000871 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +0000872}
873
Jack Jansenf5c20571997-01-30 15:48:07 +0000874static PyObject *
Jack Jansen2c673621995-06-18 20:05:14 +0000875mfs_FindFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000876 PyObject *self; /* Not used */
877 PyObject *args;
Jack Jansen2c673621995-06-18 20:05:14 +0000878{
879 OSErr err;
880 short where;
881 OSType which;
882 int create;
883 short refnum;
884 long dirid;
885
Jack Jansenf5c20571997-01-30 15:48:07 +0000886 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +0000887 return NULL;
888 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
889 if ( err ) {
890 PyErr_Mac(ErrorObject, err);
891 return NULL;
892 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000893 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +0000894}
895
Jack Jansenf5c20571997-01-30 15:48:07 +0000896static PyObject *
Jack Jansen924ca851996-09-20 15:25:16 +0000897mfs_FindApplication(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000898 PyObject *self; /* Not used */
899 PyObject *args;
Jack Jansen924ca851996-09-20 15:25:16 +0000900{
901 OSErr err;
902 OSType which;
903 FSSpec fss;
904
Jack Jansenf5c20571997-01-30 15:48:07 +0000905 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +0000906 return NULL;
907 err = FindApplicationFromCreator(which, &fss);
908 if ( err ) {
909 PyErr_Mac(ErrorObject, err);
910 return NULL;
911 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000912 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +0000913}
914
Jack Jansenf5c20571997-01-30 15:48:07 +0000915static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000916mfs_FInfo(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000917 PyObject *self;
918 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000919{
Jack Jansenf5c20571997-01-30 15:48:07 +0000920 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +0000921}
922
Jack Jansend9936481997-06-16 14:31:38 +0000923static PyObject *
924mfs_NewAliasMinimalFromFullPath(self, args)
925 PyObject *self; /* Not used */
926 PyObject *args;
927{
928 OSErr err;
929 char *fullpath;
930 int fullpathlen;
931 AliasHandle alias;
932 Str32 zonename;
933 Str31 servername;
934
935 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
936 return NULL;
937 zonename[0] = 0;
938 servername[0] = 0;
939 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
940 servername, &alias);
941 if ( err ) {
942 PyErr_Mac(ErrorObject, err);
943 return NULL;
944 }
945 return (PyObject *)newmfsaobject(alias);
946}
947
948
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000949/* List of methods defined in the module */
950
Jack Jansenf5c20571997-01-30 15:48:07 +0000951static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000952 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
953 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +0000954 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000955 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +0000956 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +0000957 {"SetFolder", mfs_SetFolder, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000958 {"FSSpec", mfs_FSSpec, 1},
Jack Jansenc889b761995-02-13 12:00:46 +0000959 {"RawFSSpec", mfs_RawFSSpec, 1},
960 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +0000961 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +0000962 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +0000963 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +0000964 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000965
966 {NULL, NULL} /* sentinel */
967};
968
969
970/* Initialization function for the module (*must* be called initmacfs) */
971
972void
973initmacfs()
974{
Jack Jansenf5c20571997-01-30 15:48:07 +0000975 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000976
977 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000978 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000979
980 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000981 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +0000982 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +0000983 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000984
Jack Jansena755e681997-09-20 17:40:22 +0000985 Mfsatype.ob_type = &PyType_Type;
986 Py_INCREF(&Mfsatype);
987 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
988 Mfsstype.ob_type = &PyType_Type;
989 Py_INCREF(&Mfsstype);
990 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
991 Mfsitype.ob_type = &PyType_Type;
992 Py_INCREF(&Mfsitype);
993 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000994 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000995}