1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
27 #include "pymactoolbox.h"
29 #ifdef WITHOUT_FRAMEWORKS
33 #include <StandardFile.h>
37 #include <Carbon/Carbon.h>
40 #include "getapplbycreator.h"
42 #ifndef TARGET_API_MAC_OSX
43 #include "pythonresources.h"
44 extern PyMac_PrefRecord PyMac_options
;
47 #ifdef USE_TOOLBOX_OBJECT_GLUE
48 extern int _PyMac_GetFSSpec(PyObject
*, FSSpec
*);
49 extern PyObject
*_PyMac_BuildFSSpec(FSSpec
*);
50 extern int _PyMac_GetFSRef(PyObject
*, FSRef
*);
51 extern PyObject
*_PyMac_BuildFSRef(FSRef
*);
52 #define PyMac_GetFSSpec _PyMac_GetFSSpec
53 #define PyMac_BuildFSSpec _PyMac_BuildFSSpec
54 #define PyMac_GetFSRef _PyMac_GetFSRef
55 #define PyMac_BuildFSRef _PyMac_BuildFSRef
57 static PyObject
*ErrorObject
;
59 #ifdef TARGET_API_MAC_OSX
60 #define PATHNAMELEN 1024
62 #define PATHNAMELEN 256
65 /* ----------------------------------------------------- */
66 /* Declarations for objects of type Alias */
73 staticforward PyTypeObject Mfsatype
;
75 #define is_mfsaobject(v) ((v)->ob_type == &Mfsatype)
77 /* ---------------------------------------------------------------- */
78 /* Declarations for objects of type FSSpec */
85 staticforward PyTypeObject Mfsstype
;
87 #define is_mfssobject(v) ((v)->ob_type == &Mfsstype)
89 /* ---------------------------------------------------------------- */
90 /* Declarations for objects of type FSRef */
97 staticforward PyTypeObject Mfsrtype
;
99 #define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype)
102 /* ---------------------------------------------------------------- */
103 /* Declarations for objects of type FInfo */
110 staticforward PyTypeObject Mfsitype
;
112 #define is_mfsiobject(v) ((v)->ob_type == &Mfsitype)
115 staticforward mfssobject
*newmfssobject(FSSpec
*fss
); /* Forward */
116 staticforward mfsrobject
*newmfsrobject(FSRef
*fsr
); /* Forward */
118 /* ---------------------------------------------------------------- */
121 mfsa_Resolve(mfsaobject
*self
, PyObject
*args
)
123 FSSpec from
, *fromp
, result
;
128 if (!PyArg_ParseTuple(args
, "|O&", PyMac_GetFSSpec
, &from
))
134 err
= ResolveAlias(fromp
, self
->alias
, &result
, &changed
);
135 if ( err
&& err
!= fnfErr
) {
136 PyErr_Mac(ErrorObject
, err
);
139 return Py_BuildValue("(Oi)", newmfssobject(&result
), (int)changed
);
143 mfsa_GetInfo(mfsaobject
*self
, PyObject
*args
)
149 if (!PyArg_ParseTuple(args
, "i", &i
))
151 err
= GetAliasInfo(self
->alias
, (AliasInfoType
)i
, value
);
153 PyErr_Mac(ErrorObject
, err
);
156 return PyString_FromStringAndSize((char *)&value
[1], value
[0]);
160 mfsa_Update(mfsaobject
*self
, PyObject
*args
)
162 FSSpec target
, fromfile
, *fromfilep
;
166 fromfile
.name
[0] = 0;
167 if (!PyArg_ParseTuple(args
, "O&|O&", PyMac_GetFSSpec
, &target
,
168 PyMac_GetFSSpec
, &fromfile
))
170 if ( fromfile
.name
[0] )
171 fromfilep
= &fromfile
;
174 err
= UpdateAlias(fromfilep
, &target
, self
->alias
, &changed
);
176 PyErr_Mac(ErrorObject
, err
);
179 return Py_BuildValue("i", (int)changed
);
182 static struct PyMethodDef mfsa_methods
[] = {
183 {"Resolve", (PyCFunction
)mfsa_Resolve
, 1},
184 {"GetInfo", (PyCFunction
)mfsa_GetInfo
, 1},
185 {"Update", (PyCFunction
)mfsa_Update
, 1},
187 {NULL
, NULL
} /* sentinel */
193 mfsa_getattr(mfsaobject
*self
, char *name
)
195 if ( strcmp(name
, "data") == 0 ) {
199 size
= GetHandleSize((Handle
)self
->alias
);
200 HLock((Handle
)self
->alias
);
201 rv
= PyString_FromStringAndSize(*(Handle
)self
->alias
, size
);
202 HUnlock((Handle
)self
->alias
);
205 return Py_FindMethod(mfsa_methods
, (PyObject
*)self
, name
);
209 newmfsaobject(AliasHandle alias
)
213 self
= PyObject_NEW(mfsaobject
, &Mfsatype
);
222 mfsa_dealloc(mfsaobject
*self
)
226 should we
do something here
?
233 statichere PyTypeObject Mfsatype
= {
234 PyObject_HEAD_INIT(&PyType_Type
)
236 "macfs.Alias", /*tp_name*/
237 sizeof(mfsaobject
), /*tp_basicsize*/
240 (destructor
)mfsa_dealloc
, /*tp_dealloc*/
241 (printfunc
)0, /*tp_print*/
242 (getattrfunc
)mfsa_getattr
, /*tp_getattr*/
243 (setattrfunc
)0, /*tp_setattr*/
244 (cmpfunc
)0, /*tp_compare*/
245 (reprfunc
)0, /*tp_repr*/
247 0, /*tp_as_sequence*/
249 (hashfunc
)0, /*tp_hash*/
252 /* End of code for Alias objects */
253 /* -------------------------------------------------------- */
255 /* ---------------------------------------------------------------- */
257 static struct PyMethodDef mfsi_methods
[] = {
259 {NULL
, NULL
} /* sentinel */
269 self
= PyObject_NEW(mfsiobject
, &Mfsitype
);
272 memset((char *)&self
->finfo
, '\0', sizeof(self
->finfo
));
277 mfsi_dealloc(mfsiobject
*self
)
283 mfsi_getattr(mfsiobject
*self
, char *name
)
285 if ( strcmp(name
, "Type") == 0 )
286 return PyMac_BuildOSType(self
->finfo
.fdType
);
287 else if ( strcmp(name
, "Creator") == 0 )
288 return PyMac_BuildOSType(self
->finfo
.fdCreator
);
289 else if ( strcmp(name
, "Flags") == 0 )
290 return Py_BuildValue("i", (int)self
->finfo
.fdFlags
);
291 else if ( strcmp(name
, "Location") == 0 )
292 return PyMac_BuildPoint(self
->finfo
.fdLocation
);
293 else if ( strcmp(name
, "Fldr") == 0 )
294 return Py_BuildValue("i", (int)self
->finfo
.fdFldr
);
295 else if ( strcmp(name
, "__members__") == 0 )
296 return Py_BuildValue("[sssss]", "Type", "Creator", "Flags", "Location", "Fldr");
298 return Py_FindMethod(mfsi_methods
, (PyObject
*)self
, name
);
303 mfsi_setattr(mfsiobject
*self
, char *name
, PyObject
*v
)
309 PyErr_SetString(PyExc_AttributeError
, "Cannot delete attribute");
312 if ( strcmp(name
, "Type") == 0 )
313 rv
= PyMac_GetOSType(v
, &self
->finfo
.fdType
);
314 else if ( strcmp(name
, "Creator") == 0 )
315 rv
= PyMac_GetOSType(v
, &self
->finfo
.fdCreator
);
316 else if ( strcmp(name
, "Flags") == 0 ) {
317 rv
= PyArg_Parse(v
, "i", &i
);
318 self
->finfo
.fdFlags
= (short)i
;
319 } else if ( strcmp(name
, "Location") == 0 )
320 rv
= PyMac_GetPoint(v
, &self
->finfo
.fdLocation
);
321 else if ( strcmp(name
, "Fldr") == 0 ) {
322 rv
= PyArg_Parse(v
, "i", &i
);
323 self
->finfo
.fdFldr
= (short)i
;
325 PyErr_SetString(PyExc_AttributeError
, "No such attribute");
334 static PyTypeObject Mfsitype
= {
335 PyObject_HEAD_INIT(&PyType_Type
)
337 "macfs.FInfo", /*tp_name*/
338 sizeof(mfsiobject
), /*tp_basicsize*/
341 (destructor
)mfsi_dealloc
, /*tp_dealloc*/
342 (printfunc
)0, /*tp_print*/
343 (getattrfunc
)mfsi_getattr
, /*tp_getattr*/
344 (setattrfunc
)mfsi_setattr
, /*tp_setattr*/
345 (cmpfunc
)0, /*tp_compare*/
346 (reprfunc
)0, /*tp_repr*/
348 0, /*tp_as_sequence*/
350 (hashfunc
)0, /*tp_hash*/
353 /* End of code for FInfo object objects */
354 /* -------------------------------------------------------- */
358 ** Helper routines for the FSRef and FSSpec creators in macglue.c
359 ** They return an FSSpec/FSRef if the Python object encapsulating
360 ** either is passed. They return a boolean success indicator.
361 ** Note that they do not set an exception on failure, they're only
365 _mfs_GetFSSpecFromFSSpec(PyObject
*self
, FSSpec
*fssp
)
367 if ( is_mfssobject(self
) ) {
368 *fssp
= ((mfssobject
*)self
)->fsspec
;
374 /* Return an FSSpec if this is an FSref */
376 _mfs_GetFSSpecFromFSRef(PyObject
*self
, FSSpec
*fssp
)
378 #if !TARGET_API_MAC_OS8
381 if ( is_mfsrobject(self
) ) {
382 fsrp
= &((mfsrobject
*)self
)->fsref
;
383 if ( FSGetCatalogInfo(&((mfsrobject
*)self
)->fsref
, kFSCatInfoNone
, NULL
, NULL
, fssp
, NULL
) == noErr
)
390 /* Return an FSRef if this is an FSRef */
392 _mfs_GetFSRefFromFSRef(PyObject
*self
, FSRef
*fsrp
)
394 #if !TARGET_API_MAC_OS8
395 if ( is_mfsrobject(self
) ) {
396 *fsrp
= ((mfsrobject
*)self
)->fsref
;
403 /* Return an FSRef if this is an FSSpec */
405 _mfs_GetFSRefFromFSSpec(PyObject
*self
, FSRef
*fsrp
)
407 #if !TARGET_API_MAC_OS8
408 if ( is_mfssobject(self
) ) {
409 if ( FSpMakeFSRef(&((mfssobject
*)self
)->fsspec
, fsrp
) == noErr
)
417 ** Two generally useful routines
420 PyMac_GetFileDates(FSSpec
*fss
, unsigned long *crdat
, unsigned long *mddat
,
421 unsigned long *bkdat
)
426 pb
.dirInfo
.ioNamePtr
= fss
->name
;
427 pb
.dirInfo
.ioFDirIndex
= 0;
428 pb
.dirInfo
.ioVRefNum
= fss
->vRefNum
;
429 pb
.dirInfo
.ioDrDirID
= fss
->parID
;
430 error
= PBGetCatInfoSync(&pb
);
431 if ( error
) return error
;
432 *crdat
= pb
.hFileInfo
.ioFlCrDat
;
433 *mddat
= pb
.hFileInfo
.ioFlMdDat
;
434 *bkdat
= pb
.hFileInfo
.ioFlBkDat
;
439 PyMac_SetFileDates(FSSpec
*fss
, unsigned long crdat
, unsigned long mddat
,
445 pb
.dirInfo
.ioNamePtr
= fss
->name
;
446 pb
.dirInfo
.ioFDirIndex
= 0;
447 pb
.dirInfo
.ioVRefNum
= fss
->vRefNum
;
448 pb
.dirInfo
.ioDrDirID
= fss
->parID
;
449 error
= PBGetCatInfoSync(&pb
);
450 if ( error
) return error
;
451 pb
.dirInfo
.ioNamePtr
= fss
->name
;
452 pb
.dirInfo
.ioFDirIndex
= 0;
453 pb
.dirInfo
.ioVRefNum
= fss
->vRefNum
;
454 pb
.dirInfo
.ioDrDirID
= fss
->parID
;
455 pb
.hFileInfo
.ioFlCrDat
= crdat
;
456 pb
.hFileInfo
.ioFlMdDat
= mddat
;
457 pb
.hFileInfo
.ioFlBkDat
= bkdat
;
458 error
= PBSetCatInfoSync(&pb
);
463 mfss_as_pathname(mfssobject
*self
, PyObject
*args
)
465 char strbuf
[PATHNAMELEN
];
468 if (!PyArg_ParseTuple(args
, ""))
470 err
= PyMac_GetFullPathname(&self
->fsspec
, strbuf
, PATHNAMELEN
);
472 PyErr_Mac(ErrorObject
, err
);
475 return PyString_FromString(strbuf
);
479 mfss_as_tuple(mfssobject
*self
, PyObject
*args
)
481 if (!PyArg_ParseTuple(args
, ""))
483 return Py_BuildValue("(iis#)", self
->fsspec
.vRefNum
, self
->fsspec
.parID
,
484 &self
->fsspec
.name
[1], self
->fsspec
.name
[0]);
488 mfss_NewAlias(mfssobject
*self
, PyObject
*args
)
495 if (!PyArg_ParseTuple(args
, "|O&", PyMac_GetFSSpec
, &src
))
501 err
= NewAlias(srcp
, &self
->fsspec
, &alias
);
503 PyErr_Mac(ErrorObject
, err
);
507 return (PyObject
*)newmfsaobject(alias
);
511 mfss_NewAliasMinimal(mfssobject
*self
, PyObject
*args
)
516 if (!PyArg_ParseTuple(args
, ""))
518 err
= NewAliasMinimal(&self
->fsspec
, &alias
);
520 PyErr_Mac(ErrorObject
, err
);
523 return (PyObject
*)newmfsaobject(alias
);
527 mfss_FSpMakeFSRef(mfssobject
*self
, PyObject
*args
)
529 #if TARGET_API_MAC_OS8
530 PyErr_SetString(PyExc_NotImplementedError
, "FSRef objects not supported on this platform");
536 if (!PyArg_ParseTuple(args
, ""))
538 err
= FSpMakeFSRef(&self
->fsspec
, &fsref
);
540 PyErr_Mac(ErrorObject
, err
);
543 return (PyObject
*)newmfsrobject(&fsref
);
547 /* XXXX These routines should be replaced by a wrapper to the *FInfo routines */
549 mfss_GetCreatorType(mfssobject
*self
, PyObject
*args
)
554 if (!PyArg_ParseTuple(args
, ""))
556 err
= FSpGetFInfo(&self
->fsspec
, &info
);
558 PyErr_Mac(ErrorObject
, err
);
561 return Py_BuildValue("(O&O&)",
562 PyMac_BuildOSType
, info
.fdCreator
, PyMac_BuildOSType
, info
.fdType
);
566 mfss_SetCreatorType(mfssobject
*self
, PyObject
*args
)
569 OSType creator
, type
;
572 if (!PyArg_ParseTuple(args
, "O&O&", PyMac_GetOSType
, &creator
, PyMac_GetOSType
, &type
))
574 err
= FSpGetFInfo(&self
->fsspec
, &info
);
576 PyErr_Mac(ErrorObject
, err
);
580 info
.fdCreator
= creator
;
581 err
= FSpSetFInfo(&self
->fsspec
, &info
);
583 PyErr_Mac(ErrorObject
, err
);
591 mfss_GetFInfo(mfssobject
*self
, PyObject
*args
)
597 if (!PyArg_ParseTuple(args
, ""))
599 if ( (fip
=newmfsiobject()) == NULL
)
601 err
= FSpGetFInfo(&self
->fsspec
, &fip
->finfo
);
603 PyErr_Mac(ErrorObject
, err
);
607 return (PyObject
*)fip
;
611 mfss_SetFInfo(mfssobject
*self
, PyObject
*args
)
616 if (!PyArg_ParseTuple(args
, "O!", &Mfsitype
, &fip
))
618 err
= FSpSetFInfo(&self
->fsspec
, &fip
->finfo
);
620 PyErr_Mac(ErrorObject
, err
);
628 mfss_GetDates(mfssobject
*self
, PyObject
*args
)
631 unsigned long crdat
, mddat
, bkdat
;
633 if (!PyArg_ParseTuple(args
, ""))
635 err
= PyMac_GetFileDates(&self
->fsspec
, &crdat
, &mddat
, &bkdat
);
637 PyErr_Mac(ErrorObject
, err
);
640 return Py_BuildValue("ddd", (double)crdat
, (double)mddat
, (double)bkdat
);
644 mfss_SetDates(mfssobject
*self
, PyObject
*args
)
647 double crdat
, mddat
, bkdat
;
649 if (!PyArg_ParseTuple(args
, "ddd", &crdat
, &mddat
, &bkdat
))
651 err
= PyMac_SetFileDates(&self
->fsspec
, (unsigned long)crdat
,
652 (unsigned long)mddat
, (unsigned long)bkdat
);
654 PyErr_Mac(ErrorObject
, err
);
661 static struct PyMethodDef mfss_methods
[] = {
662 {"as_pathname", (PyCFunction
)mfss_as_pathname
, 1},
663 {"as_tuple", (PyCFunction
)mfss_as_tuple
, 1},
664 {"as_fsref", (PyCFunction
)mfss_FSpMakeFSRef
, 1},
665 {"FSpMakeFSRef", (PyCFunction
)mfss_FSpMakeFSRef
, 1},
666 {"NewAlias", (PyCFunction
)mfss_NewAlias
, 1},
667 {"NewAliasMinimal", (PyCFunction
)mfss_NewAliasMinimal
, 1},
668 {"GetCreatorType", (PyCFunction
)mfss_GetCreatorType
, 1},
669 {"SetCreatorType", (PyCFunction
)mfss_SetCreatorType
, 1},
670 {"GetFInfo", (PyCFunction
)mfss_GetFInfo
, 1},
671 {"SetFInfo", (PyCFunction
)mfss_SetFInfo
, 1},
672 {"GetDates", (PyCFunction
)mfss_GetDates
, 1},
673 {"SetDates", (PyCFunction
)mfss_SetDates
, 1},
675 {NULL
, NULL
} /* sentinel */
681 mfss_getattr(mfssobject
*self
, char *name
)
683 if ( strcmp(name
, "data") == 0)
684 return PyString_FromStringAndSize((char *)&self
->fsspec
, sizeof(FSSpec
));
685 return Py_FindMethod(mfss_methods
, (PyObject
*)self
, name
);
689 newmfssobject(FSSpec
*fss
)
693 self
= PyObject_NEW(mfssobject
, &Mfsstype
);
701 mfss_dealloc(mfssobject
*self
)
707 mfss_repr(mfssobject
*self
)
711 PyOS_snprintf(buf
, sizeof(buf
), "FSSpec((%d, %ld, '%.*s'))",
712 self
->fsspec
.vRefNum
,
714 self
->fsspec
.name
[0], self
->fsspec
.name
+1);
715 return PyString_FromString(buf
);
719 mfss_compare(mfssobject
*v
, mfssobject
*w
)
724 if ( v
->fsspec
.vRefNum
< w
->fsspec
.vRefNum
) return -1;
725 if ( v
->fsspec
.vRefNum
> w
->fsspec
.vRefNum
) return 1;
726 if ( v
->fsspec
.parID
< w
->fsspec
.parID
) return -1;
727 if ( v
->fsspec
.parID
> w
->fsspec
.parID
) return 1;
728 minlen
= v
->fsspec
.name
[0];
729 if ( w
->fsspec
.name
[0] < minlen
) minlen
= w
->fsspec
.name
[0];
730 res
= strncmp((char *)v
->fsspec
.name
+1, (char *)w
->fsspec
.name
+1, minlen
);
731 if ( res
) return res
;
732 if ( v
->fsspec
.name
[0] < w
->fsspec
.name
[0] ) return -1;
733 if ( v
->fsspec
.name
[0] > w
->fsspec
.name
[0] ) return 1;
737 statichere PyTypeObject Mfsstype
= {
738 PyObject_HEAD_INIT(&PyType_Type
)
740 "macfs.FSSpec", /*tp_name*/
741 sizeof(mfssobject
), /*tp_basicsize*/
744 (destructor
)mfss_dealloc
, /*tp_dealloc*/
745 (printfunc
)0, /*tp_print*/
746 (getattrfunc
)mfss_getattr
, /*tp_getattr*/
747 (setattrfunc
)0, /*tp_setattr*/
748 (cmpfunc
)mfss_compare
, /*tp_compare*/
749 (reprfunc
)mfss_repr
, /*tp_repr*/
751 0, /*tp_as_sequence*/
753 (hashfunc
)0, /*tp_hash*/
756 /* End of code for FSSpec objects */
757 /* -------------------------------------------------------- */
758 #if !TARGET_API_MAC_OS8
760 mfsr_as_fsspec(mfsrobject
*self
, PyObject
*args
)
765 if (!PyArg_ParseTuple(args
, ""))
767 err
= FSGetCatalogInfo(&self
->fsref
, kFSCatInfoNone
, NULL
, NULL
, &fss
, NULL
);
769 PyErr_Mac(ErrorObject
, err
);
773 return (PyObject
*)newmfssobject(&fss
);
777 mfsr_as_pathname(mfsrobject
*self
, PyObject
*args
)
779 unsigned char strbuf
[PATHNAMELEN
];
782 if (!PyArg_ParseTuple(args
, ""))
784 err
= FSRefMakePath(&self
->fsref
, strbuf
, PATHNAMELEN
);
786 PyErr_Mac(ErrorObject
, err
);
789 return PyString_FromString((char *)strbuf
);
792 static struct PyMethodDef mfsr_methods
[] = {
793 {"as_fsspec", (PyCFunction
)mfsr_as_fsspec
, 1},
794 {"as_pathname", (PyCFunction
)mfsr_as_pathname
, 1},
796 {"as_tuple", (PyCFunction
)mfss_as_tuple
, 1},
797 {"NewAlias", (PyCFunction
)mfss_NewAlias
, 1},
798 {"NewAliasMinimal", (PyCFunction
)mfss_NewAliasMinimal
, 1},
799 {"GetCreatorType", (PyCFunction
)mfss_GetCreatorType
, 1},
800 {"SetCreatorType", (PyCFunction
)mfss_SetCreatorType
, 1},
801 {"GetFInfo", (PyCFunction
)mfss_GetFInfo
, 1},
802 {"SetFInfo", (PyCFunction
)mfss_SetFInfo
, 1},
803 {"GetDates", (PyCFunction
)mfss_GetDates
, 1},
804 {"SetDates", (PyCFunction
)mfss_SetDates
, 1},
807 {NULL
, NULL
} /* sentinel */
813 mfsr_getattr(mfsrobject
*self
, char *name
)
815 if ( strcmp(name
, "data") == 0)
816 return PyString_FromStringAndSize((char *)&self
->fsref
, sizeof(FSRef
));
817 return Py_FindMethod(mfsr_methods
, (PyObject
*)self
, name
);
821 newmfsrobject(FSRef
*fsr
)
825 self
= PyObject_NEW(mfsrobject
, &Mfsrtype
);
833 mfsr_compare(mfsrobject
*v
, mfsrobject
*w
)
837 if ( v
== w
) return 0;
838 err
= FSCompareFSRefs(&v
->fsref
, &w
->fsref
);
847 mfsr_dealloc(mfsrobject
*self
)
852 statichere PyTypeObject Mfsrtype
= {
853 PyObject_HEAD_INIT(&PyType_Type
)
855 "macfs.FSRef", /*tp_name*/
856 sizeof(mfsrobject
), /*tp_basicsize*/
859 (destructor
)mfsr_dealloc
, /*tp_dealloc*/
860 (printfunc
)0, /*tp_print*/
861 (getattrfunc
)mfsr_getattr
, /*tp_getattr*/
862 (setattrfunc
)0, /*tp_setattr*/
863 (cmpfunc
)mfsr_compare
, /*tp_compare*/
864 (reprfunc
)0, /*tp_repr*/
866 0, /*tp_as_sequence*/
868 (hashfunc
)0, /*tp_hash*/
871 /* End of code for FSRef objects */
872 #endif /* !TARGET_API_MAC_OS8 */
873 /* -------------------------------------------------------- */
876 mfs_ResolveAliasFile(PyObject
*self
, PyObject
*args
)
879 Boolean chain
= 1, isfolder
, wasaliased
;
882 if (!PyArg_ParseTuple(args
, "O&|i", PyMac_GetFSSpec
, &fss
, &chain
))
884 err
= ResolveAliasFile(&fss
, chain
, &isfolder
, &wasaliased
);
886 PyErr_Mac(ErrorObject
, err
);
889 return Py_BuildValue("Oii", newmfssobject(&fss
), (int)isfolder
, (int)wasaliased
);
892 #if !TARGET_API_MAC_CARBON
894 mfs_StandardGetFile(PyObject
*self
, PyObject
*args
)
898 StandardFileReply reply
;
900 list
[0] = list
[1] = list
[2] = list
[3] = 0;
902 if (!PyArg_ParseTuple(args
, "|O&O&O&O&", PyMac_GetOSType
, &list
[0],
903 PyMac_GetOSType
, &list
[1], PyMac_GetOSType
, &list
[2],
904 PyMac_GetOSType
, &list
[3]) )
906 while ( numtypes
< 4 && list
[numtypes
] ) {
911 StandardGetFile((FileFilterUPP
)0, numtypes
, list
, &reply
);
912 return Py_BuildValue("(Oi)", newmfssobject(&reply
.sfFile
), reply
.sfGood
);
916 mfs_PromptGetFile(PyObject
*self
, PyObject
*args
)
920 StandardFileReply reply
;
923 list
[0] = list
[1] = list
[2] = list
[3] = 0;
925 if (!PyArg_ParseTuple(args
, "s|O&O&O&O&", &prompt
, PyMac_GetOSType
, &list
[0],
926 PyMac_GetOSType
, &list
[1], PyMac_GetOSType
, &list
[2],
927 PyMac_GetOSType
, &list
[3]) )
929 while ( numtypes
< 4 && list
[numtypes
] ) {
934 PyMac_PromptGetFile(numtypes
, list
, &reply
, prompt
);
935 return Py_BuildValue("(Oi)", newmfssobject(&reply
.sfFile
), reply
.sfGood
);
939 mfs_StandardPutFile(PyObject
*self
, PyObject
*args
)
942 StandardFileReply reply
;
945 if (!PyArg_ParseTuple(args
, "O&|O&", PyMac_GetStr255
, &prompt
, PyMac_GetStr255
, &dft
) )
947 StandardPutFile(prompt
, dft
, &reply
);
948 return Py_BuildValue("(Oi)",newmfssobject(&reply
.sfFile
), reply
.sfGood
);
952 ** Set initial directory for file dialogs */
954 mfs_SetFolder(PyObject
*self
, PyObject
*args
)
962 orefnum
= -LMGetSFSaveDisk();
963 oparid
= LMGetCurDirStore();
964 (void)FSMakeFSSpec(orefnum
, oparid
, "\pplaceholder", &ospec
);
966 /* Go to working directory by default */
967 (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec
);
968 if (!PyArg_ParseTuple(args
, "|O&", PyMac_GetFSSpec
, &spec
))
970 /* Set standard-file working directory */
971 LMSetSFSaveDisk(-spec
.vRefNum
);
972 LMSetCurDirStore(spec
.parID
);
973 return (PyObject
*)newmfssobject(&ospec
);
978 mfs_FSSpec(PyObject
*self
, PyObject
*args
)
982 if (!PyArg_ParseTuple(args
, "O&", PyMac_GetFSSpec
, &fss
))
984 return (PyObject
*)newmfssobject(&fss
);
988 mfs_FSRef(PyObject
*self
, PyObject
*args
)
990 #if TARGET_API_MAC_OS8
991 PyErr_SetString(PyExc_NotImplementedError
, "FSRef objects not supported on this platform");
996 if (!PyArg_ParseTuple(args
, "O&", PyMac_GetFSRef
, &fsr
))
998 return (PyObject
*)newmfsrobject(&fsr
);
1003 mfs_RawFSSpec(PyObject
*self
, PyObject
*args
)
1008 if (!PyArg_ParseTuple(args
, "s#", &fssp
, &size
))
1010 if ( size
!= sizeof(FSSpec
) ) {
1011 PyErr_SetString(PyExc_TypeError
, "Incorrect size for FSSpec record");
1014 return (PyObject
*)newmfssobject(fssp
);
1018 mfs_RawAlias(PyObject
*self
, PyObject
*args
)
1024 if (!PyArg_ParseTuple(args
, "s#", &dataptr
, &size
))
1026 h
= NewHandle(size
);
1032 memcpy((char *)*h
, dataptr
, size
);
1034 return (PyObject
*)newmfsaobject((AliasHandle
)h
);
1037 #if !TARGET_API_MAC_CARBON
1039 mfs_GetDirectory(PyObject
*self
, PyObject
*args
)
1043 char *prompt
= NULL
;
1045 if (!PyArg_ParseTuple(args
, "|s", &prompt
) )
1048 ok
= PyMac_GetDirectory(&fsdir
, prompt
);
1049 return Py_BuildValue("(Oi)", newmfssobject(&fsdir
), ok
);
1054 mfs_FindFolder(PyObject
*self
, PyObject
*args
)
1063 if (!PyArg_ParseTuple(args
, "hO&i", &where
, PyMac_GetOSType
, &which
, &create
) )
1065 err
= FindFolder(where
, which
, (Boolean
)create
, &refnum
, &dirid
);
1067 PyErr_Mac(ErrorObject
, err
);
1070 return Py_BuildValue("(ii)", refnum
, dirid
);
1074 mfs_FindApplication(PyObject
*self
, PyObject
*args
)
1080 if (!PyArg_ParseTuple(args
, "O&", PyMac_GetOSType
, &which
) )
1082 err
= FindApplicationFromCreator(which
, &fss
);
1084 PyErr_Mac(ErrorObject
, err
);
1087 return (PyObject
*)newmfssobject(&fss
);
1091 mfs_FInfo(PyObject
*self
, PyObject
*args
)
1093 return (PyObject
*)newmfsiobject();
1097 mfs_NewAliasMinimalFromFullPath(PyObject
*self
, PyObject
*args
)
1106 if (!PyArg_ParseTuple(args
, "s#", &fullpath
, &fullpathlen
) )
1110 err
= NewAliasMinimalFromFullPath(fullpathlen
, (Ptr
)fullpath
, zonename
,
1111 servername
, &alias
);
1113 PyErr_Mac(ErrorObject
, err
);
1116 return (PyObject
*)newmfsaobject(alias
);
1120 /* List of methods defined in the module */
1122 static struct PyMethodDef mfs_methods
[] = {
1123 {"ResolveAliasFile", mfs_ResolveAliasFile
, 1},
1124 #if !TARGET_API_MAC_CARBON
1125 {"StandardGetFile", mfs_StandardGetFile
, 1},
1126 {"PromptGetFile", mfs_PromptGetFile
, 1},
1127 {"StandardPutFile", mfs_StandardPutFile
, 1},
1128 {"GetDirectory", mfs_GetDirectory
, 1},
1129 {"SetFolder", mfs_SetFolder
, 1},
1131 {"FSSpec", mfs_FSSpec
, 1},
1132 {"FSRef", mfs_FSRef
, 1},
1133 {"RawFSSpec", mfs_RawFSSpec
, 1},
1134 {"RawAlias", mfs_RawAlias
, 1},
1135 {"FindFolder", mfs_FindFolder
, 1},
1136 {"FindApplication", mfs_FindApplication
, 1},
1137 {"FInfo", mfs_FInfo
, 1},
1138 {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath
, 1},
1140 {NULL
, NULL
} /* sentinel */
1144 ** Convert a Python object to an FSSpec.
1145 ** The object may either be a full pathname, an FSSpec, an FSRef or a triple
1146 ** (vrefnum, dirid, path).
1149 PyMac_GetFSRef(PyObject
*v
, FSRef
*fsr
)
1151 #if TARGET_API_MAC_OS8
1152 PyErr_SetString(PyExc_TypeError
, "FSRef objects not supported on this platform");
1155 /* If it's an FSRef we're also okay. */
1156 if (_mfs_GetFSRefFromFSRef(v
, fsr
))
1158 /* first check whether it already is an FSSpec */
1159 if ( _mfs_GetFSRefFromFSSpec(v
, fsr
) )
1161 if ( PyString_Check(v
) ) {
1162 #if TARGET_API_MAC_OSX
1164 if ( (err
=FSPathMakeRef(PyString_AsString(v
), fsr
, NULL
)) ) {
1165 PyErr_Mac(ErrorObject
, err
);
1170 PyErr_SetString(PyExc_NotImplementedError
, "Cannot create an FSRef from a pathname on this platform");
1174 PyErr_SetString(PyExc_TypeError
, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname");
1179 /* Convert FSSpec to PyObject */
1180 PyObject
*PyMac_BuildFSRef(FSRef
*v
)
1182 #if TARGET_API_MAC_OS8
1185 return (PyObject
*)newmfsrobject(v
);
1190 ** Convert a Python object to an FSRef.
1191 ** The object may either be a full pathname (OSX only), an FSSpec or an FSRef.
1194 PyMac_GetFSSpec(PyObject
*v
, FSSpec
*fs
)
1201 /* first check whether it already is an FSSpec */
1202 if ( _mfs_GetFSSpecFromFSSpec(v
, fs
) )
1204 /* If it's an FSRef we're also okay. */
1205 if (_mfs_GetFSSpecFromFSRef(v
, fs
))
1207 if ( PyString_Check(v
) ) {
1208 #if TARGET_API_MAC_OSX
1211 if ( !PyMac_GetFSRef(v
, &fsr
) )
1213 if ( FSGetCatalogInfo(&fsr
, kFSCatInfoNone
, NULL
, NULL
, fs
, NULL
) == noErr
)
1217 /* It's a pathname */
1218 if( !PyArg_Parse(v
, "O&", PyMac_GetStr255
, &path
) )
1220 refnum
= 0; /* XXXX Should get CurWD here?? */
1224 if( !PyArg_Parse(v
, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)",
1225 &refnum
, &parid
, PyMac_GetStr255
, &path
)) {
1229 err
= FSMakeFSSpec(refnum
, parid
, path
, fs
);
1230 if ( err
&& err
!= fnfErr
) {
1237 /* Convert FSSpec to PyObject */
1238 PyObject
*PyMac_BuildFSSpec(FSSpec
*v
)
1240 return (PyObject
*)newmfssobject(v
);
1245 ** Import the macfsn module, which will override the Standard File
1246 ** calls in the macfs builtin module by Navigation Services versions,
1247 ** if available on this machine.
1250 PyMac_InstallNavServicesForSF(void)
1252 #ifndef TARGET_API_MAC_OSX
1253 if ( !PyMac_options
.nonavservice
) {
1255 PyObject
*m
= PyImport_ImportModule("macfsn");
1258 PySys_WriteStderr("'import macfsn' failed; ");
1259 if (Py_VerboseFlag
) {
1260 PySys_WriteStderr("traceback:\n");
1264 PySys_WriteStderr("use -v for traceback\n");
1268 #ifndef TARGET_API_MAC_OSX
1274 /* Initialization function for the module (*must* be called initmacfs) */
1281 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec
, PyMac_GetFSSpec
);
1282 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSRef
, PyMac_GetFSRef
);
1283 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSSpec
*, PyMac_BuildFSSpec
);
1284 PyMac_INIT_TOOLBOX_OBJECT_NEW(FSRef
*, PyMac_BuildFSRef
);
1286 /* Create the module and add the functions */
1287 m
= Py_InitModule("macfs", mfs_methods
);
1289 /* Add some symbolic constants to the module */
1290 d
= PyModule_GetDict(m
);
1291 ErrorObject
= PyMac_GetOSErrException();
1292 PyDict_SetItemString(d
, "error", ErrorObject
);
1294 Mfsatype
.ob_type
= &PyType_Type
;
1295 Py_INCREF(&Mfsatype
);
1296 PyDict_SetItemString(d
, "AliasType", (PyObject
*)&Mfsatype
);
1297 Mfsstype
.ob_type
= &PyType_Type
;
1298 Py_INCREF(&Mfsstype
);
1299 PyDict_SetItemString(d
, "FSSpecType", (PyObject
*)&Mfsstype
);
1300 Mfsitype
.ob_type
= &PyType_Type
;
1301 Py_INCREF(&Mfsitype
);
1302 PyDict_SetItemString(d
, "FInfoType", (PyObject
*)&Mfsitype
);
1304 PyMac_InstallNavServicesForSF();