blob: f16cd363dc7626c6a1592ae8a3a585486e713670 [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
Just van Rossumab57c7d2001-10-31 22:55:08 +000042#include "pythonresources.h"
43extern PyMac_PrefRecord PyMac_options;
44
Jack Jansena5bca572001-08-03 15:39:27 +000045#ifdef USE_TOOLBOX_OBJECT_GLUE
46extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
Jack Jansen61142972001-09-02 00:09:35 +000047extern PyObject *_PyMac_BuildFSSpec(FSSpec *);
48extern int _PyMac_GetFSRef(PyObject *, FSRef *);
Jack Jansenfabd00f2001-09-01 23:39:58 +000049extern PyObject *_PyMac_BuildFSRef(FSRef *);
Jack Jansena5bca572001-08-03 15:39:27 +000050#define PyMac_GetFSSpec _PyMac_GetFSSpec
Jack Jansenfabd00f2001-09-01 23:39:58 +000051#define PyMac_BuildFSSpec _PyMac_BuildFSSpec
52#define PyMac_GetFSRef _PyMac_GetFSRef
53#define PyMac_BuildFSRef _PyMac_BuildFSRef
Jack Jansena5bca572001-08-03 15:39:27 +000054#endif
Jack Jansenf5c20571997-01-30 15:48:07 +000055static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000056
Jack Jansen697842f2001-09-10 22:00:39 +000057#ifdef TARGET_API_MAC_OSX
58#define PATHNAMELEN 1024
59#else
60#define PATHNAMELEN 256
61#endif
62
Jack Jansen84fa5ec1995-01-18 14:04:40 +000063/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000064/* Declarations for objects of type Alias */
65
66typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000067 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000068 AliasHandle alias;
69} mfsaobject;
70
Jack Jansenf5c20571997-01-30 15:48:07 +000071staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000072
73#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
74
75/* ---------------------------------------------------------------- */
76/* Declarations for objects of type FSSpec */
77
78typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000079 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000080 FSSpec fsspec;
81} mfssobject;
82
Jack Jansenf5c20571997-01-30 15:48:07 +000083staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000084
85#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
86
Jack Jansen4e566ab2001-07-08 22:07:23 +000087/* ---------------------------------------------------------------- */
88/* Declarations for objects of type FSRef */
89
90typedef struct {
91 PyObject_HEAD
92 FSRef fsref;
93} mfsrobject;
94
95staticforward PyTypeObject Mfsrtype;
96
97#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
98
Guido van Rossumefd97671995-01-26 22:56:16 +000099
Jack Jansen3d185931995-08-07 14:04:10 +0000100/* ---------------------------------------------------------------- */
101/* Declarations for objects of type FInfo */
102
103typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000104 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +0000105 FInfo finfo;
106} mfsiobject;
107
Jack Jansenf5c20571997-01-30 15:48:07 +0000108staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +0000109
110#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
111
112
Jack Jansen4e566ab2001-07-08 22:07:23 +0000113staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
114staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000115
Jack Jansen17ba43f1995-01-26 16:22:07 +0000116/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000117
Jack Jansenf5c20571997-01-30 15:48:07 +0000118static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000119mfsa_Resolve(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000120{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000121 FSSpec from, *fromp, result;
122 Boolean changed;
123 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000124
Jack Jansen17ba43f1995-01-26 16:22:07 +0000125 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000126 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000127 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000128 if (from.name[0] )
129 fromp = &from;
130 else
131 fromp = NULL;
132 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000133 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000134 PyErr_Mac(ErrorObject, err);
135 return NULL;
136 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000137 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000138}
139
Jack Jansenf5c20571997-01-30 15:48:07 +0000140static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000141mfsa_GetInfo(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000142{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000143 Str63 value;
144 int i;
145 OSErr err;
146
Jack Jansenf5c20571997-01-30 15:48:07 +0000147 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000148 return NULL;
149 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
150 if ( err ) {
151 PyErr_Mac(ErrorObject, err);
152 return 0;
153 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000154 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000155}
156
Jack Jansenf5c20571997-01-30 15:48:07 +0000157static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000158mfsa_Update(mfsaobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000159{
160 FSSpec target, fromfile, *fromfilep;
161 OSErr err;
162 Boolean changed;
163
164 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000165 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000166 PyMac_GetFSSpec, &fromfile))
167 return NULL;
168 if ( fromfile.name[0] )
169 fromfilep = &fromfile;
170 else
171 fromfilep = NULL;
172 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
173 if ( err ) {
174 PyErr_Mac(ErrorObject, err);
175 return 0;
176 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000177 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000178}
179
Jack Jansenf5c20571997-01-30 15:48:07 +0000180static struct PyMethodDef mfsa_methods[] = {
181 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
182 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
183 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000184
185 {NULL, NULL} /* sentinel */
186};
187
188/* ---------- */
189
Jack Jansenf5c20571997-01-30 15:48:07 +0000190static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000191mfsa_getattr(mfsaobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000192{
Jack Jansenc889b761995-02-13 12:00:46 +0000193 if ( strcmp(name, "data") == 0 ) {
194 int size;
195 PyObject *rv;
196
197 size = GetHandleSize((Handle)self->alias);
198 HLock((Handle)self->alias);
199 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
200 HUnlock((Handle)self->alias);
201 return rv;
202 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000203 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000204}
205
Jack Jansen9ae898b2000-07-11 21:16:03 +0000206static mfsaobject *
207newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000208{
209 mfsaobject *self;
210
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000212 if (self == NULL)
213 return NULL;
214 self->alias = alias;
215 return self;
216}
217
218
219static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000220mfsa_dealloc(mfsaobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000221{
222#if 0
223 if ( self->alias ) {
224 should we do something here?
225 }
226#endif
227
Jack Jansenf5c20571997-01-30 15:48:07 +0000228 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000229}
230
Jack Jansenf5c20571997-01-30 15:48:07 +0000231statichere PyTypeObject Mfsatype = {
232 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000233 0, /*ob_size*/
234 "Alias", /*tp_name*/
235 sizeof(mfsaobject), /*tp_basicsize*/
236 0, /*tp_itemsize*/
237 /* methods */
238 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000239 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000240 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000241 (setattrfunc)0, /*tp_setattr*/
242 (cmpfunc)0, /*tp_compare*/
243 (reprfunc)0, /*tp_repr*/
244 0, /*tp_as_number*/
245 0, /*tp_as_sequence*/
246 0, /*tp_as_mapping*/
247 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000248};
249
250/* End of code for Alias objects */
251/* -------------------------------------------------------- */
252
Jack Jansen3d185931995-08-07 14:04:10 +0000253/* ---------------------------------------------------------------- */
254
Jack Jansenf5c20571997-01-30 15:48:07 +0000255static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000256
257 {NULL, NULL} /* sentinel */
258};
259
260/* ---------- */
261
262static mfsiobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000263newmfsiobject(void)
Jack Jansen3d185931995-08-07 14:04:10 +0000264{
265 mfsiobject *self;
266
Jack Jansenf5c20571997-01-30 15:48:07 +0000267 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000268 if (self == NULL)
269 return NULL;
270 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
271 return self;
272}
273
274static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000275mfsi_dealloc(mfsiobject *self)
Jack Jansen3d185931995-08-07 14:04:10 +0000276{
Jack Jansenf5c20571997-01-30 15:48:07 +0000277 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000278}
279
Jack Jansenf5c20571997-01-30 15:48:07 +0000280static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000281mfsi_getattr(mfsiobject *self, char *name)
Jack Jansen3d185931995-08-07 14:04:10 +0000282{
Jack Jansen3d185931995-08-07 14:04:10 +0000283 if ( strcmp(name, "Type") == 0 )
284 return PyMac_BuildOSType(self->finfo.fdType);
285 else if ( strcmp(name, "Creator") == 0 )
286 return PyMac_BuildOSType(self->finfo.fdCreator);
287 else if ( strcmp(name, "Flags") == 0 )
288 return Py_BuildValue("i", (int)self->finfo.fdFlags);
289 else if ( strcmp(name, "Location") == 0 )
290 return PyMac_BuildPoint(self->finfo.fdLocation);
291 else if ( strcmp(name, "Fldr") == 0 )
292 return Py_BuildValue("i", (int)self->finfo.fdFldr);
293 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000294 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000295}
296
297
298static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000299mfsi_setattr(mfsiobject *self, char *name, PyObject *v)
Jack Jansen3d185931995-08-07 14:04:10 +0000300{
301 int rv;
302 int i;
303
304 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000306 return -1;
307 }
308 if ( strcmp(name, "Type") == 0 )
309 rv = PyMac_GetOSType(v, &self->finfo.fdType);
310 else if ( strcmp(name, "Creator") == 0 )
311 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
312 else if ( strcmp(name, "Flags") == 0 ) {
313 rv = PyArg_Parse(v, "i", &i);
314 self->finfo.fdFlags = (short)i;
315 } else if ( strcmp(name, "Location") == 0 )
316 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
317 else if ( strcmp(name, "Fldr") == 0 ) {
318 rv = PyArg_Parse(v, "i", &i);
319 self->finfo.fdFldr = (short)i;
320 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000321 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000322 return -1;
323 }
324 if (rv)
325 return 0;
326 return -1;
327}
328
329
Jack Jansenf5c20571997-01-30 15:48:07 +0000330static PyTypeObject Mfsitype = {
331 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000332 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000333 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000334 sizeof(mfsiobject), /*tp_basicsize*/
335 0, /*tp_itemsize*/
336 /* methods */
337 (destructor)mfsi_dealloc, /*tp_dealloc*/
338 (printfunc)0, /*tp_print*/
339 (getattrfunc)mfsi_getattr, /*tp_getattr*/
340 (setattrfunc)mfsi_setattr, /*tp_setattr*/
341 (cmpfunc)0, /*tp_compare*/
342 (reprfunc)0, /*tp_repr*/
343 0, /*tp_as_number*/
344 0, /*tp_as_sequence*/
345 0, /*tp_as_mapping*/
346 (hashfunc)0, /*tp_hash*/
347};
348
349/* End of code for FInfo object objects */
350/* -------------------------------------------------------- */
351
352
Jack Jansen17ba43f1995-01-26 16:22:07 +0000353/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000354** Helper routines for the FSRef and FSSpec creators in macglue.c
355** They return an FSSpec/FSRef if the Python object encapsulating
356** either is passed. They return a boolean success indicator.
357** Note that they do not set an exception on failure, they're only
358** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000359*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000360static int
361_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000362{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000363 if ( is_mfssobject(self) ) {
364 *fssp = ((mfssobject *)self)->fsspec;
365 return 1;
366 }
367 return 0;
368}
369
370/* Return an FSSpec if this is an FSref */
371static int
372_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
373{
Jack Jansencbed91b2001-08-03 13:31:36 +0000374#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000375 static FSRef *fsrp;
376
377 if ( is_mfsrobject(self) ) {
378 fsrp = &((mfsrobject *)self)->fsref;
379 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
380 return 1;
381 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000382#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000383 return 0;
384}
385
386/* Return an FSRef if this is an FSRef */
387static int
388_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
389{
Jack Jansencbed91b2001-08-03 13:31:36 +0000390#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000391 if ( is_mfsrobject(self) ) {
392 *fsrp = ((mfsrobject *)self)->fsref;
393 return 1;
394 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000395#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000396 return 0;
397}
398
399/* Return an FSRef if this is an FSSpec */
400static int
401_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
402{
Jack Jansencbed91b2001-08-03 13:31:36 +0000403#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000404 if ( is_mfssobject(self) ) {
405 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
406 return 1;
407 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000408#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000409 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000410}
411
Jack Jansen0bdf9791996-09-15 22:11:25 +0000412/*
413** Two generally useful routines
414*/
415static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000416PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat,
417 unsigned long *bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000418{
419 CInfoPBRec pb;
420 OSErr error;
421
422 pb.dirInfo.ioNamePtr = fss->name;
423 pb.dirInfo.ioFDirIndex = 0;
424 pb.dirInfo.ioVRefNum = fss->vRefNum;
425 pb.dirInfo.ioDrDirID = fss->parID;
426 error = PBGetCatInfoSync(&pb);
427 if ( error ) return error;
428 *crdat = pb.hFileInfo.ioFlCrDat;
429 *mddat = pb.hFileInfo.ioFlMdDat;
430 *bkdat = pb.hFileInfo.ioFlBkDat;
431 return 0;
432}
433
434static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000435PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
436 unsigned long bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000437{
438 CInfoPBRec pb;
439 OSErr error;
440
441 pb.dirInfo.ioNamePtr = fss->name;
442 pb.dirInfo.ioFDirIndex = 0;
443 pb.dirInfo.ioVRefNum = fss->vRefNum;
444 pb.dirInfo.ioDrDirID = fss->parID;
445 error = PBGetCatInfoSync(&pb);
446 if ( error ) return error;
447 pb.dirInfo.ioNamePtr = fss->name;
448 pb.dirInfo.ioFDirIndex = 0;
449 pb.dirInfo.ioVRefNum = fss->vRefNum;
450 pb.dirInfo.ioDrDirID = fss->parID;
451 pb.hFileInfo.ioFlCrDat = crdat;
452 pb.hFileInfo.ioFlMdDat = mddat;
453 pb.hFileInfo.ioFlBkDat = bkdat;
454 error = PBSetCatInfoSync(&pb);
455 return error;
456}
457
Jack Jansenf5c20571997-01-30 15:48:07 +0000458static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000459mfss_as_pathname(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000460{
Jack Jansen697842f2001-09-10 22:00:39 +0000461 char strbuf[PATHNAMELEN];
Jack Jansen17ba43f1995-01-26 16:22:07 +0000462 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000463
Jack Jansenf5c20571997-01-30 15:48:07 +0000464 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000465 return NULL;
Jack Jansen697842f2001-09-10 22:00:39 +0000466 err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000467 if ( err ) {
468 PyErr_Mac(ErrorObject, err);
469 return NULL;
470 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000471 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000472}
473
Jack Jansenf5c20571997-01-30 15:48:07 +0000474static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000475mfss_as_tuple(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000476{
Jack Jansenf5c20571997-01-30 15:48:07 +0000477 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000478 return NULL;
479 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
480 &self->fsspec.name[1], self->fsspec.name[0]);
481}
482
Jack Jansenf5c20571997-01-30 15:48:07 +0000483static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000484mfss_NewAlias(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000485{
486 FSSpec src, *srcp;
487 OSErr err;
488 AliasHandle alias;
489
490 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000491 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000492 return NULL;
493 if ( src.name[0] )
494 srcp = &src;
495 else
496 srcp = NULL;
497 err = NewAlias(srcp, &self->fsspec, &alias);
498 if ( err ) {
499 PyErr_Mac(ErrorObject, err);
500 return NULL;
501 }
502
Jack Jansenf5c20571997-01-30 15:48:07 +0000503 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000504}
505
Jack Jansenf5c20571997-01-30 15:48:07 +0000506static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000507mfss_NewAliasMinimal(mfssobject *self, 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 *
Jack Jansendeefbe52001-08-08 13:46:49 +0000523mfss_FSpMakeFSRef(mfssobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000524{
Jack Jansencbed91b2001-08-03 13:31:36 +0000525#if TARGET_API_MAC_OS8
526 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
527 return 0;
528#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000529 OSErr err;
530 FSRef fsref;
531
532 if (!PyArg_ParseTuple(args, ""))
533 return NULL;
534 err = FSpMakeFSRef(&self->fsspec, &fsref);
535 if ( err ) {
536 PyErr_Mac(ErrorObject, err);
537 return NULL;
538 }
539 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000540#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000541}
542
Jack Jansen3d185931995-08-07 14:04:10 +0000543/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000544static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000545mfss_GetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000546{
547 OSErr err;
548 FInfo info;
549
Jack Jansenf5c20571997-01-30 15:48:07 +0000550 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000551 return NULL;
552 err = FSpGetFInfo(&self->fsspec, &info);
553 if ( err ) {
554 PyErr_Mac(ErrorObject, err);
555 return NULL;
556 }
557 return Py_BuildValue("(O&O&)",
558 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
559}
560
Jack Jansenf5c20571997-01-30 15:48:07 +0000561static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000562mfss_SetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000563{
564 OSErr err;
565 OSType creator, type;
566 FInfo info;
567
Jack Jansenf5c20571997-01-30 15:48:07 +0000568 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000569 return NULL;
570 err = FSpGetFInfo(&self->fsspec, &info);
571 if ( err ) {
572 PyErr_Mac(ErrorObject, err);
573 return NULL;
574 }
575 info.fdType = type;
576 info.fdCreator = creator;
577 err = FSpSetFInfo(&self->fsspec, &info);
578 if ( err ) {
579 PyErr_Mac(ErrorObject, err);
580 return NULL;
581 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000582 Py_INCREF(Py_None);
583 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000584}
585
Jack Jansenf5c20571997-01-30 15:48:07 +0000586static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000587mfss_GetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000588{
589 OSErr err;
590 mfsiobject *fip;
591
592
Jack Jansenf5c20571997-01-30 15:48:07 +0000593 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000594 return NULL;
595 if ( (fip=newmfsiobject()) == NULL )
596 return NULL;
597 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
598 if ( err ) {
599 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000600 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000601 return NULL;
602 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000603 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000604}
605
Jack Jansenf5c20571997-01-30 15:48:07 +0000606static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000607mfss_SetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000608{
609 OSErr err;
610 mfsiobject *fip;
611
Jack Jansenf5c20571997-01-30 15:48:07 +0000612 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000613 return NULL;
614 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
615 if ( err ) {
616 PyErr_Mac(ErrorObject, err);
617 return NULL;
618 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000619 Py_INCREF(Py_None);
620 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000621}
622
Jack Jansenf5c20571997-01-30 15:48:07 +0000623static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000624mfss_GetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000625{
626 OSErr err;
627 unsigned long crdat, mddat, bkdat;
628
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000630 return NULL;
631 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
632 if ( err ) {
633 PyErr_Mac(ErrorObject, err);
634 return NULL;
635 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000636 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000637}
638
Jack Jansenf5c20571997-01-30 15:48:07 +0000639static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000640mfss_SetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000641{
642 OSErr err;
643 double crdat, mddat, bkdat;
644
Jack Jansenf5c20571997-01-30 15:48:07 +0000645 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000646 return NULL;
647 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
648 (unsigned long)mddat, (unsigned long)bkdat);
649 if ( err ) {
650 PyErr_Mac(ErrorObject, err);
651 return NULL;
652 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000653 Py_INCREF(Py_None);
654 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000655}
656
Jack Jansenf5c20571997-01-30 15:48:07 +0000657static struct PyMethodDef mfss_methods[] = {
658 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
659 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000660 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
661 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000662 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
663 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
664 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
665 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
666 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
667 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
668 {"GetDates", (PyCFunction)mfss_GetDates, 1},
669 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000670
671 {NULL, NULL} /* sentinel */
672};
673
674/* ---------- */
675
Jack Jansenf5c20571997-01-30 15:48:07 +0000676static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000677mfss_getattr(mfssobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000678{
Jack Jansenc889b761995-02-13 12:00:46 +0000679 if ( strcmp(name, "data") == 0)
680 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000681 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000682}
683
684mfssobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000685newmfssobject(FSSpec *fss)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000686{
687 mfssobject *self;
688
Jack Jansenf5c20571997-01-30 15:48:07 +0000689 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000690 if (self == NULL)
691 return NULL;
692 self->fsspec = *fss;
693 return self;
694}
695
696static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000697mfss_dealloc(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000698{
Jack Jansenf5c20571997-01-30 15:48:07 +0000699 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000700}
701
Jack Jansenf5c20571997-01-30 15:48:07 +0000702static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000703mfss_repr(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000704{
705 char buf[512];
706
Jack Jansendeefbe52001-08-08 13:46:49 +0000707 sprintf(buf, "FSSpec((%d, %ld, '%.*s'))",
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000708 self->fsspec.vRefNum,
709 self->fsspec.parID,
710 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000711 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000712}
713
714static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000715mfss_compare(mfssobject *v, mfssobject *w)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000716{
717 int minlen;
718 int res;
719
720 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
721 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
722 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
723 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
724 minlen = v->fsspec.name[0];
725 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
726 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
727 if ( res ) return res;
728 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
729 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
730 return res;
731}
732
Jack Jansenf5c20571997-01-30 15:48:07 +0000733statichere PyTypeObject Mfsstype = {
734 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000735 0, /*ob_size*/
736 "FSSpec", /*tp_name*/
737 sizeof(mfssobject), /*tp_basicsize*/
738 0, /*tp_itemsize*/
739 /* methods */
740 (destructor)mfss_dealloc, /*tp_dealloc*/
741 (printfunc)0, /*tp_print*/
742 (getattrfunc)mfss_getattr, /*tp_getattr*/
743 (setattrfunc)0, /*tp_setattr*/
744 (cmpfunc)mfss_compare, /*tp_compare*/
745 (reprfunc)mfss_repr, /*tp_repr*/
746 0, /*tp_as_number*/
747 0, /*tp_as_sequence*/
748 0, /*tp_as_mapping*/
749 (hashfunc)0, /*tp_hash*/
750};
751
752/* End of code for FSSpec objects */
753/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000754#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000755static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000756mfsr_as_fsspec(mfsrobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000757{
758 OSErr err;
759 FSSpec fss;
760
761 if (!PyArg_ParseTuple(args, ""))
762 return NULL;
763 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
764 if ( err ) {
765 PyErr_Mac(ErrorObject, err);
766 return NULL;
767 }
768 Py_INCREF(Py_None);
769 return (PyObject *)newmfssobject(&fss);
770}
771
772static struct PyMethodDef mfsr_methods[] = {
773 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
774#if 0
775 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
776 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
777 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
778 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
779 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
780 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
781 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
782 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
783 {"GetDates", (PyCFunction)mfss_GetDates, 1},
784 {"SetDates", (PyCFunction)mfss_SetDates, 1},
785#endif
786
787 {NULL, NULL} /* sentinel */
788};
789
790/* ---------- */
791
792static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000793mfsr_getattr(mfsrobject *self, char *name)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000794{
795 if ( strcmp(name, "data") == 0)
796 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
797 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
798}
799
800mfsrobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000801newmfsrobject(FSRef *fsr)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000802{
803 mfsrobject *self;
804
805 self = PyObject_NEW(mfsrobject, &Mfsrtype);
806 if (self == NULL)
807 return NULL;
808 self->fsref = *fsr;
809 return self;
810}
811
812static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000813mfsr_compare(mfsrobject *v, mfsrobject *w)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000814{
815 OSErr err;
816
817 if ( v == w ) return 0;
818 err = FSCompareFSRefs(&v->fsref, &w->fsref);
819 if ( err == 0 )
820 return 0;
821 if (v < w )
822 return -1;
823 return 1;
824}
825
826static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000827mfsr_dealloc(mfsrobject *self)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000828{
829 PyMem_DEL(self);
830}
831
832statichere PyTypeObject Mfsrtype = {
833 PyObject_HEAD_INIT(&PyType_Type)
834 0, /*ob_size*/
835 "FSRef", /*tp_name*/
836 sizeof(mfsrobject), /*tp_basicsize*/
837 0, /*tp_itemsize*/
838 /* methods */
839 (destructor)mfsr_dealloc, /*tp_dealloc*/
840 (printfunc)0, /*tp_print*/
841 (getattrfunc)mfsr_getattr, /*tp_getattr*/
842 (setattrfunc)0, /*tp_setattr*/
843 (cmpfunc)mfsr_compare, /*tp_compare*/
844 (reprfunc)0, /*tp_repr*/
845 0, /*tp_as_number*/
846 0, /*tp_as_sequence*/
847 0, /*tp_as_mapping*/
848 (hashfunc)0, /*tp_hash*/
849};
850
851/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000852#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000853/* -------------------------------------------------------- */
854
855static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000856mfs_ResolveAliasFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000857{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000858 FSSpec fss;
859 Boolean chain = 1, isfolder, wasaliased;
860 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000861
Jack Jansenf5c20571997-01-30 15:48:07 +0000862 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000863 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000864 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
865 if ( err ) {
866 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000867 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000868 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000869 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000870}
871
Jack Jansen74a1e632000-07-14 22:37:27 +0000872#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000873static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000874mfs_StandardGetFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000875{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000876 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000877 short numtypes;
878 StandardFileReply reply;
879
880 list[0] = list[1] = list[2] = list[3] = 0;
881 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000882 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000883 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
884 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000885 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000886 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000887 numtypes++;
888 }
Jack Jansenc2632861995-06-03 21:15:50 +0000889 if ( numtypes == 0 )
890 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000891 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000892 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000893}
894
Jack Jansenf5c20571997-01-30 15:48:07 +0000895static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000896mfs_PromptGetFile(PyObject *self, PyObject *args)
Jack Jansend5d5f461995-08-14 12:22:56 +0000897{
898 SFTypeList list;
899 short numtypes;
900 StandardFileReply reply;
901 char *prompt = NULL;
902
903 list[0] = list[1] = list[2] = list[3] = 0;
904 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000905 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000906 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
907 PyMac_GetOSType, &list[3]) )
908 return NULL;
909 while ( numtypes < 4 && list[numtypes] ) {
910 numtypes++;
911 }
912 if ( numtypes == 0 )
913 numtypes = -1;
914 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000915 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000916}
917
Jack Jansenf5c20571997-01-30 15:48:07 +0000918static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000919mfs_StandardPutFile(PyObject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000920{
921 Str255 prompt, dft;
922 StandardFileReply reply;
923
924 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000925 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000926 return NULL;
927 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000928 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000929}
930
Jack Jansend99d2831996-07-22 15:26:01 +0000931/*
932** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000933static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000934mfs_SetFolder(PyObject *self, PyObject *args)
Jack Jansend99d2831996-07-22 15:26:01 +0000935{
936 FSSpec spec;
937 FSSpec ospec;
938 short orefnum;
939 long oparid;
940
941 /* Get old values */
942 orefnum = -LMGetSFSaveDisk();
943 oparid = LMGetCurDirStore();
944 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
945
946 /* Go to working directory by default */
947 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000948 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000949 return NULL;
950 /* Set standard-file working directory */
951 LMSetSFSaveDisk(-spec.vRefNum);
952 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000953 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000954}
Jack Jansene79dc762000-06-02 21:35:07 +0000955#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000956
Jack Jansenf5c20571997-01-30 15:48:07 +0000957static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000958mfs_FSSpec(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000959{
960 FSSpec fss;
961
Jack Jansenf5c20571997-01-30 15:48:07 +0000962 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000963 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000964 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000965}
966
Jack Jansenf5c20571997-01-30 15:48:07 +0000967static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000968mfs_FSRef(PyObject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000969{
Jack Jansencbed91b2001-08-03 13:31:36 +0000970#if TARGET_API_MAC_OS8
971 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
972 return 0;
973#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000974 FSRef fsr;
975
976 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
977 return NULL;
978 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +0000979#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000980}
981
982static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000983mfs_RawFSSpec(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000984{
985 FSSpec *fssp;
986 int size;
987
Jack Janseneeccca91997-05-07 15:46:31 +0000988 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000989 return NULL;
990 if ( size != sizeof(FSSpec) ) {
991 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
992 return NULL;
993 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000994 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000995}
996
Jack Jansenf5c20571997-01-30 15:48:07 +0000997static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000998mfs_RawAlias(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000999{
1000 char *dataptr;
1001 Handle h;
1002 int size;
1003
Jack Janseneeccca91997-05-07 15:46:31 +00001004 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001005 return NULL;
1006 h = NewHandle(size);
1007 if ( h == NULL ) {
1008 PyErr_NoMemory();
1009 return NULL;
1010 }
1011 HLock(h);
1012 memcpy((char *)*h, dataptr, size);
1013 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001014 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001015}
1016
Jack Jansen74a1e632000-07-14 22:37:27 +00001017#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001018static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001019mfs_GetDirectory(PyObject *self, PyObject *args)
Jack Jansen81f51c71995-02-20 15:45:25 +00001020{
1021 FSSpec fsdir;
1022 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001023 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001024
Jack Jansenf5c20571997-01-30 15:48:07 +00001025 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001026 return NULL;
1027
Jack Jansend5d5f461995-08-14 12:22:56 +00001028 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001029 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001030}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001031#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001032
Jack Jansenf5c20571997-01-30 15:48:07 +00001033static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001034mfs_FindFolder(PyObject *self, PyObject *args)
Jack Jansen2c673621995-06-18 20:05:14 +00001035{
1036 OSErr err;
1037 short where;
1038 OSType which;
1039 int create;
1040 short refnum;
1041 long dirid;
1042
Jack Jansenabd703d2001-03-15 14:38:10 +00001043 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001044 return NULL;
1045 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1046 if ( err ) {
1047 PyErr_Mac(ErrorObject, err);
1048 return NULL;
1049 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001050 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001051}
1052
Jack Jansenf5c20571997-01-30 15:48:07 +00001053static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001054mfs_FindApplication(PyObject *self, PyObject *args)
Jack Jansen924ca851996-09-20 15:25:16 +00001055{
1056 OSErr err;
1057 OSType which;
1058 FSSpec fss;
1059
Jack Jansenf5c20571997-01-30 15:48:07 +00001060 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001061 return NULL;
1062 err = FindApplicationFromCreator(which, &fss);
1063 if ( err ) {
1064 PyErr_Mac(ErrorObject, err);
1065 return NULL;
1066 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001067 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001068}
1069
Jack Jansenf5c20571997-01-30 15:48:07 +00001070static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001071mfs_FInfo(PyObject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +00001072{
Jack Jansenf5c20571997-01-30 15:48:07 +00001073 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001074}
1075
Jack Jansend9936481997-06-16 14:31:38 +00001076static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001077mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args)
Jack Jansend9936481997-06-16 14:31:38 +00001078{
1079 OSErr err;
1080 char *fullpath;
1081 int fullpathlen;
1082 AliasHandle alias;
1083 Str32 zonename;
1084 Str31 servername;
1085
1086 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1087 return NULL;
1088 zonename[0] = 0;
1089 servername[0] = 0;
1090 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1091 servername, &alias);
1092 if ( err ) {
1093 PyErr_Mac(ErrorObject, err);
1094 return NULL;
1095 }
1096 return (PyObject *)newmfsaobject(alias);
1097}
1098
1099
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001100/* List of methods defined in the module */
1101
Jack Jansenf5c20571997-01-30 15:48:07 +00001102static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001103 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001104#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001105 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001106 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001107 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001108 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001109 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001110#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001111 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001112 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001113 {"RawFSSpec", mfs_RawFSSpec, 1},
1114 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001115 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001116 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001117 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001118 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001119
1120 {NULL, NULL} /* sentinel */
1121};
1122
Jack Jansen4e566ab2001-07-08 22:07:23 +00001123/*
1124** Convert a Python object to an FSSpec.
1125** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1126** (vrefnum, dirid, path).
1127*/
1128int
1129PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1130{
Jack Jansencbed91b2001-08-03 13:31:36 +00001131#if TARGET_API_MAC_OS8
1132 return 0;
1133#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001134 /* If it's an FSRef we're also okay. */
1135 if (_mfs_GetFSRefFromFSRef(v, fsr))
1136 return 1;
1137 /* first check whether it already is an FSSpec */
1138 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1139 return 1;
1140 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001141#if TARGET_API_MAC_OSX
1142 OSStatus err;
1143 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1144 PyErr_Mac(ErrorObject, err);
1145 return 0;
1146 }
1147 return 1;
1148#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001149 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1150 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001151#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001152 }
1153 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1154 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001155#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001156}
1157
1158/* Convert FSSpec to PyObject */
1159PyObject *PyMac_BuildFSRef(FSRef *v)
1160{
Jack Jansencbed91b2001-08-03 13:31:36 +00001161#if TARGET_API_MAC_OS8
1162 return NULL;
1163#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001164 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001165#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001166}
1167
1168/*
1169** Convert a Python object to an FSRef.
1170** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1171*/
1172int
1173PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1174{
1175 Str255 path;
1176 short refnum;
1177 long parid;
1178 OSErr err;
1179
1180 /* first check whether it already is an FSSpec */
1181 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1182 return 1;
1183 /* If it's an FSRef we're also okay. */
1184 if (_mfs_GetFSSpecFromFSRef(v, fs))
1185 return 1;
1186 if ( PyString_Check(v) ) {
1187 /* It's a pathname */
1188 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1189 return 0;
1190 refnum = 0; /* XXXX Should get CurWD here?? */
1191 parid = 0;
1192 } else {
1193 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1194 &refnum, &parid, PyMac_GetStr255, &path)) {
1195 return 0;
1196 }
1197 }
1198 err = FSMakeFSSpec(refnum, parid, path, fs);
1199 if ( err && err != fnfErr ) {
1200 PyMac_Error(err);
1201 return 0;
1202 }
1203 return 1;
1204}
1205
1206/* Convert FSSpec to PyObject */
1207PyObject *PyMac_BuildFSSpec(FSSpec *v)
1208{
1209 return (PyObject *)newmfssobject(v);
1210}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001211
Just van Rossumab57c7d2001-10-31 22:55:08 +00001212
1213/*
1214** Import the macfsn module, which will override the Standard File
1215** calls in the macfs builtin module by Navigation Services versions,
1216** if available on this machine.
1217*/
1218static void
1219PyMac_InstallNavServicesForSF(void)
1220{
1221 if ( !PyMac_options.nonavservice ) {
1222 PyObject *m = PyImport_ImportModule("macfsn");
1223
1224 if ( m == NULL ) {
1225 PySys_WriteStderr("'import macfsn' failed; ");
1226 if (Py_VerboseFlag) {
1227 PySys_WriteStderr("traceback:\n");
1228 PyErr_Print();
1229 }
1230 else {
1231 PySys_WriteStderr("use -v for traceback\n");
1232 }
1233 PyErr_Clear();
1234 }
1235 }
1236}
1237
1238
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001239/* Initialization function for the module (*must* be called initmacfs) */
1240
1241void
Jack Jansendeefbe52001-08-08 13:46:49 +00001242initmacfs(void)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001243{
Jack Jansenf5c20571997-01-30 15:48:07 +00001244 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001245
Jack Jansen45900492001-08-06 15:32:30 +00001246 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
Jack Jansenfabd00f2001-09-01 23:39:58 +00001247 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef, PyMac_GetFSRef);
1248 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec *, PyMac_BuildFSSpec);
1249 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef *, PyMac_BuildFSRef);
Jack Jansena5bca572001-08-03 15:39:27 +00001250
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001251 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001252 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001253
1254 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001255 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001256 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001257 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001258
Jack Jansena755e681997-09-20 17:40:22 +00001259 Mfsatype.ob_type = &PyType_Type;
1260 Py_INCREF(&Mfsatype);
1261 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1262 Mfsstype.ob_type = &PyType_Type;
1263 Py_INCREF(&Mfsstype);
1264 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1265 Mfsitype.ob_type = &PyType_Type;
1266 Py_INCREF(&Mfsitype);
1267 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Just van Rossumab57c7d2001-10-31 22:55:08 +00001268
1269 PyMac_InstallNavServicesForSF();
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001270}