blob: 15d9f30857d50838bdbb815c32b681d64c197c27 [file] [log] [blame]
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossumefd97671995-01-26 22:56:16 +00003The Netherlands.
Jack Jansen84fa5ec1995-01-18 14:04:40 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Jack Jansenf5c20571997-01-30 15:48:07 +000025#include "Python.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000026#include "macglue.h"
Jack Jansena5bca572001-08-03 15:39:27 +000027#include "pymactoolbox.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000028
Jack Jansen6143d532001-05-19 12:34:59 +000029#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumbecdbec1995-02-14 01:27:24 +000030#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000031#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000032#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000033#include <StandardFile.h>
34#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000035#include <LowMem.h>
Jack Jansen6143d532001-05-19 12:34:59 +000036#else
37#include <Carbon/Carbon.h>
38#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +000039
Jack Jansen924ca851996-09-20 15:25:16 +000040#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000041
Jack Jansena5bca572001-08-03 15:39:27 +000042#ifdef USE_TOOLBOX_OBJECT_GLUE
43extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
Jack Jansen61142972001-09-02 00:09:35 +000044extern PyObject *_PyMac_BuildFSSpec(FSSpec *);
45extern int _PyMac_GetFSRef(PyObject *, FSRef *);
Jack Jansenfabd00f2001-09-01 23:39:58 +000046extern PyObject *_PyMac_BuildFSRef(FSRef *);
Jack Jansena5bca572001-08-03 15:39:27 +000047#define PyMac_GetFSSpec _PyMac_GetFSSpec
Jack Jansenfabd00f2001-09-01 23:39:58 +000048#define PyMac_BuildFSSpec _PyMac_BuildFSSpec
49#define PyMac_GetFSRef _PyMac_GetFSRef
50#define PyMac_BuildFSRef _PyMac_BuildFSRef
Jack Jansena5bca572001-08-03 15:39:27 +000051#endif
Jack Jansenf5c20571997-01-30 15:48:07 +000052static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000053
Jack Jansen697842f2001-09-10 22:00:39 +000054#ifdef TARGET_API_MAC_OSX
55#define PATHNAMELEN 1024
56#else
57#define PATHNAMELEN 256
58#endif
59
Jack Jansen84fa5ec1995-01-18 14:04:40 +000060/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000061/* Declarations for objects of type Alias */
62
63typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000064 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000065 AliasHandle alias;
66} mfsaobject;
67
Jack Jansenf5c20571997-01-30 15:48:07 +000068staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000069
70#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
71
72/* ---------------------------------------------------------------- */
73/* Declarations for objects of type FSSpec */
74
75typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000076 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000077 FSSpec fsspec;
78} mfssobject;
79
Jack Jansenf5c20571997-01-30 15:48:07 +000080staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000081
82#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
83
Jack Jansen4e566ab2001-07-08 22:07:23 +000084/* ---------------------------------------------------------------- */
85/* Declarations for objects of type FSRef */
86
87typedef struct {
88 PyObject_HEAD
89 FSRef fsref;
90} mfsrobject;
91
92staticforward PyTypeObject Mfsrtype;
93
94#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
95
Guido van Rossumefd97671995-01-26 22:56:16 +000096
Jack Jansen3d185931995-08-07 14:04:10 +000097/* ---------------------------------------------------------------- */
98/* Declarations for objects of type FInfo */
99
100typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000101 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +0000102 FInfo finfo;
103} mfsiobject;
104
Jack Jansenf5c20571997-01-30 15:48:07 +0000105staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +0000106
107#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
108
109
Jack Jansen4e566ab2001-07-08 22:07:23 +0000110staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
111staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000112
Jack Jansen17ba43f1995-01-26 16:22:07 +0000113/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000114
Jack Jansenf5c20571997-01-30 15:48:07 +0000115static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000116mfsa_Resolve(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000117{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000118 FSSpec from, *fromp, result;
119 Boolean changed;
120 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000121
Jack Jansen17ba43f1995-01-26 16:22:07 +0000122 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000123 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000124 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000125 if (from.name[0] )
126 fromp = &from;
127 else
128 fromp = NULL;
129 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000130 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000131 PyErr_Mac(ErrorObject, err);
132 return NULL;
133 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000134 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000135}
136
Jack Jansenf5c20571997-01-30 15:48:07 +0000137static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000138mfsa_GetInfo(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000139{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000140 Str63 value;
141 int i;
142 OSErr err;
143
Jack Jansenf5c20571997-01-30 15:48:07 +0000144 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000145 return NULL;
146 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
147 if ( err ) {
148 PyErr_Mac(ErrorObject, err);
149 return 0;
150 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000151 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000152}
153
Jack Jansenf5c20571997-01-30 15:48:07 +0000154static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000155mfsa_Update(mfsaobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000156{
157 FSSpec target, fromfile, *fromfilep;
158 OSErr err;
159 Boolean changed;
160
161 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000162 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000163 PyMac_GetFSSpec, &fromfile))
164 return NULL;
165 if ( fromfile.name[0] )
166 fromfilep = &fromfile;
167 else
168 fromfilep = NULL;
169 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
170 if ( err ) {
171 PyErr_Mac(ErrorObject, err);
172 return 0;
173 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000175}
176
Jack Jansenf5c20571997-01-30 15:48:07 +0000177static struct PyMethodDef mfsa_methods[] = {
178 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
179 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
180 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000181
182 {NULL, NULL} /* sentinel */
183};
184
185/* ---------- */
186
Jack Jansenf5c20571997-01-30 15:48:07 +0000187static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000188mfsa_getattr(mfsaobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000189{
Jack Jansenc889b761995-02-13 12:00:46 +0000190 if ( strcmp(name, "data") == 0 ) {
191 int size;
192 PyObject *rv;
193
194 size = GetHandleSize((Handle)self->alias);
195 HLock((Handle)self->alias);
196 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
197 HUnlock((Handle)self->alias);
198 return rv;
199 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000201}
202
Jack Jansen9ae898b2000-07-11 21:16:03 +0000203static mfsaobject *
204newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000205{
206 mfsaobject *self;
207
Jack Jansenf5c20571997-01-30 15:48:07 +0000208 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000209 if (self == NULL)
210 return NULL;
211 self->alias = alias;
212 return self;
213}
214
215
216static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000217mfsa_dealloc(mfsaobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000218{
219#if 0
220 if ( self->alias ) {
221 should we do something here?
222 }
223#endif
224
Jack Jansenf5c20571997-01-30 15:48:07 +0000225 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000226}
227
Jack Jansenf5c20571997-01-30 15:48:07 +0000228statichere PyTypeObject Mfsatype = {
229 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000230 0, /*ob_size*/
231 "Alias", /*tp_name*/
232 sizeof(mfsaobject), /*tp_basicsize*/
233 0, /*tp_itemsize*/
234 /* methods */
235 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000236 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000237 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000238 (setattrfunc)0, /*tp_setattr*/
239 (cmpfunc)0, /*tp_compare*/
240 (reprfunc)0, /*tp_repr*/
241 0, /*tp_as_number*/
242 0, /*tp_as_sequence*/
243 0, /*tp_as_mapping*/
244 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000245};
246
247/* End of code for Alias objects */
248/* -------------------------------------------------------- */
249
Jack Jansen3d185931995-08-07 14:04:10 +0000250/* ---------------------------------------------------------------- */
251
Jack Jansenf5c20571997-01-30 15:48:07 +0000252static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000253
254 {NULL, NULL} /* sentinel */
255};
256
257/* ---------- */
258
259static mfsiobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000260newmfsiobject(void)
Jack Jansen3d185931995-08-07 14:04:10 +0000261{
262 mfsiobject *self;
263
Jack Jansenf5c20571997-01-30 15:48:07 +0000264 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000265 if (self == NULL)
266 return NULL;
267 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
268 return self;
269}
270
271static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000272mfsi_dealloc(mfsiobject *self)
Jack Jansen3d185931995-08-07 14:04:10 +0000273{
Jack Jansenf5c20571997-01-30 15:48:07 +0000274 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000275}
276
Jack Jansenf5c20571997-01-30 15:48:07 +0000277static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000278mfsi_getattr(mfsiobject *self, char *name)
Jack Jansen3d185931995-08-07 14:04:10 +0000279{
Jack Jansen3d185931995-08-07 14:04:10 +0000280 if ( strcmp(name, "Type") == 0 )
281 return PyMac_BuildOSType(self->finfo.fdType);
282 else if ( strcmp(name, "Creator") == 0 )
283 return PyMac_BuildOSType(self->finfo.fdCreator);
284 else if ( strcmp(name, "Flags") == 0 )
285 return Py_BuildValue("i", (int)self->finfo.fdFlags);
286 else if ( strcmp(name, "Location") == 0 )
287 return PyMac_BuildPoint(self->finfo.fdLocation);
288 else if ( strcmp(name, "Fldr") == 0 )
289 return Py_BuildValue("i", (int)self->finfo.fdFldr);
290 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000292}
293
294
295static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000296mfsi_setattr(mfsiobject *self, char *name, PyObject *v)
Jack Jansen3d185931995-08-07 14:04:10 +0000297{
298 int rv;
299 int i;
300
301 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000302 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000303 return -1;
304 }
305 if ( strcmp(name, "Type") == 0 )
306 rv = PyMac_GetOSType(v, &self->finfo.fdType);
307 else if ( strcmp(name, "Creator") == 0 )
308 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
309 else if ( strcmp(name, "Flags") == 0 ) {
310 rv = PyArg_Parse(v, "i", &i);
311 self->finfo.fdFlags = (short)i;
312 } else if ( strcmp(name, "Location") == 0 )
313 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
314 else if ( strcmp(name, "Fldr") == 0 ) {
315 rv = PyArg_Parse(v, "i", &i);
316 self->finfo.fdFldr = (short)i;
317 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000318 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000319 return -1;
320 }
321 if (rv)
322 return 0;
323 return -1;
324}
325
326
Jack Jansenf5c20571997-01-30 15:48:07 +0000327static PyTypeObject Mfsitype = {
328 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000329 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000330 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000331 sizeof(mfsiobject), /*tp_basicsize*/
332 0, /*tp_itemsize*/
333 /* methods */
334 (destructor)mfsi_dealloc, /*tp_dealloc*/
335 (printfunc)0, /*tp_print*/
336 (getattrfunc)mfsi_getattr, /*tp_getattr*/
337 (setattrfunc)mfsi_setattr, /*tp_setattr*/
338 (cmpfunc)0, /*tp_compare*/
339 (reprfunc)0, /*tp_repr*/
340 0, /*tp_as_number*/
341 0, /*tp_as_sequence*/
342 0, /*tp_as_mapping*/
343 (hashfunc)0, /*tp_hash*/
344};
345
346/* End of code for FInfo object objects */
347/* -------------------------------------------------------- */
348
349
Jack Jansen17ba43f1995-01-26 16:22:07 +0000350/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000351** Helper routines for the FSRef and FSSpec creators in macglue.c
352** They return an FSSpec/FSRef if the Python object encapsulating
353** either is passed. They return a boolean success indicator.
354** Note that they do not set an exception on failure, they're only
355** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000356*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000357static int
358_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000359{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000360 if ( is_mfssobject(self) ) {
361 *fssp = ((mfssobject *)self)->fsspec;
362 return 1;
363 }
364 return 0;
365}
366
367/* Return an FSSpec if this is an FSref */
368static int
369_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
370{
Jack Jansencbed91b2001-08-03 13:31:36 +0000371#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000372 static FSRef *fsrp;
373
374 if ( is_mfsrobject(self) ) {
375 fsrp = &((mfsrobject *)self)->fsref;
376 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
377 return 1;
378 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000379#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000380 return 0;
381}
382
383/* Return an FSRef if this is an FSRef */
384static int
385_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
386{
Jack Jansencbed91b2001-08-03 13:31:36 +0000387#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000388 if ( is_mfsrobject(self) ) {
389 *fsrp = ((mfsrobject *)self)->fsref;
390 return 1;
391 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000392#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000393 return 0;
394}
395
396/* Return an FSRef if this is an FSSpec */
397static int
398_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
399{
Jack Jansencbed91b2001-08-03 13:31:36 +0000400#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000401 if ( is_mfssobject(self) ) {
402 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
403 return 1;
404 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000405#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000406 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000407}
408
Jack Jansen0bdf9791996-09-15 22:11:25 +0000409/*
410** Two generally useful routines
411*/
412static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000413PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat,
414 unsigned long *bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000415{
416 CInfoPBRec pb;
417 OSErr error;
418
419 pb.dirInfo.ioNamePtr = fss->name;
420 pb.dirInfo.ioFDirIndex = 0;
421 pb.dirInfo.ioVRefNum = fss->vRefNum;
422 pb.dirInfo.ioDrDirID = fss->parID;
423 error = PBGetCatInfoSync(&pb);
424 if ( error ) return error;
425 *crdat = pb.hFileInfo.ioFlCrDat;
426 *mddat = pb.hFileInfo.ioFlMdDat;
427 *bkdat = pb.hFileInfo.ioFlBkDat;
428 return 0;
429}
430
431static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000432PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
433 unsigned long bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000434{
435 CInfoPBRec pb;
436 OSErr error;
437
438 pb.dirInfo.ioNamePtr = fss->name;
439 pb.dirInfo.ioFDirIndex = 0;
440 pb.dirInfo.ioVRefNum = fss->vRefNum;
441 pb.dirInfo.ioDrDirID = fss->parID;
442 error = PBGetCatInfoSync(&pb);
443 if ( error ) return error;
444 pb.dirInfo.ioNamePtr = fss->name;
445 pb.dirInfo.ioFDirIndex = 0;
446 pb.dirInfo.ioVRefNum = fss->vRefNum;
447 pb.dirInfo.ioDrDirID = fss->parID;
448 pb.hFileInfo.ioFlCrDat = crdat;
449 pb.hFileInfo.ioFlMdDat = mddat;
450 pb.hFileInfo.ioFlBkDat = bkdat;
451 error = PBSetCatInfoSync(&pb);
452 return error;
453}
454
Jack Jansenf5c20571997-01-30 15:48:07 +0000455static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000456mfss_as_pathname(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000457{
Jack Jansen697842f2001-09-10 22:00:39 +0000458 char strbuf[PATHNAMELEN];
Jack Jansen17ba43f1995-01-26 16:22:07 +0000459 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000460
Jack Jansenf5c20571997-01-30 15:48:07 +0000461 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000462 return NULL;
Jack Jansen697842f2001-09-10 22:00:39 +0000463 err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000464 if ( err ) {
465 PyErr_Mac(ErrorObject, err);
466 return NULL;
467 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000468 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000469}
470
Jack Jansenf5c20571997-01-30 15:48:07 +0000471static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000472mfss_as_tuple(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000473{
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000475 return NULL;
476 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
477 &self->fsspec.name[1], self->fsspec.name[0]);
478}
479
Jack Jansenf5c20571997-01-30 15:48:07 +0000480static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000481mfss_NewAlias(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000482{
483 FSSpec src, *srcp;
484 OSErr err;
485 AliasHandle alias;
486
487 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000488 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000489 return NULL;
490 if ( src.name[0] )
491 srcp = &src;
492 else
493 srcp = NULL;
494 err = NewAlias(srcp, &self->fsspec, &alias);
495 if ( err ) {
496 PyErr_Mac(ErrorObject, err);
497 return NULL;
498 }
499
Jack Jansenf5c20571997-01-30 15:48:07 +0000500 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000501}
502
Jack Jansenf5c20571997-01-30 15:48:07 +0000503static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000504mfss_NewAliasMinimal(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000505{
506 OSErr err;
507 AliasHandle alias;
508
Jack Jansenf5c20571997-01-30 15:48:07 +0000509 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000510 return NULL;
511 err = NewAliasMinimal(&self->fsspec, &alias);
512 if ( err ) {
513 PyErr_Mac(ErrorObject, err);
514 return NULL;
515 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000516 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000517}
518
Jack Jansen4e566ab2001-07-08 22:07:23 +0000519static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000520mfss_FSpMakeFSRef(mfssobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000521{
Jack Jansencbed91b2001-08-03 13:31:36 +0000522#if TARGET_API_MAC_OS8
523 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
524 return 0;
525#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000526 OSErr err;
527 FSRef fsref;
528
529 if (!PyArg_ParseTuple(args, ""))
530 return NULL;
531 err = FSpMakeFSRef(&self->fsspec, &fsref);
532 if ( err ) {
533 PyErr_Mac(ErrorObject, err);
534 return NULL;
535 }
536 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000537#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000538}
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 Jansendeefbe52001-08-08 13:46:49 +0000542mfss_GetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000543{
544 OSErr err;
545 FInfo info;
546
Jack Jansenf5c20571997-01-30 15:48:07 +0000547 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000548 return NULL;
549 err = FSpGetFInfo(&self->fsspec, &info);
550 if ( err ) {
551 PyErr_Mac(ErrorObject, err);
552 return NULL;
553 }
554 return Py_BuildValue("(O&O&)",
555 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
556}
557
Jack Jansenf5c20571997-01-30 15:48:07 +0000558static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000559mfss_SetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000560{
561 OSErr err;
562 OSType creator, type;
563 FInfo info;
564
Jack Jansenf5c20571997-01-30 15:48:07 +0000565 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000566 return NULL;
567 err = FSpGetFInfo(&self->fsspec, &info);
568 if ( err ) {
569 PyErr_Mac(ErrorObject, err);
570 return NULL;
571 }
572 info.fdType = type;
573 info.fdCreator = creator;
574 err = FSpSetFInfo(&self->fsspec, &info);
575 if ( err ) {
576 PyErr_Mac(ErrorObject, err);
577 return NULL;
578 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 Py_INCREF(Py_None);
580 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000581}
582
Jack Jansenf5c20571997-01-30 15:48:07 +0000583static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000584mfss_GetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000585{
586 OSErr err;
587 mfsiobject *fip;
588
589
Jack Jansenf5c20571997-01-30 15:48:07 +0000590 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000591 return NULL;
592 if ( (fip=newmfsiobject()) == NULL )
593 return NULL;
594 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
595 if ( err ) {
596 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000597 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000598 return NULL;
599 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000600 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000601}
602
Jack Jansenf5c20571997-01-30 15:48:07 +0000603static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000604mfss_SetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000605{
606 OSErr err;
607 mfsiobject *fip;
608
Jack Jansenf5c20571997-01-30 15:48:07 +0000609 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000610 return NULL;
611 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
612 if ( err ) {
613 PyErr_Mac(ErrorObject, err);
614 return NULL;
615 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000616 Py_INCREF(Py_None);
617 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000618}
619
Jack Jansenf5c20571997-01-30 15:48:07 +0000620static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000621mfss_GetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000622{
623 OSErr err;
624 unsigned long crdat, mddat, bkdat;
625
Jack Jansenf5c20571997-01-30 15:48:07 +0000626 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000627 return NULL;
628 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
629 if ( err ) {
630 PyErr_Mac(ErrorObject, err);
631 return NULL;
632 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000633 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000634}
635
Jack Jansenf5c20571997-01-30 15:48:07 +0000636static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000637mfss_SetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000638{
639 OSErr err;
640 double crdat, mddat, bkdat;
641
Jack Jansenf5c20571997-01-30 15:48:07 +0000642 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000643 return NULL;
644 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
645 (unsigned long)mddat, (unsigned long)bkdat);
646 if ( err ) {
647 PyErr_Mac(ErrorObject, err);
648 return NULL;
649 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000650 Py_INCREF(Py_None);
651 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000652}
653
Jack Jansenf5c20571997-01-30 15:48:07 +0000654static struct PyMethodDef mfss_methods[] = {
655 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
656 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000657 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
658 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000659 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
660 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
661 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
662 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
663 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
664 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
665 {"GetDates", (PyCFunction)mfss_GetDates, 1},
666 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000667
668 {NULL, NULL} /* sentinel */
669};
670
671/* ---------- */
672
Jack Jansenf5c20571997-01-30 15:48:07 +0000673static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000674mfss_getattr(mfssobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000675{
Jack Jansenc889b761995-02-13 12:00:46 +0000676 if ( strcmp(name, "data") == 0)
677 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000678 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000679}
680
681mfssobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000682newmfssobject(FSSpec *fss)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000683{
684 mfssobject *self;
685
Jack Jansenf5c20571997-01-30 15:48:07 +0000686 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000687 if (self == NULL)
688 return NULL;
689 self->fsspec = *fss;
690 return self;
691}
692
693static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000694mfss_dealloc(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000695{
Jack Jansenf5c20571997-01-30 15:48:07 +0000696 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000697}
698
Jack Jansenf5c20571997-01-30 15:48:07 +0000699static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000700mfss_repr(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000701{
702 char buf[512];
703
Jack Jansendeefbe52001-08-08 13:46:49 +0000704 sprintf(buf, "FSSpec((%d, %ld, '%.*s'))",
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000705 self->fsspec.vRefNum,
706 self->fsspec.parID,
707 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000708 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000709}
710
711static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000712mfss_compare(mfssobject *v, mfssobject *w)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000713{
714 int minlen;
715 int res;
716
717 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
718 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
719 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
720 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
721 minlen = v->fsspec.name[0];
722 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
723 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
724 if ( res ) return res;
725 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
726 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
727 return res;
728}
729
Jack Jansenf5c20571997-01-30 15:48:07 +0000730statichere PyTypeObject Mfsstype = {
731 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000732 0, /*ob_size*/
733 "FSSpec", /*tp_name*/
734 sizeof(mfssobject), /*tp_basicsize*/
735 0, /*tp_itemsize*/
736 /* methods */
737 (destructor)mfss_dealloc, /*tp_dealloc*/
738 (printfunc)0, /*tp_print*/
739 (getattrfunc)mfss_getattr, /*tp_getattr*/
740 (setattrfunc)0, /*tp_setattr*/
741 (cmpfunc)mfss_compare, /*tp_compare*/
742 (reprfunc)mfss_repr, /*tp_repr*/
743 0, /*tp_as_number*/
744 0, /*tp_as_sequence*/
745 0, /*tp_as_mapping*/
746 (hashfunc)0, /*tp_hash*/
747};
748
749/* End of code for FSSpec objects */
750/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000751#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000752static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000753mfsr_as_fsspec(mfsrobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000754{
755 OSErr err;
756 FSSpec fss;
757
758 if (!PyArg_ParseTuple(args, ""))
759 return NULL;
760 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
761 if ( err ) {
762 PyErr_Mac(ErrorObject, err);
763 return NULL;
764 }
765 Py_INCREF(Py_None);
766 return (PyObject *)newmfssobject(&fss);
767}
768
769static struct PyMethodDef mfsr_methods[] = {
770 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
771#if 0
772 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
773 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
774 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
775 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
776 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
777 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
778 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
779 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
780 {"GetDates", (PyCFunction)mfss_GetDates, 1},
781 {"SetDates", (PyCFunction)mfss_SetDates, 1},
782#endif
783
784 {NULL, NULL} /* sentinel */
785};
786
787/* ---------- */
788
789static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000790mfsr_getattr(mfsrobject *self, char *name)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000791{
792 if ( strcmp(name, "data") == 0)
793 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
794 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
795}
796
797mfsrobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000798newmfsrobject(FSRef *fsr)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000799{
800 mfsrobject *self;
801
802 self = PyObject_NEW(mfsrobject, &Mfsrtype);
803 if (self == NULL)
804 return NULL;
805 self->fsref = *fsr;
806 return self;
807}
808
809static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000810mfsr_compare(mfsrobject *v, mfsrobject *w)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000811{
812 OSErr err;
813
814 if ( v == w ) return 0;
815 err = FSCompareFSRefs(&v->fsref, &w->fsref);
816 if ( err == 0 )
817 return 0;
818 if (v < w )
819 return -1;
820 return 1;
821}
822
823static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000824mfsr_dealloc(mfsrobject *self)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000825{
826 PyMem_DEL(self);
827}
828
829statichere PyTypeObject Mfsrtype = {
830 PyObject_HEAD_INIT(&PyType_Type)
831 0, /*ob_size*/
832 "FSRef", /*tp_name*/
833 sizeof(mfsrobject), /*tp_basicsize*/
834 0, /*tp_itemsize*/
835 /* methods */
836 (destructor)mfsr_dealloc, /*tp_dealloc*/
837 (printfunc)0, /*tp_print*/
838 (getattrfunc)mfsr_getattr, /*tp_getattr*/
839 (setattrfunc)0, /*tp_setattr*/
840 (cmpfunc)mfsr_compare, /*tp_compare*/
841 (reprfunc)0, /*tp_repr*/
842 0, /*tp_as_number*/
843 0, /*tp_as_sequence*/
844 0, /*tp_as_mapping*/
845 (hashfunc)0, /*tp_hash*/
846};
847
848/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000849#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000850/* -------------------------------------------------------- */
851
852static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000853mfs_ResolveAliasFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000854{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000855 FSSpec fss;
856 Boolean chain = 1, isfolder, wasaliased;
857 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000858
Jack Jansenf5c20571997-01-30 15:48:07 +0000859 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000860 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000861 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
862 if ( err ) {
863 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000864 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000865 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000866 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000867}
868
Jack Jansen74a1e632000-07-14 22:37:27 +0000869#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000870static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000871mfs_StandardGetFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000872{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000873 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000874 short numtypes;
875 StandardFileReply reply;
876
877 list[0] = list[1] = list[2] = list[3] = 0;
878 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000879 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000880 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
881 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000882 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000883 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000884 numtypes++;
885 }
Jack Jansenc2632861995-06-03 21:15:50 +0000886 if ( numtypes == 0 )
887 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000888 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000889 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000890}
891
Jack Jansenf5c20571997-01-30 15:48:07 +0000892static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000893mfs_PromptGetFile(PyObject *self, PyObject *args)
Jack Jansend5d5f461995-08-14 12:22:56 +0000894{
895 SFTypeList list;
896 short numtypes;
897 StandardFileReply reply;
898 char *prompt = NULL;
899
900 list[0] = list[1] = list[2] = list[3] = 0;
901 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000902 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000903 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
904 PyMac_GetOSType, &list[3]) )
905 return NULL;
906 while ( numtypes < 4 && list[numtypes] ) {
907 numtypes++;
908 }
909 if ( numtypes == 0 )
910 numtypes = -1;
911 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000912 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000913}
914
Jack Jansenf5c20571997-01-30 15:48:07 +0000915static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000916mfs_StandardPutFile(PyObject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000917{
918 Str255 prompt, dft;
919 StandardFileReply reply;
920
921 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000922 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000923 return NULL;
924 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000925 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000926}
927
Jack Jansend99d2831996-07-22 15:26:01 +0000928/*
929** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000930static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000931mfs_SetFolder(PyObject *self, PyObject *args)
Jack Jansend99d2831996-07-22 15:26:01 +0000932{
933 FSSpec spec;
934 FSSpec ospec;
935 short orefnum;
936 long oparid;
937
938 /* Get old values */
939 orefnum = -LMGetSFSaveDisk();
940 oparid = LMGetCurDirStore();
941 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
942
943 /* Go to working directory by default */
944 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000945 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000946 return NULL;
947 /* Set standard-file working directory */
948 LMSetSFSaveDisk(-spec.vRefNum);
949 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000950 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000951}
Jack Jansene79dc762000-06-02 21:35:07 +0000952#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000953
Jack Jansenf5c20571997-01-30 15:48:07 +0000954static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000955mfs_FSSpec(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000956{
957 FSSpec fss;
958
Jack Jansenf5c20571997-01-30 15:48:07 +0000959 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000960 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000961 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000962}
963
Jack Jansenf5c20571997-01-30 15:48:07 +0000964static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000965mfs_FSRef(PyObject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000966{
Jack Jansencbed91b2001-08-03 13:31:36 +0000967#if TARGET_API_MAC_OS8
968 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
969 return 0;
970#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000971 FSRef fsr;
972
973 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
974 return NULL;
975 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +0000976#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000977}
978
979static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000980mfs_RawFSSpec(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000981{
982 FSSpec *fssp;
983 int size;
984
Jack Janseneeccca91997-05-07 15:46:31 +0000985 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000986 return NULL;
987 if ( size != sizeof(FSSpec) ) {
988 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
989 return NULL;
990 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000991 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000992}
993
Jack Jansenf5c20571997-01-30 15:48:07 +0000994static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000995mfs_RawAlias(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000996{
997 char *dataptr;
998 Handle h;
999 int size;
1000
Jack Janseneeccca91997-05-07 15:46:31 +00001001 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001002 return NULL;
1003 h = NewHandle(size);
1004 if ( h == NULL ) {
1005 PyErr_NoMemory();
1006 return NULL;
1007 }
1008 HLock(h);
1009 memcpy((char *)*h, dataptr, size);
1010 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001011 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001012}
1013
Jack Jansen74a1e632000-07-14 22:37:27 +00001014#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001015static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001016mfs_GetDirectory(PyObject *self, PyObject *args)
Jack Jansen81f51c71995-02-20 15:45:25 +00001017{
1018 FSSpec fsdir;
1019 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001020 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001021
Jack Jansenf5c20571997-01-30 15:48:07 +00001022 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001023 return NULL;
1024
Jack Jansend5d5f461995-08-14 12:22:56 +00001025 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001026 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001027}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001028#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001029
Jack Jansenf5c20571997-01-30 15:48:07 +00001030static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001031mfs_FindFolder(PyObject *self, PyObject *args)
Jack Jansen2c673621995-06-18 20:05:14 +00001032{
1033 OSErr err;
1034 short where;
1035 OSType which;
1036 int create;
1037 short refnum;
1038 long dirid;
1039
Jack Jansenabd703d2001-03-15 14:38:10 +00001040 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001041 return NULL;
1042 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1043 if ( err ) {
1044 PyErr_Mac(ErrorObject, err);
1045 return NULL;
1046 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001047 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001048}
1049
Jack Jansenf5c20571997-01-30 15:48:07 +00001050static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001051mfs_FindApplication(PyObject *self, PyObject *args)
Jack Jansen924ca851996-09-20 15:25:16 +00001052{
1053 OSErr err;
1054 OSType which;
1055 FSSpec fss;
1056
Jack Jansenf5c20571997-01-30 15:48:07 +00001057 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001058 return NULL;
1059 err = FindApplicationFromCreator(which, &fss);
1060 if ( err ) {
1061 PyErr_Mac(ErrorObject, err);
1062 return NULL;
1063 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001064 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001065}
1066
Jack Jansenf5c20571997-01-30 15:48:07 +00001067static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001068mfs_FInfo(PyObject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +00001069{
Jack Jansenf5c20571997-01-30 15:48:07 +00001070 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001071}
1072
Jack Jansend9936481997-06-16 14:31:38 +00001073static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001074mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args)
Jack Jansend9936481997-06-16 14:31:38 +00001075{
1076 OSErr err;
1077 char *fullpath;
1078 int fullpathlen;
1079 AliasHandle alias;
1080 Str32 zonename;
1081 Str31 servername;
1082
1083 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1084 return NULL;
1085 zonename[0] = 0;
1086 servername[0] = 0;
1087 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1088 servername, &alias);
1089 if ( err ) {
1090 PyErr_Mac(ErrorObject, err);
1091 return NULL;
1092 }
1093 return (PyObject *)newmfsaobject(alias);
1094}
1095
1096
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001097/* List of methods defined in the module */
1098
Jack Jansenf5c20571997-01-30 15:48:07 +00001099static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001100 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001101#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001102 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001103 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001104 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001105 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001106 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001107#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001108 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001109 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001110 {"RawFSSpec", mfs_RawFSSpec, 1},
1111 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001112 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001113 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001114 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001115 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001116
1117 {NULL, NULL} /* sentinel */
1118};
1119
Jack Jansen4e566ab2001-07-08 22:07:23 +00001120/*
1121** Convert a Python object to an FSSpec.
1122** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1123** (vrefnum, dirid, path).
1124*/
1125int
1126PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1127{
Jack Jansencbed91b2001-08-03 13:31:36 +00001128#if TARGET_API_MAC_OS8
1129 return 0;
1130#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001131 /* If it's an FSRef we're also okay. */
1132 if (_mfs_GetFSRefFromFSRef(v, fsr))
1133 return 1;
1134 /* first check whether it already is an FSSpec */
1135 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1136 return 1;
1137 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001138#if TARGET_API_MAC_OSX
1139 OSStatus err;
1140 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1141 PyErr_Mac(ErrorObject, err);
1142 return 0;
1143 }
1144 return 1;
1145#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001146 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1147 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001148#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001149 }
1150 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1151 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001152#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001153}
1154
1155/* Convert FSSpec to PyObject */
1156PyObject *PyMac_BuildFSRef(FSRef *v)
1157{
Jack Jansencbed91b2001-08-03 13:31:36 +00001158#if TARGET_API_MAC_OS8
1159 return NULL;
1160#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001161 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001162#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001163}
1164
1165/*
1166** Convert a Python object to an FSRef.
1167** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1168*/
1169int
1170PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1171{
1172 Str255 path;
1173 short refnum;
1174 long parid;
1175 OSErr err;
1176
1177 /* first check whether it already is an FSSpec */
1178 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1179 return 1;
1180 /* If it's an FSRef we're also okay. */
1181 if (_mfs_GetFSSpecFromFSRef(v, fs))
1182 return 1;
1183 if ( PyString_Check(v) ) {
1184 /* It's a pathname */
1185 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1186 return 0;
1187 refnum = 0; /* XXXX Should get CurWD here?? */
1188 parid = 0;
1189 } else {
1190 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1191 &refnum, &parid, PyMac_GetStr255, &path)) {
1192 return 0;
1193 }
1194 }
1195 err = FSMakeFSSpec(refnum, parid, path, fs);
1196 if ( err && err != fnfErr ) {
1197 PyMac_Error(err);
1198 return 0;
1199 }
1200 return 1;
1201}
1202
1203/* Convert FSSpec to PyObject */
1204PyObject *PyMac_BuildFSSpec(FSSpec *v)
1205{
1206 return (PyObject *)newmfssobject(v);
1207}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001208
1209/* Initialization function for the module (*must* be called initmacfs) */
1210
1211void
Jack Jansendeefbe52001-08-08 13:46:49 +00001212initmacfs(void)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001213{
Jack Jansenf5c20571997-01-30 15:48:07 +00001214 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001215
Jack Jansen45900492001-08-06 15:32:30 +00001216 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
Jack Jansenfabd00f2001-09-01 23:39:58 +00001217 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef, PyMac_GetFSRef);
1218 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec *, PyMac_BuildFSSpec);
1219 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef *, PyMac_BuildFSRef);
Jack Jansena5bca572001-08-03 15:39:27 +00001220
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001221 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001222 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001223
1224 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001225 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001226 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001227 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001228
Jack Jansena755e681997-09-20 17:40:22 +00001229 Mfsatype.ob_type = &PyType_Type;
1230 Py_INCREF(&Mfsatype);
1231 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1232 Mfsstype.ob_type = &PyType_Type;
1233 Py_INCREF(&Mfsstype);
1234 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1235 Mfsitype.ob_type = &PyType_Type;
1236 Py_INCREF(&Mfsitype);
1237 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001238 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001239}