blob: 6204cee967cbd892c209450921413ab41878de79 [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"
Jack Jansena5bca572001-08-03 15:39:27 +000027#include "pymactoolbox.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000028
Jack Jansen6143d532001-05-19 12:34:59 +000029#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumbecdbec1995-02-14 01:27:24 +000030#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000031#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000032#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000033#include <StandardFile.h>
34#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000035#include <LowMem.h>
Jack Jansen6143d532001-05-19 12:34:59 +000036#else
37#include <Carbon/Carbon.h>
38#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +000039
Jack Jansen924ca851996-09-20 15:25:16 +000040#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000041
Jack Jansena5bca572001-08-03 15:39:27 +000042#ifdef USE_TOOLBOX_OBJECT_GLUE
43extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
44#define PyMac_GetFSSpec _PyMac_GetFSSpec
45#endif
Jack Jansenf5c20571997-01-30 15:48:07 +000046static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000047
48/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000049/* Declarations for objects of type Alias */
50
51typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000052 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000053 AliasHandle alias;
54} mfsaobject;
55
Jack Jansenf5c20571997-01-30 15:48:07 +000056staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000057
58#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
59
60/* ---------------------------------------------------------------- */
61/* Declarations for objects of type FSSpec */
62
63typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000064 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000065 FSSpec fsspec;
66} mfssobject;
67
Jack Jansenf5c20571997-01-30 15:48:07 +000068staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000069
70#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
71
Jack Jansen4e566ab2001-07-08 22:07:23 +000072/* ---------------------------------------------------------------- */
73/* Declarations for objects of type FSRef */
74
75typedef struct {
76 PyObject_HEAD
77 FSRef fsref;
78} mfsrobject;
79
80staticforward PyTypeObject Mfsrtype;
81
82#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
83
Guido van Rossumefd97671995-01-26 22:56:16 +000084
Jack Jansen3d185931995-08-07 14:04:10 +000085/* ---------------------------------------------------------------- */
86/* Declarations for objects of type FInfo */
87
88typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000089 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000090 FInfo finfo;
91} mfsiobject;
92
Jack Jansenf5c20571997-01-30 15:48:07 +000093staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +000094
95#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
96
97
Jack Jansen4e566ab2001-07-08 22:07:23 +000098staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
99staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000100
Jack Jansen17ba43f1995-01-26 16:22:07 +0000101/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000102
Jack Jansenf5c20571997-01-30 15:48:07 +0000103static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000104mfsa_Resolve(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000105{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000106 FSSpec from, *fromp, result;
107 Boolean changed;
108 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000109
Jack Jansen17ba43f1995-01-26 16:22:07 +0000110 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000111 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000112 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000113 if (from.name[0] )
114 fromp = &from;
115 else
116 fromp = NULL;
117 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000118 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000119 PyErr_Mac(ErrorObject, err);
120 return NULL;
121 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000122 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000123}
124
Jack Jansenf5c20571997-01-30 15:48:07 +0000125static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000126mfsa_GetInfo(mfsaobject *self, 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 Jansendeefbe52001-08-08 13:46:49 +0000143mfsa_Update(mfsaobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000144{
145 FSSpec target, fromfile, *fromfilep;
146 OSErr err;
147 Boolean changed;
148
149 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000151 PyMac_GetFSSpec, &fromfile))
152 return NULL;
153 if ( fromfile.name[0] )
154 fromfilep = &fromfile;
155 else
156 fromfilep = NULL;
157 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
158 if ( err ) {
159 PyErr_Mac(ErrorObject, err);
160 return 0;
161 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000162 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000163}
164
Jack Jansenf5c20571997-01-30 15:48:07 +0000165static struct PyMethodDef mfsa_methods[] = {
166 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
167 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
168 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000169
170 {NULL, NULL} /* sentinel */
171};
172
173/* ---------- */
174
Jack Jansenf5c20571997-01-30 15:48:07 +0000175static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000176mfsa_getattr(mfsaobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000177{
Jack Jansenc889b761995-02-13 12:00:46 +0000178 if ( strcmp(name, "data") == 0 ) {
179 int size;
180 PyObject *rv;
181
182 size = GetHandleSize((Handle)self->alias);
183 HLock((Handle)self->alias);
184 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
185 HUnlock((Handle)self->alias);
186 return rv;
187 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000188 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000189}
190
Jack Jansen9ae898b2000-07-11 21:16:03 +0000191static mfsaobject *
192newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000193{
194 mfsaobject *self;
195
Jack Jansenf5c20571997-01-30 15:48:07 +0000196 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000197 if (self == NULL)
198 return NULL;
199 self->alias = alias;
200 return self;
201}
202
203
204static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000205mfsa_dealloc(mfsaobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000206{
207#if 0
208 if ( self->alias ) {
209 should we do something here?
210 }
211#endif
212
Jack Jansenf5c20571997-01-30 15:48:07 +0000213 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000214}
215
Jack Jansenf5c20571997-01-30 15:48:07 +0000216statichere PyTypeObject Mfsatype = {
217 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000218 0, /*ob_size*/
219 "Alias", /*tp_name*/
220 sizeof(mfsaobject), /*tp_basicsize*/
221 0, /*tp_itemsize*/
222 /* methods */
223 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000224 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000225 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000226 (setattrfunc)0, /*tp_setattr*/
227 (cmpfunc)0, /*tp_compare*/
228 (reprfunc)0, /*tp_repr*/
229 0, /*tp_as_number*/
230 0, /*tp_as_sequence*/
231 0, /*tp_as_mapping*/
232 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000233};
234
235/* End of code for Alias objects */
236/* -------------------------------------------------------- */
237
Jack Jansen3d185931995-08-07 14:04:10 +0000238/* ---------------------------------------------------------------- */
239
Jack Jansenf5c20571997-01-30 15:48:07 +0000240static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000241
242 {NULL, NULL} /* sentinel */
243};
244
245/* ---------- */
246
247static mfsiobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000248newmfsiobject(void)
Jack Jansen3d185931995-08-07 14:04:10 +0000249{
250 mfsiobject *self;
251
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000253 if (self == NULL)
254 return NULL;
255 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
256 return self;
257}
258
259static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000260mfsi_dealloc(mfsiobject *self)
Jack Jansen3d185931995-08-07 14:04:10 +0000261{
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000263}
264
Jack Jansenf5c20571997-01-30 15:48:07 +0000265static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000266mfsi_getattr(mfsiobject *self, char *name)
Jack Jansen3d185931995-08-07 14:04:10 +0000267{
Jack Jansen3d185931995-08-07 14:04:10 +0000268 if ( strcmp(name, "Type") == 0 )
269 return PyMac_BuildOSType(self->finfo.fdType);
270 else if ( strcmp(name, "Creator") == 0 )
271 return PyMac_BuildOSType(self->finfo.fdCreator);
272 else if ( strcmp(name, "Flags") == 0 )
273 return Py_BuildValue("i", (int)self->finfo.fdFlags);
274 else if ( strcmp(name, "Location") == 0 )
275 return PyMac_BuildPoint(self->finfo.fdLocation);
276 else if ( strcmp(name, "Fldr") == 0 )
277 return Py_BuildValue("i", (int)self->finfo.fdFldr);
278 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000279 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000280}
281
282
283static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000284mfsi_setattr(mfsiobject *self, char *name, PyObject *v)
Jack Jansen3d185931995-08-07 14:04:10 +0000285{
286 int rv;
287 int i;
288
289 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000290 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000291 return -1;
292 }
293 if ( strcmp(name, "Type") == 0 )
294 rv = PyMac_GetOSType(v, &self->finfo.fdType);
295 else if ( strcmp(name, "Creator") == 0 )
296 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
297 else if ( strcmp(name, "Flags") == 0 ) {
298 rv = PyArg_Parse(v, "i", &i);
299 self->finfo.fdFlags = (short)i;
300 } else if ( strcmp(name, "Location") == 0 )
301 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
302 else if ( strcmp(name, "Fldr") == 0 ) {
303 rv = PyArg_Parse(v, "i", &i);
304 self->finfo.fdFldr = (short)i;
305 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000306 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000307 return -1;
308 }
309 if (rv)
310 return 0;
311 return -1;
312}
313
314
Jack Jansenf5c20571997-01-30 15:48:07 +0000315static PyTypeObject Mfsitype = {
316 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000317 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000318 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000319 sizeof(mfsiobject), /*tp_basicsize*/
320 0, /*tp_itemsize*/
321 /* methods */
322 (destructor)mfsi_dealloc, /*tp_dealloc*/
323 (printfunc)0, /*tp_print*/
324 (getattrfunc)mfsi_getattr, /*tp_getattr*/
325 (setattrfunc)mfsi_setattr, /*tp_setattr*/
326 (cmpfunc)0, /*tp_compare*/
327 (reprfunc)0, /*tp_repr*/
328 0, /*tp_as_number*/
329 0, /*tp_as_sequence*/
330 0, /*tp_as_mapping*/
331 (hashfunc)0, /*tp_hash*/
332};
333
334/* End of code for FInfo object objects */
335/* -------------------------------------------------------- */
336
337
Jack Jansen17ba43f1995-01-26 16:22:07 +0000338/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000339** Helper routines for the FSRef and FSSpec creators in macglue.c
340** They return an FSSpec/FSRef if the Python object encapsulating
341** either is passed. They return a boolean success indicator.
342** Note that they do not set an exception on failure, they're only
343** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000344*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000345static int
346_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000347{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000348 if ( is_mfssobject(self) ) {
349 *fssp = ((mfssobject *)self)->fsspec;
350 return 1;
351 }
352 return 0;
353}
354
355/* Return an FSSpec if this is an FSref */
356static int
357_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
358{
Jack Jansencbed91b2001-08-03 13:31:36 +0000359#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000360 static FSRef *fsrp;
361
362 if ( is_mfsrobject(self) ) {
363 fsrp = &((mfsrobject *)self)->fsref;
364 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
365 return 1;
366 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000367#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000368 return 0;
369}
370
371/* Return an FSRef if this is an FSRef */
372static int
373_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
374{
Jack Jansencbed91b2001-08-03 13:31:36 +0000375#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000376 if ( is_mfsrobject(self) ) {
377 *fsrp = ((mfsrobject *)self)->fsref;
378 return 1;
379 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000380#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000381 return 0;
382}
383
384/* Return an FSRef if this is an FSSpec */
385static int
386_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
387{
Jack Jansencbed91b2001-08-03 13:31:36 +0000388#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000389 if ( is_mfssobject(self) ) {
390 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
391 return 1;
392 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000393#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000394 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000395}
396
Jack Jansen0bdf9791996-09-15 22:11:25 +0000397/*
398** Two generally useful routines
399*/
400static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000401PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat,
402 unsigned long *bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000403{
404 CInfoPBRec pb;
405 OSErr error;
406
407 pb.dirInfo.ioNamePtr = fss->name;
408 pb.dirInfo.ioFDirIndex = 0;
409 pb.dirInfo.ioVRefNum = fss->vRefNum;
410 pb.dirInfo.ioDrDirID = fss->parID;
411 error = PBGetCatInfoSync(&pb);
412 if ( error ) return error;
413 *crdat = pb.hFileInfo.ioFlCrDat;
414 *mddat = pb.hFileInfo.ioFlMdDat;
415 *bkdat = pb.hFileInfo.ioFlBkDat;
416 return 0;
417}
418
419static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000420PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
421 unsigned long bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000422{
423 CInfoPBRec pb;
424 OSErr error;
425
426 pb.dirInfo.ioNamePtr = fss->name;
427 pb.dirInfo.ioFDirIndex = 0;
428 pb.dirInfo.ioVRefNum = fss->vRefNum;
429 pb.dirInfo.ioDrDirID = fss->parID;
430 error = PBGetCatInfoSync(&pb);
431 if ( error ) return error;
432 pb.dirInfo.ioNamePtr = fss->name;
433 pb.dirInfo.ioFDirIndex = 0;
434 pb.dirInfo.ioVRefNum = fss->vRefNum;
435 pb.dirInfo.ioDrDirID = fss->parID;
436 pb.hFileInfo.ioFlCrDat = crdat;
437 pb.hFileInfo.ioFlMdDat = mddat;
438 pb.hFileInfo.ioFlBkDat = bkdat;
439 error = PBSetCatInfoSync(&pb);
440 return error;
441}
442
Jack Jansenf5c20571997-01-30 15:48:07 +0000443static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000444mfss_as_pathname(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000445{
Jack Jansendeefbe52001-08-08 13:46:49 +0000446#if TARGET_API_MAC_OSX
447 PyErr_SetString(PyExc_NotImplementedError, "FSSpec.as_pathname not supported on this platform");
448 return 0;
449#else
Jack Jansen17ba43f1995-01-26 16:22:07 +0000450 char strbuf[257];
451 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000452
Jack Jansenf5c20571997-01-30 15:48:07 +0000453 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000454 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000455 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000456 if ( err ) {
457 PyErr_Mac(ErrorObject, err);
458 return NULL;
459 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000460 return PyString_FromString(strbuf);
Jack Jansendeefbe52001-08-08 13:46:49 +0000461#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000462}
463
Jack Jansenf5c20571997-01-30 15:48:07 +0000464static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000465mfss_as_tuple(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000466{
Jack Jansenf5c20571997-01-30 15:48:07 +0000467 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000468 return NULL;
469 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
470 &self->fsspec.name[1], self->fsspec.name[0]);
471}
472
Jack Jansenf5c20571997-01-30 15:48:07 +0000473static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000474mfss_NewAlias(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000475{
476 FSSpec src, *srcp;
477 OSErr err;
478 AliasHandle alias;
479
480 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000481 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000482 return NULL;
483 if ( src.name[0] )
484 srcp = &src;
485 else
486 srcp = NULL;
487 err = NewAlias(srcp, &self->fsspec, &alias);
488 if ( err ) {
489 PyErr_Mac(ErrorObject, err);
490 return NULL;
491 }
492
Jack Jansenf5c20571997-01-30 15:48:07 +0000493 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000494}
495
Jack Jansenf5c20571997-01-30 15:48:07 +0000496static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000497mfss_NewAliasMinimal(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000498{
499 OSErr err;
500 AliasHandle alias;
501
Jack Jansenf5c20571997-01-30 15:48:07 +0000502 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000503 return NULL;
504 err = NewAliasMinimal(&self->fsspec, &alias);
505 if ( err ) {
506 PyErr_Mac(ErrorObject, err);
507 return NULL;
508 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000509 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000510}
511
Jack Jansen4e566ab2001-07-08 22:07:23 +0000512static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000513mfss_FSpMakeFSRef(mfssobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000514{
Jack Jansencbed91b2001-08-03 13:31:36 +0000515#if TARGET_API_MAC_OS8
516 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
517 return 0;
518#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000519 OSErr err;
520 FSRef fsref;
521
522 if (!PyArg_ParseTuple(args, ""))
523 return NULL;
524 err = FSpMakeFSRef(&self->fsspec, &fsref);
525 if ( err ) {
526 PyErr_Mac(ErrorObject, err);
527 return NULL;
528 }
529 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000530#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000531}
532
Jack Jansen3d185931995-08-07 14:04:10 +0000533/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000534static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000535mfss_GetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000536{
537 OSErr err;
538 FInfo info;
539
Jack Jansenf5c20571997-01-30 15:48:07 +0000540 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000541 return NULL;
542 err = FSpGetFInfo(&self->fsspec, &info);
543 if ( err ) {
544 PyErr_Mac(ErrorObject, err);
545 return NULL;
546 }
547 return Py_BuildValue("(O&O&)",
548 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
549}
550
Jack Jansenf5c20571997-01-30 15:48:07 +0000551static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000552mfss_SetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000553{
554 OSErr err;
555 OSType creator, type;
556 FInfo info;
557
Jack Jansenf5c20571997-01-30 15:48:07 +0000558 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000559 return NULL;
560 err = FSpGetFInfo(&self->fsspec, &info);
561 if ( err ) {
562 PyErr_Mac(ErrorObject, err);
563 return NULL;
564 }
565 info.fdType = type;
566 info.fdCreator = creator;
567 err = FSpSetFInfo(&self->fsspec, &info);
568 if ( err ) {
569 PyErr_Mac(ErrorObject, err);
570 return NULL;
571 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000572 Py_INCREF(Py_None);
573 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000574}
575
Jack Jansenf5c20571997-01-30 15:48:07 +0000576static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000577mfss_GetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000578{
579 OSErr err;
580 mfsiobject *fip;
581
582
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000584 return NULL;
585 if ( (fip=newmfsiobject()) == NULL )
586 return NULL;
587 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
588 if ( err ) {
589 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000590 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000591 return NULL;
592 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000593 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000594}
595
Jack Jansenf5c20571997-01-30 15:48:07 +0000596static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000597mfss_SetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000598{
599 OSErr err;
600 mfsiobject *fip;
601
Jack Jansenf5c20571997-01-30 15:48:07 +0000602 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000603 return NULL;
604 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
605 if ( err ) {
606 PyErr_Mac(ErrorObject, err);
607 return NULL;
608 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000609 Py_INCREF(Py_None);
610 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000611}
612
Jack Jansenf5c20571997-01-30 15:48:07 +0000613static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000614mfss_GetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000615{
616 OSErr err;
617 unsigned long crdat, mddat, bkdat;
618
Jack Jansenf5c20571997-01-30 15:48:07 +0000619 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000620 return NULL;
621 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
622 if ( err ) {
623 PyErr_Mac(ErrorObject, err);
624 return NULL;
625 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000626 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000627}
628
Jack Jansenf5c20571997-01-30 15:48:07 +0000629static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000630mfss_SetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000631{
632 OSErr err;
633 double crdat, mddat, bkdat;
634
Jack Jansenf5c20571997-01-30 15:48:07 +0000635 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000636 return NULL;
637 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
638 (unsigned long)mddat, (unsigned long)bkdat);
639 if ( err ) {
640 PyErr_Mac(ErrorObject, err);
641 return NULL;
642 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000643 Py_INCREF(Py_None);
644 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000645}
646
Jack Jansenf5c20571997-01-30 15:48:07 +0000647static struct PyMethodDef mfss_methods[] = {
648 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
649 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000650 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
651 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000652 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
653 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
654 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
655 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
656 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
657 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
658 {"GetDates", (PyCFunction)mfss_GetDates, 1},
659 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000660
661 {NULL, NULL} /* sentinel */
662};
663
664/* ---------- */
665
Jack Jansenf5c20571997-01-30 15:48:07 +0000666static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000667mfss_getattr(mfssobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000668{
Jack Jansenc889b761995-02-13 12:00:46 +0000669 if ( strcmp(name, "data") == 0)
670 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000671 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000672}
673
674mfssobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000675newmfssobject(FSSpec *fss)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000676{
677 mfssobject *self;
678
Jack Jansenf5c20571997-01-30 15:48:07 +0000679 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000680 if (self == NULL)
681 return NULL;
682 self->fsspec = *fss;
683 return self;
684}
685
686static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000687mfss_dealloc(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000688{
Jack Jansenf5c20571997-01-30 15:48:07 +0000689 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000690}
691
Jack Jansenf5c20571997-01-30 15:48:07 +0000692static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000693mfss_repr(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000694{
695 char buf[512];
696
Jack Jansendeefbe52001-08-08 13:46:49 +0000697 sprintf(buf, "FSSpec((%d, %ld, '%.*s'))",
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000698 self->fsspec.vRefNum,
699 self->fsspec.parID,
700 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000701 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000702}
703
704static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000705mfss_compare(mfssobject *v, mfssobject *w)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000706{
707 int minlen;
708 int res;
709
710 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
711 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
712 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
713 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
714 minlen = v->fsspec.name[0];
715 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
716 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
717 if ( res ) return res;
718 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
719 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
720 return res;
721}
722
Jack Jansenf5c20571997-01-30 15:48:07 +0000723statichere PyTypeObject Mfsstype = {
724 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000725 0, /*ob_size*/
726 "FSSpec", /*tp_name*/
727 sizeof(mfssobject), /*tp_basicsize*/
728 0, /*tp_itemsize*/
729 /* methods */
730 (destructor)mfss_dealloc, /*tp_dealloc*/
731 (printfunc)0, /*tp_print*/
732 (getattrfunc)mfss_getattr, /*tp_getattr*/
733 (setattrfunc)0, /*tp_setattr*/
734 (cmpfunc)mfss_compare, /*tp_compare*/
735 (reprfunc)mfss_repr, /*tp_repr*/
736 0, /*tp_as_number*/
737 0, /*tp_as_sequence*/
738 0, /*tp_as_mapping*/
739 (hashfunc)0, /*tp_hash*/
740};
741
742/* End of code for FSSpec objects */
743/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000744#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000745static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000746mfsr_as_fsspec(mfsrobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000747{
748 OSErr err;
749 FSSpec fss;
750
751 if (!PyArg_ParseTuple(args, ""))
752 return NULL;
753 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
754 if ( err ) {
755 PyErr_Mac(ErrorObject, err);
756 return NULL;
757 }
758 Py_INCREF(Py_None);
759 return (PyObject *)newmfssobject(&fss);
760}
761
762static struct PyMethodDef mfsr_methods[] = {
763 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
764#if 0
765 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
766 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
767 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
768 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
769 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
770 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
771 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
772 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
773 {"GetDates", (PyCFunction)mfss_GetDates, 1},
774 {"SetDates", (PyCFunction)mfss_SetDates, 1},
775#endif
776
777 {NULL, NULL} /* sentinel */
778};
779
780/* ---------- */
781
782static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000783mfsr_getattr(mfsrobject *self, char *name)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000784{
785 if ( strcmp(name, "data") == 0)
786 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
787 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
788}
789
790mfsrobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000791newmfsrobject(FSRef *fsr)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000792{
793 mfsrobject *self;
794
795 self = PyObject_NEW(mfsrobject, &Mfsrtype);
796 if (self == NULL)
797 return NULL;
798 self->fsref = *fsr;
799 return self;
800}
801
802static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000803mfsr_compare(mfsrobject *v, mfsrobject *w)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000804{
805 OSErr err;
806
807 if ( v == w ) return 0;
808 err = FSCompareFSRefs(&v->fsref, &w->fsref);
809 if ( err == 0 )
810 return 0;
811 if (v < w )
812 return -1;
813 return 1;
814}
815
816static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000817mfsr_dealloc(mfsrobject *self)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000818{
819 PyMem_DEL(self);
820}
821
822statichere PyTypeObject Mfsrtype = {
823 PyObject_HEAD_INIT(&PyType_Type)
824 0, /*ob_size*/
825 "FSRef", /*tp_name*/
826 sizeof(mfsrobject), /*tp_basicsize*/
827 0, /*tp_itemsize*/
828 /* methods */
829 (destructor)mfsr_dealloc, /*tp_dealloc*/
830 (printfunc)0, /*tp_print*/
831 (getattrfunc)mfsr_getattr, /*tp_getattr*/
832 (setattrfunc)0, /*tp_setattr*/
833 (cmpfunc)mfsr_compare, /*tp_compare*/
834 (reprfunc)0, /*tp_repr*/
835 0, /*tp_as_number*/
836 0, /*tp_as_sequence*/
837 0, /*tp_as_mapping*/
838 (hashfunc)0, /*tp_hash*/
839};
840
841/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000842#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000843/* -------------------------------------------------------- */
844
845static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000846mfs_ResolveAliasFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000847{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000848 FSSpec fss;
849 Boolean chain = 1, isfolder, wasaliased;
850 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000851
Jack Jansenf5c20571997-01-30 15:48:07 +0000852 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000853 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000854 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
855 if ( err ) {
856 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000857 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000858 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000859 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000860}
861
Jack Jansen74a1e632000-07-14 22:37:27 +0000862#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000863static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000864mfs_StandardGetFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000865{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000866 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000867 short numtypes;
868 StandardFileReply reply;
869
870 list[0] = list[1] = list[2] = list[3] = 0;
871 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000872 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000873 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
874 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000875 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000876 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000877 numtypes++;
878 }
Jack Jansenc2632861995-06-03 21:15:50 +0000879 if ( numtypes == 0 )
880 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000881 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000882 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000883}
884
Jack Jansenf5c20571997-01-30 15:48:07 +0000885static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000886mfs_PromptGetFile(PyObject *self, PyObject *args)
Jack Jansend5d5f461995-08-14 12:22:56 +0000887{
888 SFTypeList list;
889 short numtypes;
890 StandardFileReply reply;
891 char *prompt = NULL;
892
893 list[0] = list[1] = list[2] = list[3] = 0;
894 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000895 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000896 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
897 PyMac_GetOSType, &list[3]) )
898 return NULL;
899 while ( numtypes < 4 && list[numtypes] ) {
900 numtypes++;
901 }
902 if ( numtypes == 0 )
903 numtypes = -1;
904 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000905 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000906}
907
Jack Jansenf5c20571997-01-30 15:48:07 +0000908static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000909mfs_StandardPutFile(PyObject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000910{
911 Str255 prompt, dft;
912 StandardFileReply reply;
913
914 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000915 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000916 return NULL;
917 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000918 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000919}
920
Jack Jansend99d2831996-07-22 15:26:01 +0000921/*
922** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000923static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000924mfs_SetFolder(PyObject *self, PyObject *args)
Jack Jansend99d2831996-07-22 15:26:01 +0000925{
926 FSSpec spec;
927 FSSpec ospec;
928 short orefnum;
929 long oparid;
930
931 /* Get old values */
932 orefnum = -LMGetSFSaveDisk();
933 oparid = LMGetCurDirStore();
934 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
935
936 /* Go to working directory by default */
937 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000938 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000939 return NULL;
940 /* Set standard-file working directory */
941 LMSetSFSaveDisk(-spec.vRefNum);
942 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000943 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000944}
Jack Jansene79dc762000-06-02 21:35:07 +0000945#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000946
Jack Jansenf5c20571997-01-30 15:48:07 +0000947static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000948mfs_FSSpec(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000949{
950 FSSpec fss;
951
Jack Jansenf5c20571997-01-30 15:48:07 +0000952 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000953 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000954 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000955}
956
Jack Jansenf5c20571997-01-30 15:48:07 +0000957static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000958mfs_FSRef(PyObject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000959{
Jack Jansencbed91b2001-08-03 13:31:36 +0000960#if TARGET_API_MAC_OS8
961 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
962 return 0;
963#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000964 FSRef fsr;
965
966 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
967 return NULL;
968 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +0000969#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000970}
971
972static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000973mfs_RawFSSpec(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000974{
975 FSSpec *fssp;
976 int size;
977
Jack Janseneeccca91997-05-07 15:46:31 +0000978 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000979 return NULL;
980 if ( size != sizeof(FSSpec) ) {
981 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
982 return NULL;
983 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000984 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000985}
986
Jack Jansenf5c20571997-01-30 15:48:07 +0000987static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000988mfs_RawAlias(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000989{
990 char *dataptr;
991 Handle h;
992 int size;
993
Jack Janseneeccca91997-05-07 15:46:31 +0000994 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000995 return NULL;
996 h = NewHandle(size);
997 if ( h == NULL ) {
998 PyErr_NoMemory();
999 return NULL;
1000 }
1001 HLock(h);
1002 memcpy((char *)*h, dataptr, size);
1003 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001004 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001005}
1006
Jack Jansen74a1e632000-07-14 22:37:27 +00001007#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001008static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001009mfs_GetDirectory(PyObject *self, PyObject *args)
Jack Jansen81f51c71995-02-20 15:45:25 +00001010{
1011 FSSpec fsdir;
1012 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001013 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001014
Jack Jansenf5c20571997-01-30 15:48:07 +00001015 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001016 return NULL;
1017
Jack Jansend5d5f461995-08-14 12:22:56 +00001018 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001019 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001020}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001021#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001022
Jack Jansenf5c20571997-01-30 15:48:07 +00001023static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001024mfs_FindFolder(PyObject *self, PyObject *args)
Jack Jansen2c673621995-06-18 20:05:14 +00001025{
1026 OSErr err;
1027 short where;
1028 OSType which;
1029 int create;
1030 short refnum;
1031 long dirid;
1032
Jack Jansenabd703d2001-03-15 14:38:10 +00001033 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001034 return NULL;
1035 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1036 if ( err ) {
1037 PyErr_Mac(ErrorObject, err);
1038 return NULL;
1039 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001040 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001041}
1042
Jack Jansenf5c20571997-01-30 15:48:07 +00001043static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001044mfs_FindApplication(PyObject *self, PyObject *args)
Jack Jansen924ca851996-09-20 15:25:16 +00001045{
1046 OSErr err;
1047 OSType which;
1048 FSSpec fss;
1049
Jack Jansenf5c20571997-01-30 15:48:07 +00001050 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001051 return NULL;
1052 err = FindApplicationFromCreator(which, &fss);
1053 if ( err ) {
1054 PyErr_Mac(ErrorObject, err);
1055 return NULL;
1056 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001057 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001058}
1059
Jack Jansenf5c20571997-01-30 15:48:07 +00001060static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001061mfs_FInfo(PyObject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +00001062{
Jack Jansenf5c20571997-01-30 15:48:07 +00001063 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001064}
1065
Jack Jansend9936481997-06-16 14:31:38 +00001066static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001067mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args)
Jack Jansend9936481997-06-16 14:31:38 +00001068{
1069 OSErr err;
1070 char *fullpath;
1071 int fullpathlen;
1072 AliasHandle alias;
1073 Str32 zonename;
1074 Str31 servername;
1075
1076 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1077 return NULL;
1078 zonename[0] = 0;
1079 servername[0] = 0;
1080 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1081 servername, &alias);
1082 if ( err ) {
1083 PyErr_Mac(ErrorObject, err);
1084 return NULL;
1085 }
1086 return (PyObject *)newmfsaobject(alias);
1087}
1088
1089
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001090/* List of methods defined in the module */
1091
Jack Jansenf5c20571997-01-30 15:48:07 +00001092static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001093 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001094#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001095 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001096 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001097 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001098 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001099 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001100#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001101 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001102 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001103 {"RawFSSpec", mfs_RawFSSpec, 1},
1104 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001105 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001106 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001107 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001108 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001109
1110 {NULL, NULL} /* sentinel */
1111};
1112
Jack Jansen4e566ab2001-07-08 22:07:23 +00001113/*
1114** Convert a Python object to an FSSpec.
1115** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1116** (vrefnum, dirid, path).
1117*/
1118int
1119PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1120{
Jack Jansencbed91b2001-08-03 13:31:36 +00001121#if TARGET_API_MAC_OS8
1122 return 0;
1123#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001124 /* If it's an FSRef we're also okay. */
1125 if (_mfs_GetFSRefFromFSRef(v, fsr))
1126 return 1;
1127 /* first check whether it already is an FSSpec */
1128 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1129 return 1;
1130 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001131#if TARGET_API_MAC_OSX
1132 OSStatus err;
1133 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1134 PyErr_Mac(ErrorObject, err);
1135 return 0;
1136 }
1137 return 1;
1138#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001139 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1140 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001141#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001142 }
1143 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1144 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001145#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001146}
1147
1148/* Convert FSSpec to PyObject */
1149PyObject *PyMac_BuildFSRef(FSRef *v)
1150{
Jack Jansencbed91b2001-08-03 13:31:36 +00001151#if TARGET_API_MAC_OS8
1152 return NULL;
1153#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001154 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001155#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001156}
1157
1158/*
1159** Convert a Python object to an FSRef.
1160** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1161*/
1162int
1163PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1164{
1165 Str255 path;
1166 short refnum;
1167 long parid;
1168 OSErr err;
1169
1170 /* first check whether it already is an FSSpec */
1171 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1172 return 1;
1173 /* If it's an FSRef we're also okay. */
1174 if (_mfs_GetFSSpecFromFSRef(v, fs))
1175 return 1;
1176 if ( PyString_Check(v) ) {
1177 /* It's a pathname */
1178 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1179 return 0;
1180 refnum = 0; /* XXXX Should get CurWD here?? */
1181 parid = 0;
1182 } else {
1183 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1184 &refnum, &parid, PyMac_GetStr255, &path)) {
1185 return 0;
1186 }
1187 }
1188 err = FSMakeFSSpec(refnum, parid, path, fs);
1189 if ( err && err != fnfErr ) {
1190 PyMac_Error(err);
1191 return 0;
1192 }
1193 return 1;
1194}
1195
1196/* Convert FSSpec to PyObject */
1197PyObject *PyMac_BuildFSSpec(FSSpec *v)
1198{
1199 return (PyObject *)newmfssobject(v);
1200}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001201
1202/* Initialization function for the module (*must* be called initmacfs) */
1203
1204void
Jack Jansendeefbe52001-08-08 13:46:49 +00001205initmacfs(void)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001206{
Jack Jansenf5c20571997-01-30 15:48:07 +00001207 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001208
Jack Jansen45900492001-08-06 15:32:30 +00001209 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
Jack Jansena5bca572001-08-03 15:39:27 +00001210
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001211 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001212 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001213
1214 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001215 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001216 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001217 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001218
Jack Jansena755e681997-09-20 17:40:22 +00001219 Mfsatype.ob_type = &PyType_Type;
1220 Py_INCREF(&Mfsatype);
1221 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1222 Mfsstype.ob_type = &PyType_Type;
1223 Py_INCREF(&Mfsstype);
1224 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1225 Mfsitype.ob_type = &PyType_Type;
1226 Py_INCREF(&Mfsitype);
1227 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001228 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001229}