blob: e6f11aeec4e35becd11da5a69a6a6ff0728c1ad2 [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
Jack Jansen6143d532001-05-19 12:34:59 +000028#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumbecdbec1995-02-14 01:27:24 +000029#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000030#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000031#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000032#include <StandardFile.h>
33#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000034#include <LowMem.h>
Jack Jansen6143d532001-05-19 12:34:59 +000035#else
36#include <Carbon/Carbon.h>
37#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +000038
Jack Jansen924ca851996-09-20 15:25:16 +000039#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000040
Jack Jansen9ae898b2000-07-11 21:16:03 +000041
Jack Jansenf5c20571997-01-30 15:48:07 +000042static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000043
44/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000045/* Declarations for objects of type Alias */
46
47typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000048 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000049 AliasHandle alias;
50} mfsaobject;
51
Jack Jansenf5c20571997-01-30 15:48:07 +000052staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000053
54#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
55
56/* ---------------------------------------------------------------- */
57/* Declarations for objects of type FSSpec */
58
59typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000060 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000061 FSSpec fsspec;
62} mfssobject;
63
Jack Jansenf5c20571997-01-30 15:48:07 +000064staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000065
66#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
67
Jack Jansen4e566ab2001-07-08 22:07:23 +000068/* ---------------------------------------------------------------- */
69/* Declarations for objects of type FSRef */
70
71typedef struct {
72 PyObject_HEAD
73 FSRef fsref;
74} mfsrobject;
75
76staticforward PyTypeObject Mfsrtype;
77
78#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
79
Guido van Rossumefd97671995-01-26 22:56:16 +000080
Jack Jansen3d185931995-08-07 14:04:10 +000081/* ---------------------------------------------------------------- */
82/* Declarations for objects of type FInfo */
83
84typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000085 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000086 FInfo finfo;
87} mfsiobject;
88
Jack Jansenf5c20571997-01-30 15:48:07 +000089staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +000090
91#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
92
93
Jack Jansen4e566ab2001-07-08 22:07:23 +000094staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
95staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +000096
Jack Jansen17ba43f1995-01-26 16:22:07 +000097/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +000098
Jack Jansenf5c20571997-01-30 15:48:07 +000099static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000100mfsa_Resolve(self, args)
101 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000102 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000103{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000104 FSSpec from, *fromp, result;
105 Boolean changed;
106 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000107
Jack Jansen17ba43f1995-01-26 16:22:07 +0000108 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000109 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000110 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000111 if (from.name[0] )
112 fromp = &from;
113 else
114 fromp = NULL;
115 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000116 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000117 PyErr_Mac(ErrorObject, err);
118 return NULL;
119 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000120 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000121}
122
Jack Jansenf5c20571997-01-30 15:48:07 +0000123static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000124mfsa_GetInfo(self, args)
125 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000126 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000127{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000128 Str63 value;
129 int i;
130 OSErr err;
131
Jack Jansenf5c20571997-01-30 15:48:07 +0000132 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000133 return NULL;
134 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
135 if ( err ) {
136 PyErr_Mac(ErrorObject, err);
137 return 0;
138 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000139 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000140}
141
Jack Jansenf5c20571997-01-30 15:48:07 +0000142static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000143mfsa_Update(self, args)
144 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000146{
147 FSSpec target, fromfile, *fromfilep;
148 OSErr err;
149 Boolean changed;
150
151 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000152 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000153 PyMac_GetFSSpec, &fromfile))
154 return NULL;
155 if ( fromfile.name[0] )
156 fromfilep = &fromfile;
157 else
158 fromfilep = NULL;
159 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
160 if ( err ) {
161 PyErr_Mac(ErrorObject, err);
162 return 0;
163 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000164 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000165}
166
Jack Jansenf5c20571997-01-30 15:48:07 +0000167static struct PyMethodDef mfsa_methods[] = {
168 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
169 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
170 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000171
172 {NULL, NULL} /* sentinel */
173};
174
175/* ---------- */
176
Jack Jansenf5c20571997-01-30 15:48:07 +0000177static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000178mfsa_getattr(self, name)
179 mfsaobject *self;
180 char *name;
181{
Jack Jansenc889b761995-02-13 12:00:46 +0000182 if ( strcmp(name, "data") == 0 ) {
183 int size;
184 PyObject *rv;
185
186 size = GetHandleSize((Handle)self->alias);
187 HLock((Handle)self->alias);
188 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
189 HUnlock((Handle)self->alias);
190 return rv;
191 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000192 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000193}
194
Jack Jansen9ae898b2000-07-11 21:16:03 +0000195static mfsaobject *
196newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000197{
198 mfsaobject *self;
199
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000201 if (self == NULL)
202 return NULL;
203 self->alias = alias;
204 return self;
205}
206
207
208static void
209mfsa_dealloc(self)
210 mfsaobject *self;
211{
212#if 0
213 if ( self->alias ) {
214 should we do something here?
215 }
216#endif
217
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000219}
220
Jack Jansenf5c20571997-01-30 15:48:07 +0000221statichere PyTypeObject Mfsatype = {
222 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000223 0, /*ob_size*/
224 "Alias", /*tp_name*/
225 sizeof(mfsaobject), /*tp_basicsize*/
226 0, /*tp_itemsize*/
227 /* methods */
228 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000229 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000230 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000231 (setattrfunc)0, /*tp_setattr*/
232 (cmpfunc)0, /*tp_compare*/
233 (reprfunc)0, /*tp_repr*/
234 0, /*tp_as_number*/
235 0, /*tp_as_sequence*/
236 0, /*tp_as_mapping*/
237 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000238};
239
240/* End of code for Alias objects */
241/* -------------------------------------------------------- */
242
Jack Jansen3d185931995-08-07 14:04:10 +0000243/* ---------------------------------------------------------------- */
244
Jack Jansenf5c20571997-01-30 15:48:07 +0000245static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000246
247 {NULL, NULL} /* sentinel */
248};
249
250/* ---------- */
251
252static mfsiobject *
253newmfsiobject()
254{
255 mfsiobject *self;
256
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000258 if (self == NULL)
259 return NULL;
260 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
261 return self;
262}
263
264static void
265mfsi_dealloc(self)
266 mfsiobject *self;
267{
Jack Jansenf5c20571997-01-30 15:48:07 +0000268 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000269}
270
Jack Jansenf5c20571997-01-30 15:48:07 +0000271static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000272mfsi_getattr(self, name)
273 mfsiobject *self;
274 char *name;
275{
Jack Jansen3d185931995-08-07 14:04:10 +0000276 if ( strcmp(name, "Type") == 0 )
277 return PyMac_BuildOSType(self->finfo.fdType);
278 else if ( strcmp(name, "Creator") == 0 )
279 return PyMac_BuildOSType(self->finfo.fdCreator);
280 else if ( strcmp(name, "Flags") == 0 )
281 return Py_BuildValue("i", (int)self->finfo.fdFlags);
282 else if ( strcmp(name, "Location") == 0 )
283 return PyMac_BuildPoint(self->finfo.fdLocation);
284 else if ( strcmp(name, "Fldr") == 0 )
285 return Py_BuildValue("i", (int)self->finfo.fdFldr);
286 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000287 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000288}
289
290
291static int
292mfsi_setattr(self, name, v)
293 mfsiobject *self;
294 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000295 PyObject *v;
Jack Jansen3d185931995-08-07 14:04:10 +0000296{
297 int rv;
298 int i;
299
300 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000302 return -1;
303 }
304 if ( strcmp(name, "Type") == 0 )
305 rv = PyMac_GetOSType(v, &self->finfo.fdType);
306 else if ( strcmp(name, "Creator") == 0 )
307 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
308 else if ( strcmp(name, "Flags") == 0 ) {
309 rv = PyArg_Parse(v, "i", &i);
310 self->finfo.fdFlags = (short)i;
311 } else if ( strcmp(name, "Location") == 0 )
312 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
313 else if ( strcmp(name, "Fldr") == 0 ) {
314 rv = PyArg_Parse(v, "i", &i);
315 self->finfo.fdFldr = (short)i;
316 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000317 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000318 return -1;
319 }
320 if (rv)
321 return 0;
322 return -1;
323}
324
325
Jack Jansenf5c20571997-01-30 15:48:07 +0000326static PyTypeObject Mfsitype = {
327 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000328 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000329 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000330 sizeof(mfsiobject), /*tp_basicsize*/
331 0, /*tp_itemsize*/
332 /* methods */
333 (destructor)mfsi_dealloc, /*tp_dealloc*/
334 (printfunc)0, /*tp_print*/
335 (getattrfunc)mfsi_getattr, /*tp_getattr*/
336 (setattrfunc)mfsi_setattr, /*tp_setattr*/
337 (cmpfunc)0, /*tp_compare*/
338 (reprfunc)0, /*tp_repr*/
339 0, /*tp_as_number*/
340 0, /*tp_as_sequence*/
341 0, /*tp_as_mapping*/
342 (hashfunc)0, /*tp_hash*/
343};
344
345/* End of code for FInfo object objects */
346/* -------------------------------------------------------- */
347
348
Jack Jansen17ba43f1995-01-26 16:22:07 +0000349/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000350** Helper routines for the FSRef and FSSpec creators in macglue.c
351** They return an FSSpec/FSRef if the Python object encapsulating
352** either is passed. They return a boolean success indicator.
353** Note that they do not set an exception on failure, they're only
354** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000355*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000356static int
357_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000358{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000359 if ( is_mfssobject(self) ) {
360 *fssp = ((mfssobject *)self)->fsspec;
361 return 1;
362 }
363 return 0;
364}
365
366/* Return an FSSpec if this is an FSref */
367static int
368_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
369{
Jack Jansencbed91b2001-08-03 13:31:36 +0000370#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000371 static FSRef *fsrp;
372
373 if ( is_mfsrobject(self) ) {
374 fsrp = &((mfsrobject *)self)->fsref;
375 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
376 return 1;
377 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000378#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000379 return 0;
380}
381
382/* Return an FSRef if this is an FSRef */
383static int
384_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
385{
Jack Jansencbed91b2001-08-03 13:31:36 +0000386#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000387 if ( is_mfsrobject(self) ) {
388 *fsrp = ((mfsrobject *)self)->fsref;
389 return 1;
390 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000391#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000392 return 0;
393}
394
395/* Return an FSRef if this is an FSSpec */
396static int
397_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
398{
Jack Jansencbed91b2001-08-03 13:31:36 +0000399#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000400 if ( is_mfssobject(self) ) {
401 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
402 return 1;
403 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000404#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000405 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000406}
407
Jack Jansen0bdf9791996-09-15 22:11:25 +0000408/*
409** Two generally useful routines
410*/
411static OSErr
412PyMac_GetFileDates(fss, crdat, mddat, bkdat)
413 FSSpec *fss;
414 unsigned long *crdat, *mddat, *bkdat;
415{
416 CInfoPBRec pb;
417 OSErr error;
418
419 pb.dirInfo.ioNamePtr = fss->name;
420 pb.dirInfo.ioFDirIndex = 0;
421 pb.dirInfo.ioVRefNum = fss->vRefNum;
422 pb.dirInfo.ioDrDirID = fss->parID;
423 error = PBGetCatInfoSync(&pb);
424 if ( error ) return error;
425 *crdat = pb.hFileInfo.ioFlCrDat;
426 *mddat = pb.hFileInfo.ioFlMdDat;
427 *bkdat = pb.hFileInfo.ioFlBkDat;
428 return 0;
429}
430
431static OSErr
432PyMac_SetFileDates(fss, crdat, mddat, bkdat)
433 FSSpec *fss;
434 unsigned long crdat, mddat, bkdat;
435{
436 CInfoPBRec pb;
437 OSErr error;
438
439 pb.dirInfo.ioNamePtr = fss->name;
440 pb.dirInfo.ioFDirIndex = 0;
441 pb.dirInfo.ioVRefNum = fss->vRefNum;
442 pb.dirInfo.ioDrDirID = fss->parID;
443 error = PBGetCatInfoSync(&pb);
444 if ( error ) return error;
445 pb.dirInfo.ioNamePtr = fss->name;
446 pb.dirInfo.ioFDirIndex = 0;
447 pb.dirInfo.ioVRefNum = fss->vRefNum;
448 pb.dirInfo.ioDrDirID = fss->parID;
449 pb.hFileInfo.ioFlCrDat = crdat;
450 pb.hFileInfo.ioFlMdDat = mddat;
451 pb.hFileInfo.ioFlBkDat = bkdat;
452 error = PBSetCatInfoSync(&pb);
453 return error;
454}
455
Jack Jansenf5c20571997-01-30 15:48:07 +0000456static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000457mfss_as_pathname(self, args)
458 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000459 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000460{
461 char strbuf[257];
462 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000463
Jack Jansenf5c20571997-01-30 15:48:07 +0000464 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000465 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000466 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000467 if ( err ) {
468 PyErr_Mac(ErrorObject, err);
469 return NULL;
470 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000471 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000472}
473
Jack Jansenf5c20571997-01-30 15:48:07 +0000474static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000475mfss_as_tuple(self, args)
476 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000477 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000478{
Jack Jansenf5c20571997-01-30 15:48:07 +0000479 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000480 return NULL;
481 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
482 &self->fsspec.name[1], self->fsspec.name[0]);
483}
484
Jack Jansenf5c20571997-01-30 15:48:07 +0000485static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000486mfss_NewAlias(self, args)
487 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000488 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000489{
490 FSSpec src, *srcp;
491 OSErr err;
492 AliasHandle alias;
493
494 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000495 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000496 return NULL;
497 if ( src.name[0] )
498 srcp = &src;
499 else
500 srcp = NULL;
501 err = NewAlias(srcp, &self->fsspec, &alias);
502 if ( err ) {
503 PyErr_Mac(ErrorObject, err);
504 return NULL;
505 }
506
Jack Jansenf5c20571997-01-30 15:48:07 +0000507 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000508}
509
Jack Jansenf5c20571997-01-30 15:48:07 +0000510static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000511mfss_NewAliasMinimal(self, args)
512 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000513 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000514{
515 OSErr err;
516 AliasHandle alias;
517
Jack Jansenf5c20571997-01-30 15:48:07 +0000518 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000519 return NULL;
520 err = NewAliasMinimal(&self->fsspec, &alias);
521 if ( err ) {
522 PyErr_Mac(ErrorObject, err);
523 return NULL;
524 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000525 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000526}
527
Jack Jansen4e566ab2001-07-08 22:07:23 +0000528static PyObject *
529mfss_FSpMakeFSRef(self, args)
530 mfssobject *self;
531 PyObject *args;
532{
Jack Jansencbed91b2001-08-03 13:31:36 +0000533#if TARGET_API_MAC_OS8
534 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
535 return 0;
536#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000537 OSErr err;
538 FSRef fsref;
539
540 if (!PyArg_ParseTuple(args, ""))
541 return NULL;
542 err = FSpMakeFSRef(&self->fsspec, &fsref);
543 if ( err ) {
544 PyErr_Mac(ErrorObject, err);
545 return NULL;
546 }
547 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000548#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000549}
550
Jack Jansen3d185931995-08-07 14:04:10 +0000551/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000552static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000553mfss_GetCreatorType(self, args)
554 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000555 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000556{
557 OSErr err;
558 FInfo info;
559
Jack Jansenf5c20571997-01-30 15:48:07 +0000560 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000561 return NULL;
562 err = FSpGetFInfo(&self->fsspec, &info);
563 if ( err ) {
564 PyErr_Mac(ErrorObject, err);
565 return NULL;
566 }
567 return Py_BuildValue("(O&O&)",
568 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
569}
570
Jack Jansenf5c20571997-01-30 15:48:07 +0000571static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000572mfss_SetCreatorType(self, args)
573 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000575{
576 OSErr err;
577 OSType creator, type;
578 FInfo info;
579
Jack Jansenf5c20571997-01-30 15:48:07 +0000580 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000581 return NULL;
582 err = FSpGetFInfo(&self->fsspec, &info);
583 if ( err ) {
584 PyErr_Mac(ErrorObject, err);
585 return NULL;
586 }
587 info.fdType = type;
588 info.fdCreator = creator;
589 err = FSpSetFInfo(&self->fsspec, &info);
590 if ( err ) {
591 PyErr_Mac(ErrorObject, err);
592 return NULL;
593 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000594 Py_INCREF(Py_None);
595 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000596}
597
Jack Jansenf5c20571997-01-30 15:48:07 +0000598static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000599mfss_GetFInfo(self, args)
600 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000601 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000602{
603 OSErr err;
604 mfsiobject *fip;
605
606
Jack Jansenf5c20571997-01-30 15:48:07 +0000607 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000608 return NULL;
609 if ( (fip=newmfsiobject()) == NULL )
610 return NULL;
611 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
612 if ( err ) {
613 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000614 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000615 return NULL;
616 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000617 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000618}
619
Jack Jansenf5c20571997-01-30 15:48:07 +0000620static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000621mfss_SetFInfo(self, args)
622 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000623 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000624{
625 OSErr err;
626 mfsiobject *fip;
627
Jack Jansenf5c20571997-01-30 15:48:07 +0000628 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000629 return NULL;
630 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
631 if ( err ) {
632 PyErr_Mac(ErrorObject, err);
633 return NULL;
634 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000635 Py_INCREF(Py_None);
636 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000637}
638
Jack Jansenf5c20571997-01-30 15:48:07 +0000639static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000640mfss_GetDates(self, args)
641 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000642 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000643{
644 OSErr err;
645 unsigned long crdat, mddat, bkdat;
646
Jack Jansenf5c20571997-01-30 15:48:07 +0000647 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000648 return NULL;
649 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
650 if ( err ) {
651 PyErr_Mac(ErrorObject, err);
652 return NULL;
653 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000654 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000655}
656
Jack Jansenf5c20571997-01-30 15:48:07 +0000657static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000658mfss_SetDates(self, args)
659 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000660 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000661{
662 OSErr err;
663 double crdat, mddat, bkdat;
664
Jack Jansenf5c20571997-01-30 15:48:07 +0000665 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000666 return NULL;
667 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
668 (unsigned long)mddat, (unsigned long)bkdat);
669 if ( err ) {
670 PyErr_Mac(ErrorObject, err);
671 return NULL;
672 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000673 Py_INCREF(Py_None);
674 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000675}
676
Jack Jansenf5c20571997-01-30 15:48:07 +0000677static struct PyMethodDef mfss_methods[] = {
678 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
679 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000680 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
681 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000682 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
683 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
684 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
685 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
686 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
687 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
688 {"GetDates", (PyCFunction)mfss_GetDates, 1},
689 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000690
691 {NULL, NULL} /* sentinel */
692};
693
694/* ---------- */
695
Jack Jansenf5c20571997-01-30 15:48:07 +0000696static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000697mfss_getattr(self, name)
698 mfssobject *self;
699 char *name;
700{
Jack Jansenc889b761995-02-13 12:00:46 +0000701 if ( strcmp(name, "data") == 0)
702 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000703 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000704}
705
706mfssobject *
707newmfssobject(fss)
708 FSSpec *fss;
709{
710 mfssobject *self;
711
Jack Jansenf5c20571997-01-30 15:48:07 +0000712 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000713 if (self == NULL)
714 return NULL;
715 self->fsspec = *fss;
716 return self;
717}
718
719static void
720mfss_dealloc(self)
721 mfssobject *self;
722{
Jack Jansenf5c20571997-01-30 15:48:07 +0000723 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000724}
725
Jack Jansenf5c20571997-01-30 15:48:07 +0000726static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000727mfss_repr(self)
728 mfssobject *self;
729{
730 char buf[512];
731
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000732 sprintf(buf, "FSSpec((%d, %d, '%.*s'))",
733 self->fsspec.vRefNum,
734 self->fsspec.parID,
735 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000736 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000737}
738
739static int
740mfss_compare(v, w)
741 mfssobject *v, *w;
742{
743 int minlen;
744 int res;
745
746 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
747 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
748 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
749 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
750 minlen = v->fsspec.name[0];
751 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
752 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
753 if ( res ) return res;
754 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
755 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
756 return res;
757}
758
Jack Jansenf5c20571997-01-30 15:48:07 +0000759statichere PyTypeObject Mfsstype = {
760 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000761 0, /*ob_size*/
762 "FSSpec", /*tp_name*/
763 sizeof(mfssobject), /*tp_basicsize*/
764 0, /*tp_itemsize*/
765 /* methods */
766 (destructor)mfss_dealloc, /*tp_dealloc*/
767 (printfunc)0, /*tp_print*/
768 (getattrfunc)mfss_getattr, /*tp_getattr*/
769 (setattrfunc)0, /*tp_setattr*/
770 (cmpfunc)mfss_compare, /*tp_compare*/
771 (reprfunc)mfss_repr, /*tp_repr*/
772 0, /*tp_as_number*/
773 0, /*tp_as_sequence*/
774 0, /*tp_as_mapping*/
775 (hashfunc)0, /*tp_hash*/
776};
777
778/* End of code for FSSpec objects */
779/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000780#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000781static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +0000782mfsr_as_fsspec(self, args)
783 mfsrobject *self;
784 PyObject *args;
785{
786 OSErr err;
787 FSSpec fss;
788
789 if (!PyArg_ParseTuple(args, ""))
790 return NULL;
791 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
792 if ( err ) {
793 PyErr_Mac(ErrorObject, err);
794 return NULL;
795 }
796 Py_INCREF(Py_None);
797 return (PyObject *)newmfssobject(&fss);
798}
799
800static struct PyMethodDef mfsr_methods[] = {
801 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
802#if 0
803 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
804 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
805 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
806 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
807 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
808 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
809 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
810 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
811 {"GetDates", (PyCFunction)mfss_GetDates, 1},
812 {"SetDates", (PyCFunction)mfss_SetDates, 1},
813#endif
814
815 {NULL, NULL} /* sentinel */
816};
817
818/* ---------- */
819
820static PyObject *
821mfsr_getattr(self, name)
822 mfsrobject *self;
823 char *name;
824{
825 if ( strcmp(name, "data") == 0)
826 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
827 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
828}
829
830mfsrobject *
831newmfsrobject(fsr)
832 FSRef *fsr;
833{
834 mfsrobject *self;
835
836 self = PyObject_NEW(mfsrobject, &Mfsrtype);
837 if (self == NULL)
838 return NULL;
839 self->fsref = *fsr;
840 return self;
841}
842
843static int
844mfsr_compare(v, w)
845 mfsrobject *v, *w;
846{
847 OSErr err;
848
849 if ( v == w ) return 0;
850 err = FSCompareFSRefs(&v->fsref, &w->fsref);
851 if ( err == 0 )
852 return 0;
853 if (v < w )
854 return -1;
855 return 1;
856}
857
858static void
859mfsr_dealloc(self)
860 mfsrobject *self;
861{
862 PyMem_DEL(self);
863}
864
865statichere PyTypeObject Mfsrtype = {
866 PyObject_HEAD_INIT(&PyType_Type)
867 0, /*ob_size*/
868 "FSRef", /*tp_name*/
869 sizeof(mfsrobject), /*tp_basicsize*/
870 0, /*tp_itemsize*/
871 /* methods */
872 (destructor)mfsr_dealloc, /*tp_dealloc*/
873 (printfunc)0, /*tp_print*/
874 (getattrfunc)mfsr_getattr, /*tp_getattr*/
875 (setattrfunc)0, /*tp_setattr*/
876 (cmpfunc)mfsr_compare, /*tp_compare*/
877 (reprfunc)0, /*tp_repr*/
878 0, /*tp_as_number*/
879 0, /*tp_as_sequence*/
880 0, /*tp_as_mapping*/
881 (hashfunc)0, /*tp_hash*/
882};
883
884/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000885#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000886/* -------------------------------------------------------- */
887
888static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000889mfs_ResolveAliasFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000890 PyObject *self; /* Not used */
891 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000892{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000893 FSSpec fss;
894 Boolean chain = 1, isfolder, wasaliased;
895 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000896
Jack Jansenf5c20571997-01-30 15:48:07 +0000897 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000898 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000899 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
900 if ( err ) {
901 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000902 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000903 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000904 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000905}
906
Jack Jansen74a1e632000-07-14 22:37:27 +0000907#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000908static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000909mfs_StandardGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000910 PyObject *self; /* Not used */
911 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000912{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000913 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000914 short numtypes;
915 StandardFileReply reply;
916
917 list[0] = list[1] = list[2] = list[3] = 0;
918 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000919 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000920 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
921 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000922 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000923 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000924 numtypes++;
925 }
Jack Jansenc2632861995-06-03 21:15:50 +0000926 if ( numtypes == 0 )
927 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000928 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000929 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000930}
931
Jack Jansenf5c20571997-01-30 15:48:07 +0000932static PyObject *
Jack Jansend5d5f461995-08-14 12:22:56 +0000933mfs_PromptGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000934 PyObject *self; /* Not used */
935 PyObject *args;
Jack Jansend5d5f461995-08-14 12:22:56 +0000936{
937 SFTypeList list;
938 short numtypes;
939 StandardFileReply reply;
940 char *prompt = NULL;
941
942 list[0] = list[1] = list[2] = list[3] = 0;
943 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000944 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000945 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
946 PyMac_GetOSType, &list[3]) )
947 return NULL;
948 while ( numtypes < 4 && list[numtypes] ) {
949 numtypes++;
950 }
951 if ( numtypes == 0 )
952 numtypes = -1;
953 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000954 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000955}
956
Jack Jansenf5c20571997-01-30 15:48:07 +0000957static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000958mfs_StandardPutFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000959 PyObject *self; /* Not used */
960 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000961{
962 Str255 prompt, dft;
963 StandardFileReply reply;
964
965 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000966 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000967 return NULL;
968 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000969 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000970}
971
Jack Jansend99d2831996-07-22 15:26:01 +0000972/*
973** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000974static PyObject *
Jack Jansend99d2831996-07-22 15:26:01 +0000975mfs_SetFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000976 PyObject *self;
977 PyObject *args;
Jack Jansend99d2831996-07-22 15:26:01 +0000978{
979 FSSpec spec;
980 FSSpec ospec;
981 short orefnum;
982 long oparid;
983
984 /* Get old values */
985 orefnum = -LMGetSFSaveDisk();
986 oparid = LMGetCurDirStore();
987 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
988
989 /* Go to working directory by default */
990 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000991 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000992 return NULL;
993 /* Set standard-file working directory */
994 LMSetSFSaveDisk(-spec.vRefNum);
995 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000996 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000997}
Jack Jansene79dc762000-06-02 21:35:07 +0000998#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000999
Jack Jansenf5c20571997-01-30 15:48:07 +00001000static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +00001001mfs_FSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001002 PyObject *self; /* Not used */
1003 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001004{
1005 FSSpec fss;
1006
Jack Jansenf5c20571997-01-30 15:48:07 +00001007 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001008 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +00001009 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001010}
1011
Jack Jansenf5c20571997-01-30 15:48:07 +00001012static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +00001013mfs_FSRef(self, args)
1014 PyObject *self; /* Not used */
1015 PyObject *args;
1016{
Jack Jansencbed91b2001-08-03 13:31:36 +00001017#if TARGET_API_MAC_OS8
1018 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
1019 return 0;
1020#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001021 FSRef fsr;
1022
1023 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
1024 return NULL;
1025 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +00001026#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001027}
1028
1029static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001030mfs_RawFSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001031 PyObject *self; /* Not used */
1032 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +00001033{
1034 FSSpec *fssp;
1035 int size;
1036
Jack Janseneeccca91997-05-07 15:46:31 +00001037 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001038 return NULL;
1039 if ( size != sizeof(FSSpec) ) {
1040 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
1041 return NULL;
1042 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001043 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +00001044}
1045
Jack Jansenf5c20571997-01-30 15:48:07 +00001046static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001047mfs_RawAlias(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001048 PyObject *self; /* Not used */
1049 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +00001050{
1051 char *dataptr;
1052 Handle h;
1053 int size;
1054
Jack Janseneeccca91997-05-07 15:46:31 +00001055 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001056 return NULL;
1057 h = NewHandle(size);
1058 if ( h == NULL ) {
1059 PyErr_NoMemory();
1060 return NULL;
1061 }
1062 HLock(h);
1063 memcpy((char *)*h, dataptr, size);
1064 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001065 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001066}
1067
Jack Jansen74a1e632000-07-14 22:37:27 +00001068#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001069static PyObject *
Jack Jansen81f51c71995-02-20 15:45:25 +00001070mfs_GetDirectory(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001071 PyObject *self; /* Not used */
1072 PyObject *args;
Jack Jansen81f51c71995-02-20 15:45:25 +00001073{
1074 FSSpec fsdir;
1075 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001076 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001077
Jack Jansenf5c20571997-01-30 15:48:07 +00001078 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001079 return NULL;
1080
Jack Jansend5d5f461995-08-14 12:22:56 +00001081 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001082 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001083}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001084#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001085
Jack Jansenf5c20571997-01-30 15:48:07 +00001086static PyObject *
Jack Jansen2c673621995-06-18 20:05:14 +00001087mfs_FindFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001088 PyObject *self; /* Not used */
1089 PyObject *args;
Jack Jansen2c673621995-06-18 20:05:14 +00001090{
1091 OSErr err;
1092 short where;
1093 OSType which;
1094 int create;
1095 short refnum;
1096 long dirid;
1097
Jack Jansenabd703d2001-03-15 14:38:10 +00001098 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001099 return NULL;
1100 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1101 if ( err ) {
1102 PyErr_Mac(ErrorObject, err);
1103 return NULL;
1104 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001105 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001106}
1107
Jack Jansenf5c20571997-01-30 15:48:07 +00001108static PyObject *
Jack Jansen924ca851996-09-20 15:25:16 +00001109mfs_FindApplication(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001110 PyObject *self; /* Not used */
1111 PyObject *args;
Jack Jansen924ca851996-09-20 15:25:16 +00001112{
1113 OSErr err;
1114 OSType which;
1115 FSSpec fss;
1116
Jack Jansenf5c20571997-01-30 15:48:07 +00001117 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001118 return NULL;
1119 err = FindApplicationFromCreator(which, &fss);
1120 if ( err ) {
1121 PyErr_Mac(ErrorObject, err);
1122 return NULL;
1123 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001124 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001125}
1126
Jack Jansenf5c20571997-01-30 15:48:07 +00001127static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +00001128mfs_FInfo(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001129 PyObject *self;
1130 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +00001131{
Jack Jansenf5c20571997-01-30 15:48:07 +00001132 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001133}
1134
Jack Jansend9936481997-06-16 14:31:38 +00001135static PyObject *
1136mfs_NewAliasMinimalFromFullPath(self, args)
1137 PyObject *self; /* Not used */
1138 PyObject *args;
1139{
1140 OSErr err;
1141 char *fullpath;
1142 int fullpathlen;
1143 AliasHandle alias;
1144 Str32 zonename;
1145 Str31 servername;
1146
1147 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1148 return NULL;
1149 zonename[0] = 0;
1150 servername[0] = 0;
1151 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1152 servername, &alias);
1153 if ( err ) {
1154 PyErr_Mac(ErrorObject, err);
1155 return NULL;
1156 }
1157 return (PyObject *)newmfsaobject(alias);
1158}
1159
1160
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001161/* List of methods defined in the module */
1162
Jack Jansenf5c20571997-01-30 15:48:07 +00001163static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001164 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001165#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001166 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001167 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001168 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001169 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001170 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001171#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001172 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001173 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001174 {"RawFSSpec", mfs_RawFSSpec, 1},
1175 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001176 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001177 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001178 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001179 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001180
1181 {NULL, NULL} /* sentinel */
1182};
1183
Jack Jansen4e566ab2001-07-08 22:07:23 +00001184/*
1185** Convert a Python object to an FSSpec.
1186** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1187** (vrefnum, dirid, path).
1188*/
1189int
1190PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1191{
Jack Jansencbed91b2001-08-03 13:31:36 +00001192#if TARGET_API_MAC_OS8
1193 return 0;
1194#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001195 OSErr err;
1196
1197 /* If it's an FSRef we're also okay. */
1198 if (_mfs_GetFSRefFromFSRef(v, fsr))
1199 return 1;
1200 /* first check whether it already is an FSSpec */
1201 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1202 return 1;
1203 if ( PyString_Check(v) ) {
1204 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1205 return 0;
1206 }
1207 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1208 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001209#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001210}
1211
1212/* Convert FSSpec to PyObject */
1213PyObject *PyMac_BuildFSRef(FSRef *v)
1214{
Jack Jansencbed91b2001-08-03 13:31:36 +00001215#if TARGET_API_MAC_OS8
1216 return NULL;
1217#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001218 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001219#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001220}
1221
1222/*
1223** Convert a Python object to an FSRef.
1224** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1225*/
1226int
1227PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1228{
1229 Str255 path;
1230 short refnum;
1231 long parid;
1232 OSErr err;
1233
1234 /* first check whether it already is an FSSpec */
1235 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1236 return 1;
1237 /* If it's an FSRef we're also okay. */
1238 if (_mfs_GetFSSpecFromFSRef(v, fs))
1239 return 1;
1240 if ( PyString_Check(v) ) {
1241 /* It's a pathname */
1242 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1243 return 0;
1244 refnum = 0; /* XXXX Should get CurWD here?? */
1245 parid = 0;
1246 } else {
1247 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1248 &refnum, &parid, PyMac_GetStr255, &path)) {
1249 return 0;
1250 }
1251 }
1252 err = FSMakeFSSpec(refnum, parid, path, fs);
1253 if ( err && err != fnfErr ) {
1254 PyMac_Error(err);
1255 return 0;
1256 }
1257 return 1;
1258}
1259
1260/* Convert FSSpec to PyObject */
1261PyObject *PyMac_BuildFSSpec(FSSpec *v)
1262{
1263 return (PyObject *)newmfssobject(v);
1264}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001265
1266/* Initialization function for the module (*must* be called initmacfs) */
1267
1268void
1269initmacfs()
1270{
Jack Jansenf5c20571997-01-30 15:48:07 +00001271 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001272
1273 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001274 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001275
1276 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001277 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001278 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001279 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001280
Jack Jansena755e681997-09-20 17:40:22 +00001281 Mfsatype.ob_type = &PyType_Type;
1282 Py_INCREF(&Mfsatype);
1283 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1284 Mfsstype.ob_type = &PyType_Type;
1285 Py_INCREF(&Mfsstype);
1286 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1287 Mfsitype.ob_type = &PyType_Type;
1288 Py_INCREF(&Mfsitype);
1289 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001290 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001291}