blob: c16bed0029af203e3859621190c57df6240d9497 [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"
27
Jack Jansen6143d532001-05-19 12:34:59 +000028#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumbecdbec1995-02-14 01:27:24 +000029#include <Memory.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000030#include <Files.h>
Jack Jansen2c673621995-06-18 20:05:14 +000031#include <Folders.h>
Jack Jansen84fa5ec1995-01-18 14:04:40 +000032#include <StandardFile.h>
33#include <Aliases.h>
Jack Jansend99d2831996-07-22 15:26:01 +000034#include <LowMem.h>
Jack Jansen6143d532001-05-19 12:34:59 +000035#else
36#include <Carbon/Carbon.h>
37#endif
Jack Jansen84fa5ec1995-01-18 14:04:40 +000038
Jack Jansen924ca851996-09-20 15:25:16 +000039#include "getapplbycreator.h"
Jack Jansen84fa5ec1995-01-18 14:04:40 +000040
Jack Jansen9ae898b2000-07-11 21:16:03 +000041/* Should this be in macglue.h? */
42extern FSSpec *mfs_GetFSSpecFSSpec(PyObject *);
43
Jack Jansenf5c20571997-01-30 15:48:07 +000044static PyObject *ErrorObject;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000045
46/* ----------------------------------------------------- */
Jack Jansen17ba43f1995-01-26 16:22:07 +000047/* Declarations for objects of type Alias */
48
49typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000050 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000051 AliasHandle alias;
52} mfsaobject;
53
Jack Jansenf5c20571997-01-30 15:48:07 +000054staticforward PyTypeObject Mfsatype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000055
56#define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
57
58/* ---------------------------------------------------------------- */
59/* Declarations for objects of type FSSpec */
60
61typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000062 PyObject_HEAD
Jack Jansen17ba43f1995-01-26 16:22:07 +000063 FSSpec fsspec;
64} mfssobject;
65
Jack Jansenf5c20571997-01-30 15:48:07 +000066staticforward PyTypeObject Mfsstype;
Jack Jansen17ba43f1995-01-26 16:22:07 +000067
68#define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
69
Guido van Rossumefd97671995-01-26 22:56:16 +000070
Jack Jansen3d185931995-08-07 14:04:10 +000071/* ---------------------------------------------------------------- */
72/* Declarations for objects of type FInfo */
73
74typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000075 PyObject_HEAD
Jack Jansen3d185931995-08-07 14:04:10 +000076 FInfo finfo;
77} mfsiobject;
78
Jack Jansenf5c20571997-01-30 15:48:07 +000079staticforward PyTypeObject Mfsitype;
Jack Jansen3d185931995-08-07 14:04:10 +000080
81#define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
82
83
Guido van Rossumefd97671995-01-26 22:56:16 +000084mfssobject *newmfssobject(FSSpec *fss); /* Forward */
85
Jack Jansen17ba43f1995-01-26 16:22:07 +000086/* ---------------------------------------------------------------- */
Jack Jansen84fa5ec1995-01-18 14:04:40 +000087
Jack Jansenf5c20571997-01-30 15:48:07 +000088static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +000089mfsa_Resolve(self, args)
90 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +000091 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000092{
Jack Jansen17ba43f1995-01-26 16:22:07 +000093 FSSpec from, *fromp, result;
94 Boolean changed;
95 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +000096
Jack Jansen17ba43f1995-01-26 16:22:07 +000097 from.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +000098 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from))
Jack Jansen84fa5ec1995-01-18 14:04:40 +000099 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000100 if (from.name[0] )
101 fromp = &from;
102 else
103 fromp = NULL;
104 err = ResolveAlias(fromp, self->alias, &result, &changed);
Jack Jansend9936481997-06-16 14:31:38 +0000105 if ( err && err != fnfErr ) {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000106 PyErr_Mac(ErrorObject, err);
107 return NULL;
108 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000109 return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000110}
111
Jack Jansenf5c20571997-01-30 15:48:07 +0000112static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000113mfsa_GetInfo(self, args)
114 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000115 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000116{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000117 Str63 value;
118 int i;
119 OSErr err;
120
Jack Jansenf5c20571997-01-30 15:48:07 +0000121 if (!PyArg_ParseTuple(args, "i", &i))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000122 return NULL;
123 err = GetAliasInfo(self->alias, (AliasInfoType)i, value);
124 if ( err ) {
125 PyErr_Mac(ErrorObject, err);
126 return 0;
127 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000128 return PyString_FromStringAndSize((char *)&value[1], value[0]);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000129}
130
Jack Jansenf5c20571997-01-30 15:48:07 +0000131static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000132mfsa_Update(self, args)
133 mfsaobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000134 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000135{
136 FSSpec target, fromfile, *fromfilep;
137 OSErr err;
138 Boolean changed;
139
140 fromfile.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000141 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target,
Jack Jansen17ba43f1995-01-26 16:22:07 +0000142 PyMac_GetFSSpec, &fromfile))
143 return NULL;
144 if ( fromfile.name[0] )
145 fromfilep = &fromfile;
146 else
147 fromfilep = NULL;
148 err = UpdateAlias(fromfilep, &target, self->alias, &changed);
149 if ( err ) {
150 PyErr_Mac(ErrorObject, err);
151 return 0;
152 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 return Py_BuildValue("i", (int)changed);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000154}
155
Jack Jansenf5c20571997-01-30 15:48:07 +0000156static struct PyMethodDef mfsa_methods[] = {
157 {"Resolve", (PyCFunction)mfsa_Resolve, 1},
158 {"GetInfo", (PyCFunction)mfsa_GetInfo, 1},
159 {"Update", (PyCFunction)mfsa_Update, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000160
161 {NULL, NULL} /* sentinel */
162};
163
164/* ---------- */
165
Jack Jansenf5c20571997-01-30 15:48:07 +0000166static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000167mfsa_getattr(self, name)
168 mfsaobject *self;
169 char *name;
170{
Jack Jansenc889b761995-02-13 12:00:46 +0000171 if ( strcmp(name, "data") == 0 ) {
172 int size;
173 PyObject *rv;
174
175 size = GetHandleSize((Handle)self->alias);
176 HLock((Handle)self->alias);
177 rv = PyString_FromStringAndSize(*(Handle)self->alias, size);
178 HUnlock((Handle)self->alias);
179 return rv;
180 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000181 return Py_FindMethod(mfsa_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000182}
183
Jack Jansen9ae898b2000-07-11 21:16:03 +0000184static mfsaobject *
185newmfsaobject(AliasHandle alias)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000186{
187 mfsaobject *self;
188
Jack Jansenf5c20571997-01-30 15:48:07 +0000189 self = PyObject_NEW(mfsaobject, &Mfsatype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000190 if (self == NULL)
191 return NULL;
192 self->alias = alias;
193 return self;
194}
195
196
197static void
198mfsa_dealloc(self)
199 mfsaobject *self;
200{
201#if 0
202 if ( self->alias ) {
203 should we do something here?
204 }
205#endif
206
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000208}
209
Jack Jansenf5c20571997-01-30 15:48:07 +0000210statichere PyTypeObject Mfsatype = {
211 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000212 0, /*ob_size*/
213 "Alias", /*tp_name*/
214 sizeof(mfsaobject), /*tp_basicsize*/
215 0, /*tp_itemsize*/
216 /* methods */
217 (destructor)mfsa_dealloc, /*tp_dealloc*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000218 (printfunc)0, /*tp_print*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000219 (getattrfunc)mfsa_getattr, /*tp_getattr*/
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000220 (setattrfunc)0, /*tp_setattr*/
221 (cmpfunc)0, /*tp_compare*/
222 (reprfunc)0, /*tp_repr*/
223 0, /*tp_as_number*/
224 0, /*tp_as_sequence*/
225 0, /*tp_as_mapping*/
226 (hashfunc)0, /*tp_hash*/
Jack Jansen17ba43f1995-01-26 16:22:07 +0000227};
228
229/* End of code for Alias objects */
230/* -------------------------------------------------------- */
231
Jack Jansen3d185931995-08-07 14:04:10 +0000232/* ---------------------------------------------------------------- */
233
Jack Jansenf5c20571997-01-30 15:48:07 +0000234static struct PyMethodDef mfsi_methods[] = {
Jack Jansen3d185931995-08-07 14:04:10 +0000235
236 {NULL, NULL} /* sentinel */
237};
238
239/* ---------- */
240
241static mfsiobject *
242newmfsiobject()
243{
244 mfsiobject *self;
245
Jack Jansenf5c20571997-01-30 15:48:07 +0000246 self = PyObject_NEW(mfsiobject, &Mfsitype);
Jack Jansen3d185931995-08-07 14:04:10 +0000247 if (self == NULL)
248 return NULL;
249 memset((char *)&self->finfo, '\0', sizeof(self->finfo));
250 return self;
251}
252
253static void
254mfsi_dealloc(self)
255 mfsiobject *self;
256{
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 PyMem_DEL(self);
Jack Jansen3d185931995-08-07 14:04:10 +0000258}
259
Jack Jansenf5c20571997-01-30 15:48:07 +0000260static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000261mfsi_getattr(self, name)
262 mfsiobject *self;
263 char *name;
264{
Jack Jansen3d185931995-08-07 14:04:10 +0000265 if ( strcmp(name, "Type") == 0 )
266 return PyMac_BuildOSType(self->finfo.fdType);
267 else if ( strcmp(name, "Creator") == 0 )
268 return PyMac_BuildOSType(self->finfo.fdCreator);
269 else if ( strcmp(name, "Flags") == 0 )
270 return Py_BuildValue("i", (int)self->finfo.fdFlags);
271 else if ( strcmp(name, "Location") == 0 )
272 return PyMac_BuildPoint(self->finfo.fdLocation);
273 else if ( strcmp(name, "Fldr") == 0 )
274 return Py_BuildValue("i", (int)self->finfo.fdFldr);
275 else
Jack Jansenf5c20571997-01-30 15:48:07 +0000276 return Py_FindMethod(mfsi_methods, (PyObject *)self, name);
Jack Jansen3d185931995-08-07 14:04:10 +0000277}
278
279
280static int
281mfsi_setattr(self, name, v)
282 mfsiobject *self;
283 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000284 PyObject *v;
Jack Jansen3d185931995-08-07 14:04:10 +0000285{
286 int rv;
287 int i;
288
289 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000290 PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000291 return -1;
292 }
293 if ( strcmp(name, "Type") == 0 )
294 rv = PyMac_GetOSType(v, &self->finfo.fdType);
295 else if ( strcmp(name, "Creator") == 0 )
296 rv = PyMac_GetOSType(v, &self->finfo.fdCreator);
297 else if ( strcmp(name, "Flags") == 0 ) {
298 rv = PyArg_Parse(v, "i", &i);
299 self->finfo.fdFlags = (short)i;
300 } else if ( strcmp(name, "Location") == 0 )
301 rv = PyMac_GetPoint(v, &self->finfo.fdLocation);
302 else if ( strcmp(name, "Fldr") == 0 ) {
303 rv = PyArg_Parse(v, "i", &i);
304 self->finfo.fdFldr = (short)i;
305 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000306 PyErr_SetString(PyExc_AttributeError, "No such attribute");
Jack Jansen3d185931995-08-07 14:04:10 +0000307 return -1;
308 }
309 if (rv)
310 return 0;
311 return -1;
312}
313
314
Jack Jansenf5c20571997-01-30 15:48:07 +0000315static PyTypeObject Mfsitype = {
316 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen3d185931995-08-07 14:04:10 +0000317 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000318 "FInfo", /*tp_name*/
Jack Jansen3d185931995-08-07 14:04:10 +0000319 sizeof(mfsiobject), /*tp_basicsize*/
320 0, /*tp_itemsize*/
321 /* methods */
322 (destructor)mfsi_dealloc, /*tp_dealloc*/
323 (printfunc)0, /*tp_print*/
324 (getattrfunc)mfsi_getattr, /*tp_getattr*/
325 (setattrfunc)mfsi_setattr, /*tp_setattr*/
326 (cmpfunc)0, /*tp_compare*/
327 (reprfunc)0, /*tp_repr*/
328 0, /*tp_as_number*/
329 0, /*tp_as_sequence*/
330 0, /*tp_as_mapping*/
331 (hashfunc)0, /*tp_hash*/
332};
333
334/* End of code for FInfo object objects */
335/* -------------------------------------------------------- */
336
337
Jack Jansen17ba43f1995-01-26 16:22:07 +0000338/*
339** Helper routine for other modules: return an FSSpec * if the
340** object is a python fsspec object, else NULL
341*/
342FSSpec *
Jack Jansen9ae898b2000-07-11 21:16:03 +0000343mfs_GetFSSpecFSSpec(PyObject *self)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000344{
345 if ( is_mfssobject(self) )
346 return &((mfssobject *)self)->fsspec;
347 return NULL;
348}
349
Jack Jansen0bdf9791996-09-15 22:11:25 +0000350/*
351** Two generally useful routines
352*/
353static OSErr
354PyMac_GetFileDates(fss, crdat, mddat, bkdat)
355 FSSpec *fss;
356 unsigned long *crdat, *mddat, *bkdat;
357{
358 CInfoPBRec pb;
359 OSErr error;
360
361 pb.dirInfo.ioNamePtr = fss->name;
362 pb.dirInfo.ioFDirIndex = 0;
363 pb.dirInfo.ioVRefNum = fss->vRefNum;
364 pb.dirInfo.ioDrDirID = fss->parID;
365 error = PBGetCatInfoSync(&pb);
366 if ( error ) return error;
367 *crdat = pb.hFileInfo.ioFlCrDat;
368 *mddat = pb.hFileInfo.ioFlMdDat;
369 *bkdat = pb.hFileInfo.ioFlBkDat;
370 return 0;
371}
372
373static OSErr
374PyMac_SetFileDates(fss, crdat, mddat, bkdat)
375 FSSpec *fss;
376 unsigned long crdat, mddat, bkdat;
377{
378 CInfoPBRec pb;
379 OSErr error;
380
381 pb.dirInfo.ioNamePtr = fss->name;
382 pb.dirInfo.ioFDirIndex = 0;
383 pb.dirInfo.ioVRefNum = fss->vRefNum;
384 pb.dirInfo.ioDrDirID = fss->parID;
385 error = PBGetCatInfoSync(&pb);
386 if ( error ) return error;
387 pb.dirInfo.ioNamePtr = fss->name;
388 pb.dirInfo.ioFDirIndex = 0;
389 pb.dirInfo.ioVRefNum = fss->vRefNum;
390 pb.dirInfo.ioDrDirID = fss->parID;
391 pb.hFileInfo.ioFlCrDat = crdat;
392 pb.hFileInfo.ioFlMdDat = mddat;
393 pb.hFileInfo.ioFlBkDat = bkdat;
394 error = PBSetCatInfoSync(&pb);
395 return error;
396}
397
Jack Jansenf5c20571997-01-30 15:48:07 +0000398static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000399mfss_as_pathname(self, args)
400 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000401 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000402{
403 char strbuf[257];
404 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000405
Jack Jansenf5c20571997-01-30 15:48:07 +0000406 if (!PyArg_ParseTuple(args, ""))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000407 return NULL;
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000408 err = PyMac_GetFullPath(&self->fsspec, strbuf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000409 if ( err ) {
410 PyErr_Mac(ErrorObject, err);
411 return NULL;
412 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000413 return PyString_FromString(strbuf);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000414}
415
Jack Jansenf5c20571997-01-30 15:48:07 +0000416static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000417mfss_as_tuple(self, args)
418 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000419 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000420{
Jack Jansenf5c20571997-01-30 15:48:07 +0000421 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000422 return NULL;
423 return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID,
424 &self->fsspec.name[1], self->fsspec.name[0]);
425}
426
Jack Jansenf5c20571997-01-30 15:48:07 +0000427static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000428mfss_NewAlias(self, args)
429 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000430 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000431{
432 FSSpec src, *srcp;
433 OSErr err;
434 AliasHandle alias;
435
436 src.name[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000437 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000438 return NULL;
439 if ( src.name[0] )
440 srcp = &src;
441 else
442 srcp = NULL;
443 err = NewAlias(srcp, &self->fsspec, &alias);
444 if ( err ) {
445 PyErr_Mac(ErrorObject, err);
446 return NULL;
447 }
448
Jack Jansenf5c20571997-01-30 15:48:07 +0000449 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000450}
451
Jack Jansenf5c20571997-01-30 15:48:07 +0000452static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000453mfss_NewAliasMinimal(self, args)
454 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000455 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000456{
457 OSErr err;
458 AliasHandle alias;
459
Jack Jansenf5c20571997-01-30 15:48:07 +0000460 if (!PyArg_ParseTuple(args, ""))
Jack Jansen17ba43f1995-01-26 16:22:07 +0000461 return NULL;
462 err = NewAliasMinimal(&self->fsspec, &alias);
463 if ( err ) {
464 PyErr_Mac(ErrorObject, err);
465 return NULL;
466 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000467 return (PyObject *)newmfsaobject(alias);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000468}
469
Jack Jansen3d185931995-08-07 14:04:10 +0000470/* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
Jack Jansenf5c20571997-01-30 15:48:07 +0000471static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000472mfss_GetCreatorType(self, args)
473 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000475{
476 OSErr err;
477 FInfo info;
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479 if (!PyArg_ParseTuple(args, ""))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000480 return NULL;
481 err = FSpGetFInfo(&self->fsspec, &info);
482 if ( err ) {
483 PyErr_Mac(ErrorObject, err);
484 return NULL;
485 }
486 return Py_BuildValue("(O&O&)",
487 PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType);
488}
489
Jack Jansenf5c20571997-01-30 15:48:07 +0000490static PyObject *
Jack Jansen8828fcf1995-02-02 14:23:52 +0000491mfss_SetCreatorType(self, args)
492 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000493 PyObject *args;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000494{
495 OSErr err;
496 OSType creator, type;
497 FInfo info;
498
Jack Jansenf5c20571997-01-30 15:48:07 +0000499 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Jack Jansen8828fcf1995-02-02 14:23:52 +0000500 return NULL;
501 err = FSpGetFInfo(&self->fsspec, &info);
502 if ( err ) {
503 PyErr_Mac(ErrorObject, err);
504 return NULL;
505 }
506 info.fdType = type;
507 info.fdCreator = creator;
508 err = FSpSetFInfo(&self->fsspec, &info);
509 if ( err ) {
510 PyErr_Mac(ErrorObject, err);
511 return NULL;
512 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000513 Py_INCREF(Py_None);
514 return Py_None;
Jack Jansen8828fcf1995-02-02 14:23:52 +0000515}
516
Jack Jansenf5c20571997-01-30 15:48:07 +0000517static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000518mfss_GetFInfo(self, args)
519 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000520 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000521{
522 OSErr err;
523 mfsiobject *fip;
524
525
Jack Jansenf5c20571997-01-30 15:48:07 +0000526 if (!PyArg_ParseTuple(args, ""))
Jack Jansen3d185931995-08-07 14:04:10 +0000527 return NULL;
528 if ( (fip=newmfsiobject()) == NULL )
529 return NULL;
530 err = FSpGetFInfo(&self->fsspec, &fip->finfo);
531 if ( err ) {
532 PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000533 Py_DECREF(fip);
Jack Jansen3d185931995-08-07 14:04:10 +0000534 return NULL;
535 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000536 return (PyObject *)fip;
Jack Jansen3d185931995-08-07 14:04:10 +0000537}
538
Jack Jansenf5c20571997-01-30 15:48:07 +0000539static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000540mfss_SetFInfo(self, args)
541 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000542 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000543{
544 OSErr err;
545 mfsiobject *fip;
546
Jack Jansenf5c20571997-01-30 15:48:07 +0000547 if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip))
Jack Jansen3d185931995-08-07 14:04:10 +0000548 return NULL;
549 err = FSpSetFInfo(&self->fsspec, &fip->finfo);
550 if ( err ) {
551 PyErr_Mac(ErrorObject, err);
552 return NULL;
553 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000554 Py_INCREF(Py_None);
555 return Py_None;
Jack Jansen3d185931995-08-07 14:04:10 +0000556}
557
Jack Jansenf5c20571997-01-30 15:48:07 +0000558static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000559mfss_GetDates(self, args)
560 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000561 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000562{
563 OSErr err;
564 unsigned long crdat, mddat, bkdat;
565
Jack Jansenf5c20571997-01-30 15:48:07 +0000566 if (!PyArg_ParseTuple(args, ""))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000567 return NULL;
568 err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat);
569 if ( err ) {
570 PyErr_Mac(ErrorObject, err);
571 return NULL;
572 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000573 return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat);
Jack Jansen0bdf9791996-09-15 22:11:25 +0000574}
575
Jack Jansenf5c20571997-01-30 15:48:07 +0000576static PyObject *
Jack Jansen0bdf9791996-09-15 22:11:25 +0000577mfss_SetDates(self, args)
578 mfssobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 PyObject *args;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000580{
581 OSErr err;
582 double crdat, mddat, bkdat;
583
Jack Jansenf5c20571997-01-30 15:48:07 +0000584 if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat))
Jack Jansen0bdf9791996-09-15 22:11:25 +0000585 return NULL;
586 err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat,
587 (unsigned long)mddat, (unsigned long)bkdat);
588 if ( err ) {
589 PyErr_Mac(ErrorObject, err);
590 return NULL;
591 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000592 Py_INCREF(Py_None);
593 return Py_None;
Jack Jansen0bdf9791996-09-15 22:11:25 +0000594}
595
Jack Jansenf5c20571997-01-30 15:48:07 +0000596static struct PyMethodDef mfss_methods[] = {
597 {"as_pathname", (PyCFunction)mfss_as_pathname, 1},
598 {"as_tuple", (PyCFunction)mfss_as_tuple, 1},
599 {"NewAlias", (PyCFunction)mfss_NewAlias, 1},
600 {"NewAliasMinimal", (PyCFunction)mfss_NewAliasMinimal, 1},
601 {"GetCreatorType", (PyCFunction)mfss_GetCreatorType, 1},
602 {"SetCreatorType", (PyCFunction)mfss_SetCreatorType, 1},
603 {"GetFInfo", (PyCFunction)mfss_GetFInfo, 1},
604 {"SetFInfo", (PyCFunction)mfss_SetFInfo, 1},
605 {"GetDates", (PyCFunction)mfss_GetDates, 1},
606 {"SetDates", (PyCFunction)mfss_SetDates, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000607
608 {NULL, NULL} /* sentinel */
609};
610
611/* ---------- */
612
Jack Jansenf5c20571997-01-30 15:48:07 +0000613static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000614mfss_getattr(self, name)
615 mfssobject *self;
616 char *name;
617{
Jack Jansenc889b761995-02-13 12:00:46 +0000618 if ( strcmp(name, "data") == 0)
619 return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec));
Jack Jansenf5c20571997-01-30 15:48:07 +0000620 return Py_FindMethod(mfss_methods, (PyObject *)self, name);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000621}
622
623mfssobject *
624newmfssobject(fss)
625 FSSpec *fss;
626{
627 mfssobject *self;
628
Jack Jansenf5c20571997-01-30 15:48:07 +0000629 self = PyObject_NEW(mfssobject, &Mfsstype);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000630 if (self == NULL)
631 return NULL;
632 self->fsspec = *fss;
633 return self;
634}
635
636static void
637mfss_dealloc(self)
638 mfssobject *self;
639{
Jack Jansenf5c20571997-01-30 15:48:07 +0000640 PyMem_DEL(self);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000641}
642
Jack Jansenf5c20571997-01-30 15:48:07 +0000643static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000644mfss_repr(self)
645 mfssobject *self;
646{
647 char buf[512];
648
Guido van Rossumf45b53b1995-02-20 23:43:29 +0000649 sprintf(buf, "FSSpec((%d, %d, '%.*s'))",
650 self->fsspec.vRefNum,
651 self->fsspec.parID,
652 self->fsspec.name[0], self->fsspec.name+1);
Jack Jansenf5c20571997-01-30 15:48:07 +0000653 return PyString_FromString(buf);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000654}
655
656static int
657mfss_compare(v, w)
658 mfssobject *v, *w;
659{
660 int minlen;
661 int res;
662
663 if ( v->fsspec.vRefNum < w->fsspec.vRefNum ) return -1;
664 if ( v->fsspec.vRefNum > w->fsspec.vRefNum ) return 1;
665 if ( v->fsspec.parID < w->fsspec.parID ) return -1;
666 if ( v->fsspec.parID > w->fsspec.parID ) return 1;
667 minlen = v->fsspec.name[0];
668 if ( w->fsspec.name[0] < minlen ) minlen = w->fsspec.name[0];
669 res = strncmp((char *)v->fsspec.name+1, (char *)w->fsspec.name+1, minlen);
670 if ( res ) return res;
671 if ( v->fsspec.name[0] < w->fsspec.name[0] ) return -1;
672 if ( v->fsspec.name[0] > w->fsspec.name[0] ) return 1;
673 return res;
674}
675
Jack Jansenf5c20571997-01-30 15:48:07 +0000676statichere PyTypeObject Mfsstype = {
677 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansen17ba43f1995-01-26 16:22:07 +0000678 0, /*ob_size*/
679 "FSSpec", /*tp_name*/
680 sizeof(mfssobject), /*tp_basicsize*/
681 0, /*tp_itemsize*/
682 /* methods */
683 (destructor)mfss_dealloc, /*tp_dealloc*/
684 (printfunc)0, /*tp_print*/
685 (getattrfunc)mfss_getattr, /*tp_getattr*/
686 (setattrfunc)0, /*tp_setattr*/
687 (cmpfunc)mfss_compare, /*tp_compare*/
688 (reprfunc)mfss_repr, /*tp_repr*/
689 0, /*tp_as_number*/
690 0, /*tp_as_sequence*/
691 0, /*tp_as_mapping*/
692 (hashfunc)0, /*tp_hash*/
693};
694
695/* End of code for FSSpec objects */
696/* -------------------------------------------------------- */
697
Jack Jansenf5c20571997-01-30 15:48:07 +0000698static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000699mfs_ResolveAliasFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000700 PyObject *self; /* Not used */
701 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000702{
Jack Jansen17ba43f1995-01-26 16:22:07 +0000703 FSSpec fss;
704 Boolean chain = 1, isfolder, wasaliased;
705 OSErr err;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000706
Jack Jansenf5c20571997-01-30 15:48:07 +0000707 if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000708 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000709 err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased);
710 if ( err ) {
711 PyErr_Mac(ErrorObject, err);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000712 return NULL;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000713 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000714 return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000715}
716
Jack Jansen74a1e632000-07-14 22:37:27 +0000717#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000718static PyObject *
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000719mfs_StandardGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000720 PyObject *self; /* Not used */
721 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000722{
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000723 SFTypeList list;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000724 short numtypes;
725 StandardFileReply reply;
726
727 list[0] = list[1] = list[2] = list[3] = 0;
728 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000729 if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0],
Jack Jansen17ba43f1995-01-26 16:22:07 +0000730 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
731 PyMac_GetOSType, &list[3]) )
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000732 return NULL;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000733 while ( numtypes < 4 && list[numtypes] ) {
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000734 numtypes++;
735 }
Jack Jansenc2632861995-06-03 21:15:50 +0000736 if ( numtypes == 0 )
737 numtypes = -1;
Guido van Rossumb2f524a1995-01-30 08:56:06 +0000738 StandardGetFile((FileFilterUPP)0, numtypes, list, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000739 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000740}
741
Jack Jansenf5c20571997-01-30 15:48:07 +0000742static PyObject *
Jack Jansend5d5f461995-08-14 12:22:56 +0000743mfs_PromptGetFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000744 PyObject *self; /* Not used */
745 PyObject *args;
Jack Jansend5d5f461995-08-14 12:22:56 +0000746{
747 SFTypeList list;
748 short numtypes;
749 StandardFileReply reply;
750 char *prompt = NULL;
751
752 list[0] = list[1] = list[2] = list[3] = 0;
753 numtypes = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000754 if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0],
Jack Jansend5d5f461995-08-14 12:22:56 +0000755 PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2],
756 PyMac_GetOSType, &list[3]) )
757 return NULL;
758 while ( numtypes < 4 && list[numtypes] ) {
759 numtypes++;
760 }
761 if ( numtypes == 0 )
762 numtypes = -1;
763 PyMac_PromptGetFile(numtypes, list, &reply, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000764 return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansend5d5f461995-08-14 12:22:56 +0000765}
766
Jack Jansenf5c20571997-01-30 15:48:07 +0000767static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000768mfs_StandardPutFile(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000769 PyObject *self; /* Not used */
770 PyObject *args;
Jack Jansen17ba43f1995-01-26 16:22:07 +0000771{
772 Str255 prompt, dft;
773 StandardFileReply reply;
774
775 dft[0] = 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000776 if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) )
Jack Jansen17ba43f1995-01-26 16:22:07 +0000777 return NULL;
778 StandardPutFile(prompt, dft, &reply);
Jack Jansenf5c20571997-01-30 15:48:07 +0000779 return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood);
Jack Jansen17ba43f1995-01-26 16:22:07 +0000780}
781
Jack Jansend99d2831996-07-22 15:26:01 +0000782/*
783** Set initial directory for file dialogs */
Jack Jansenf5c20571997-01-30 15:48:07 +0000784static PyObject *
Jack Jansend99d2831996-07-22 15:26:01 +0000785mfs_SetFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000786 PyObject *self;
787 PyObject *args;
Jack Jansend99d2831996-07-22 15:26:01 +0000788{
789 FSSpec spec;
790 FSSpec ospec;
791 short orefnum;
792 long oparid;
793
794 /* Get old values */
795 orefnum = -LMGetSFSaveDisk();
796 oparid = LMGetCurDirStore();
797 (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec);
798
799 /* Go to working directory by default */
800 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec);
Jack Jansenf5c20571997-01-30 15:48:07 +0000801 if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec))
Jack Jansend99d2831996-07-22 15:26:01 +0000802 return NULL;
803 /* Set standard-file working directory */
804 LMSetSFSaveDisk(-spec.vRefNum);
805 LMSetCurDirStore(spec.parID);
Jack Jansenf5c20571997-01-30 15:48:07 +0000806 return (PyObject *)newmfssobject(&ospec);
Jack Jansend99d2831996-07-22 15:26:01 +0000807}
Jack Jansene79dc762000-06-02 21:35:07 +0000808#endif
Jack Jansend99d2831996-07-22 15:26:01 +0000809
Jack Jansenf5c20571997-01-30 15:48:07 +0000810static PyObject *
Jack Jansen17ba43f1995-01-26 16:22:07 +0000811mfs_FSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000812 PyObject *self; /* Not used */
813 PyObject *args;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000814{
815 FSSpec fss;
816
Jack Jansenf5c20571997-01-30 15:48:07 +0000817 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000818 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000819 return (PyObject *)newmfssobject(&fss);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000820}
821
Jack Jansenf5c20571997-01-30 15:48:07 +0000822static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +0000823mfs_RawFSSpec(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000824 PyObject *self; /* Not used */
825 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +0000826{
827 FSSpec *fssp;
828 int size;
829
Jack Janseneeccca91997-05-07 15:46:31 +0000830 if (!PyArg_ParseTuple(args, "s#", &fssp, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000831 return NULL;
832 if ( size != sizeof(FSSpec) ) {
833 PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record");
834 return NULL;
835 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000836 return (PyObject *)newmfssobject(fssp);
Jack Jansenc889b761995-02-13 12:00:46 +0000837}
838
Jack Jansenf5c20571997-01-30 15:48:07 +0000839static PyObject *
Jack Jansenc889b761995-02-13 12:00:46 +0000840mfs_RawAlias(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000841 PyObject *self; /* Not used */
842 PyObject *args;
Jack Jansenc889b761995-02-13 12:00:46 +0000843{
844 char *dataptr;
845 Handle h;
846 int size;
847
Jack Janseneeccca91997-05-07 15:46:31 +0000848 if (!PyArg_ParseTuple(args, "s#", &dataptr, &size))
Jack Jansenc889b761995-02-13 12:00:46 +0000849 return NULL;
850 h = NewHandle(size);
851 if ( h == NULL ) {
852 PyErr_NoMemory();
853 return NULL;
854 }
855 HLock(h);
856 memcpy((char *)*h, dataptr, size);
857 HUnlock(h);
Jack Jansenf5c20571997-01-30 15:48:07 +0000858 return (PyObject *)newmfsaobject((AliasHandle)h);
Jack Jansenc889b761995-02-13 12:00:46 +0000859}
860
Jack Jansen74a1e632000-07-14 22:37:27 +0000861#if !TARGET_API_MAC_CARBON
Jack Jansenf5c20571997-01-30 15:48:07 +0000862static PyObject *
Jack Jansen81f51c71995-02-20 15:45:25 +0000863mfs_GetDirectory(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000864 PyObject *self; /* Not used */
865 PyObject *args;
Jack Jansen81f51c71995-02-20 15:45:25 +0000866{
867 FSSpec fsdir;
868 int ok;
Jack Jansend5d5f461995-08-14 12:22:56 +0000869 char *prompt = NULL;
Jack Jansen81f51c71995-02-20 15:45:25 +0000870
Jack Jansenf5c20571997-01-30 15:48:07 +0000871 if (!PyArg_ParseTuple(args, "|s", &prompt) )
Jack Jansen81f51c71995-02-20 15:45:25 +0000872 return NULL;
873
Jack Jansend5d5f461995-08-14 12:22:56 +0000874 ok = PyMac_GetDirectory(&fsdir, prompt);
Jack Jansenf5c20571997-01-30 15:48:07 +0000875 return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok);
Jack Jansen81f51c71995-02-20 15:45:25 +0000876}
Jack Jansen9d8b96c2000-07-14 22:16:45 +0000877#endif
Jack Jansen81f51c71995-02-20 15:45:25 +0000878
Jack Jansenf5c20571997-01-30 15:48:07 +0000879static PyObject *
Jack Jansen2c673621995-06-18 20:05:14 +0000880mfs_FindFolder(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000881 PyObject *self; /* Not used */
882 PyObject *args;
Jack Jansen2c673621995-06-18 20:05:14 +0000883{
884 OSErr err;
885 short where;
886 OSType which;
887 int create;
888 short refnum;
889 long dirid;
890
Jack Jansenabd703d2001-03-15 14:38:10 +0000891 if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) )
Jack Jansen2c673621995-06-18 20:05:14 +0000892 return NULL;
893 err = FindFolder(where, which, (Boolean)create, &refnum, &dirid);
894 if ( err ) {
895 PyErr_Mac(ErrorObject, err);
896 return NULL;
897 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000898 return Py_BuildValue("(ii)", refnum, dirid);
Jack Jansen2c673621995-06-18 20:05:14 +0000899}
900
Jack Jansenf5c20571997-01-30 15:48:07 +0000901static PyObject *
Jack Jansen924ca851996-09-20 15:25:16 +0000902mfs_FindApplication(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000903 PyObject *self; /* Not used */
904 PyObject *args;
Jack Jansen924ca851996-09-20 15:25:16 +0000905{
906 OSErr err;
907 OSType which;
908 FSSpec fss;
909
Jack Jansenf5c20571997-01-30 15:48:07 +0000910 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) )
Jack Jansen924ca851996-09-20 15:25:16 +0000911 return NULL;
912 err = FindApplicationFromCreator(which, &fss);
913 if ( err ) {
914 PyErr_Mac(ErrorObject, err);
915 return NULL;
916 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000917 return (PyObject *)newmfssobject(&fss);
Jack Jansen924ca851996-09-20 15:25:16 +0000918}
919
Jack Jansenf5c20571997-01-30 15:48:07 +0000920static PyObject *
Jack Jansen3d185931995-08-07 14:04:10 +0000921mfs_FInfo(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000922 PyObject *self;
923 PyObject *args;
Jack Jansen3d185931995-08-07 14:04:10 +0000924{
Jack Jansenf5c20571997-01-30 15:48:07 +0000925 return (PyObject *)newmfsiobject();
Jack Jansen3d185931995-08-07 14:04:10 +0000926}
927
Jack Jansend9936481997-06-16 14:31:38 +0000928static PyObject *
929mfs_NewAliasMinimalFromFullPath(self, args)
930 PyObject *self; /* Not used */
931 PyObject *args;
932{
933 OSErr err;
934 char *fullpath;
935 int fullpathlen;
936 AliasHandle alias;
937 Str32 zonename;
938 Str31 servername;
939
940 if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) )
941 return NULL;
942 zonename[0] = 0;
943 servername[0] = 0;
944 err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename,
945 servername, &alias);
946 if ( err ) {
947 PyErr_Mac(ErrorObject, err);
948 return NULL;
949 }
950 return (PyObject *)newmfsaobject(alias);
951}
952
953
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000954/* List of methods defined in the module */
955
Jack Jansenf5c20571997-01-30 15:48:07 +0000956static struct PyMethodDef mfs_methods[] = {
Jack Jansen17ba43f1995-01-26 16:22:07 +0000957 {"ResolveAliasFile", mfs_ResolveAliasFile, 1},
Jack Jansen74a1e632000-07-14 22:37:27 +0000958#if !TARGET_API_MAC_CARBON
Jack Jansen17ba43f1995-01-26 16:22:07 +0000959 {"StandardGetFile", mfs_StandardGetFile, 1},
Jack Jansend5d5f461995-08-14 12:22:56 +0000960 {"PromptGetFile", mfs_PromptGetFile, 1},
Jack Jansen17ba43f1995-01-26 16:22:07 +0000961 {"StandardPutFile", mfs_StandardPutFile, 1},
Jack Jansen81f51c71995-02-20 15:45:25 +0000962 {"GetDirectory", mfs_GetDirectory, 1},
Jack Jansend99d2831996-07-22 15:26:01 +0000963 {"SetFolder", mfs_SetFolder, 1},
Jack Jansene79dc762000-06-02 21:35:07 +0000964#endif
Jack Jansen17ba43f1995-01-26 16:22:07 +0000965 {"FSSpec", mfs_FSSpec, 1},
Jack Jansenc889b761995-02-13 12:00:46 +0000966 {"RawFSSpec", mfs_RawFSSpec, 1},
967 {"RawAlias", mfs_RawAlias, 1},
Jack Jansen2c673621995-06-18 20:05:14 +0000968 {"FindFolder", mfs_FindFolder, 1},
Jack Jansen924ca851996-09-20 15:25:16 +0000969 {"FindApplication", mfs_FindApplication, 1},
Jack Jansen3d185931995-08-07 14:04:10 +0000970 {"FInfo", mfs_FInfo, 1},
Jack Jansend9936481997-06-16 14:31:38 +0000971 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1},
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000972
973 {NULL, NULL} /* sentinel */
974};
975
976
977/* Initialization function for the module (*must* be called initmacfs) */
978
979void
980initmacfs()
981{
Jack Jansenf5c20571997-01-30 15:48:07 +0000982 PyObject *m, *d;
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000983
984 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000985 m = Py_InitModule("macfs", mfs_methods);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000986
987 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000988 d = PyModule_GetDict(m);
Jack Jansen4377a1a2000-01-24 10:37:59 +0000989 ErrorObject = PyMac_GetOSErrException();
Jack Jansenf5c20571997-01-30 15:48:07 +0000990 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansen84fa5ec1995-01-18 14:04:40 +0000991
Jack Jansena755e681997-09-20 17:40:22 +0000992 Mfsatype.ob_type = &PyType_Type;
993 Py_INCREF(&Mfsatype);
994 PyDict_SetItemString(d, "AliasType", (PyObject *)&Mfsatype);
995 Mfsstype.ob_type = &PyType_Type;
996 Py_INCREF(&Mfsstype);
997 PyDict_SetItemString(d, "FSSpecType", (PyObject *)&Mfsstype);
998 Mfsitype.ob_type = &PyType_Type;
999 Py_INCREF(&Mfsitype);
1000 PyDict_SetItemString(d, "FInfoType", (PyObject *)&Mfsitype);
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001001 /* XXXX Add constants here */
Jack Jansen84fa5ec1995-01-18 14:04:40 +00001002}