blob: 19724cdd761b49b9fdd051314ac21e7b5ce774be [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 Jansenfabd00f2001-09-01 23:39:58 +000044extern PyObject *_PyMac_BuildFSRef(FSRef *);
45extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
46extern 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
54/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000055/* Declarations for objects of type Alias */
56
57typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000058 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000059 AliasHandle alias;
60} mfsaobject;
61
Jack Jansenf5c20571997-01-30 15:48:07 +000062staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000063
64#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
65
66/* ---------------------------------------------------------------- */
67/* Declarations for objects of type FSSpec */
68
69typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000070 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000071 FSSpec fsspec;
72} mfssobject;
73
Jack Jansenf5c20571997-01-30 15:48:07 +000074staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000075
76#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
77
Jack Jansen4e566ab2001-07-08 22:07:23 +000078/* ---------------------------------------------------------------- */
79/* Declarations for objects of type FSRef */
80
81typedef struct {
82 PyObject_HEAD
83 FSRef fsref;
84} mfsrobject;
85
86staticforward PyTypeObject Mfsrtype;
87
88#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
89
Guido van Rossumefd97671995-01-26 22:56:16 +000090
Jack Jansen3d185931995-08-07 14:04:10 +000091/* ---------------------------------------------------------------- */
92/* Declarations for objects of type FInfo */
93
94typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000095 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000096 FInfo finfo;
97} mfsiobject;
98
Jack Jansenf5c20571997-01-30 15:48:07 +000099staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +0000100
101#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
102
103
Jack Jansen4e566ab2001-07-08 22:07:23 +0000104staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
105staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000106
Jack Jansen17ba43f1995-01-26 16:22:07 +0000107/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000108
Jack Jansenf5c20571997-01-30 15:48:07 +0000109static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000110mfsa_Resolve(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000111{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000112 FSSpec from, *fromp, result;
113 Boolean changed;
114 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000115
Jack Jansen17ba43f1995-01-26 16:22:07 +0000116 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000117 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000118 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000119 if (from.name[0] )
120 fromp = &from;
121 else
122 fromp = NULL;
123 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000124 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000125 PyErr_Mac(ErrorObject, err);
126 return NULL;
127 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000128 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000129}
130
Jack Jansenf5c20571997-01-30 15:48:07 +0000131static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000132mfsa_GetInfo(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000133{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000134 Str63 value;
135 int i;
136 OSErr err;
137
Jack Jansenf5c20571997-01-30 15:48:07 +0000138 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000139 return NULL;
140 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
141 if ( err ) {
142 PyErr_Mac(ErrorObject, err);
143 return 0;
144 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000146}
147
Jack Jansenf5c20571997-01-30 15:48:07 +0000148static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000149mfsa_Update(mfsaobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000150{
151 FSSpec target, fromfile, *fromfilep;
152 OSErr err;
153 Boolean changed;
154
155 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000157 PyMac_GetFSSpec, &fromfile))
158 return NULL;
159 if ( fromfile.name[0] )
160 fromfilep = &fromfile;
161 else
162 fromfilep = NULL;
163 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
164 if ( err ) {
165 PyErr_Mac(ErrorObject, err);
166 return 0;
167 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000168 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000169}
170
Jack Jansenf5c20571997-01-30 15:48:07 +0000171static struct PyMethodDef mfsa_methods[] = {
172 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
173 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
174 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000175
176 {NULL, NULL} /* sentinel */
177};
178
179/* ---------- */
180
Jack Jansenf5c20571997-01-30 15:48:07 +0000181static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000182mfsa_getattr(mfsaobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000183{
Jack Jansenc889b761995-02-13 12:00:46 +0000184 if ( strcmp(name, "data") == 0 ) {
185 int size;
186 PyObject *rv;
187
188 size = GetHandleSize((Handle)self->alias);
189 HLock((Handle)self->alias);
190 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
191 HUnlock((Handle)self->alias);
192 return rv;
193 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000194 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000195}
196
Jack Jansen9ae898b2000-07-11 21:16:03 +0000197static mfsaobject *
198newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000199{
200 mfsaobject *self;
201
Jack Jansenf5c20571997-01-30 15:48:07 +0000202 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000203 if (self == NULL)
204 return NULL;
205 self->alias = alias;
206 return self;
207}
208
209
210static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000211mfsa_dealloc(mfsaobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000212{
213#if 0
214 if ( self->alias ) {
215 should we do something here?
216 }
217#endif
218
Jack Jansenf5c20571997-01-30 15:48:07 +0000219 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000220}
221
Jack Jansenf5c20571997-01-30 15:48:07 +0000222statichere PyTypeObject Mfsatype = {
223 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000224 0, /*ob_size*/
225 "Alias", /*tp_name*/
226 sizeof(mfsaobject), /*tp_basicsize*/
227 0, /*tp_itemsize*/
228 /* methods */
229 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000230 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000231 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000232 (setattrfunc)0, /*tp_setattr*/
233 (cmpfunc)0, /*tp_compare*/
234 (reprfunc)0, /*tp_repr*/
235 0, /*tp_as_number*/
236 0, /*tp_as_sequence*/
237 0, /*tp_as_mapping*/
238 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000239};
240
241/* End of code for Alias objects */
242/* -------------------------------------------------------- */
243
Jack Jansen3d185931995-08-07 14:04:10 +0000244/* ---------------------------------------------------------------- */
245
Jack Jansenf5c20571997-01-30 15:48:07 +0000246static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000247
248 {NULL, NULL} /* sentinel */
249};
250
251/* ---------- */
252
253static mfsiobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000254newmfsiobject(void)
Jack Jansen3d185931995-08-07 14:04:10 +0000255{
256 mfsiobject *self;
257
Jack Jansenf5c20571997-01-30 15:48:07 +0000258 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000259 if (self == NULL)
260 return NULL;
261 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
262 return self;
263}
264
265static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000266mfsi_dealloc(mfsiobject *self)
Jack Jansen3d185931995-08-07 14:04:10 +0000267{
Jack Jansenf5c20571997-01-30 15:48:07 +0000268 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000269}
270
Jack Jansenf5c20571997-01-30 15:48:07 +0000271static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000272mfsi_getattr(mfsiobject *self, char *name)
Jack Jansen3d185931995-08-07 14:04:10 +0000273{
Jack Jansen3d185931995-08-07 14:04:10 +0000274 if ( strcmp(name, "Type") == 0 )
275 return PyMac_BuildOSType(self->finfo.fdType);
276 else if ( strcmp(name, "Creator") == 0 )
277 return PyMac_BuildOSType(self->finfo.fdCreator);
278 else if ( strcmp(name, "Flags") == 0 )
279 return Py_BuildValue("i", (int)self->finfo.fdFlags);
280 else if ( strcmp(name, "Location") == 0 )
281 return PyMac_BuildPoint(self->finfo.fdLocation);
282 else if ( strcmp(name, "Fldr") == 0 )
283 return Py_BuildValue("i", (int)self->finfo.fdFldr);
284 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000285 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000286}
287
288
289static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000290mfsi_setattr(mfsiobject *self, char *name, PyObject *v)
Jack Jansen3d185931995-08-07 14:04:10 +0000291{
292 int rv;
293 int i;
294
295 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000296 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000297 return -1;
298 }
299 if ( strcmp(name, "Type") == 0 )
300 rv = PyMac_GetOSType(v, &self->finfo.fdType);
301 else if ( strcmp(name, "Creator") == 0 )
302 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
303 else if ( strcmp(name, "Flags") == 0 ) {
304 rv = PyArg_Parse(v, "i", &i);
305 self->finfo.fdFlags = (short)i;
306 } else if ( strcmp(name, "Location") == 0 )
307 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
308 else if ( strcmp(name, "Fldr") == 0 ) {
309 rv = PyArg_Parse(v, "i", &i);
310 self->finfo.fdFldr = (short)i;
311 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000312 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000313 return -1;
314 }
315 if (rv)
316 return 0;
317 return -1;
318}
319
320
Jack Jansenf5c20571997-01-30 15:48:07 +0000321static PyTypeObject Mfsitype = {
322 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000323 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000324 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000325 sizeof(mfsiobject), /*tp_basicsize*/
326 0, /*tp_itemsize*/
327 /* methods */
328 (destructor)mfsi_dealloc, /*tp_dealloc*/
329 (printfunc)0, /*tp_print*/
330 (getattrfunc)mfsi_getattr, /*tp_getattr*/
331 (setattrfunc)mfsi_setattr, /*tp_setattr*/
332 (cmpfunc)0, /*tp_compare*/
333 (reprfunc)0, /*tp_repr*/
334 0, /*tp_as_number*/
335 0, /*tp_as_sequence*/
336 0, /*tp_as_mapping*/
337 (hashfunc)0, /*tp_hash*/
338};
339
340/* End of code for FInfo object objects */
341/* -------------------------------------------------------- */
342
343
Jack Jansen17ba43f1995-01-26 16:22:07 +0000344/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000345** Helper routines for the FSRef and FSSpec creators in macglue.c
346** They return an FSSpec/FSRef if the Python object encapsulating
347** either is passed. They return a boolean success indicator.
348** Note that they do not set an exception on failure, they're only
349** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000350*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000351static int
352_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000353{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000354 if ( is_mfssobject(self) ) {
355 *fssp = ((mfssobject *)self)->fsspec;
356 return 1;
357 }
358 return 0;
359}
360
361/* Return an FSSpec if this is an FSref */
362static int
363_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
364{
Jack Jansencbed91b2001-08-03 13:31:36 +0000365#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000366 static FSRef *fsrp;
367
368 if ( is_mfsrobject(self) ) {
369 fsrp = &((mfsrobject *)self)->fsref;
370 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
371 return 1;
372 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000373#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000374 return 0;
375}
376
377/* Return an FSRef if this is an FSRef */
378static int
379_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
380{
Jack Jansencbed91b2001-08-03 13:31:36 +0000381#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000382 if ( is_mfsrobject(self) ) {
383 *fsrp = ((mfsrobject *)self)->fsref;
384 return 1;
385 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000386#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000387 return 0;
388}
389
390/* Return an FSRef if this is an FSSpec */
391static int
392_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
393{
Jack Jansencbed91b2001-08-03 13:31:36 +0000394#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000395 if ( is_mfssobject(self) ) {
396 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
397 return 1;
398 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000399#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000400 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000401}
402
Jack Jansen0bdf9791996-09-15 22:11:25 +0000403/*
404** Two generally useful routines
405*/
406static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000407PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat,
408 unsigned long *bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000409{
410 CInfoPBRec pb;
411 OSErr error;
412
413 pb.dirInfo.ioNamePtr = fss->name;
414 pb.dirInfo.ioFDirIndex = 0;
415 pb.dirInfo.ioVRefNum = fss->vRefNum;
416 pb.dirInfo.ioDrDirID = fss->parID;
417 error = PBGetCatInfoSync(&pb);
418 if ( error ) return error;
419 *crdat = pb.hFileInfo.ioFlCrDat;
420 *mddat = pb.hFileInfo.ioFlMdDat;
421 *bkdat = pb.hFileInfo.ioFlBkDat;
422 return 0;
423}
424
425static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000426PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
427 unsigned long bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000428{
429 CInfoPBRec pb;
430 OSErr error;
431
432 pb.dirInfo.ioNamePtr = fss->name;
433 pb.dirInfo.ioFDirIndex = 0;
434 pb.dirInfo.ioVRefNum = fss->vRefNum;
435 pb.dirInfo.ioDrDirID = fss->parID;
436 error = PBGetCatInfoSync(&pb);
437 if ( error ) return error;
438 pb.dirInfo.ioNamePtr = fss->name;
439 pb.dirInfo.ioFDirIndex = 0;
440 pb.dirInfo.ioVRefNum = fss->vRefNum;
441 pb.dirInfo.ioDrDirID = fss->parID;
442 pb.hFileInfo.ioFlCrDat = crdat;
443 pb.hFileInfo.ioFlMdDat = mddat;
444 pb.hFileInfo.ioFlBkDat = bkdat;
445 error = PBSetCatInfoSync(&pb);
446 return error;
447}
448
Jack Jansenf5c20571997-01-30 15:48:07 +0000449static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000450mfss_as_pathname(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000451{
Jack Jansendeefbe52001-08-08 13:46:49 +0000452#if TARGET_API_MAC_OSX
453 PyErr_SetString(PyExc_NotImplementedError, "FSSpec.as_pathname not supported on this platform");
454 return 0;
455#else
Jack Jansen17ba43f1995-01-26 16:22:07 +0000456 char strbuf[257];
457 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000458
Jack Jansenf5c20571997-01-30 15:48:07 +0000459 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000460 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000461 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000462 if ( err ) {
463 PyErr_Mac(ErrorObject, err);
464 return NULL;
465 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000466 return PyString_FromString(strbuf);
Jack Jansendeefbe52001-08-08 13:46:49 +0000467#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000468}
469
Jack Jansenf5c20571997-01-30 15:48:07 +0000470static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000471mfss_as_tuple(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000472{
Jack Jansenf5c20571997-01-30 15:48:07 +0000473 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000474 return NULL;
475 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
476 &self->fsspec.name[1], self->fsspec.name[0]);
477}
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000480mfss_NewAlias(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000481{
482 FSSpec src, *srcp;
483 OSErr err;
484 AliasHandle alias;
485
486 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000487 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000488 return NULL;
489 if ( src.name[0] )
490 srcp = &src;
491 else
492 srcp = NULL;
493 err = NewAlias(srcp, &self->fsspec, &alias);
494 if ( err ) {
495 PyErr_Mac(ErrorObject, err);
496 return NULL;
497 }
498
Jack Jansenf5c20571997-01-30 15:48:07 +0000499 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000500}
501
Jack Jansenf5c20571997-01-30 15:48:07 +0000502static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000503mfss_NewAliasMinimal(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000504{
505 OSErr err;
506 AliasHandle alias;
507
Jack Jansenf5c20571997-01-30 15:48:07 +0000508 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000509 return NULL;
510 err = NewAliasMinimal(&self->fsspec, &alias);
511 if ( err ) {
512 PyErr_Mac(ErrorObject, err);
513 return NULL;
514 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000515 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000516}
517
Jack Jansen4e566ab2001-07-08 22:07:23 +0000518static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000519mfss_FSpMakeFSRef(mfssobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000520{
Jack Jansencbed91b2001-08-03 13:31:36 +0000521#if TARGET_API_MAC_OS8
522 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
523 return 0;
524#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000525 OSErr err;
526 FSRef fsref;
527
528 if (!PyArg_ParseTuple(args, ""))
529 return NULL;
530 err = FSpMakeFSRef(&self->fsspec, &fsref);
531 if ( err ) {
532 PyErr_Mac(ErrorObject, err);
533 return NULL;
534 }
535 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000536#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000537}
538
Jack Jansen3d185931995-08-07 14:04:10 +0000539/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000540static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000541mfss_GetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000542{
543 OSErr err;
544 FInfo info;
545
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000547 return NULL;
548 err = FSpGetFInfo(&self->fsspec, &info);
549 if ( err ) {
550 PyErr_Mac(ErrorObject, err);
551 return NULL;
552 }
553 return Py_BuildValue("(O&O&)",
554 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
555}
556
Jack Jansenf5c20571997-01-30 15:48:07 +0000557static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000558mfss_SetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000559{
560 OSErr err;
561 OSType creator, type;
562 FInfo info;
563
Jack Jansenf5c20571997-01-30 15:48:07 +0000564 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000565 return NULL;
566 err = FSpGetFInfo(&self->fsspec, &info);
567 if ( err ) {
568 PyErr_Mac(ErrorObject, err);
569 return NULL;
570 }
571 info.fdType = type;
572 info.fdCreator = creator;
573 err = FSpSetFInfo(&self->fsspec, &info);
574 if ( err ) {
575 PyErr_Mac(ErrorObject, err);
576 return NULL;
577 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000578 Py_INCREF(Py_None);
579 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000580}
581
Jack Jansenf5c20571997-01-30 15:48:07 +0000582static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000583mfss_GetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000584{
585 OSErr err;
586 mfsiobject *fip;
587
588
Jack Jansenf5c20571997-01-30 15:48:07 +0000589 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000590 return NULL;
591 if ( (fip=newmfsiobject()) == NULL )
592 return NULL;
593 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
594 if ( err ) {
595 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000596 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000597 return NULL;
598 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000599 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000600}
601
Jack Jansenf5c20571997-01-30 15:48:07 +0000602static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000603mfss_SetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000604{
605 OSErr err;
606 mfsiobject *fip;
607
Jack Jansenf5c20571997-01-30 15:48:07 +0000608 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000609 return NULL;
610 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
611 if ( err ) {
612 PyErr_Mac(ErrorObject, err);
613 return NULL;
614 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000615 Py_INCREF(Py_None);
616 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000617}
618
Jack Jansenf5c20571997-01-30 15:48:07 +0000619static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000620mfss_GetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000621{
622 OSErr err;
623 unsigned long crdat, mddat, bkdat;
624
Jack Jansenf5c20571997-01-30 15:48:07 +0000625 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000626 return NULL;
627 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
628 if ( err ) {
629 PyErr_Mac(ErrorObject, err);
630 return NULL;
631 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000632 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000633}
634
Jack Jansenf5c20571997-01-30 15:48:07 +0000635static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000636mfss_SetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000637{
638 OSErr err;
639 double crdat, mddat, bkdat;
640
Jack Jansenf5c20571997-01-30 15:48:07 +0000641 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000642 return NULL;
643 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
644 (unsigned long)mddat, (unsigned long)bkdat);
645 if ( err ) {
646 PyErr_Mac(ErrorObject, err);
647 return NULL;
648 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000649 Py_INCREF(Py_None);
650 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000651}
652
Jack Jansenf5c20571997-01-30 15:48:07 +0000653static struct PyMethodDef mfss_methods[] = {
654 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
655 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000656 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
657 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000658 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
659 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
660 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
661 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
662 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
663 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
664 {"GetDates", (PyCFunction)mfss_GetDates, 1},
665 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000666
667 {NULL, NULL} /* sentinel */
668};
669
670/* ---------- */
671
Jack Jansenf5c20571997-01-30 15:48:07 +0000672static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000673mfss_getattr(mfssobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000674{
Jack Jansenc889b761995-02-13 12:00:46 +0000675 if ( strcmp(name, "data") == 0)
676 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000677 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000678}
679
680mfssobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000681newmfssobject(FSSpec *fss)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000682{
683 mfssobject *self;
684
Jack Jansenf5c20571997-01-30 15:48:07 +0000685 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000686 if (self == NULL)
687 return NULL;
688 self->fsspec = *fss;
689 return self;
690}
691
692static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000693mfss_dealloc(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000694{
Jack Jansenf5c20571997-01-30 15:48:07 +0000695 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000696}
697
Jack Jansenf5c20571997-01-30 15:48:07 +0000698static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000699mfss_repr(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000700{
701 char buf[512];
702
Jack Jansendeefbe52001-08-08 13:46:49 +0000703 sprintf(buf, "FSSpec((%d, %ld, '%.*s'))",
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000704 self->fsspec.vRefNum,
705 self->fsspec.parID,
706 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000707 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000708}
709
710static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000711mfss_compare(mfssobject *v, mfssobject *w)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000712{
713 int minlen;
714 int res;
715
716 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
717 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
718 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
719 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
720 minlen = v->fsspec.name[0];
721 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
722 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
723 if ( res ) return res;
724 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
725 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
726 return res;
727}
728
Jack Jansenf5c20571997-01-30 15:48:07 +0000729statichere PyTypeObject Mfsstype = {
730 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000731 0, /*ob_size*/
732 "FSSpec", /*tp_name*/
733 sizeof(mfssobject), /*tp_basicsize*/
734 0, /*tp_itemsize*/
735 /* methods */
736 (destructor)mfss_dealloc, /*tp_dealloc*/
737 (printfunc)0, /*tp_print*/
738 (getattrfunc)mfss_getattr, /*tp_getattr*/
739 (setattrfunc)0, /*tp_setattr*/
740 (cmpfunc)mfss_compare, /*tp_compare*/
741 (reprfunc)mfss_repr, /*tp_repr*/
742 0, /*tp_as_number*/
743 0, /*tp_as_sequence*/
744 0, /*tp_as_mapping*/
745 (hashfunc)0, /*tp_hash*/
746};
747
748/* End of code for FSSpec objects */
749/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000750#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000751static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000752mfsr_as_fsspec(mfsrobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000753{
754 OSErr err;
755 FSSpec fss;
756
757 if (!PyArg_ParseTuple(args, ""))
758 return NULL;
759 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
760 if ( err ) {
761 PyErr_Mac(ErrorObject, err);
762 return NULL;
763 }
764 Py_INCREF(Py_None);
765 return (PyObject *)newmfssobject(&fss);
766}
767
768static struct PyMethodDef mfsr_methods[] = {
769 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
770#if 0
771 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
772 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
773 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
774 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
775 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
776 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
777 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
778 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
779 {"GetDates", (PyCFunction)mfss_GetDates, 1},
780 {"SetDates", (PyCFunction)mfss_SetDates, 1},
781#endif
782
783 {NULL, NULL} /* sentinel */
784};
785
786/* ---------- */
787
788static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000789mfsr_getattr(mfsrobject *self, char *name)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000790{
791 if ( strcmp(name, "data") == 0)
792 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
793 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
794}
795
796mfsrobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000797newmfsrobject(FSRef *fsr)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000798{
799 mfsrobject *self;
800
801 self = PyObject_NEW(mfsrobject, &Mfsrtype);
802 if (self == NULL)
803 return NULL;
804 self->fsref = *fsr;
805 return self;
806}
807
808static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000809mfsr_compare(mfsrobject *v, mfsrobject *w)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000810{
811 OSErr err;
812
813 if ( v == w ) return 0;
814 err = FSCompareFSRefs(&v->fsref, &w->fsref);
815 if ( err == 0 )
816 return 0;
817 if (v < w )
818 return -1;
819 return 1;
820}
821
822static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000823mfsr_dealloc(mfsrobject *self)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000824{
825 PyMem_DEL(self);
826}
827
828statichere PyTypeObject Mfsrtype = {
829 PyObject_HEAD_INIT(&PyType_Type)
830 0, /*ob_size*/
831 "FSRef", /*tp_name*/
832 sizeof(mfsrobject), /*tp_basicsize*/
833 0, /*tp_itemsize*/
834 /* methods */
835 (destructor)mfsr_dealloc, /*tp_dealloc*/
836 (printfunc)0, /*tp_print*/
837 (getattrfunc)mfsr_getattr, /*tp_getattr*/
838 (setattrfunc)0, /*tp_setattr*/
839 (cmpfunc)mfsr_compare, /*tp_compare*/
840 (reprfunc)0, /*tp_repr*/
841 0, /*tp_as_number*/
842 0, /*tp_as_sequence*/
843 0, /*tp_as_mapping*/
844 (hashfunc)0, /*tp_hash*/
845};
846
847/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000848#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000849/* -------------------------------------------------------- */
850
851static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000852mfs_ResolveAliasFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000853{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000854 FSSpec fss;
855 Boolean chain = 1, isfolder, wasaliased;
856 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000857
Jack Jansenf5c20571997-01-30 15:48:07 +0000858 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000859 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000860 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
861 if ( err ) {
862 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000863 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000864 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000865 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000866}
867
Jack Jansen74a1e632000-07-14 22:37:27 +0000868#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000869static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000870mfs_StandardGetFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000871{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000872 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000873 short numtypes;
874 StandardFileReply reply;
875
876 list[0] = list[1] = list[2] = list[3] = 0;
877 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000878 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000879 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
880 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000881 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000882 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000883 numtypes++;
884 }
Jack Jansenc2632861995-06-03 21:15:50 +0000885 if ( numtypes == 0 )
886 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000887 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000888 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000889}
890
Jack Jansenf5c20571997-01-30 15:48:07 +0000891static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000892mfs_PromptGetFile(PyObject *self, PyObject *args)
Jack Jansend5d5f461995-08-14 12:22:56 +0000893{
894 SFTypeList list;
895 short numtypes;
896 StandardFileReply reply;
897 char *prompt = NULL;
898
899 list[0] = list[1] = list[2] = list[3] = 0;
900 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000901 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000902 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
903 PyMac_GetOSType, &list[3]) )
904 return NULL;
905 while ( numtypes < 4 && list[numtypes] ) {
906 numtypes++;
907 }
908 if ( numtypes == 0 )
909 numtypes = -1;
910 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000911 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000912}
913
Jack Jansenf5c20571997-01-30 15:48:07 +0000914static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000915mfs_StandardPutFile(PyObject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000916{
917 Str255 prompt, dft;
918 StandardFileReply reply;
919
920 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000921 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000922 return NULL;
923 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000924 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000925}
926
Jack Jansend99d2831996-07-22 15:26:01 +0000927/*
928** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000929static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000930mfs_SetFolder(PyObject *self, PyObject *args)
Jack Jansend99d2831996-07-22 15:26:01 +0000931{
932 FSSpec spec;
933 FSSpec ospec;
934 short orefnum;
935 long oparid;
936
937 /* Get old values */
938 orefnum = -LMGetSFSaveDisk();
939 oparid = LMGetCurDirStore();
940 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
941
942 /* Go to working directory by default */
943 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000944 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000945 return NULL;
946 /* Set standard-file working directory */
947 LMSetSFSaveDisk(-spec.vRefNum);
948 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000949 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000950}
Jack Jansene79dc762000-06-02 21:35:07 +0000951#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000952
Jack Jansenf5c20571997-01-30 15:48:07 +0000953static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000954mfs_FSSpec(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000955{
956 FSSpec fss;
957
Jack Jansenf5c20571997-01-30 15:48:07 +0000958 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000959 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000960 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000961}
962
Jack Jansenf5c20571997-01-30 15:48:07 +0000963static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000964mfs_FSRef(PyObject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000965{
Jack Jansencbed91b2001-08-03 13:31:36 +0000966#if TARGET_API_MAC_OS8
967 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
968 return 0;
969#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000970 FSRef fsr;
971
972 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
973 return NULL;
974 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +0000975#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000976}
977
978static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000979mfs_RawFSSpec(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000980{
981 FSSpec *fssp;
982 int size;
983
Jack Janseneeccca91997-05-07 15:46:31 +0000984 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000985 return NULL;
986 if ( size != sizeof(FSSpec) ) {
987 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
988 return NULL;
989 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000990 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000991}
992
Jack Jansenf5c20571997-01-30 15:48:07 +0000993static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000994mfs_RawAlias(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +0000995{
996 char *dataptr;
997 Handle h;
998 int size;
999
Jack Janseneeccca91997-05-07 15:46:31 +00001000 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001001 return NULL;
1002 h = NewHandle(size);
1003 if ( h == NULL ) {
1004 PyErr_NoMemory();
1005 return NULL;
1006 }
1007 HLock(h);
1008 memcpy((char *)*h, dataptr, size);
1009 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001010 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001011}
1012
Jack Jansen74a1e632000-07-14 22:37:27 +00001013#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001014static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001015mfs_GetDirectory(PyObject *self, PyObject *args)
Jack Jansen81f51c71995-02-20 15:45:25 +00001016{
1017 FSSpec fsdir;
1018 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001019 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001020
Jack Jansenf5c20571997-01-30 15:48:07 +00001021 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001022 return NULL;
1023
Jack Jansend5d5f461995-08-14 12:22:56 +00001024 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001025 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001026}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001027#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001028
Jack Jansenf5c20571997-01-30 15:48:07 +00001029static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001030mfs_FindFolder(PyObject *self, PyObject *args)
Jack Jansen2c673621995-06-18 20:05:14 +00001031{
1032 OSErr err;
1033 short where;
1034 OSType which;
1035 int create;
1036 short refnum;
1037 long dirid;
1038
Jack Jansenabd703d2001-03-15 14:38:10 +00001039 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001040 return NULL;
1041 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1042 if ( err ) {
1043 PyErr_Mac(ErrorObject, err);
1044 return NULL;
1045 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001046 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001047}
1048
Jack Jansenf5c20571997-01-30 15:48:07 +00001049static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001050mfs_FindApplication(PyObject *self, PyObject *args)
Jack Jansen924ca851996-09-20 15:25:16 +00001051{
1052 OSErr err;
1053 OSType which;
1054 FSSpec fss;
1055
Jack Jansenf5c20571997-01-30 15:48:07 +00001056 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001057 return NULL;
1058 err = FindApplicationFromCreator(which, &fss);
1059 if ( err ) {
1060 PyErr_Mac(ErrorObject, err);
1061 return NULL;
1062 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001063 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001064}
1065
Jack Jansenf5c20571997-01-30 15:48:07 +00001066static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001067mfs_FInfo(PyObject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +00001068{
Jack Jansenf5c20571997-01-30 15:48:07 +00001069 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001070}
1071
Jack Jansend9936481997-06-16 14:31:38 +00001072static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001073mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args)
Jack Jansend9936481997-06-16 14:31:38 +00001074{
1075 OSErr err;
1076 char *fullpath;
1077 int fullpathlen;
1078 AliasHandle alias;
1079 Str32 zonename;
1080 Str31 servername;
1081
1082 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1083 return NULL;
1084 zonename[0] = 0;
1085 servername[0] = 0;
1086 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1087 servername, &alias);
1088 if ( err ) {
1089 PyErr_Mac(ErrorObject, err);
1090 return NULL;
1091 }
1092 return (PyObject *)newmfsaobject(alias);
1093}
1094
1095
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001096/* List of methods defined in the module */
1097
Jack Jansenf5c20571997-01-30 15:48:07 +00001098static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001099 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001100#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001101 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001102 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001103 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001104 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001105 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001106#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001107 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001108 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001109 {"RawFSSpec", mfs_RawFSSpec, 1},
1110 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001111 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001112 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001113 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001114 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001115
1116 {NULL, NULL} /* sentinel */
1117};
1118
Jack Jansen4e566ab2001-07-08 22:07:23 +00001119/*
1120** Convert a Python object to an FSSpec.
1121** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1122** (vrefnum, dirid, path).
1123*/
1124int
1125PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1126{
Jack Jansencbed91b2001-08-03 13:31:36 +00001127#if TARGET_API_MAC_OS8
1128 return 0;
1129#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001130 /* If it's an FSRef we're also okay. */
1131 if (_mfs_GetFSRefFromFSRef(v, fsr))
1132 return 1;
1133 /* first check whether it already is an FSSpec */
1134 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1135 return 1;
1136 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001137#if TARGET_API_MAC_OSX
1138 OSStatus err;
1139 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1140 PyErr_Mac(ErrorObject, err);
1141 return 0;
1142 }
1143 return 1;
1144#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001145 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1146 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001147#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001148 }
1149 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1150 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001151#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001152}
1153
1154/* Convert FSSpec to PyObject */
1155PyObject *PyMac_BuildFSRef(FSRef *v)
1156{
Jack Jansencbed91b2001-08-03 13:31:36 +00001157#if TARGET_API_MAC_OS8
1158 return NULL;
1159#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001160 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001161#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001162}
1163
1164/*
1165** Convert a Python object to an FSRef.
1166** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1167*/
1168int
1169PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1170{
1171 Str255 path;
1172 short refnum;
1173 long parid;
1174 OSErr err;
1175
1176 /* first check whether it already is an FSSpec */
1177 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1178 return 1;
1179 /* If it's an FSRef we're also okay. */
1180 if (_mfs_GetFSSpecFromFSRef(v, fs))
1181 return 1;
1182 if ( PyString_Check(v) ) {
1183 /* It's a pathname */
1184 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1185 return 0;
1186 refnum = 0; /* XXXX Should get CurWD here?? */
1187 parid = 0;
1188 } else {
1189 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1190 &refnum, &parid, PyMac_GetStr255, &path)) {
1191 return 0;
1192 }
1193 }
1194 err = FSMakeFSSpec(refnum, parid, path, fs);
1195 if ( err && err != fnfErr ) {
1196 PyMac_Error(err);
1197 return 0;
1198 }
1199 return 1;
1200}
1201
1202/* Convert FSSpec to PyObject */
1203PyObject *PyMac_BuildFSSpec(FSSpec *v)
1204{
1205 return (PyObject *)newmfssobject(v);
1206}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001207
1208/* Initialization function for the module (*must* be called initmacfs) */
1209
1210void
Jack Jansendeefbe52001-08-08 13:46:49 +00001211initmacfs(void)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001212{
Jack Jansenf5c20571997-01-30 15:48:07 +00001213 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001214
Jack Jansen45900492001-08-06 15:32:30 +00001215 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
Jack Jansenfabd00f2001-09-01 23:39:58 +00001216 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef, PyMac_GetFSRef);
1217 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec *, PyMac_BuildFSSpec);
1218 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef *, PyMac_BuildFSRef);
Jack Jansena5bca572001-08-03 15:39:27 +00001219
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001220 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001221 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001222
1223 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001224 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001225 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001226 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001227
Jack Jansena755e681997-09-20 17:40:22 +00001228 Mfsatype.ob_type = &PyType_Type;
1229 Py_INCREF(&Mfsatype);
1230 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1231 Mfsstype.ob_type = &PyType_Type;
1232 Py_INCREF(&Mfsstype);
1233 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1234 Mfsitype.ob_type = &PyType_Type;
1235 Py_INCREF(&Mfsitype);
1236 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001237 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001238}