blob: f8c3428ce73cc220a54e6bc1493f238f2d200b73 [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 Jansen9c5b61b2002-08-06 12:59:44 +000042/*
43** The next define uses PBGetCatInfoSync for GetFInfo, allowing you
44** to get FInfo for folders. This works on OSX, but it may result
45** in problems on OS9, hence the define (for the time being).
46*/
47#define USE_CATINFO_FOR_FINFO
48
Jack Jansen01a94622001-11-01 14:00:19 +000049#ifndef TARGET_API_MAC_OSX
Just van Rossumab57c7d2001-10-31 22:55:08 +000050#include "pythonresources.h"
51extern PyMac_PrefRecord PyMac_options;
Jack Jansen01a94622001-11-01 14:00:19 +000052#endif
Just van Rossumab57c7d2001-10-31 22:55:08 +000053
Jack Jansena5bca572001-08-03 15:39:27 +000054#ifdef USE_TOOLBOX_OBJECT_GLUE
55extern int _PyMac_GetFSSpec(PyObject *, FSSpec *);
Jack Jansen61142972001-09-02 00:09:35 +000056extern PyObject *_PyMac_BuildFSSpec(FSSpec *);
57extern int _PyMac_GetFSRef(PyObject *, FSRef *);
Jack Jansenfabd00f2001-09-01 23:39:58 +000058extern PyObject *_PyMac_BuildFSRef(FSRef *);
Jack Jansena5bca572001-08-03 15:39:27 +000059#define PyMac_GetFSSpec _PyMac_GetFSSpec
Jack Jansenfabd00f2001-09-01 23:39:58 +000060#define PyMac_BuildFSSpec _PyMac_BuildFSSpec
61#define PyMac_GetFSRef _PyMac_GetFSRef
62#define PyMac_BuildFSRef _PyMac_BuildFSRef
Jack Jansena5bca572001-08-03 15:39:27 +000063#endif
Jack Jansenf5c20571997-01-30 15:48:07 +000064static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000065
Jack Jansen697842f2001-09-10 22:00:39 +000066#ifdef TARGET_API_MAC_OSX
67#define PATHNAMELEN 1024
68#else
69#define PATHNAMELEN 256
70#endif
71
Jack Jansen84fa5ec1995-01-18 14:04:40 +000072/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000073/* Declarations for objects of type Alias */
74
75typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000076 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000077 AliasHandle alias;
78} mfsaobject;
79
Jeremy Hylton938ace62002-07-17 16:30:39 +000080static PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000081
82#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
83
84/* ---------------------------------------------------------------- */
85/* Declarations for objects of type FSSpec */
86
87typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000088 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000089 FSSpec fsspec;
90} mfssobject;
91
Jeremy Hylton938ace62002-07-17 16:30:39 +000092static PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000093
94#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
95
Jack Jansen4e566ab2001-07-08 22:07:23 +000096/* ---------------------------------------------------------------- */
97/* Declarations for objects of type FSRef */
98
99typedef struct {
100 PyObject_HEAD
101 FSRef fsref;
102} mfsrobject;
103
Jeremy Hylton938ace62002-07-17 16:30:39 +0000104static PyTypeObject Mfsrtype;
Jack Jansen4e566ab2001-07-08 22:07:23 +0000105
106#define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
107
Guido van Rossumefd97671995-01-26 22:56:16 +0000108
Jack Jansen3d185931995-08-07 14:04:10 +0000109/* ---------------------------------------------------------------- */
110/* Declarations for objects of type FInfo */
111
112typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000113 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +0000114 FInfo finfo;
115} mfsiobject;
116
Jeremy Hylton938ace62002-07-17 16:30:39 +0000117static PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +0000118
119#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
120
121
Jeremy Hylton938ace62002-07-17 16:30:39 +0000122static mfssobject *newmfssobject(FSSpec *fss); /* Forward */
123static mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */
Guido van Rossumefd97671995-01-26 22:56:16 +0000124
Jack Jansen17ba43f1995-01-26 16:22:07 +0000125/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000126
Jack Jansenf5c20571997-01-30 15:48:07 +0000127static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000128mfsa_Resolve(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000129{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000130 FSSpec from, *fromp, result;
131 Boolean changed;
132 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000133
Jack Jansen17ba43f1995-01-26 16:22:07 +0000134 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000136 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000137 if (from.name[0] )
138 fromp = &from;
139 else
140 fromp = NULL;
141 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000142 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000143 PyErr_Mac(ErrorObject, err);
144 return NULL;
145 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000146 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000147}
148
Jack Jansenf5c20571997-01-30 15:48:07 +0000149static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000150mfsa_GetInfo(mfsaobject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000151{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000152 Str63 value;
153 int i;
154 OSErr err;
155
Jack Jansenf5c20571997-01-30 15:48:07 +0000156 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000157 return NULL;
158 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
159 if ( err ) {
160 PyErr_Mac(ErrorObject, err);
161 return 0;
162 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000163 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000164}
165
Jack Jansenf5c20571997-01-30 15:48:07 +0000166static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000167mfsa_Update(mfsaobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000168{
169 FSSpec target, fromfile, *fromfilep;
170 OSErr err;
171 Boolean changed;
172
173 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000174 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000175 PyMac_GetFSSpec, &fromfile))
176 return NULL;
177 if ( fromfile.name[0] )
178 fromfilep = &fromfile;
179 else
180 fromfilep = NULL;
181 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
182 if ( err ) {
183 PyErr_Mac(ErrorObject, err);
184 return 0;
185 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000186 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000187}
188
Jack Jansenf5c20571997-01-30 15:48:07 +0000189static struct PyMethodDef mfsa_methods[] = {
190 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
191 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
192 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000193
194 {NULL, NULL} /* sentinel */
195};
196
197/* ---------- */
198
Jack Jansenf5c20571997-01-30 15:48:07 +0000199static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000200mfsa_getattr(mfsaobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000201{
Jack Jansenc889b761995-02-13 12:00:46 +0000202 if ( strcmp(name, "data") == 0 ) {
203 int size;
204 PyObject *rv;
205
206 size = GetHandleSize((Handle)self->alias);
207 HLock((Handle)self->alias);
208 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
209 HUnlock((Handle)self->alias);
210 return rv;
211 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000213}
214
Jack Jansen9ae898b2000-07-11 21:16:03 +0000215static mfsaobject *
216newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000217{
218 mfsaobject *self;
219
Jack Jansenf5c20571997-01-30 15:48:07 +0000220 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000221 if (self == NULL)
222 return NULL;
223 self->alias = alias;
224 return self;
225}
226
227
228static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000229mfsa_dealloc(mfsaobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000230{
231#if 0
232 if ( self->alias ) {
233 should we do something here?
234 }
235#endif
236
Jack Jansen0e2f7982002-05-22 14:31:48 +0000237 PyObject_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000238}
239
Jack Jansenf5c20571997-01-30 15:48:07 +0000240statichere PyTypeObject Mfsatype = {
241 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000242 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000243 "macfs.Alias", /*tp_name*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000244 sizeof(mfsaobject), /*tp_basicsize*/
245 0, /*tp_itemsize*/
246 /* methods */
247 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000248 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000249 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000250 (setattrfunc)0, /*tp_setattr*/
251 (cmpfunc)0, /*tp_compare*/
252 (reprfunc)0, /*tp_repr*/
253 0, /*tp_as_number*/
254 0, /*tp_as_sequence*/
255 0, /*tp_as_mapping*/
256 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000257};
258
259/* End of code for Alias objects */
260/* -------------------------------------------------------- */
261
Jack Jansen3d185931995-08-07 14:04:10 +0000262/* ---------------------------------------------------------------- */
263
Jack Jansenf5c20571997-01-30 15:48:07 +0000264static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000265
266 {NULL, NULL} /* sentinel */
267};
268
269/* ---------- */
270
271static mfsiobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000272newmfsiobject(void)
Jack Jansen3d185931995-08-07 14:04:10 +0000273{
274 mfsiobject *self;
275
Jack Jansenf5c20571997-01-30 15:48:07 +0000276 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000277 if (self == NULL)
278 return NULL;
279 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
280 return self;
281}
282
283static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000284mfsi_dealloc(mfsiobject *self)
Jack Jansen3d185931995-08-07 14:04:10 +0000285{
Jack Jansen0e2f7982002-05-22 14:31:48 +0000286 PyObject_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000287}
288
Jack Jansenf5c20571997-01-30 15:48:07 +0000289static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000290mfsi_getattr(mfsiobject *self, char *name)
Jack Jansen3d185931995-08-07 14:04:10 +0000291{
Jack Jansen3d185931995-08-07 14:04:10 +0000292 if ( strcmp(name, "Type") == 0 )
293 return PyMac_BuildOSType(self->finfo.fdType);
294 else if ( strcmp(name, "Creator") == 0 )
295 return PyMac_BuildOSType(self->finfo.fdCreator);
296 else if ( strcmp(name, "Flags") == 0 )
297 return Py_BuildValue("i", (int)self->finfo.fdFlags);
298 else if ( strcmp(name, "Location") == 0 )
299 return PyMac_BuildPoint(self->finfo.fdLocation);
300 else if ( strcmp(name, "Fldr") == 0 )
301 return Py_BuildValue("i", (int)self->finfo.fdFldr);
Just van Rossum14666cc2001-12-11 14:04:12 +0000302 else if ( strcmp(name, "__members__") == 0 )
303 return Py_BuildValue("[sssss]", "Type", "Creator", "Flags", "Location", "Fldr");
Jack Jansen3d185931995-08-07 14:04:10 +0000304 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000305 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000306}
307
308
309static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000310mfsi_setattr(mfsiobject *self, char *name, PyObject *v)
Jack Jansen3d185931995-08-07 14:04:10 +0000311{
312 int rv;
313 int i;
314
315 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000316 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000317 return -1;
318 }
319 if ( strcmp(name, "Type") == 0 )
320 rv = PyMac_GetOSType(v, &self->finfo.fdType);
321 else if ( strcmp(name, "Creator") == 0 )
322 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
323 else if ( strcmp(name, "Flags") == 0 ) {
324 rv = PyArg_Parse(v, "i", &i);
325 self->finfo.fdFlags = (short)i;
326 } else if ( strcmp(name, "Location") == 0 )
327 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
328 else if ( strcmp(name, "Fldr") == 0 ) {
329 rv = PyArg_Parse(v, "i", &i);
330 self->finfo.fdFldr = (short)i;
331 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000332 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000333 return -1;
334 }
335 if (rv)
336 return 0;
337 return -1;
338}
339
340
Jack Jansenf5c20571997-01-30 15:48:07 +0000341static PyTypeObject Mfsitype = {
342 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000343 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000344 "macfs.FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000345 sizeof(mfsiobject), /*tp_basicsize*/
346 0, /*tp_itemsize*/
347 /* methods */
348 (destructor)mfsi_dealloc, /*tp_dealloc*/
349 (printfunc)0, /*tp_print*/
350 (getattrfunc)mfsi_getattr, /*tp_getattr*/
351 (setattrfunc)mfsi_setattr, /*tp_setattr*/
352 (cmpfunc)0, /*tp_compare*/
353 (reprfunc)0, /*tp_repr*/
354 0, /*tp_as_number*/
355 0, /*tp_as_sequence*/
356 0, /*tp_as_mapping*/
357 (hashfunc)0, /*tp_hash*/
358};
359
360/* End of code for FInfo object objects */
361/* -------------------------------------------------------- */
362
363
Jack Jansen17ba43f1995-01-26 16:22:07 +0000364/*
Jack Jansen4e566ab2001-07-08 22:07:23 +0000365** Helper routines for the FSRef and FSSpec creators in macglue.c
366** They return an FSSpec/FSRef if the Python object encapsulating
367** either is passed. They return a boolean success indicator.
368** Note that they do not set an exception on failure, they're only
369** helper routines.
Jack Jansen17ba43f1995-01-26 16:22:07 +0000370*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000371static int
372_mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000373{
Jack Jansen4e566ab2001-07-08 22:07:23 +0000374 if ( is_mfssobject(self) ) {
375 *fssp = ((mfssobject *)self)->fsspec;
376 return 1;
377 }
378 return 0;
379}
380
381/* Return an FSSpec if this is an FSref */
382static int
383_mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp)
384{
Jack Jansencbed91b2001-08-03 13:31:36 +0000385#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000386 static FSRef *fsrp;
387
388 if ( is_mfsrobject(self) ) {
389 fsrp = &((mfsrobject *)self)->fsref;
390 if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr )
391 return 1;
392 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000393#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000394 return 0;
395}
396
397/* Return an FSRef if this is an FSRef */
398static int
399_mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp)
400{
Jack Jansencbed91b2001-08-03 13:31:36 +0000401#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000402 if ( is_mfsrobject(self) ) {
403 *fsrp = ((mfsrobject *)self)->fsref;
404 return 1;
405 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000406#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000407 return 0;
408}
409
410/* Return an FSRef if this is an FSSpec */
411static int
412_mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp)
413{
Jack Jansencbed91b2001-08-03 13:31:36 +0000414#if !TARGET_API_MAC_OS8
Jack Jansen4e566ab2001-07-08 22:07:23 +0000415 if ( is_mfssobject(self) ) {
416 if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr )
417 return 1;
418 }
Jack Jansencbed91b2001-08-03 13:31:36 +0000419#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000420 return 0;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000421}
422
Jack Jansen0bdf9791996-09-15 22:11:25 +0000423/*
424** Two generally useful routines
425*/
426static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000427PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat,
428 unsigned long *bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000429{
430 CInfoPBRec pb;
431 OSErr error;
432
433 pb.dirInfo.ioNamePtr = fss->name;
434 pb.dirInfo.ioFDirIndex = 0;
435 pb.dirInfo.ioVRefNum = fss->vRefNum;
436 pb.dirInfo.ioDrDirID = fss->parID;
437 error = PBGetCatInfoSync(&pb);
438 if ( error ) return error;
439 *crdat = pb.hFileInfo.ioFlCrDat;
440 *mddat = pb.hFileInfo.ioFlMdDat;
441 *bkdat = pb.hFileInfo.ioFlBkDat;
442 return 0;
443}
444
445static OSErr
Jack Jansendeefbe52001-08-08 13:46:49 +0000446PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
447 unsigned long bkdat)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000448{
449 CInfoPBRec pb;
450 OSErr error;
451
452 pb.dirInfo.ioNamePtr = fss->name;
453 pb.dirInfo.ioFDirIndex = 0;
454 pb.dirInfo.ioVRefNum = fss->vRefNum;
455 pb.dirInfo.ioDrDirID = fss->parID;
456 error = PBGetCatInfoSync(&pb);
457 if ( error ) return error;
458 pb.dirInfo.ioNamePtr = fss->name;
459 pb.dirInfo.ioFDirIndex = 0;
460 pb.dirInfo.ioVRefNum = fss->vRefNum;
461 pb.dirInfo.ioDrDirID = fss->parID;
462 pb.hFileInfo.ioFlCrDat = crdat;
463 pb.hFileInfo.ioFlMdDat = mddat;
464 pb.hFileInfo.ioFlBkDat = bkdat;
465 error = PBSetCatInfoSync(&pb);
466 return error;
467}
468
Jack Jansenf5c20571997-01-30 15:48:07 +0000469static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000470mfss_as_pathname(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000471{
Jack Jansen697842f2001-09-10 22:00:39 +0000472 char strbuf[PATHNAMELEN];
Jack Jansen17ba43f1995-01-26 16:22:07 +0000473 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000474
Jack Jansenf5c20571997-01-30 15:48:07 +0000475 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000476 return NULL;
Jack Jansen697842f2001-09-10 22:00:39 +0000477 err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000478 if ( err ) {
479 PyErr_Mac(ErrorObject, err);
480 return NULL;
481 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000482 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000483}
484
Jack Jansenf5c20571997-01-30 15:48:07 +0000485static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000486mfss_as_tuple(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000487{
Jack Jansenf5c20571997-01-30 15:48:07 +0000488 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000489 return NULL;
490 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
491 &self->fsspec.name[1], self->fsspec.name[0]);
492}
493
Jack Jansenf5c20571997-01-30 15:48:07 +0000494static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000495mfss_NewAlias(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000496{
497 FSSpec src, *srcp;
498 OSErr err;
499 AliasHandle alias;
500
501 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000502 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000503 return NULL;
504 if ( src.name[0] )
505 srcp = &src;
506 else
507 srcp = NULL;
508 err = NewAlias(srcp, &self->fsspec, &alias);
509 if ( err ) {
510 PyErr_Mac(ErrorObject, err);
511 return NULL;
512 }
513
Jack Jansenf5c20571997-01-30 15:48:07 +0000514 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000515}
516
Jack Jansenf5c20571997-01-30 15:48:07 +0000517static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000518mfss_NewAliasMinimal(mfssobject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000519{
520 OSErr err;
521 AliasHandle alias;
522
Jack Jansenf5c20571997-01-30 15:48:07 +0000523 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000524 return NULL;
525 err = NewAliasMinimal(&self->fsspec, &alias);
526 if ( err ) {
527 PyErr_Mac(ErrorObject, err);
528 return NULL;
529 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000530 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000531}
532
Jack Jansen4e566ab2001-07-08 22:07:23 +0000533static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000534mfss_FSpMakeFSRef(mfssobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000535{
Jack Jansencbed91b2001-08-03 13:31:36 +0000536#if TARGET_API_MAC_OS8
537 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
538 return 0;
539#else
Jack Jansen4e566ab2001-07-08 22:07:23 +0000540 OSErr err;
541 FSRef fsref;
542
543 if (!PyArg_ParseTuple(args, ""))
544 return NULL;
545 err = FSpMakeFSRef(&self->fsspec, &fsref);
546 if ( err ) {
547 PyErr_Mac(ErrorObject, err);
548 return NULL;
549 }
550 return (PyObject *)newmfsrobject(&fsref);
Jack Jansencbed91b2001-08-03 13:31:36 +0000551#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +0000552}
553
Jack Jansen3d185931995-08-07 14:04:10 +0000554/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000555static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000556mfss_GetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000557{
558 OSErr err;
559 FInfo info;
560
Jack Jansenf5c20571997-01-30 15:48:07 +0000561 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000562 return NULL;
563 err = FSpGetFInfo(&self->fsspec, &info);
564 if ( err ) {
565 PyErr_Mac(ErrorObject, err);
566 return NULL;
567 }
568 return Py_BuildValue("(O&O&)",
569 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
570}
571
Jack Jansenf5c20571997-01-30 15:48:07 +0000572static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000573mfss_SetCreatorType(mfssobject *self, PyObject *args)
Jack Jansen8828fcf1995-02-02 14:23:52 +0000574{
575 OSErr err;
576 OSType creator, type;
577 FInfo info;
578
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000580 return NULL;
581 err = FSpGetFInfo(&self->fsspec, &info);
582 if ( err ) {
583 PyErr_Mac(ErrorObject, err);
584 return NULL;
585 }
586 info.fdType = type;
587 info.fdCreator = creator;
588 err = FSpSetFInfo(&self->fsspec, &info);
589 if ( err ) {
590 PyErr_Mac(ErrorObject, err);
591 return NULL;
592 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000593 Py_INCREF(Py_None);
594 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000595}
596
Jack Jansenf5c20571997-01-30 15:48:07 +0000597static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000598mfss_GetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000599{
600 OSErr err;
601 mfsiobject *fip;
Jack Jansen9c5b61b2002-08-06 12:59:44 +0000602 CInfoPBRec pb;
603 FSSpec* fss = &self->fsspec;
604
Jack Jansenf5c20571997-01-30 15:48:07 +0000605 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000606 return NULL;
607 if ( (fip=newmfsiobject()) == NULL )
608 return NULL;
Jack Jansen9c5b61b2002-08-06 12:59:44 +0000609#ifdef USE_CATINFO_FOR_FINFO
610 pb.dirInfo.ioNamePtr = fss->name;
611 pb.dirInfo.ioFDirIndex = 0;
612 pb.dirInfo.ioVRefNum = fss->vRefNum;
613 pb.dirInfo.ioDrDirID = fss->parID;
614 err = PBGetCatInfoSync(&pb);
615 if ( err ) {
616 PyErr_Mac(ErrorObject, err);
617 Py_DECREF(fip);
618 return NULL;
619 }
620 memcpy(&fip->finfo, &pb.hFileInfo.ioFlFndrInfo, sizeof(FileInfo));
621#else
Jack Jansen3d185931995-08-07 14:04:10 +0000622 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
623 if ( err ) {
624 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000625 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000626 return NULL;
627 }
Jack Jansen9c5b61b2002-08-06 12:59:44 +0000628#endif
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000630}
631
Jack Jansenf5c20571997-01-30 15:48:07 +0000632static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000633mfss_SetFInfo(mfssobject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +0000634{
635 OSErr err;
636 mfsiobject *fip;
637
Jack Jansenf5c20571997-01-30 15:48:07 +0000638 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000639 return NULL;
640 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
641 if ( err ) {
642 PyErr_Mac(ErrorObject, err);
643 return NULL;
644 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000645 Py_INCREF(Py_None);
646 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000647}
648
Jack Jansenf5c20571997-01-30 15:48:07 +0000649static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000650mfss_GetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000651{
652 OSErr err;
653 unsigned long crdat, mddat, bkdat;
654
Jack Jansenf5c20571997-01-30 15:48:07 +0000655 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000656 return NULL;
657 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
658 if ( err ) {
659 PyErr_Mac(ErrorObject, err);
660 return NULL;
661 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000662 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000663}
664
Jack Jansenf5c20571997-01-30 15:48:07 +0000665static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000666mfss_SetDates(mfssobject *self, PyObject *args)
Jack Jansen0bdf9791996-09-15 22:11:25 +0000667{
668 OSErr err;
669 double crdat, mddat, bkdat;
670
Jack Jansenf5c20571997-01-30 15:48:07 +0000671 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000672 return NULL;
673 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
674 (unsigned long)mddat, (unsigned long)bkdat);
675 if ( err ) {
676 PyErr_Mac(ErrorObject, err);
677 return NULL;
678 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000681}
682
Jack Jansenf5c20571997-01-30 15:48:07 +0000683static struct PyMethodDef mfss_methods[] = {
684 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
685 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000686 {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1},
687 {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1},
Jack Jansenf5c20571997-01-30 15:48:07 +0000688 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
689 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
690 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
691 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
692 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
693 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
694 {"GetDates", (PyCFunction)mfss_GetDates, 1},
695 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000696
697 {NULL, NULL} /* sentinel */
698};
699
700/* ---------- */
701
Jack Jansenf5c20571997-01-30 15:48:07 +0000702static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000703mfss_getattr(mfssobject *self, char *name)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000704{
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 *
Jack Jansendeefbe52001-08-08 13:46:49 +0000711newmfssobject(FSSpec *fss)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000712{
713 mfssobject *self;
714
Jack Jansenf5c20571997-01-30 15:48:07 +0000715 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000716 if (self == NULL)
717 return NULL;
718 self->fsspec = *fss;
719 return self;
720}
721
722static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000723mfss_dealloc(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000724{
Jack Jansen0e2f7982002-05-22 14:31:48 +0000725 PyObject_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000726}
727
Jack Jansenf5c20571997-01-30 15:48:07 +0000728static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000729mfss_repr(mfssobject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000730{
731 char buf[512];
732
Jack Jansen101de912001-12-05 23:27:58 +0000733 PyOS_snprintf(buf, sizeof(buf), "FSSpec((%d, %ld, '%.*s'))",
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000734 self->fsspec.vRefNum,
735 self->fsspec.parID,
736 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000737 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000738}
739
740static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000741mfss_compare(mfssobject *v, mfssobject *w)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000742{
743 int minlen;
744 int res;
745
746 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
747 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
748 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
749 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
750 minlen = v->fsspec.name[0];
751 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
752 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
753 if ( res ) return res;
754 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
755 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
756 return res;
757}
758
Jack Jansenf5c20571997-01-30 15:48:07 +0000759statichere PyTypeObject Mfsstype = {
760 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000761 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000762 "macfs.FSSpec", /*tp_name*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000763 sizeof(mfssobject), /*tp_basicsize*/
764 0, /*tp_itemsize*/
765 /* methods */
766 (destructor)mfss_dealloc, /*tp_dealloc*/
767 (printfunc)0, /*tp_print*/
768 (getattrfunc)mfss_getattr, /*tp_getattr*/
769 (setattrfunc)0, /*tp_setattr*/
770 (cmpfunc)mfss_compare, /*tp_compare*/
771 (reprfunc)mfss_repr, /*tp_repr*/
772 0, /*tp_as_number*/
773 0, /*tp_as_sequence*/
774 0, /*tp_as_mapping*/
775 (hashfunc)0, /*tp_hash*/
776};
777
778/* End of code for FSSpec objects */
779/* -------------------------------------------------------- */
Jack Jansencbed91b2001-08-03 13:31:36 +0000780#if !TARGET_API_MAC_OS8
Jack Jansenf5c20571997-01-30 15:48:07 +0000781static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000782mfsr_as_fsspec(mfsrobject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000783{
784 OSErr err;
785 FSSpec fss;
786
787 if (!PyArg_ParseTuple(args, ""))
788 return NULL;
789 err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL);
790 if ( err ) {
791 PyErr_Mac(ErrorObject, err);
792 return NULL;
793 }
794 Py_INCREF(Py_None);
795 return (PyObject *)newmfssobject(&fss);
796}
797
Jack Jansenba4fe772002-01-18 16:13:15 +0000798static PyObject *
799mfsr_as_pathname(mfsrobject *self, PyObject *args)
800{
Jack Jansen5ae815a2002-02-26 21:36:19 +0000801 unsigned char strbuf[PATHNAMELEN];
Jack Jansenba4fe772002-01-18 16:13:15 +0000802 OSStatus err;
803
804 if (!PyArg_ParseTuple(args, ""))
805 return NULL;
806 err = FSRefMakePath(&self->fsref, strbuf, PATHNAMELEN);
807 if ( err ) {
808 PyErr_Mac(ErrorObject, err);
809 return NULL;
810 }
Jack Jansen5ae815a2002-02-26 21:36:19 +0000811 return PyString_FromString((char *)strbuf);
Jack Jansenba4fe772002-01-18 16:13:15 +0000812}
813
Jack Jansen4e566ab2001-07-08 22:07:23 +0000814static struct PyMethodDef mfsr_methods[] = {
815 {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1},
Jack Jansenba4fe772002-01-18 16:13:15 +0000816 {"as_pathname", (PyCFunction)mfsr_as_pathname, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +0000817#if 0
Jack Jansen4e566ab2001-07-08 22:07:23 +0000818 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
819 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
820 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
821 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
822 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
823 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
824 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
825 {"GetDates", (PyCFunction)mfss_GetDates, 1},
826 {"SetDates", (PyCFunction)mfss_SetDates, 1},
827#endif
828
829 {NULL, NULL} /* sentinel */
830};
831
832/* ---------- */
833
834static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000835mfsr_getattr(mfsrobject *self, char *name)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000836{
837 if ( strcmp(name, "data") == 0)
838 return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef));
839 return Py_FindMethod(mfsr_methods, (PyObject *)self, name);
840}
841
842mfsrobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000843newmfsrobject(FSRef *fsr)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000844{
845 mfsrobject *self;
846
847 self = PyObject_NEW(mfsrobject, &Mfsrtype);
848 if (self == NULL)
849 return NULL;
850 self->fsref = *fsr;
851 return self;
852}
853
854static int
Jack Jansendeefbe52001-08-08 13:46:49 +0000855mfsr_compare(mfsrobject *v, mfsrobject *w)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000856{
857 OSErr err;
858
859 if ( v == w ) return 0;
860 err = FSCompareFSRefs(&v->fsref, &w->fsref);
861 if ( err == 0 )
862 return 0;
863 if (v < w )
864 return -1;
865 return 1;
866}
867
868static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000869mfsr_dealloc(mfsrobject *self)
Jack Jansen4e566ab2001-07-08 22:07:23 +0000870{
Jack Jansen0e2f7982002-05-22 14:31:48 +0000871 PyObject_DEL(self);
Jack Jansen4e566ab2001-07-08 22:07:23 +0000872}
873
874statichere PyTypeObject Mfsrtype = {
875 PyObject_HEAD_INIT(&PyType_Type)
876 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000877 "macfs.FSRef", /*tp_name*/
Jack Jansen4e566ab2001-07-08 22:07:23 +0000878 sizeof(mfsrobject), /*tp_basicsize*/
879 0, /*tp_itemsize*/
880 /* methods */
881 (destructor)mfsr_dealloc, /*tp_dealloc*/
882 (printfunc)0, /*tp_print*/
883 (getattrfunc)mfsr_getattr, /*tp_getattr*/
884 (setattrfunc)0, /*tp_setattr*/
885 (cmpfunc)mfsr_compare, /*tp_compare*/
886 (reprfunc)0, /*tp_repr*/
887 0, /*tp_as_number*/
888 0, /*tp_as_sequence*/
889 0, /*tp_as_mapping*/
890 (hashfunc)0, /*tp_hash*/
891};
892
893/* End of code for FSRef objects */
Jack Jansencbed91b2001-08-03 13:31:36 +0000894#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen4e566ab2001-07-08 22:07:23 +0000895/* -------------------------------------------------------- */
896
897static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000898mfs_ResolveAliasFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000899{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000900 FSSpec fss;
901 Boolean chain = 1, isfolder, wasaliased;
902 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000903
Jack Jansenf5c20571997-01-30 15:48:07 +0000904 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000905 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000906 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
907 if ( err ) {
908 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000909 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000910 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000911 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000912}
913
Jack Jansen74a1e632000-07-14 22:37:27 +0000914#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000915static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000916mfs_StandardGetFile(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000917{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000918 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000919 short numtypes;
920 StandardFileReply reply;
921
922 list[0] = list[1] = list[2] = list[3] = 0;
923 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000924 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000925 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
926 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000927 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000928 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000929 numtypes++;
930 }
Jack Jansenc2632861995-06-03 21:15:50 +0000931 if ( numtypes == 0 )
932 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000933 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000934 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000935}
936
Jack Jansenf5c20571997-01-30 15:48:07 +0000937static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000938mfs_PromptGetFile(PyObject *self, PyObject *args)
Jack Jansend5d5f461995-08-14 12:22:56 +0000939{
940 SFTypeList list;
941 short numtypes;
942 StandardFileReply reply;
943 char *prompt = NULL;
944
945 list[0] = list[1] = list[2] = list[3] = 0;
946 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000947 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000948 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
949 PyMac_GetOSType, &list[3]) )
950 return NULL;
951 while ( numtypes < 4 && list[numtypes] ) {
952 numtypes++;
953 }
954 if ( numtypes == 0 )
955 numtypes = -1;
956 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000957 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000958}
959
Jack Jansenf5c20571997-01-30 15:48:07 +0000960static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000961mfs_StandardPutFile(PyObject *self, PyObject *args)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000962{
963 Str255 prompt, dft;
964 StandardFileReply reply;
965
966 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000967 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000968 return NULL;
969 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000970 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000971}
972
Jack Jansend99d2831996-07-22 15:26:01 +0000973/*
974** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000975static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000976mfs_SetFolder(PyObject *self, PyObject *args)
Jack Jansend99d2831996-07-22 15:26:01 +0000977{
978 FSSpec spec;
979 FSSpec ospec;
980 short orefnum;
981 long oparid;
982
983 /* Get old values */
984 orefnum = -LMGetSFSaveDisk();
985 oparid = LMGetCurDirStore();
986 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
987
988 /* Go to working directory by default */
989 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000990 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000991 return NULL;
992 /* Set standard-file working directory */
993 LMSetSFSaveDisk(-spec.vRefNum);
994 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000995 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000996}
Jack Jansene79dc762000-06-02 21:35:07 +0000997#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000998
Jack Jansenf5c20571997-01-30 15:48:07 +0000999static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001000mfs_FSSpec(PyObject *self, PyObject *args)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001001{
1002 FSSpec fss;
1003
Jack Jansenf5c20571997-01-30 15:48:07 +00001004 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001005 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +00001006 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001007}
1008
Jack Jansenf5c20571997-01-30 15:48:07 +00001009static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001010mfs_FSRef(PyObject *self, PyObject *args)
Jack Jansen4e566ab2001-07-08 22:07:23 +00001011{
Jack Jansencbed91b2001-08-03 13:31:36 +00001012#if TARGET_API_MAC_OS8
1013 PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform");
1014 return 0;
1015#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001016 FSRef fsr;
1017
1018 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr))
1019 return NULL;
1020 return (PyObject *)newmfsrobject(&fsr);
Jack Jansencbed91b2001-08-03 13:31:36 +00001021#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001022}
1023
1024static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001025mfs_RawFSSpec(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +00001026{
1027 FSSpec *fssp;
1028 int size;
1029
Jack Janseneeccca91997-05-07 15:46:31 +00001030 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001031 return NULL;
1032 if ( size != sizeof(FSSpec) ) {
1033 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
1034 return NULL;
1035 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001036 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +00001037}
1038
Jack Jansenf5c20571997-01-30 15:48:07 +00001039static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001040mfs_RawAlias(PyObject *self, PyObject *args)
Jack Jansenc889b761995-02-13 12:00:46 +00001041{
1042 char *dataptr;
1043 Handle h;
1044 int size;
1045
Jack Janseneeccca91997-05-07 15:46:31 +00001046 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +00001047 return NULL;
1048 h = NewHandle(size);
1049 if ( h == NULL ) {
1050 PyErr_NoMemory();
1051 return NULL;
1052 }
1053 HLock(h);
1054 memcpy((char *)*h, dataptr, size);
1055 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +00001056 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +00001057}
1058
Jack Jansen74a1e632000-07-14 22:37:27 +00001059#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +00001060static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001061mfs_GetDirectory(PyObject *self, PyObject *args)
Jack Jansen81f51c71995-02-20 15:45:25 +00001062{
1063 FSSpec fsdir;
1064 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +00001065 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +00001066
Jack Jansenf5c20571997-01-30 15:48:07 +00001067 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +00001068 return NULL;
1069
Jack Jansend5d5f461995-08-14 12:22:56 +00001070 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +00001071 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +00001072}
Jack Jansen9d8b96c2000-07-14 22:16:45 +00001073#endif
Jack Jansen81f51c71995-02-20 15:45:25 +00001074
Jack Jansenf5c20571997-01-30 15:48:07 +00001075static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001076mfs_FindFolder(PyObject *self, PyObject *args)
Jack Jansen2c673621995-06-18 20:05:14 +00001077{
1078 OSErr err;
1079 short where;
1080 OSType which;
1081 int create;
1082 short refnum;
1083 long dirid;
1084
Jack Jansenabd703d2001-03-15 14:38:10 +00001085 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +00001086 return NULL;
1087 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
1088 if ( err ) {
1089 PyErr_Mac(ErrorObject, err);
1090 return NULL;
1091 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001092 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +00001093}
1094
Jack Jansenf5c20571997-01-30 15:48:07 +00001095static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001096mfs_FindApplication(PyObject *self, PyObject *args)
Jack Jansen924ca851996-09-20 15:25:16 +00001097{
1098 OSErr err;
1099 OSType which;
1100 FSSpec fss;
1101
Jack Jansenf5c20571997-01-30 15:48:07 +00001102 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +00001103 return NULL;
1104 err = FindApplicationFromCreator(which, &fss);
1105 if ( err ) {
1106 PyErr_Mac(ErrorObject, err);
1107 return NULL;
1108 }
Jack Jansenf5c20571997-01-30 15:48:07 +00001109 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +00001110}
1111
Jack Jansenf5c20571997-01-30 15:48:07 +00001112static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001113mfs_FInfo(PyObject *self, PyObject *args)
Jack Jansen3d185931995-08-07 14:04:10 +00001114{
Jack Jansenf5c20571997-01-30 15:48:07 +00001115 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +00001116}
1117
Jack Jansend9936481997-06-16 14:31:38 +00001118static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +00001119mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args)
Jack Jansend9936481997-06-16 14:31:38 +00001120{
1121 OSErr err;
1122 char *fullpath;
1123 int fullpathlen;
1124 AliasHandle alias;
1125 Str32 zonename;
1126 Str31 servername;
1127
1128 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
1129 return NULL;
1130 zonename[0] = 0;
1131 servername[0] = 0;
1132 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
1133 servername, &alias);
1134 if ( err ) {
1135 PyErr_Mac(ErrorObject, err);
1136 return NULL;
1137 }
1138 return (PyObject *)newmfsaobject(alias);
1139}
1140
1141
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001142/* List of methods defined in the module */
1143
Jack Jansenf5c20571997-01-30 15:48:07 +00001144static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +00001145 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +00001146#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +00001147 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +00001148 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +00001149 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +00001150 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +00001151 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +00001152#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +00001153 {"FSSpec", mfs_FSSpec, 1},
Jack Jansen4e566ab2001-07-08 22:07:23 +00001154 {"FSRef", mfs_FSRef, 1},
Jack Jansenc889b761995-02-13 12:00:46 +00001155 {"RawFSSpec", mfs_RawFSSpec, 1},
1156 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +00001157 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +00001158 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +00001159 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +00001160 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001161
1162 {NULL, NULL} /* sentinel */
1163};
1164
Jack Jansen4e566ab2001-07-08 22:07:23 +00001165/*
1166** Convert a Python object to an FSSpec.
1167** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1168** (vrefnum, dirid, path).
1169*/
1170int
1171PyMac_GetFSRef(PyObject *v, FSRef *fsr)
1172{
Jack Jansencbed91b2001-08-03 13:31:36 +00001173#if TARGET_API_MAC_OS8
Jack Jansen2bf52da2001-11-06 15:57:26 +00001174 PyErr_SetString(PyExc_TypeError, "FSRef objects not supported on this platform");
Jack Jansencbed91b2001-08-03 13:31:36 +00001175 return 0;
1176#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001177 /* If it's an FSRef we're also okay. */
1178 if (_mfs_GetFSRefFromFSRef(v, fsr))
1179 return 1;
1180 /* first check whether it already is an FSSpec */
1181 if ( _mfs_GetFSRefFromFSSpec(v, fsr) )
1182 return 1;
1183 if ( PyString_Check(v) ) {
Jack Jansena5bca572001-08-03 15:39:27 +00001184#if TARGET_API_MAC_OSX
1185 OSStatus err;
1186 if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
1187 PyErr_Mac(ErrorObject, err);
1188 return 0;
1189 }
1190 return 1;
1191#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001192 PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform");
1193 return 0;
Jack Jansena5bca572001-08-03 15:39:27 +00001194#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001195 }
1196 PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1197 return 0;
Jack Jansencbed91b2001-08-03 13:31:36 +00001198#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001199}
1200
1201/* Convert FSSpec to PyObject */
1202PyObject *PyMac_BuildFSRef(FSRef *v)
1203{
Jack Jansencbed91b2001-08-03 13:31:36 +00001204#if TARGET_API_MAC_OS8
1205 return NULL;
1206#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001207 return (PyObject *)newmfsrobject(v);
Jack Jansencbed91b2001-08-03 13:31:36 +00001208#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001209}
1210
1211/*
1212** Convert a Python object to an FSRef.
1213** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1214*/
1215int
1216PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
1217{
1218 Str255 path;
1219 short refnum;
1220 long parid;
1221 OSErr err;
1222
1223 /* first check whether it already is an FSSpec */
1224 if ( _mfs_GetFSSpecFromFSSpec(v, fs) )
1225 return 1;
1226 /* If it's an FSRef we're also okay. */
1227 if (_mfs_GetFSSpecFromFSRef(v, fs))
1228 return 1;
1229 if ( PyString_Check(v) ) {
Jack Jansend48b1062002-01-13 23:16:32 +00001230#if TARGET_API_MAC_OSX
1231 FSRef fsr;
1232
1233 if ( !PyMac_GetFSRef(v, &fsr) )
1234 return 0;
1235 if ( FSGetCatalogInfo(&fsr, kFSCatInfoNone, NULL, NULL, fs, NULL) == noErr )
1236 return 1;
1237 return 0;
1238#else
Jack Jansen4e566ab2001-07-08 22:07:23 +00001239 /* It's a pathname */
1240 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
1241 return 0;
1242 refnum = 0; /* XXXX Should get CurWD here?? */
1243 parid = 0;
Jack Jansend48b1062002-01-13 23:16:32 +00001244#endif
Jack Jansen4e566ab2001-07-08 22:07:23 +00001245 } else {
1246 if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1247 &refnum, &parid, PyMac_GetStr255, &path)) {
1248 return 0;
1249 }
1250 }
1251 err = FSMakeFSSpec(refnum, parid, path, fs);
1252 if ( err && err != fnfErr ) {
1253 PyMac_Error(err);
1254 return 0;
1255 }
1256 return 1;
1257}
1258
1259/* Convert FSSpec to PyObject */
1260PyObject *PyMac_BuildFSSpec(FSSpec *v)
1261{
1262 return (PyObject *)newmfssobject(v);
1263}
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001264
Just van Rossumab57c7d2001-10-31 22:55:08 +00001265
1266/*
1267** Import the macfsn module, which will override the Standard File
1268** calls in the macfs builtin module by Navigation Services versions,
1269** if available on this machine.
1270*/
1271static void
1272PyMac_InstallNavServicesForSF(void)
1273{
Jack Jansen01a94622001-11-01 14:00:19 +00001274#ifndef TARGET_API_MAC_OSX
Just van Rossumab57c7d2001-10-31 22:55:08 +00001275 if ( !PyMac_options.nonavservice ) {
Jack Jansen01a94622001-11-01 14:00:19 +00001276#endif
Just van Rossumab57c7d2001-10-31 22:55:08 +00001277 PyObject *m = PyImport_ImportModule("macfsn");
1278
1279 if ( m == NULL ) {
1280 PySys_WriteStderr("'import macfsn' failed; ");
1281 if (Py_VerboseFlag) {
1282 PySys_WriteStderr("traceback:\n");
1283 PyErr_Print();
1284 }
1285 else {
1286 PySys_WriteStderr("use -v for traceback\n");
1287 }
1288 PyErr_Clear();
1289 }
Jack Jansen01a94622001-11-01 14:00:19 +00001290#ifndef TARGET_API_MAC_OSX
Just van Rossumab57c7d2001-10-31 22:55:08 +00001291 }
Jack Jansen01a94622001-11-01 14:00:19 +00001292#endif
Just van Rossumab57c7d2001-10-31 22:55:08 +00001293}
1294
1295
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001296/* Initialization function for the module (*must* be called initmacfs) */
1297
1298void
Jack Jansendeefbe52001-08-08 13:46:49 +00001299initmacfs(void)
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001300{
Jack Jansenf5c20571997-01-30 15:48:07 +00001301 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001302
Jack Jansen45900492001-08-06 15:32:30 +00001303 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec);
Jack Jansenfabd00f2001-09-01 23:39:58 +00001304 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef, PyMac_GetFSRef);
1305 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec *, PyMac_BuildFSSpec);
1306 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef *, PyMac_BuildFSRef);
Jack Jansena5bca572001-08-03 15:39:27 +00001307
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001308 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +00001309 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001310
1311 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +00001312 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +00001313 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +00001314 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001315
Jack Jansena755e681997-09-20 17:40:22 +00001316 Mfsatype.ob_type = &PyType_Type;
1317 Py_INCREF(&Mfsatype);
1318 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
1319 Mfsstype.ob_type = &PyType_Type;
1320 Py_INCREF(&Mfsstype);
1321 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
1322 Mfsitype.ob_type = &PyType_Type;
1323 Py_INCREF(&Mfsitype);
1324 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Just van Rossumab57c7d2001-10-31 22:55:08 +00001325
1326 PyMac_InstallNavServicesForSF();
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001327}