blob: 5e34a648fc5e1f08ae59de8267876ed62b2f6c29 [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{
370 static FSRef *fsrp;
371
372 if ( is_mfsrobject(self) ) {
373 fsrp = &((mfsrobject *)self)->fsref;
374 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
375 return 1;
376 }
377 return 0;
378}
379
380/* Return an FSRef if this is an FSRef */
381static int
382_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
383{
384 if ( is_mfsrobject(self) ) {
385 *fsrp = ((mfsrobject *)self)->fsref;
386 return 1;
387 }
388 return 0;
389}
390
391/* Return an FSRef if this is an FSSpec */
392static int
393_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
394{
395 if ( is_mfssobject(self) ) {
396 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
397 return 1;
398 }
399 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000400}
401
Jack Jansen0bdf9791996-09-15 22:11:25 +0000402/*
403** Two generally useful routines
404*/
405static OSErr
406PyMac_GetFileDates(fss, crdat, mddat, bkdat)
407 FSSpec *fss;
408 unsigned long *crdat, *mddat, *bkdat;
409{
410 CInfoPBRec pb;
411 OSErr error;
412
413 pb.dirInfo.ioNamePtr = fss->name;
414 pb.dirInfo.ioFDirIndex = 0;
415 pb.dirInfo.ioVRefNum = fss->vRefNum;
416 pb.dirInfo.ioDrDirID = fss->parID;
417 error = PBGetCatInfoSync(&pb);
418 if ( error ) return error;
419 *crdat = pb.hFileInfo.ioFlCrDat;
420 *mddat = pb.hFileInfo.ioFlMdDat;
421 *bkdat = pb.hFileInfo.ioFlBkDat;
422 return 0;
423}
424
425static OSErr
426PyMac_SetFileDates(fss, crdat, mddat, bkdat)
427 FSSpec *fss;
428 unsigned long crdat, mddat, bkdat;
429{
430 CInfoPBRec pb;
431 OSErr error;
432
433 pb.dirInfo.ioNamePtr = fss->name;
434 pb.dirInfo.ioFDirIndex = 0;
435 pb.dirInfo.ioVRefNum = fss->vRefNum;
436 pb.dirInfo.ioDrDirID = fss->parID;
437 error = PBGetCatInfoSync(&pb);
438 if ( error ) return error;
439 pb.dirInfo.ioNamePtr = fss->name;
440 pb.dirInfo.ioFDirIndex = 0;
441 pb.dirInfo.ioVRefNum = fss->vRefNum;
442 pb.dirInfo.ioDrDirID = fss->parID;
443 pb.hFileInfo.ioFlCrDat = crdat;
444 pb.hFileInfo.ioFlMdDat = mddat;
445 pb.hFileInfo.ioFlBkDat = bkdat;
446 error = PBSetCatInfoSync(&pb);
447 return error;
448}
449
Jack Jansenf5c20571997-01-30 15:48:07 +0000450static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000451mfss_as_pathname(self, args)
452 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000453 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000454{
455 char strbuf[257];
456 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000457
Jack Jansenf5c20571997-01-30 15:48:07 +0000458 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000459 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000460 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000461 if ( err ) {
462 PyErr_Mac(ErrorObject, err);
463 return NULL;
464 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000465 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000466}
467
Jack Jansenf5c20571997-01-30 15:48:07 +0000468static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000469mfss_as_tuple(self, args)
470 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000471 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000472{
Jack Jansenf5c20571997-01-30 15:48:07 +0000473 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000474 return NULL;
475 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
476 &self->fsspec.name[1], self->fsspec.name[0]);
477}
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000480mfss_NewAlias(self, args)
481 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000482 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000483{
484 FSSpec src, *srcp;
485 OSErr err;
486 AliasHandle alias;
487
488 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000489 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000490 return NULL;
491 if ( src.name[0] )
492 srcp = &src;
493 else
494 srcp = NULL;
495 err = NewAlias(srcp, &self->fsspec, &alias);
496 if ( err ) {
497 PyErr_Mac(ErrorObject, err);
498 return NULL;
499 }
500
Jack Jansenf5c20571997-01-30 15:48:07 +0000501 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000502}
503
Jack Jansenf5c20571997-01-30 15:48:07 +0000504static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000505mfss_NewAliasMinimal(self, args)
506 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000507 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000508{
509 OSErr err;
510 AliasHandle alias;
511
Jack Jansenf5c20571997-01-30 15:48:07 +0000512 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000513 return NULL;
514 err = NewAliasMinimal(&self->fsspec, &alias);
515 if ( err ) {
516 PyErr_Mac(ErrorObject, err);
517 return NULL;
518 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000519 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000520}
521
Jack Jansen4e566ab2001-07-08 22:07:23 +0000522static PyObject *
523mfss_FSpMakeFSRef(self, args)
524 mfssobject *self;
525 PyObject *args;
526{
527 OSErr err;
528 FSRef fsref;
529
530 if (!PyArg_ParseTuple(args, ""))
531 return NULL;
532 err = FSpMakeFSRef(&self->fsspec, &fsref);
533 if ( err ) {
534 PyErr_Mac(ErrorObject, err);
535 return NULL;
536 }
537 return (PyObject *)newmfsrobject(&fsref);
538}
539
Jack Jansen3d185931995-08-07 14:04:10 +0000540/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000541static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000542mfss_GetCreatorType(self, args)
543 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000545{
546 OSErr err;
547 FInfo info;
548
Jack Jansenf5c20571997-01-30 15:48:07 +0000549 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000550 return NULL;
551 err = FSpGetFInfo(&self->fsspec, &info);
552 if ( err ) {
553 PyErr_Mac(ErrorObject, err);
554 return NULL;
555 }
556 return Py_BuildValue("(O&O&)",
557 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
558}
559
Jack Jansenf5c20571997-01-30 15:48:07 +0000560static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000561mfss_SetCreatorType(self, args)
562 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000563 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000564{
565 OSErr err;
566 OSType creator, type;
567 FInfo info;
568
Jack Jansenf5c20571997-01-30 15:48:07 +0000569 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000570 return NULL;
571 err = FSpGetFInfo(&self->fsspec, &info);
572 if ( err ) {
573 PyErr_Mac(ErrorObject, err);
574 return NULL;
575 }
576 info.fdType = type;
577 info.fdCreator = creator;
578 err = FSpSetFInfo(&self->fsspec, &info);
579 if ( err ) {
580 PyErr_Mac(ErrorObject, err);
581 return NULL;
582 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 Py_INCREF(Py_None);
584 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000585}
586
Jack Jansenf5c20571997-01-30 15:48:07 +0000587static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000588mfss_GetFInfo(self, args)
589 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000590 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000591{
592 OSErr err;
593 mfsiobject *fip;
594
595
Jack Jansenf5c20571997-01-30 15:48:07 +0000596 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000597 return NULL;
598 if ( (fip=newmfsiobject()) == NULL )
599 return NULL;
600 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
601 if ( err ) {
602 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000603 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000604 return NULL;
605 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000606 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000607}
608
Jack Jansenf5c20571997-01-30 15:48:07 +0000609static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000610mfss_SetFInfo(self, args)
611 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000612 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000613{
614 OSErr err;
615 mfsiobject *fip;
616
Jack Jansenf5c20571997-01-30 15:48:07 +0000617 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000618 return NULL;
619 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
620 if ( err ) {
621 PyErr_Mac(ErrorObject, err);
622 return NULL;
623 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000624 Py_INCREF(Py_None);
625 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000626}
627
Jack Jansenf5c20571997-01-30 15:48:07 +0000628static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000629mfss_GetDates(self, args)
630 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000631 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000632{
633 OSErr err;
634 unsigned long crdat, mddat, bkdat;
635
Jack Jansenf5c20571997-01-30 15:48:07 +0000636 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000637 return NULL;
638 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
639 if ( err ) {
640 PyErr_Mac(ErrorObject, err);
641 return NULL;
642 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000643 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000644}
645
Jack Jansenf5c20571997-01-30 15:48:07 +0000646static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000647mfss_SetDates(self, args)
648 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000649 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000650{
651 OSErr err;
652 double crdat, mddat, bkdat;
653
Jack Jansenf5c20571997-01-30 15:48:07 +0000654 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000655 return NULL;
656 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
657 (unsigned long)mddat, (unsigned long)bkdat);
658 if ( err ) {
659 PyErr_Mac(ErrorObject, err);
660 return NULL;
661 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000662 Py_INCREF(Py_None);
663 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000664}
665
Jack Jansenf5c20571997-01-30 15:48:07 +0000666static struct PyMethodDef mfss_methods[] = {
667 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
668 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000669 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
670 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000671 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
672 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
673 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
674 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
675 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
676 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
677 {"GetDates", (PyCFunction)mfss_GetDates, 1},
678 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000679
680 {NULL, NULL} /* sentinel */
681};
682
683/* ---------- */
684
Jack Jansenf5c20571997-01-30 15:48:07 +0000685static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000686mfss_getattr(self, name)
687 mfssobject *self;
688 char *name;
689{
Jack Jansenc889b761995-02-13 12:00:46 +0000690 if ( strcmp(name, "data") == 0)
691 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000692 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000693}
694
695mfssobject *
696newmfssobject(fss)
697 FSSpec *fss;
698{
699 mfssobject *self;
700
Jack Jansenf5c20571997-01-30 15:48:07 +0000701 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000702 if (self == NULL)
703 return NULL;
704 self->fsspec = *fss;
705 return self;
706}
707
708static void
709mfss_dealloc(self)
710 mfssobject *self;
711{
Jack Jansenf5c20571997-01-30 15:48:07 +0000712 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000713}
714
Jack Jansenf5c20571997-01-30 15:48:07 +0000715static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000716mfss_repr(self)
717 mfssobject *self;
718{
719 char buf[512];
720
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000721 sprintf(buf, "FSSpec((%d, %d, '%.*s'))",
722 self->fsspec.vRefNum,
723 self->fsspec.parID,
724 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000725 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000726}
727
728static int
729mfss_compare(v, w)
730 mfssobject *v, *w;
731{
732 int minlen;
733 int res;
734
735 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
736 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
737 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
738 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
739 minlen = v->fsspec.name[0];
740 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
741 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
742 if ( res ) return res;
743 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
744 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
745 return res;
746}
747
Jack Jansenf5c20571997-01-30 15:48:07 +0000748statichere PyTypeObject Mfsstype = {
749 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000750 0, /*ob_size*/
751 "FSSpec", /*tp_name*/
752 sizeof(mfssobject), /*tp_basicsize*/
753 0, /*tp_itemsize*/
754 /* methods */
755 (destructor)mfss_dealloc, /*tp_dealloc*/
756 (printfunc)0, /*tp_print*/
757 (getattrfunc)mfss_getattr, /*tp_getattr*/
758 (setattrfunc)0, /*tp_setattr*/
759 (cmpfunc)mfss_compare, /*tp_compare*/
760 (reprfunc)mfss_repr, /*tp_repr*/
761 0, /*tp_as_number*/
762 0, /*tp_as_sequence*/
763 0, /*tp_as_mapping*/
764 (hashfunc)0, /*tp_hash*/
765};
766
767/* End of code for FSSpec objects */
768/* -------------------------------------------------------- */
769
Jack Jansenf5c20571997-01-30 15:48:07 +0000770static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +0000771mfsr_as_fsspec(self, args)
772 mfsrobject *self;
773 PyObject *args;
774{
775 OSErr err;
776 FSSpec fss;
777
778 if (!PyArg_ParseTuple(args, ""))
779 return NULL;
780 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
781 if ( err ) {
782 PyErr_Mac(ErrorObject, err);
783 return NULL;
784 }
785 Py_INCREF(Py_None);
786 return (PyObject *)newmfssobject(&fss);
787}
788
789static struct PyMethodDef mfsr_methods[] = {
790 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
791#if 0
792 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
793 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
794 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
795 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
796 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
797 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
798 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
799 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
800 {"GetDates", (PyCFunction)mfss_GetDates, 1},
801 {"SetDates", (PyCFunction)mfss_SetDates, 1},
802#endif
803
804 {NULL, NULL} /* sentinel */
805};
806
807/* ---------- */
808
809static PyObject *
810mfsr_getattr(self, name)
811 mfsrobject *self;
812 char *name;
813{
814 if ( strcmp(name, "data") == 0)
815 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
816 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
817}
818
819mfsrobject *
820newmfsrobject(fsr)
821 FSRef *fsr;
822{
823 mfsrobject *self;
824
825 self = PyObject_NEW(mfsrobject, &Mfsrtype);
826 if (self == NULL)
827 return NULL;
828 self->fsref = *fsr;
829 return self;
830}
831
832static int
833mfsr_compare(v, w)
834 mfsrobject *v, *w;
835{
836 OSErr err;
837
838 if ( v == w ) return 0;
839 err = FSCompareFSRefs(&v->fsref, &w->fsref);
840 if ( err == 0 )
841 return 0;
842 if (v < w )
843 return -1;
844 return 1;
845}
846
847static void
848mfsr_dealloc(self)
849 mfsrobject *self;
850{
851 PyMem_DEL(self);
852}
853
854statichere PyTypeObject Mfsrtype = {
855 PyObject_HEAD_INIT(&PyType_Type)
856 0, /*ob_size*/
857 "FSRef", /*tp_name*/
858 sizeof(mfsrobject), /*tp_basicsize*/
859 0, /*tp_itemsize*/
860 /* methods */
861 (destructor)mfsr_dealloc, /*tp_dealloc*/
862 (printfunc)0, /*tp_print*/
863 (getattrfunc)mfsr_getattr, /*tp_getattr*/
864 (setattrfunc)0, /*tp_setattr*/
865 (cmpfunc)mfsr_compare, /*tp_compare*/
866 (reprfunc)0, /*tp_repr*/
867 0, /*tp_as_number*/
868 0, /*tp_as_sequence*/
869 0, /*tp_as_mapping*/
870 (hashfunc)0, /*tp_hash*/
871};
872
873/* End of code for FSRef objects */
874/* -------------------------------------------------------- */
875
876static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000877mfs_ResolveAliasFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000878 PyObject *self; /* Not used */
879 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000880{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000881 FSSpec fss;
882 Boolean chain = 1, isfolder, wasaliased;
883 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000884
Jack Jansenf5c20571997-01-30 15:48:07 +0000885 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000886 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000887 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
888 if ( err ) {
889 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000890 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000891 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000892 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000893}
894
Jack Jansen74a1e632000-07-14 22:37:27 +0000895#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000896static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000897mfs_StandardGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000898 PyObject *self; /* Not used */
899 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000900{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000901 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000902 short numtypes;
903 StandardFileReply reply;
904
905 list[0] = list[1] = list[2] = list[3] = 0;
906 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000907 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000908 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
909 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000910 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000911 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000912 numtypes++;
913 }
Jack Jansenc2632861995-06-03 21:15:50 +0000914 if ( numtypes == 0 )
915 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000916 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000917 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000918}
919
Jack Jansenf5c20571997-01-30 15:48:07 +0000920static PyObject *
Jack Jansend5d5f461995-08-14 12:22:56 +0000921mfs_PromptGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000922 PyObject *self; /* Not used */
923 PyObject *args;
Jack Jansend5d5f461995-08-14 12:22:56 +0000924{
925 SFTypeList list;
926 short numtypes;
927 StandardFileReply reply;
928 char *prompt = NULL;
929
930 list[0] = list[1] = list[2] = list[3] = 0;
931 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000932 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000933 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
934 PyMac_GetOSType, &list[3]) )
935 return NULL;
936 while ( numtypes < 4 && list[numtypes] ) {
937 numtypes++;
938 }
939 if ( numtypes == 0 )
940 numtypes = -1;
941 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000942 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000943}
944
Jack Jansenf5c20571997-01-30 15:48:07 +0000945static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000946mfs_StandardPutFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000947 PyObject *self; /* Not used */
948 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000949{
950 Str255 prompt, dft;
951 StandardFileReply reply;
952
953 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000954 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000955 return NULL;
956 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000957 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000958}
959
Jack Jansend99d2831996-07-22 15:26:01 +0000960/*
961** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000962static PyObject *
Jack Jansend99d2831996-07-22 15:26:01 +0000963mfs_SetFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000964 PyObject *self;
965 PyObject *args;
Jack Jansend99d2831996-07-22 15:26:01 +0000966{
967 FSSpec spec;
968 FSSpec ospec;
969 short orefnum;
970 long oparid;
971
972 /* Get old values */
973 orefnum = -LMGetSFSaveDisk();
974 oparid = LMGetCurDirStore();
975 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
976
977 /* Go to working directory by default */
978 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000979 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000980 return NULL;
981 /* Set standard-file working directory */
982 LMSetSFSaveDisk(-spec.vRefNum);
983 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000984 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000985}
Jack Jansene79dc762000-06-02 21:35:07 +0000986#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000987
Jack Jansenf5c20571997-01-30 15:48:07 +0000988static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000989mfs_FSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000990 PyObject *self; /* Not used */
991 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000992{
993 FSSpec fss;
994
Jack Jansenf5c20571997-01-30 15:48:07 +0000995 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000996 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000997 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000998}
999
Jack Jansenf5c20571997-01-30 15:48:07 +00001000static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +00001001mfs_FSRef(self, args)
1002 PyObject *self; /* Not used */
1003 PyObject *args;
1004{
1005 FSRef fsr;
1006
1007 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
1008 return NULL;
1009 return (PyObject *)newmfsrobject(&fsr);
1010}
1011
1012static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001013mfs_RawFSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001014 PyObject *self; /* Not used */
1015 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +00001016{
1017 FSSpec *fssp;
1018 int size;
1019
Jack Janseneeccca91997-05-07 15:46:31 +00001020 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001021 return NULL;
1022 if ( size != sizeof(FSSpec) ) {
1023 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
1024 return NULL;
1025 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001026 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +00001027}
1028
Jack Jansenf5c20571997-01-30 15:48:07 +00001029static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001030mfs_RawAlias(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 char *dataptr;
1035 Handle h;
1036 int size;
1037
Jack Janseneeccca91997-05-07 15:46:31 +00001038 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001039 return NULL;
1040 h = NewHandle(size);
1041 if ( h == NULL ) {
1042 PyErr_NoMemory();
1043 return NULL;
1044 }
1045 HLock(h);
1046 memcpy((char *)*h, dataptr, size);
1047 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001048 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001049}
1050
Jack Jansen74a1e632000-07-14 22:37:27 +00001051#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001052static PyObject *
Jack Jansen81f51c71995-02-20 15:45:25 +00001053mfs_GetDirectory(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001054 PyObject *self; /* Not used */
1055 PyObject *args;
Jack Jansen81f51c71995-02-20 15:45:25 +00001056{
1057 FSSpec fsdir;
1058 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001059 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001060
Jack Jansenf5c20571997-01-30 15:48:07 +00001061 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001062 return NULL;
1063
Jack Jansend5d5f461995-08-14 12:22:56 +00001064 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001065 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001066}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001067#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001068
Jack Jansenf5c20571997-01-30 15:48:07 +00001069static PyObject *
Jack Jansen2c673621995-06-18 20:05:14 +00001070mfs_FindFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001071 PyObject *self; /* Not used */
1072 PyObject *args;
Jack Jansen2c673621995-06-18 20:05:14 +00001073{
1074 OSErr err;
1075 short where;
1076 OSType which;
1077 int create;
1078 short refnum;
1079 long dirid;
1080
Jack Jansenabd703d2001-03-15 14:38:10 +00001081 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001082 return NULL;
1083 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1084 if ( err ) {
1085 PyErr_Mac(ErrorObject, err);
1086 return NULL;
1087 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001088 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001089}
1090
Jack Jansenf5c20571997-01-30 15:48:07 +00001091static PyObject *
Jack Jansen924ca851996-09-20 15:25:16 +00001092mfs_FindApplication(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001093 PyObject *self; /* Not used */
1094 PyObject *args;
Jack Jansen924ca851996-09-20 15:25:16 +00001095{
1096 OSErr err;
1097 OSType which;
1098 FSSpec fss;
1099
Jack Jansenf5c20571997-01-30 15:48:07 +00001100 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001101 return NULL;
1102 err = FindApplicationFromCreator(which, &fss);
1103 if ( err ) {
1104 PyErr_Mac(ErrorObject, err);
1105 return NULL;
1106 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001107 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001108}
1109
Jack Jansenf5c20571997-01-30 15:48:07 +00001110static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +00001111mfs_FInfo(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001112 PyObject *self;
1113 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +00001114{
Jack Jansenf5c20571997-01-30 15:48:07 +00001115 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001116}
1117
Jack Jansend9936481997-06-16 14:31:38 +00001118static PyObject *
1119mfs_NewAliasMinimalFromFullPath(self, args)
1120 PyObject *self; /* Not used */
1121 PyObject *args;
1122{
1123 OSErr err;
1124 char *fullpath;
1125 int fullpathlen;
1126 AliasHandle alias;
1127 Str32 zonename;
1128 Str31 servername;
1129
1130 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1131 return NULL;
1132 zonename[0] = 0;
1133 servername[0] = 0;
1134 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1135 servername, &alias);
1136 if ( err ) {
1137 PyErr_Mac(ErrorObject, err);
1138 return NULL;
1139 }
1140 return (PyObject *)newmfsaobject(alias);
1141}
1142
1143
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001144/* List of methods defined in the module */
1145
Jack Jansenf5c20571997-01-30 15:48:07 +00001146static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001147 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001148#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001149 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001150 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001151 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001152 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001153 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001154#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001155 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001156 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001157 {"RawFSSpec", mfs_RawFSSpec, 1},
1158 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001159 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001160 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001161 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001162 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001163
1164 {NULL, NULL} /* sentinel */
1165};
1166
Jack Jansen4e566ab2001-07-08 22:07:23 +00001167/*
1168** Convert a Python object to an FSSpec.
1169** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1170** (vrefnum, dirid, path).
1171*/
1172int
1173PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1174{
1175 OSErr err;
1176
1177 /* If it's an FSRef we're also okay. */
1178 if (_mfs_GetFSRefFromFSRef(v, fsr))
1179 return 1;
1180 /* first check whether it already is an FSSpec */
1181 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1182 return 1;
1183 if ( PyString_Check(v) ) {
1184 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1185 return 0;
1186 }
1187 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1188 return 0;
1189}
1190
1191/* Convert FSSpec to PyObject */
1192PyObject *PyMac_BuildFSRef(FSRef *v)
1193{
1194 return (PyObject *)newmfsrobject(v);
1195}
1196
1197/*
1198** Convert a Python object to an FSRef.
1199** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1200*/
1201int
1202PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1203{
1204 Str255 path;
1205 short refnum;
1206 long parid;
1207 OSErr err;
1208
1209 /* first check whether it already is an FSSpec */
1210 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1211 return 1;
1212 /* If it's an FSRef we're also okay. */
1213 if (_mfs_GetFSSpecFromFSRef(v, fs))
1214 return 1;
1215 if ( PyString_Check(v) ) {
1216 /* It's a pathname */
1217 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1218 return 0;
1219 refnum = 0; /* XXXX Should get CurWD here?? */
1220 parid = 0;
1221 } else {
1222 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1223 &refnum, &parid, PyMac_GetStr255, &path)) {
1224 return 0;
1225 }
1226 }
1227 err = FSMakeFSSpec(refnum, parid, path, fs);
1228 if ( err && err != fnfErr ) {
1229 PyMac_Error(err);
1230 return 0;
1231 }
1232 return 1;
1233}
1234
1235/* Convert FSSpec to PyObject */
1236PyObject *PyMac_BuildFSSpec(FSSpec *v)
1237{
1238 return (PyObject *)newmfssobject(v);
1239}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001240
1241/* Initialization function for the module (*must* be called initmacfs) */
1242
1243void
1244initmacfs()
1245{
Jack Jansenf5c20571997-01-30 15:48:07 +00001246 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001247
1248 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001249 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001250
1251 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001252 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001253 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001254 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001255
Jack Jansena755e681997-09-20 17:40:22 +00001256 Mfsatype.ob_type = &PyType_Type;
1257 Py_INCREF(&Mfsatype);
1258 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1259 Mfsstype.ob_type = &PyType_Type;
1260 Py_INCREF(&Mfsstype);
1261 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1262 Mfsitype.ob_type = &PyType_Type;
1263 Py_INCREF(&Mfsitype);
1264 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001265 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001266}