Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | efd9767 | 1995-01-26 22:56:16 +0000 | [diff] [blame] | 3 | The Netherlands. |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 25 | #include "Python.h" |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 26 | #include "macglue.h" |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 27 | #include "pymactoolbox.h" |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 28 | |
Jack Jansen | 6143d53 | 2001-05-19 12:34:59 +0000 | [diff] [blame] | 29 | #ifdef WITHOUT_FRAMEWORKS |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 30 | #include <Memory.h> |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 31 | #include <Files.h> |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 32 | #include <Folders.h> |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 33 | #include <StandardFile.h> |
| 34 | #include <Aliases.h> |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 35 | #include <LowMem.h> |
Jack Jansen | 6143d53 | 2001-05-19 12:34:59 +0000 | [diff] [blame] | 36 | #else |
| 37 | #include <Carbon/Carbon.h> |
| 38 | #endif |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 39 | |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 40 | #include "getapplbycreator.h" |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 41 | |
Jack Jansen | 9c5b61b | 2002-08-06 12:59:44 +0000 | [diff] [blame^] | 42 | /* |
| 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 Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 49 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 50 | #include "pythonresources.h" |
| 51 | extern PyMac_PrefRecord PyMac_options; |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 52 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 53 | |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 54 | #ifdef USE_TOOLBOX_OBJECT_GLUE |
| 55 | extern int _PyMac_GetFSSpec(PyObject *, FSSpec *); |
Jack Jansen | 6114297 | 2001-09-02 00:09:35 +0000 | [diff] [blame] | 56 | extern PyObject *_PyMac_BuildFSSpec(FSSpec *); |
| 57 | extern int _PyMac_GetFSRef(PyObject *, FSRef *); |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 58 | extern PyObject *_PyMac_BuildFSRef(FSRef *); |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 59 | #define PyMac_GetFSSpec _PyMac_GetFSSpec |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 60 | #define PyMac_BuildFSSpec _PyMac_BuildFSSpec |
| 61 | #define PyMac_GetFSRef _PyMac_GetFSRef |
| 62 | #define PyMac_BuildFSRef _PyMac_BuildFSRef |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 63 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 64 | static PyObject *ErrorObject; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 65 | |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 66 | #ifdef TARGET_API_MAC_OSX |
| 67 | #define PATHNAMELEN 1024 |
| 68 | #else |
| 69 | #define PATHNAMELEN 256 |
| 70 | #endif |
| 71 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 72 | /* ----------------------------------------------------- */ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 73 | /* Declarations for objects of type Alias */ |
| 74 | |
| 75 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 76 | PyObject_HEAD |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 77 | AliasHandle alias; |
| 78 | } mfsaobject; |
| 79 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 80 | static PyTypeObject Mfsatype; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 81 | |
| 82 | #define is_mfsaobject(v) ((v)->ob_type == &Mfsatype) |
| 83 | |
| 84 | /* ---------------------------------------------------------------- */ |
| 85 | /* Declarations for objects of type FSSpec */ |
| 86 | |
| 87 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 88 | PyObject_HEAD |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 89 | FSSpec fsspec; |
| 90 | } mfssobject; |
| 91 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 92 | static PyTypeObject Mfsstype; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 93 | |
| 94 | #define is_mfssobject(v) ((v)->ob_type == &Mfsstype) |
| 95 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 96 | /* ---------------------------------------------------------------- */ |
| 97 | /* Declarations for objects of type FSRef */ |
| 98 | |
| 99 | typedef struct { |
| 100 | PyObject_HEAD |
| 101 | FSRef fsref; |
| 102 | } mfsrobject; |
| 103 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 104 | static PyTypeObject Mfsrtype; |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 105 | |
| 106 | #define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype) |
| 107 | |
Guido van Rossum | efd9767 | 1995-01-26 22:56:16 +0000 | [diff] [blame] | 108 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 109 | /* ---------------------------------------------------------------- */ |
| 110 | /* Declarations for objects of type FInfo */ |
| 111 | |
| 112 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 113 | PyObject_HEAD |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 114 | FInfo finfo; |
| 115 | } mfsiobject; |
| 116 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 117 | static PyTypeObject Mfsitype; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 118 | |
| 119 | #define is_mfsiobject(v) ((v)->ob_type == &Mfsitype) |
| 120 | |
| 121 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 122 | static mfssobject *newmfssobject(FSSpec *fss); /* Forward */ |
| 123 | static mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */ |
Guido van Rossum | efd9767 | 1995-01-26 22:56:16 +0000 | [diff] [blame] | 124 | |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 125 | /* ---------------------------------------------------------------- */ |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 126 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 127 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 128 | mfsa_Resolve(mfsaobject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 129 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 130 | FSSpec from, *fromp, result; |
| 131 | Boolean changed; |
| 132 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 133 | |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 134 | from.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 135 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 136 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 137 | if (from.name[0] ) |
| 138 | fromp = &from; |
| 139 | else |
| 140 | fromp = NULL; |
| 141 | err = ResolveAlias(fromp, self->alias, &result, &changed); |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 142 | if ( err && err != fnfErr ) { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 143 | PyErr_Mac(ErrorObject, err); |
| 144 | return NULL; |
| 145 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 146 | return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 149 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 150 | mfsa_GetInfo(mfsaobject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 151 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 152 | Str63 value; |
| 153 | int i; |
| 154 | OSErr err; |
| 155 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 156 | if (!PyArg_ParseTuple(args, "i", &i)) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 157 | return NULL; |
| 158 | err = GetAliasInfo(self->alias, (AliasInfoType)i, value); |
| 159 | if ( err ) { |
| 160 | PyErr_Mac(ErrorObject, err); |
| 161 | return 0; |
| 162 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 163 | return PyString_FromStringAndSize((char *)&value[1], value[0]); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 166 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 167 | mfsa_Update(mfsaobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 168 | { |
| 169 | FSSpec target, fromfile, *fromfilep; |
| 170 | OSErr err; |
| 171 | Boolean changed; |
| 172 | |
| 173 | fromfile.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 174 | if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 175 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 186 | return Py_BuildValue("i", (int)changed); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 189 | static struct PyMethodDef mfsa_methods[] = { |
| 190 | {"Resolve", (PyCFunction)mfsa_Resolve, 1}, |
| 191 | {"GetInfo", (PyCFunction)mfsa_GetInfo, 1}, |
| 192 | {"Update", (PyCFunction)mfsa_Update, 1}, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 193 | |
| 194 | {NULL, NULL} /* sentinel */ |
| 195 | }; |
| 196 | |
| 197 | /* ---------- */ |
| 198 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 199 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 200 | mfsa_getattr(mfsaobject *self, char *name) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 201 | { |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 202 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 212 | return Py_FindMethod(mfsa_methods, (PyObject *)self, name); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 215 | static mfsaobject * |
| 216 | newmfsaobject(AliasHandle alias) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 217 | { |
| 218 | mfsaobject *self; |
| 219 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 220 | self = PyObject_NEW(mfsaobject, &Mfsatype); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 221 | if (self == NULL) |
| 222 | return NULL; |
| 223 | self->alias = alias; |
| 224 | return self; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 229 | mfsa_dealloc(mfsaobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 230 | { |
| 231 | #if 0 |
| 232 | if ( self->alias ) { |
| 233 | should we do something here? |
| 234 | } |
| 235 | #endif |
| 236 | |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame] | 237 | PyObject_DEL(self); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 240 | statichere PyTypeObject Mfsatype = { |
| 241 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 242 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 243 | "macfs.Alias", /*tp_name*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 244 | sizeof(mfsaobject), /*tp_basicsize*/ |
| 245 | 0, /*tp_itemsize*/ |
| 246 | /* methods */ |
| 247 | (destructor)mfsa_dealloc, /*tp_dealloc*/ |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 248 | (printfunc)0, /*tp_print*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 249 | (getattrfunc)mfsa_getattr, /*tp_getattr*/ |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 250 | (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 Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | /* End of code for Alias objects */ |
| 260 | /* -------------------------------------------------------- */ |
| 261 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 262 | /* ---------------------------------------------------------------- */ |
| 263 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 264 | static struct PyMethodDef mfsi_methods[] = { |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 265 | |
| 266 | {NULL, NULL} /* sentinel */ |
| 267 | }; |
| 268 | |
| 269 | /* ---------- */ |
| 270 | |
| 271 | static mfsiobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 272 | newmfsiobject(void) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 273 | { |
| 274 | mfsiobject *self; |
| 275 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 276 | self = PyObject_NEW(mfsiobject, &Mfsitype); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 277 | if (self == NULL) |
| 278 | return NULL; |
| 279 | memset((char *)&self->finfo, '\0', sizeof(self->finfo)); |
| 280 | return self; |
| 281 | } |
| 282 | |
| 283 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 284 | mfsi_dealloc(mfsiobject *self) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 285 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame] | 286 | PyObject_DEL(self); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 289 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 290 | mfsi_getattr(mfsiobject *self, char *name) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 291 | { |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 292 | 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 Rossum | 14666cc | 2001-12-11 14:04:12 +0000 | [diff] [blame] | 302 | else if ( strcmp(name, "__members__") == 0 ) |
| 303 | return Py_BuildValue("[sssss]", "Type", "Creator", "Flags", "Location", "Fldr"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 304 | else |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 305 | return Py_FindMethod(mfsi_methods, (PyObject *)self, name); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 310 | mfsi_setattr(mfsiobject *self, char *name, PyObject *v) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 311 | { |
| 312 | int rv; |
| 313 | int i; |
| 314 | |
| 315 | if ( v == NULL ) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 316 | PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 317 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 332 | PyErr_SetString(PyExc_AttributeError, "No such attribute"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | if (rv) |
| 336 | return 0; |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 341 | static PyTypeObject Mfsitype = { |
| 342 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 343 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 344 | "macfs.FInfo", /*tp_name*/ |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 345 | 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 Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 364 | /* |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 365 | ** 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 Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 370 | */ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 371 | static int |
| 372 | _mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 373 | { |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 374 | 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 */ |
| 382 | static int |
| 383 | _mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp) |
| 384 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 385 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 386 | 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 Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 393 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* Return an FSRef if this is an FSRef */ |
| 398 | static int |
| 399 | _mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp) |
| 400 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 401 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 402 | if ( is_mfsrobject(self) ) { |
| 403 | *fsrp = ((mfsrobject *)self)->fsref; |
| 404 | return 1; |
| 405 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 406 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* Return an FSRef if this is an FSSpec */ |
| 411 | static int |
| 412 | _mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp) |
| 413 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 414 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 415 | if ( is_mfssobject(self) ) { |
| 416 | if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr ) |
| 417 | return 1; |
| 418 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 419 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 420 | return 0; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 423 | /* |
| 424 | ** Two generally useful routines |
| 425 | */ |
| 426 | static OSErr |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 427 | PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat, |
| 428 | unsigned long *bkdat) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 429 | { |
| 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 | |
| 445 | static OSErr |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 446 | PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat, |
| 447 | unsigned long bkdat) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 448 | { |
| 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 469 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 470 | mfss_as_pathname(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 471 | { |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 472 | char strbuf[PATHNAMELEN]; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 473 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 474 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 475 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 476 | return NULL; |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 477 | err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 478 | if ( err ) { |
| 479 | PyErr_Mac(ErrorObject, err); |
| 480 | return NULL; |
| 481 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 482 | return PyString_FromString(strbuf); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 485 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 486 | mfss_as_tuple(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 487 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 488 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 489 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 494 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 495 | mfss_NewAlias(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 496 | { |
| 497 | FSSpec src, *srcp; |
| 498 | OSErr err; |
| 499 | AliasHandle alias; |
| 500 | |
| 501 | src.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 502 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src)) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 503 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 514 | return (PyObject *)newmfsaobject(alias); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 517 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 518 | mfss_NewAliasMinimal(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 519 | { |
| 520 | OSErr err; |
| 521 | AliasHandle alias; |
| 522 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 523 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 524 | return NULL; |
| 525 | err = NewAliasMinimal(&self->fsspec, &alias); |
| 526 | if ( err ) { |
| 527 | PyErr_Mac(ErrorObject, err); |
| 528 | return NULL; |
| 529 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 530 | return (PyObject *)newmfsaobject(alias); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 533 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 534 | mfss_FSpMakeFSRef(mfssobject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 535 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 536 | #if TARGET_API_MAC_OS8 |
| 537 | PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform"); |
| 538 | return 0; |
| 539 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 540 | 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 Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 551 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 554 | /* XXXX These routines should be replaced by a wrapper to the *FInfo routines */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 555 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 556 | mfss_GetCreatorType(mfssobject *self, PyObject *args) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 557 | { |
| 558 | OSErr err; |
| 559 | FInfo info; |
| 560 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 561 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 562 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 572 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 573 | mfss_SetCreatorType(mfssobject *self, PyObject *args) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 574 | { |
| 575 | OSErr err; |
| 576 | OSType creator, type; |
| 577 | FInfo info; |
| 578 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 579 | if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 580 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 593 | Py_INCREF(Py_None); |
| 594 | return Py_None; |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 597 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 598 | mfss_GetFInfo(mfssobject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 599 | { |
| 600 | OSErr err; |
| 601 | mfsiobject *fip; |
Jack Jansen | 9c5b61b | 2002-08-06 12:59:44 +0000 | [diff] [blame^] | 602 | CInfoPBRec pb; |
| 603 | FSSpec* fss = &self->fsspec; |
| 604 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 605 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | if ( (fip=newmfsiobject()) == NULL ) |
| 608 | return NULL; |
Jack Jansen | 9c5b61b | 2002-08-06 12:59:44 +0000 | [diff] [blame^] | 609 | #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 Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 622 | err = FSpGetFInfo(&self->fsspec, &fip->finfo); |
| 623 | if ( err ) { |
| 624 | PyErr_Mac(ErrorObject, err); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 625 | Py_DECREF(fip); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 626 | return NULL; |
| 627 | } |
Jack Jansen | 9c5b61b | 2002-08-06 12:59:44 +0000 | [diff] [blame^] | 628 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 629 | return (PyObject *)fip; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 632 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 633 | mfss_SetFInfo(mfssobject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 634 | { |
| 635 | OSErr err; |
| 636 | mfsiobject *fip; |
| 637 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 638 | if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip)) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 639 | return NULL; |
| 640 | err = FSpSetFInfo(&self->fsspec, &fip->finfo); |
| 641 | if ( err ) { |
| 642 | PyErr_Mac(ErrorObject, err); |
| 643 | return NULL; |
| 644 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 645 | Py_INCREF(Py_None); |
| 646 | return Py_None; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 649 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 650 | mfss_GetDates(mfssobject *self, PyObject *args) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 651 | { |
| 652 | OSErr err; |
| 653 | unsigned long crdat, mddat, bkdat; |
| 654 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 655 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 656 | return NULL; |
| 657 | err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat); |
| 658 | if ( err ) { |
| 659 | PyErr_Mac(ErrorObject, err); |
| 660 | return NULL; |
| 661 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 662 | return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat); |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 665 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 666 | mfss_SetDates(mfssobject *self, PyObject *args) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 667 | { |
| 668 | OSErr err; |
| 669 | double crdat, mddat, bkdat; |
| 670 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 671 | if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat)) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 672 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 679 | Py_INCREF(Py_None); |
| 680 | return Py_None; |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 683 | static struct PyMethodDef mfss_methods[] = { |
| 684 | {"as_pathname", (PyCFunction)mfss_as_pathname, 1}, |
| 685 | {"as_tuple", (PyCFunction)mfss_as_tuple, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 686 | {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1}, |
| 687 | {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1}, |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 688 | {"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 Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 696 | |
| 697 | {NULL, NULL} /* sentinel */ |
| 698 | }; |
| 699 | |
| 700 | /* ---------- */ |
| 701 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 702 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 703 | mfss_getattr(mfssobject *self, char *name) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 704 | { |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 705 | if ( strcmp(name, "data") == 0) |
| 706 | return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec)); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 707 | return Py_FindMethod(mfss_methods, (PyObject *)self, name); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | mfssobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 711 | newmfssobject(FSSpec *fss) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 712 | { |
| 713 | mfssobject *self; |
| 714 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 715 | self = PyObject_NEW(mfssobject, &Mfsstype); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 716 | if (self == NULL) |
| 717 | return NULL; |
| 718 | self->fsspec = *fss; |
| 719 | return self; |
| 720 | } |
| 721 | |
| 722 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 723 | mfss_dealloc(mfssobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 724 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame] | 725 | PyObject_DEL(self); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 728 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 729 | mfss_repr(mfssobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 730 | { |
| 731 | char buf[512]; |
| 732 | |
Jack Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 733 | PyOS_snprintf(buf, sizeof(buf), "FSSpec((%d, %ld, '%.*s'))", |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 734 | self->fsspec.vRefNum, |
| 735 | self->fsspec.parID, |
| 736 | self->fsspec.name[0], self->fsspec.name+1); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 737 | return PyString_FromString(buf); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 741 | mfss_compare(mfssobject *v, mfssobject *w) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 742 | { |
| 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 759 | statichere PyTypeObject Mfsstype = { |
| 760 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 761 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 762 | "macfs.FSSpec", /*tp_name*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 763 | 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 Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 780 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 781 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 782 | mfsr_as_fsspec(mfsrobject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 783 | { |
| 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 Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 798 | static PyObject * |
| 799 | mfsr_as_pathname(mfsrobject *self, PyObject *args) |
| 800 | { |
Jack Jansen | 5ae815a | 2002-02-26 21:36:19 +0000 | [diff] [blame] | 801 | unsigned char strbuf[PATHNAMELEN]; |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 802 | 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 Jansen | 5ae815a | 2002-02-26 21:36:19 +0000 | [diff] [blame] | 811 | return PyString_FromString((char *)strbuf); |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 814 | static struct PyMethodDef mfsr_methods[] = { |
| 815 | {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1}, |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 816 | {"as_pathname", (PyCFunction)mfsr_as_pathname, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 817 | #if 0 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 818 | {"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 | |
| 834 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 835 | mfsr_getattr(mfsrobject *self, char *name) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 836 | { |
| 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 | |
| 842 | mfsrobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 843 | newmfsrobject(FSRef *fsr) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 844 | { |
| 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 | |
| 854 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 855 | mfsr_compare(mfsrobject *v, mfsrobject *w) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 856 | { |
| 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 | |
| 868 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 869 | mfsr_dealloc(mfsrobject *self) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 870 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame] | 871 | PyObject_DEL(self); |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | statichere PyTypeObject Mfsrtype = { |
| 875 | PyObject_HEAD_INIT(&PyType_Type) |
| 876 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 877 | "macfs.FSRef", /*tp_name*/ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 878 | 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 Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 894 | #endif /* !TARGET_API_MAC_OS8 */ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 895 | /* -------------------------------------------------------- */ |
| 896 | |
| 897 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 898 | mfs_ResolveAliasFile(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 899 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 900 | FSSpec fss; |
| 901 | Boolean chain = 1, isfolder, wasaliased; |
| 902 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 903 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 904 | if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 905 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 906 | err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased); |
| 907 | if ( err ) { |
| 908 | PyErr_Mac(ErrorObject, err); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 909 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 910 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 911 | return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 914 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 915 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 916 | mfs_StandardGetFile(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 917 | { |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 918 | SFTypeList list; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 919 | short numtypes; |
| 920 | StandardFileReply reply; |
| 921 | |
| 922 | list[0] = list[1] = list[2] = list[3] = 0; |
| 923 | numtypes = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 924 | if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0], |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 925 | PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2], |
| 926 | PyMac_GetOSType, &list[3]) ) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 927 | return NULL; |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 928 | while ( numtypes < 4 && list[numtypes] ) { |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 929 | numtypes++; |
| 930 | } |
Jack Jansen | c263286 | 1995-06-03 21:15:50 +0000 | [diff] [blame] | 931 | if ( numtypes == 0 ) |
| 932 | numtypes = -1; |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 933 | StandardGetFile((FileFilterUPP)0, numtypes, list, &reply); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 934 | return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 937 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 938 | mfs_PromptGetFile(PyObject *self, PyObject *args) |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 939 | { |
| 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 947 | if (!PyArg_ParseTuple(args, "s|O&O&O&O&", &prompt, PyMac_GetOSType, &list[0], |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 948 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 957 | return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 960 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 961 | mfs_StandardPutFile(PyObject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 962 | { |
| 963 | Str255 prompt, dft; |
| 964 | StandardFileReply reply; |
| 965 | |
| 966 | dft[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 967 | if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) ) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 968 | return NULL; |
| 969 | StandardPutFile(prompt, dft, &reply); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 970 | return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 973 | /* |
| 974 | ** Set initial directory for file dialogs */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 975 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 976 | mfs_SetFolder(PyObject *self, PyObject *args) |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 977 | { |
| 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 990 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec)) |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 991 | return NULL; |
| 992 | /* Set standard-file working directory */ |
| 993 | LMSetSFSaveDisk(-spec.vRefNum); |
| 994 | LMSetCurDirStore(spec.parID); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 995 | return (PyObject *)newmfssobject(&ospec); |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 996 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 997 | #endif |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 998 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 999 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1000 | mfs_FSSpec(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1001 | { |
| 1002 | FSSpec fss; |
| 1003 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1004 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1005 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1006 | return (PyObject *)newmfssobject(&fss); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1009 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1010 | mfs_FSRef(PyObject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1011 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1012 | #if TARGET_API_MAC_OS8 |
| 1013 | PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform"); |
| 1014 | return 0; |
| 1015 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1016 | FSRef fsr; |
| 1017 | |
| 1018 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr)) |
| 1019 | return NULL; |
| 1020 | return (PyObject *)newmfsrobject(&fsr); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1021 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1025 | mfs_RawFSSpec(PyObject *self, PyObject *args) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1026 | { |
| 1027 | FSSpec *fssp; |
| 1028 | int size; |
| 1029 | |
Jack Jansen | eeccca9 | 1997-05-07 15:46:31 +0000 | [diff] [blame] | 1030 | if (!PyArg_ParseTuple(args, "s#", &fssp, &size)) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1031 | return NULL; |
| 1032 | if ( size != sizeof(FSSpec) ) { |
| 1033 | PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record"); |
| 1034 | return NULL; |
| 1035 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1036 | return (PyObject *)newmfssobject(fssp); |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1039 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1040 | mfs_RawAlias(PyObject *self, PyObject *args) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1041 | { |
| 1042 | char *dataptr; |
| 1043 | Handle h; |
| 1044 | int size; |
| 1045 | |
Jack Jansen | eeccca9 | 1997-05-07 15:46:31 +0000 | [diff] [blame] | 1046 | if (!PyArg_ParseTuple(args, "s#", &dataptr, &size)) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1047 | 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 Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1056 | return (PyObject *)newmfsaobject((AliasHandle)h); |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 1059 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1060 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1061 | mfs_GetDirectory(PyObject *self, PyObject *args) |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1062 | { |
| 1063 | FSSpec fsdir; |
| 1064 | int ok; |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1065 | char *prompt = NULL; |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1066 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1067 | if (!PyArg_ParseTuple(args, "|s", &prompt) ) |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1068 | return NULL; |
| 1069 | |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1070 | ok = PyMac_GetDirectory(&fsdir, prompt); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1071 | return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok); |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1072 | } |
Jack Jansen | 9d8b96c | 2000-07-14 22:16:45 +0000 | [diff] [blame] | 1073 | #endif |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1074 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1075 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1076 | mfs_FindFolder(PyObject *self, PyObject *args) |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1077 | { |
| 1078 | OSErr err; |
| 1079 | short where; |
| 1080 | OSType which; |
| 1081 | int create; |
| 1082 | short refnum; |
| 1083 | long dirid; |
| 1084 | |
Jack Jansen | abd703d | 2001-03-15 14:38:10 +0000 | [diff] [blame] | 1085 | if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) ) |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1086 | return NULL; |
| 1087 | err = FindFolder(where, which, (Boolean)create, &refnum, &dirid); |
| 1088 | if ( err ) { |
| 1089 | PyErr_Mac(ErrorObject, err); |
| 1090 | return NULL; |
| 1091 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1092 | return Py_BuildValue("(ii)", refnum, dirid); |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1095 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1096 | mfs_FindApplication(PyObject *self, PyObject *args) |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1097 | { |
| 1098 | OSErr err; |
| 1099 | OSType which; |
| 1100 | FSSpec fss; |
| 1101 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1102 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) ) |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1103 | return NULL; |
| 1104 | err = FindApplicationFromCreator(which, &fss); |
| 1105 | if ( err ) { |
| 1106 | PyErr_Mac(ErrorObject, err); |
| 1107 | return NULL; |
| 1108 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1109 | return (PyObject *)newmfssobject(&fss); |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1112 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1113 | mfs_FInfo(PyObject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1114 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1115 | return (PyObject *)newmfsiobject(); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1118 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1119 | mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args) |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1120 | { |
| 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 Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1142 | /* List of methods defined in the module */ |
| 1143 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1144 | static struct PyMethodDef mfs_methods[] = { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1145 | {"ResolveAliasFile", mfs_ResolveAliasFile, 1}, |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 1146 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1147 | {"StandardGetFile", mfs_StandardGetFile, 1}, |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1148 | {"PromptGetFile", mfs_PromptGetFile, 1}, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1149 | {"StandardPutFile", mfs_StandardPutFile, 1}, |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1150 | {"GetDirectory", mfs_GetDirectory, 1}, |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 1151 | {"SetFolder", mfs_SetFolder, 1}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1152 | #endif |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1153 | {"FSSpec", mfs_FSSpec, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1154 | {"FSRef", mfs_FSRef, 1}, |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1155 | {"RawFSSpec", mfs_RawFSSpec, 1}, |
| 1156 | {"RawAlias", mfs_RawAlias, 1}, |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1157 | {"FindFolder", mfs_FindFolder, 1}, |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1158 | {"FindApplication", mfs_FindApplication, 1}, |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1159 | {"FInfo", mfs_FInfo, 1}, |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1160 | {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1}, |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1161 | |
| 1162 | {NULL, NULL} /* sentinel */ |
| 1163 | }; |
| 1164 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1165 | /* |
| 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 | */ |
| 1170 | int |
| 1171 | PyMac_GetFSRef(PyObject *v, FSRef *fsr) |
| 1172 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1173 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 2bf52da | 2001-11-06 15:57:26 +0000 | [diff] [blame] | 1174 | PyErr_SetString(PyExc_TypeError, "FSRef objects not supported on this platform"); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1175 | return 0; |
| 1176 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1177 | /* 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 Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1184 | #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 Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1192 | PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform"); |
| 1193 | return 0; |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1194 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1195 | } |
| 1196 | PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname"); |
| 1197 | return 0; |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1198 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* Convert FSSpec to PyObject */ |
| 1202 | PyObject *PyMac_BuildFSRef(FSRef *v) |
| 1203 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1204 | #if TARGET_API_MAC_OS8 |
| 1205 | return NULL; |
| 1206 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1207 | return (PyObject *)newmfsrobject(v); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1208 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1209 | } |
| 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 | */ |
| 1215 | int |
| 1216 | PyMac_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 Jansen | d48b106 | 2002-01-13 23:16:32 +0000 | [diff] [blame] | 1230 | #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 Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1239 | /* 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 Jansen | d48b106 | 2002-01-13 23:16:32 +0000 | [diff] [blame] | 1244 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1245 | } 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 */ |
| 1260 | PyObject *PyMac_BuildFSSpec(FSSpec *v) |
| 1261 | { |
| 1262 | return (PyObject *)newmfssobject(v); |
| 1263 | } |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1264 | |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1265 | |
| 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 | */ |
| 1271 | static void |
| 1272 | PyMac_InstallNavServicesForSF(void) |
| 1273 | { |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1274 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1275 | if ( !PyMac_options.nonavservice ) { |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1276 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1277 | 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 Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1290 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1291 | } |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1292 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1296 | /* Initialization function for the module (*must* be called initmacfs) */ |
| 1297 | |
| 1298 | void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1299 | initmacfs(void) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1300 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1301 | PyObject *m, *d; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1302 | |
Jack Jansen | 4590049 | 2001-08-06 15:32:30 +0000 | [diff] [blame] | 1303 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec); |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 1304 | 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 Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1307 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1308 | /* Create the module and add the functions */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1309 | m = Py_InitModule("macfs", mfs_methods); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* Add some symbolic constants to the module */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1312 | d = PyModule_GetDict(m); |
Jack Jansen | 4377a1a | 2000-01-24 10:37:59 +0000 | [diff] [blame] | 1313 | ErrorObject = PyMac_GetOSErrException(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1314 | PyDict_SetItemString(d, "error", ErrorObject); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1315 | |
Jack Jansen | a755e68 | 1997-09-20 17:40:22 +0000 | [diff] [blame] | 1316 | 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 Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1325 | |
| 1326 | PyMac_InstallNavServicesForSF(); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1327 | } |