blob: d1b8bb0bfc831d1ecdb07a459069405e35d3228c [file] [log] [blame]
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossumefd97671995-01-26 22:56:16 +00003The Netherlands.
Jack Jansen84fa5ec1995-01-18 14:04:40 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Jack Jansenf5c20571997-01-30 15:48:07 +000025#include "Python.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000026#include "macglue.h"
Jack Jansena5bca572001-08-03 15:39:27 +000027#include "pymactoolbox.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000028
Jack Jansen6143d532001-05-19 12:34:59 +000029#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumbecdbec1995-02-14 01:27:24 +000030#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000031#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000032#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000033#include <StandardFile.h>
34#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000035#include <LowMem.h>
Jack Jansen6143d532001-05-19 12:34:59 +000036#else
37#include <Carbon/Carbon.h>
38#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +000039
Jack Jansen924ca851996-09-20 15:25:16 +000040#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000041
Jack Jansena5bca572001-08-03 15:39:27 +000042#ifdef USE_TOOLBOX_OBJECT_GLUE
43extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
44#define PyMac_GetFSSpec _PyMac_GetFSSpec
45#endif
Jack Jansenf5c20571997-01-30 15:48:07 +000046static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000047
48/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000049/* Declarations for objects of type Alias */
50
51typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000052 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000053 AliasHandle alias;
54} mfsaobject;
55
Jack Jansenf5c20571997-01-30 15:48:07 +000056staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000057
58#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
59
60/* ---------------------------------------------------------------- */
61/* Declarations for objects of type FSSpec */
62
63typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000064 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000065 FSSpec fsspec;
66} mfssobject;
67
Jack Jansenf5c20571997-01-30 15:48:07 +000068staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000069
70#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
71
Jack Jansen4e566ab2001-07-08 22:07:23 +000072/* ---------------------------------------------------------------- */
73/* Declarations for objects of type FSRef */
74
75typedef struct {
76 PyObject_HEAD
77 FSRef fsref;
78} mfsrobject;
79
80staticforward PyTypeObject Mfsrtype;
81
82#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
83
Guido van Rossumefd97671995-01-26 22:56:16 +000084
Jack Jansen3d185931995-08-07 14:04:10 +000085/* ---------------------------------------------------------------- */
86/* Declarations for objects of type FInfo */
87
88typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000089 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000090 FInfo finfo;
91} mfsiobject;
92
Jack Jansenf5c20571997-01-30 15:48:07 +000093staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +000094
95#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
96
97
Jack Jansen4e566ab2001-07-08 22:07:23 +000098staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */
99staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000100
Jack Jansen17ba43f1995-01-26 16:22:07 +0000101/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000102
Jack Jansenf5c20571997-01-30 15:48:07 +0000103static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000104mfsa_Resolve(self, args)
105 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000106 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000107{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000108 FSSpec from, *fromp, result;
109 Boolean changed;
110 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000111
Jack Jansen17ba43f1995-01-26 16:22:07 +0000112 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000113 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000114 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000115 if (from.name[0] )
116 fromp = &from;
117 else
118 fromp = NULL;
119 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000120 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000121 PyErr_Mac(ErrorObject, err);
122 return NULL;
123 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000124 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000125}
126
Jack Jansenf5c20571997-01-30 15:48:07 +0000127static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000128mfsa_GetInfo(self, args)
129 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000130 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000131{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000132 Str63 value;
133 int i;
134 OSErr err;
135
Jack Jansenf5c20571997-01-30 15:48:07 +0000136 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000137 return NULL;
138 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
139 if ( err ) {
140 PyErr_Mac(ErrorObject, err);
141 return 0;
142 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000144}
145
Jack Jansenf5c20571997-01-30 15:48:07 +0000146static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000147mfsa_Update(self, args)
148 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000149 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 Jansen17ba43f1995-01-26 16:22:07 +0000182mfsa_getattr(self, name)
183 mfsaobject *self;
184 char *name;
185{
Jack Jansenc889b761995-02-13 12:00:46 +0000186 if ( strcmp(name, "data") == 0 ) {
187 int size;
188 PyObject *rv;
189
190 size = GetHandleSize((Handle)self->alias);
191 HLock((Handle)self->alias);
192 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
193 HUnlock((Handle)self->alias);
194 return rv;
195 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000196 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000197}
198
Jack Jansen9ae898b2000-07-11 21:16:03 +0000199static mfsaobject *
200newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000201{
202 mfsaobject *self;
203
Jack Jansenf5c20571997-01-30 15:48:07 +0000204 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000205 if (self == NULL)
206 return NULL;
207 self->alias = alias;
208 return self;
209}
210
211
212static void
213mfsa_dealloc(self)
214 mfsaobject *self;
215{
216#if 0
217 if ( self->alias ) {
218 should we do something here?
219 }
220#endif
221
Jack Jansenf5c20571997-01-30 15:48:07 +0000222 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000223}
224
Jack Jansenf5c20571997-01-30 15:48:07 +0000225statichere PyTypeObject Mfsatype = {
226 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000227 0, /*ob_size*/
228 "Alias", /*tp_name*/
229 sizeof(mfsaobject), /*tp_basicsize*/
230 0, /*tp_itemsize*/
231 /* methods */
232 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000233 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000234 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000235 (setattrfunc)0, /*tp_setattr*/
236 (cmpfunc)0, /*tp_compare*/
237 (reprfunc)0, /*tp_repr*/
238 0, /*tp_as_number*/
239 0, /*tp_as_sequence*/
240 0, /*tp_as_mapping*/
241 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000242};
243
244/* End of code for Alias objects */
245/* -------------------------------------------------------- */
246
Jack Jansen3d185931995-08-07 14:04:10 +0000247/* ---------------------------------------------------------------- */
248
Jack Jansenf5c20571997-01-30 15:48:07 +0000249static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000250
251 {NULL, NULL} /* sentinel */
252};
253
254/* ---------- */
255
256static mfsiobject *
257newmfsiobject()
258{
259 mfsiobject *self;
260
Jack Jansenf5c20571997-01-30 15:48:07 +0000261 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000262 if (self == NULL)
263 return NULL;
264 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
265 return self;
266}
267
268static void
269mfsi_dealloc(self)
270 mfsiobject *self;
271{
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000273}
274
Jack Jansenf5c20571997-01-30 15:48:07 +0000275static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000276mfsi_getattr(self, name)
277 mfsiobject *self;
278 char *name;
279{
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
296mfsi_setattr(self, name, v)
297 mfsiobject *self;
298 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000299 PyObject *v;
Jack Jansen3d185931995-08-07 14:04:10 +0000300{
301 int rv;
302 int i;
303
304 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000306 return -1;
307 }
308 if ( strcmp(name, "Type") == 0 )
309 rv = PyMac_GetOSType(v, &self->finfo.fdType);
310 else if ( strcmp(name, "Creator") == 0 )
311 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
312 else if ( strcmp(name, "Flags") == 0 ) {
313 rv = PyArg_Parse(v, "i", &i);
314 self->finfo.fdFlags = (short)i;
315 } else if ( strcmp(name, "Location") == 0 )
316 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
317 else if ( strcmp(name, "Fldr") == 0 ) {
318 rv = PyArg_Parse(v, "i", &i);
319 self->finfo.fdFldr = (short)i;
320 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000321 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000322 return -1;
323 }
324 if (rv)
325 return 0;
326 return -1;
327}
328
329
Jack Jansenf5c20571997-01-30 15:48:07 +0000330static PyTypeObject Mfsitype = {
331 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000332 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000333 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000334 sizeof(mfsiobject), /*tp_basicsize*/
335 0, /*tp_itemsize*/
336 /* methods */
337 (destructor)mfsi_dealloc, /*tp_dealloc*/
338 (printfunc)0, /*tp_print*/
339 (getattrfunc)mfsi_getattr, /*tp_getattr*/
340 (setattrfunc)mfsi_setattr, /*tp_setattr*/
341 (cmpfunc)0, /*tp_compare*/
342 (reprfunc)0, /*tp_repr*/
343 0, /*tp_as_number*/
344 0, /*tp_as_sequence*/
345 0, /*tp_as_mapping*/
346 (hashfunc)0, /*tp_hash*/
347};
348
349/* End of code for FInfo object objects */
350/* -------------------------------------------------------- */
351
352
Jack Jansen17ba43f1995-01-26 16:22:07 +0000353/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000354** Helper routines for the FSRef and FSSpec creators in macglue.c
355** They return an FSSpec/FSRef if the Python object encapsulating
356** either is passed. They return a boolean success indicator.
357** Note that they do not set an exception on failure, they're only
358** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000359*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000360static int
361_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000362{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000363 if ( is_mfssobject(self) ) {
364 *fssp = ((mfssobject *)self)->fsspec;
365 return 1;
366 }
367 return 0;
368}
369
370/* Return an FSSpec if this is an FSref */
371static int
372_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
373{
Jack Jansencbed91b2001-08-03 13:31:36 +0000374#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000375 static FSRef *fsrp;
376
377 if ( is_mfsrobject(self) ) {
378 fsrp = &((mfsrobject *)self)->fsref;
379 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
380 return 1;
381 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000382#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000383 return 0;
384}
385
386/* Return an FSRef if this is an FSRef */
387static int
388_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
389{
Jack Jansencbed91b2001-08-03 13:31:36 +0000390#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000391 if ( is_mfsrobject(self) ) {
392 *fsrp = ((mfsrobject *)self)->fsref;
393 return 1;
394 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000395#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000396 return 0;
397}
398
399/* Return an FSRef if this is an FSSpec */
400static int
401_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
402{
Jack Jansencbed91b2001-08-03 13:31:36 +0000403#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000404 if ( is_mfssobject(self) ) {
405 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
406 return 1;
407 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000408#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000409 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000410}
411
Jack Jansen0bdf9791996-09-15 22:11:25 +0000412/*
413** Two generally useful routines
414*/
415static OSErr
416PyMac_GetFileDates(fss, crdat, mddat, bkdat)
417 FSSpec *fss;
418 unsigned long *crdat, *mddat, *bkdat;
419{
420 CInfoPBRec pb;
421 OSErr error;
422
423 pb.dirInfo.ioNamePtr = fss->name;
424 pb.dirInfo.ioFDirIndex = 0;
425 pb.dirInfo.ioVRefNum = fss->vRefNum;
426 pb.dirInfo.ioDrDirID = fss->parID;
427 error = PBGetCatInfoSync(&pb);
428 if ( error ) return error;
429 *crdat = pb.hFileInfo.ioFlCrDat;
430 *mddat = pb.hFileInfo.ioFlMdDat;
431 *bkdat = pb.hFileInfo.ioFlBkDat;
432 return 0;
433}
434
435static OSErr
436PyMac_SetFileDates(fss, crdat, mddat, bkdat)
437 FSSpec *fss;
438 unsigned long crdat, mddat, bkdat;
439{
440 CInfoPBRec pb;
441 OSErr error;
442
443 pb.dirInfo.ioNamePtr = fss->name;
444 pb.dirInfo.ioFDirIndex = 0;
445 pb.dirInfo.ioVRefNum = fss->vRefNum;
446 pb.dirInfo.ioDrDirID = fss->parID;
447 error = PBGetCatInfoSync(&pb);
448 if ( error ) return error;
449 pb.dirInfo.ioNamePtr = fss->name;
450 pb.dirInfo.ioFDirIndex = 0;
451 pb.dirInfo.ioVRefNum = fss->vRefNum;
452 pb.dirInfo.ioDrDirID = fss->parID;
453 pb.hFileInfo.ioFlCrDat = crdat;
454 pb.hFileInfo.ioFlMdDat = mddat;
455 pb.hFileInfo.ioFlBkDat = bkdat;
456 error = PBSetCatInfoSync(&pb);
457 return error;
458}
459
Jack Jansenf5c20571997-01-30 15:48:07 +0000460static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000461mfss_as_pathname(self, args)
462 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000463 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000464{
465 char strbuf[257];
466 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000467
Jack Jansenf5c20571997-01-30 15:48:07 +0000468 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000469 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000470 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000471 if ( err ) {
472 PyErr_Mac(ErrorObject, err);
473 return NULL;
474 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000475 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000476}
477
Jack Jansenf5c20571997-01-30 15:48:07 +0000478static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000479mfss_as_tuple(self, args)
480 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000481 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000482{
Jack Jansenf5c20571997-01-30 15:48:07 +0000483 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000484 return NULL;
485 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
486 &self->fsspec.name[1], self->fsspec.name[0]);
487}
488
Jack Jansenf5c20571997-01-30 15:48:07 +0000489static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000490mfss_NewAlias(self, args)
491 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000492 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000493{
494 FSSpec src, *srcp;
495 OSErr err;
496 AliasHandle alias;
497
498 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000499 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000500 return NULL;
501 if ( src.name[0] )
502 srcp = &src;
503 else
504 srcp = NULL;
505 err = NewAlias(srcp, &self->fsspec, &alias);
506 if ( err ) {
507 PyErr_Mac(ErrorObject, err);
508 return NULL;
509 }
510
Jack Jansenf5c20571997-01-30 15:48:07 +0000511 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000512}
513
Jack Jansenf5c20571997-01-30 15:48:07 +0000514static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000515mfss_NewAliasMinimal(self, args)
516 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000517 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000518{
519 OSErr err;
520 AliasHandle alias;
521
Jack Jansenf5c20571997-01-30 15:48:07 +0000522 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000523 return NULL;
524 err = NewAliasMinimal(&self->fsspec, &alias);
525 if ( err ) {
526 PyErr_Mac(ErrorObject, err);
527 return NULL;
528 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000529 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000530}
531
Jack Jansen4e566ab2001-07-08 22:07:23 +0000532static PyObject *
533mfss_FSpMakeFSRef(self, args)
534 mfssobject *self;
535 PyObject *args;
536{
Jack Jansencbed91b2001-08-03 13:31:36 +0000537#if TARGET_API_MAC_OS8
538 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
539 return 0;
540#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000541 OSErr err;
542 FSRef fsref;
543
544 if (!PyArg_ParseTuple(args, ""))
545 return NULL;
546 err = FSpMakeFSRef(&self->fsspec, &fsref);
547 if ( err ) {
548 PyErr_Mac(ErrorObject, err);
549 return NULL;
550 }
551 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000552#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000553}
554
Jack Jansen3d185931995-08-07 14:04:10 +0000555/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000556static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000557mfss_GetCreatorType(self, args)
558 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000559 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000560{
561 OSErr err;
562 FInfo info;
563
Jack Jansenf5c20571997-01-30 15:48:07 +0000564 if (!PyArg_ParseTuple(args, ""))
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 return Py_BuildValue("(O&O&)",
572 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
573}
574
Jack Jansenf5c20571997-01-30 15:48:07 +0000575static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000576mfss_SetCreatorType(self, args)
577 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000578 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000579{
580 OSErr err;
581 OSType creator, type;
582 FInfo info;
583
Jack Jansenf5c20571997-01-30 15:48:07 +0000584 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000585 return NULL;
586 err = FSpGetFInfo(&self->fsspec, &info);
587 if ( err ) {
588 PyErr_Mac(ErrorObject, err);
589 return NULL;
590 }
591 info.fdType = type;
592 info.fdCreator = creator;
593 err = FSpSetFInfo(&self->fsspec, &info);
594 if ( err ) {
595 PyErr_Mac(ErrorObject, err);
596 return NULL;
597 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000598 Py_INCREF(Py_None);
599 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000600}
601
Jack Jansenf5c20571997-01-30 15:48:07 +0000602static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000603mfss_GetFInfo(self, args)
604 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000605 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000606{
607 OSErr err;
608 mfsiobject *fip;
609
610
Jack Jansenf5c20571997-01-30 15:48:07 +0000611 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000612 return NULL;
613 if ( (fip=newmfsiobject()) == NULL )
614 return NULL;
615 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
616 if ( err ) {
617 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000618 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000619 return NULL;
620 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000621 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000622}
623
Jack Jansenf5c20571997-01-30 15:48:07 +0000624static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000625mfss_SetFInfo(self, args)
626 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000627 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000628{
629 OSErr err;
630 mfsiobject *fip;
631
Jack Jansenf5c20571997-01-30 15:48:07 +0000632 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000633 return NULL;
634 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
635 if ( err ) {
636 PyErr_Mac(ErrorObject, err);
637 return NULL;
638 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000639 Py_INCREF(Py_None);
640 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000641}
642
Jack Jansenf5c20571997-01-30 15:48:07 +0000643static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000644mfss_GetDates(self, args)
645 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000646 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000647{
648 OSErr err;
649 unsigned long crdat, mddat, bkdat;
650
Jack Jansenf5c20571997-01-30 15:48:07 +0000651 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000652 return NULL;
653 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
654 if ( err ) {
655 PyErr_Mac(ErrorObject, err);
656 return NULL;
657 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000658 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000659}
660
Jack Jansenf5c20571997-01-30 15:48:07 +0000661static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000662mfss_SetDates(self, args)
663 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000664 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000665{
666 OSErr err;
667 double crdat, mddat, bkdat;
668
Jack Jansenf5c20571997-01-30 15:48:07 +0000669 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000670 return NULL;
671 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
672 (unsigned long)mddat, (unsigned long)bkdat);
673 if ( err ) {
674 PyErr_Mac(ErrorObject, err);
675 return NULL;
676 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000677 Py_INCREF(Py_None);
678 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000679}
680
Jack Jansenf5c20571997-01-30 15:48:07 +0000681static struct PyMethodDef mfss_methods[] = {
682 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
683 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000684 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
685 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000686 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
687 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
688 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
689 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
690 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
691 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
692 {"GetDates", (PyCFunction)mfss_GetDates, 1},
693 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000694
695 {NULL, NULL} /* sentinel */
696};
697
698/* ---------- */
699
Jack Jansenf5c20571997-01-30 15:48:07 +0000700static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000701mfss_getattr(self, name)
702 mfssobject *self;
703 char *name;
704{
Jack Jansenc889b761995-02-13 12:00:46 +0000705 if ( strcmp(name, "data") == 0)
706 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000707 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000708}
709
710mfssobject *
711newmfssobject(fss)
712 FSSpec *fss;
713{
714 mfssobject *self;
715
Jack Jansenf5c20571997-01-30 15:48:07 +0000716 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000717 if (self == NULL)
718 return NULL;
719 self->fsspec = *fss;
720 return self;
721}
722
723static void
724mfss_dealloc(self)
725 mfssobject *self;
726{
Jack Jansenf5c20571997-01-30 15:48:07 +0000727 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000728}
729
Jack Jansenf5c20571997-01-30 15:48:07 +0000730static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000731mfss_repr(self)
732 mfssobject *self;
733{
734 char buf[512];
735
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000736 sprintf(buf, "FSSpec((%d, %d, '%.*s'))",
737 self->fsspec.vRefNum,
738 self->fsspec.parID,
739 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000740 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000741}
742
743static int
744mfss_compare(v, w)
745 mfssobject *v, *w;
746{
747 int minlen;
748 int res;
749
750 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
751 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
752 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
753 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
754 minlen = v->fsspec.name[0];
755 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
756 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
757 if ( res ) return res;
758 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
759 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
760 return res;
761}
762
Jack Jansenf5c20571997-01-30 15:48:07 +0000763statichere PyTypeObject Mfsstype = {
764 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000765 0, /*ob_size*/
766 "FSSpec", /*tp_name*/
767 sizeof(mfssobject), /*tp_basicsize*/
768 0, /*tp_itemsize*/
769 /* methods */
770 (destructor)mfss_dealloc, /*tp_dealloc*/
771 (printfunc)0, /*tp_print*/
772 (getattrfunc)mfss_getattr, /*tp_getattr*/
773 (setattrfunc)0, /*tp_setattr*/
774 (cmpfunc)mfss_compare, /*tp_compare*/
775 (reprfunc)mfss_repr, /*tp_repr*/
776 0, /*tp_as_number*/
777 0, /*tp_as_sequence*/
778 0, /*tp_as_mapping*/
779 (hashfunc)0, /*tp_hash*/
780};
781
782/* End of code for FSSpec objects */
783/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000784#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000785static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +0000786mfsr_as_fsspec(self, args)
787 mfsrobject *self;
788 PyObject *args;
789{
790 OSErr err;
791 FSSpec fss;
792
793 if (!PyArg_ParseTuple(args, ""))
794 return NULL;
795 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
796 if ( err ) {
797 PyErr_Mac(ErrorObject, err);
798 return NULL;
799 }
800 Py_INCREF(Py_None);
801 return (PyObject *)newmfssobject(&fss);
802}
803
804static struct PyMethodDef mfsr_methods[] = {
805 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
806#if 0
807 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
808 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
809 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
810 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
811 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
812 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
813 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
814 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
815 {"GetDates", (PyCFunction)mfss_GetDates, 1},
816 {"SetDates", (PyCFunction)mfss_SetDates, 1},
817#endif
818
819 {NULL, NULL} /* sentinel */
820};
821
822/* ---------- */
823
824static PyObject *
825mfsr_getattr(self, name)
826 mfsrobject *self;
827 char *name;
828{
829 if ( strcmp(name, "data") == 0)
830 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
831 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
832}
833
834mfsrobject *
835newmfsrobject(fsr)
836 FSRef *fsr;
837{
838 mfsrobject *self;
839
840 self = PyObject_NEW(mfsrobject, &Mfsrtype);
841 if (self == NULL)
842 return NULL;
843 self->fsref = *fsr;
844 return self;
845}
846
847static int
848mfsr_compare(v, w)
849 mfsrobject *v, *w;
850{
851 OSErr err;
852
853 if ( v == w ) return 0;
854 err = FSCompareFSRefs(&v->fsref, &w->fsref);
855 if ( err == 0 )
856 return 0;
857 if (v < w )
858 return -1;
859 return 1;
860}
861
862static void
863mfsr_dealloc(self)
864 mfsrobject *self;
865{
866 PyMem_DEL(self);
867}
868
869statichere PyTypeObject Mfsrtype = {
870 PyObject_HEAD_INIT(&PyType_Type)
871 0, /*ob_size*/
872 "FSRef", /*tp_name*/
873 sizeof(mfsrobject), /*tp_basicsize*/
874 0, /*tp_itemsize*/
875 /* methods */
876 (destructor)mfsr_dealloc, /*tp_dealloc*/
877 (printfunc)0, /*tp_print*/
878 (getattrfunc)mfsr_getattr, /*tp_getattr*/
879 (setattrfunc)0, /*tp_setattr*/
880 (cmpfunc)mfsr_compare, /*tp_compare*/
881 (reprfunc)0, /*tp_repr*/
882 0, /*tp_as_number*/
883 0, /*tp_as_sequence*/
884 0, /*tp_as_mapping*/
885 (hashfunc)0, /*tp_hash*/
886};
887
888/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000889#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000890/* -------------------------------------------------------- */
891
892static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000893mfs_ResolveAliasFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000894 PyObject *self; /* Not used */
895 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000896{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000897 FSSpec fss;
898 Boolean chain = 1, isfolder, wasaliased;
899 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000900
Jack Jansenf5c20571997-01-30 15:48:07 +0000901 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000902 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000903 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
904 if ( err ) {
905 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000906 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000907 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000908 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000909}
910
Jack Jansen74a1e632000-07-14 22:37:27 +0000911#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000912static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000913mfs_StandardGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000914 PyObject *self; /* Not used */
915 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000916{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000917 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000918 short numtypes;
919 StandardFileReply reply;
920
921 list[0] = list[1] = list[2] = list[3] = 0;
922 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000923 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000924 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
925 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000926 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000927 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000928 numtypes++;
929 }
Jack Jansenc2632861995-06-03 21:15:50 +0000930 if ( numtypes == 0 )
931 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000932 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000933 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000934}
935
Jack Jansenf5c20571997-01-30 15:48:07 +0000936static PyObject *
Jack Jansend5d5f461995-08-14 12:22:56 +0000937mfs_PromptGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000938 PyObject *self; /* Not used */
939 PyObject *args;
Jack Jansend5d5f461995-08-14 12:22:56 +0000940{
941 SFTypeList list;
942 short numtypes;
943 StandardFileReply reply;
944 char *prompt = NULL;
945
946 list[0] = list[1] = list[2] = list[3] = 0;
947 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000948 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000949 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
950 PyMac_GetOSType, &list[3]) )
951 return NULL;
952 while ( numtypes < 4 && list[numtypes] ) {
953 numtypes++;
954 }
955 if ( numtypes == 0 )
956 numtypes = -1;
957 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000958 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000959}
960
Jack Jansenf5c20571997-01-30 15:48:07 +0000961static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000962mfs_StandardPutFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000963 PyObject *self; /* Not used */
964 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000965{
966 Str255 prompt, dft;
967 StandardFileReply reply;
968
969 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000970 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000971 return NULL;
972 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000973 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000974}
975
Jack Jansend99d2831996-07-22 15:26:01 +0000976/*
977** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000978static PyObject *
Jack Jansend99d2831996-07-22 15:26:01 +0000979mfs_SetFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000980 PyObject *self;
981 PyObject *args;
Jack Jansend99d2831996-07-22 15:26:01 +0000982{
983 FSSpec spec;
984 FSSpec ospec;
985 short orefnum;
986 long oparid;
987
988 /* Get old values */
989 orefnum = -LMGetSFSaveDisk();
990 oparid = LMGetCurDirStore();
991 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
992
993 /* Go to working directory by default */
994 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000995 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000996 return NULL;
997 /* Set standard-file working directory */
998 LMSetSFSaveDisk(-spec.vRefNum);
999 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +00001000 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +00001001}
Jack Jansene79dc762000-06-02 21:35:07 +00001002#endif
Jack Jansend99d2831996-07-22 15:26:01 +00001003
Jack Jansenf5c20571997-01-30 15:48:07 +00001004static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +00001005mfs_FSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001006 PyObject *self; /* Not used */
1007 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001008{
1009 FSSpec fss;
1010
Jack Jansenf5c20571997-01-30 15:48:07 +00001011 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001012 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +00001013 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001014}
1015
Jack Jansenf5c20571997-01-30 15:48:07 +00001016static PyObject *
Jack Jansen4e566ab2001-07-08 22:07:23 +00001017mfs_FSRef(self, args)
1018 PyObject *self; /* Not used */
1019 PyObject *args;
1020{
Jack Jansencbed91b2001-08-03 13:31:36 +00001021#if TARGET_API_MAC_OS8
1022 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
1023 return 0;
1024#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001025 FSRef fsr;
1026
1027 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
1028 return NULL;
1029 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +00001030#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001031}
1032
1033static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001034mfs_RawFSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001035 PyObject *self; /* Not used */
1036 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +00001037{
1038 FSSpec *fssp;
1039 int size;
1040
Jack Janseneeccca91997-05-07 15:46:31 +00001041 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001042 return NULL;
1043 if ( size != sizeof(FSSpec) ) {
1044 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
1045 return NULL;
1046 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001047 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +00001048}
1049
Jack Jansenf5c20571997-01-30 15:48:07 +00001050static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +00001051mfs_RawAlias(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001052 PyObject *self; /* Not used */
1053 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +00001054{
1055 char *dataptr;
1056 Handle h;
1057 int size;
1058
Jack Janseneeccca91997-05-07 15:46:31 +00001059 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001060 return NULL;
1061 h = NewHandle(size);
1062 if ( h == NULL ) {
1063 PyErr_NoMemory();
1064 return NULL;
1065 }
1066 HLock(h);
1067 memcpy((char *)*h, dataptr, size);
1068 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001069 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001070}
1071
Jack Jansen74a1e632000-07-14 22:37:27 +00001072#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001073static PyObject *
Jack Jansen81f51c71995-02-20 15:45:25 +00001074mfs_GetDirectory(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001075 PyObject *self; /* Not used */
1076 PyObject *args;
Jack Jansen81f51c71995-02-20 15:45:25 +00001077{
1078 FSSpec fsdir;
1079 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001080 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001081
Jack Jansenf5c20571997-01-30 15:48:07 +00001082 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001083 return NULL;
1084
Jack Jansend5d5f461995-08-14 12:22:56 +00001085 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001086 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001087}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001088#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001089
Jack Jansenf5c20571997-01-30 15:48:07 +00001090static PyObject *
Jack Jansen2c673621995-06-18 20:05:14 +00001091mfs_FindFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001092 PyObject *self; /* Not used */
1093 PyObject *args;
Jack Jansen2c673621995-06-18 20:05:14 +00001094{
1095 OSErr err;
1096 short where;
1097 OSType which;
1098 int create;
1099 short refnum;
1100 long dirid;
1101
Jack Jansenabd703d2001-03-15 14:38:10 +00001102 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001103 return NULL;
1104 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1105 if ( err ) {
1106 PyErr_Mac(ErrorObject, err);
1107 return NULL;
1108 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001109 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001110}
1111
Jack Jansenf5c20571997-01-30 15:48:07 +00001112static PyObject *
Jack Jansen924ca851996-09-20 15:25:16 +00001113mfs_FindApplication(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001114 PyObject *self; /* Not used */
1115 PyObject *args;
Jack Jansen924ca851996-09-20 15:25:16 +00001116{
1117 OSErr err;
1118 OSType which;
1119 FSSpec fss;
1120
Jack Jansenf5c20571997-01-30 15:48:07 +00001121 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001122 return NULL;
1123 err = FindApplicationFromCreator(which, &fss);
1124 if ( err ) {
1125 PyErr_Mac(ErrorObject, err);
1126 return NULL;
1127 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001128 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001129}
1130
Jack Jansenf5c20571997-01-30 15:48:07 +00001131static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +00001132mfs_FInfo(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +00001133 PyObject *self;
1134 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +00001135{
Jack Jansenf5c20571997-01-30 15:48:07 +00001136 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001137}
1138
Jack Jansend9936481997-06-16 14:31:38 +00001139static PyObject *
1140mfs_NewAliasMinimalFromFullPath(self, args)
1141 PyObject *self; /* Not used */
1142 PyObject *args;
1143{
1144 OSErr err;
1145 char *fullpath;
1146 int fullpathlen;
1147 AliasHandle alias;
1148 Str32 zonename;
1149 Str31 servername;
1150
1151 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1152 return NULL;
1153 zonename[0] = 0;
1154 servername[0] = 0;
1155 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1156 servername, &alias);
1157 if ( err ) {
1158 PyErr_Mac(ErrorObject, err);
1159 return NULL;
1160 }
1161 return (PyObject *)newmfsaobject(alias);
1162}
1163
1164
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001165/* List of methods defined in the module */
1166
Jack Jansenf5c20571997-01-30 15:48:07 +00001167static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001168 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001169#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001170 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001171 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001172 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001173 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001174 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001175#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001176 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001177 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001178 {"RawFSSpec", mfs_RawFSSpec, 1},
1179 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001180 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001181 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001182 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001183 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001184
1185 {NULL, NULL} /* sentinel */
1186};
1187
Jack Jansen4e566ab2001-07-08 22:07:23 +00001188/*
1189** Convert a Python object to an FSSpec.
1190** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1191** (vrefnum, dirid, path).
1192*/
1193int
1194PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1195{
Jack Jansencbed91b2001-08-03 13:31:36 +00001196#if TARGET_API_MAC_OS8
1197 return 0;
1198#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001199 OSErr err;
1200
1201 /* If it's an FSRef we're also okay. */
1202 if (_mfs_GetFSRefFromFSRef(v, fsr))
1203 return 1;
1204 /* first check whether it already is an FSSpec */
1205 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1206 return 1;
1207 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001208#if TARGET_API_MAC_OSX
1209 OSStatus err;
1210 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1211 PyErr_Mac(ErrorObject, err);
1212 return 0;
1213 }
1214 return 1;
1215#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001216 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1217 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001218#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001219 }
1220 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1221 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001222#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001223}
1224
1225/* Convert FSSpec to PyObject */
1226PyObject *PyMac_BuildFSRef(FSRef *v)
1227{
Jack Jansencbed91b2001-08-03 13:31:36 +00001228#if TARGET_API_MAC_OS8
1229 return NULL;
1230#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001231 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001232#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001233}
1234
1235/*
1236** Convert a Python object to an FSRef.
1237** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1238*/
1239int
1240PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1241{
1242 Str255 path;
1243 short refnum;
1244 long parid;
1245 OSErr err;
1246
1247 /* first check whether it already is an FSSpec */
1248 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1249 return 1;
1250 /* If it's an FSRef we're also okay. */
1251 if (_mfs_GetFSSpecFromFSRef(v, fs))
1252 return 1;
1253 if ( PyString_Check(v) ) {
1254 /* It's a pathname */
1255 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1256 return 0;
1257 refnum = 0; /* XXXX Should get CurWD here?? */
1258 parid = 0;
1259 } else {
1260 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1261 &refnum, &parid, PyMac_GetStr255, &path)) {
1262 return 0;
1263 }
1264 }
1265 err = FSMakeFSSpec(refnum, parid, path, fs);
1266 if ( err && err != fnfErr ) {
1267 PyMac_Error(err);
1268 return 0;
1269 }
1270 return 1;
1271}
1272
1273/* Convert FSSpec to PyObject */
1274PyObject *PyMac_BuildFSSpec(FSSpec *v)
1275{
1276 return (PyObject *)newmfssobject(v);
1277}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001278
1279/* Initialization function for the module (*must* be called initmacfs) */
1280
1281void
1282initmacfs()
1283{
Jack Jansenf5c20571997-01-30 15:48:07 +00001284 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001285
Jack Jansena5bca572001-08-03 15:39:27 +00001286 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, PyMac_GetFSSpec);
1287
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001288 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001289 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001290
1291 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001292 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001293 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001294 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001295
Jack Jansena755e681997-09-20 17:40:22 +00001296 Mfsatype.ob_type = &PyType_Type;
1297 Py_INCREF(&Mfsatype);
1298 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1299 Mfsstype.ob_type = &PyType_Type;
1300 Py_INCREF(&Mfsstype);
1301 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1302 Mfsitype.ob_type = &PyType_Type;
1303 Py_INCREF(&Mfsitype);
1304 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001305 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001306}