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 | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 42 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 43 | #include "pythonresources.h" |
| 44 | extern PyMac_PrefRecord PyMac_options; |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 45 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 46 | |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 47 | #ifdef USE_TOOLBOX_OBJECT_GLUE |
| 48 | extern int _PyMac_GetFSSpec(PyObject *, FSSpec *); |
Jack Jansen | 6114297 | 2001-09-02 00:09:35 +0000 | [diff] [blame] | 49 | extern PyObject *_PyMac_BuildFSSpec(FSSpec *); |
| 50 | extern int _PyMac_GetFSRef(PyObject *, FSRef *); |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 51 | extern PyObject *_PyMac_BuildFSRef(FSRef *); |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 52 | #define PyMac_GetFSSpec _PyMac_GetFSSpec |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 53 | #define PyMac_BuildFSSpec _PyMac_BuildFSSpec |
| 54 | #define PyMac_GetFSRef _PyMac_GetFSRef |
| 55 | #define PyMac_BuildFSRef _PyMac_BuildFSRef |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 56 | #endif |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 57 | static PyObject *ErrorObject; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 58 | |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 59 | #ifdef TARGET_API_MAC_OSX |
| 60 | #define PATHNAMELEN 1024 |
| 61 | #else |
| 62 | #define PATHNAMELEN 256 |
| 63 | #endif |
| 64 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 65 | /* ----------------------------------------------------- */ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 66 | /* Declarations for objects of type Alias */ |
| 67 | |
| 68 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 69 | PyObject_HEAD |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 70 | AliasHandle alias; |
| 71 | } mfsaobject; |
| 72 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 73 | staticforward PyTypeObject Mfsatype; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 74 | |
| 75 | #define is_mfsaobject(v) ((v)->ob_type == &Mfsatype) |
| 76 | |
| 77 | /* ---------------------------------------------------------------- */ |
| 78 | /* Declarations for objects of type FSSpec */ |
| 79 | |
| 80 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 81 | PyObject_HEAD |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 82 | FSSpec fsspec; |
| 83 | } mfssobject; |
| 84 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 85 | staticforward PyTypeObject Mfsstype; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 86 | |
| 87 | #define is_mfssobject(v) ((v)->ob_type == &Mfsstype) |
| 88 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 89 | /* ---------------------------------------------------------------- */ |
| 90 | /* Declarations for objects of type FSRef */ |
| 91 | |
| 92 | typedef struct { |
| 93 | PyObject_HEAD |
| 94 | FSRef fsref; |
| 95 | } mfsrobject; |
| 96 | |
| 97 | staticforward PyTypeObject Mfsrtype; |
| 98 | |
| 99 | #define is_mfsrobject(v) ((v)->ob_type == &Mfsrtype) |
| 100 | |
Guido van Rossum | efd9767 | 1995-01-26 22:56:16 +0000 | [diff] [blame] | 101 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 102 | /* ---------------------------------------------------------------- */ |
| 103 | /* Declarations for objects of type FInfo */ |
| 104 | |
| 105 | typedef struct { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 106 | PyObject_HEAD |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 107 | FInfo finfo; |
| 108 | } mfsiobject; |
| 109 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 110 | staticforward PyTypeObject Mfsitype; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 111 | |
| 112 | #define is_mfsiobject(v) ((v)->ob_type == &Mfsitype) |
| 113 | |
| 114 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 115 | staticforward mfssobject *newmfssobject(FSSpec *fss); /* Forward */ |
| 116 | staticforward mfsrobject *newmfsrobject(FSRef *fsr); /* Forward */ |
Guido van Rossum | efd9767 | 1995-01-26 22:56:16 +0000 | [diff] [blame] | 117 | |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 118 | /* ---------------------------------------------------------------- */ |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 119 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 120 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 121 | mfsa_Resolve(mfsaobject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 122 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 123 | FSSpec from, *fromp, result; |
| 124 | Boolean changed; |
| 125 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 126 | |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 127 | from.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 128 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &from)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 129 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 130 | if (from.name[0] ) |
| 131 | fromp = &from; |
| 132 | else |
| 133 | fromp = NULL; |
| 134 | err = ResolveAlias(fromp, self->alias, &result, &changed); |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 135 | if ( err && err != fnfErr ) { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 136 | PyErr_Mac(ErrorObject, err); |
| 137 | return NULL; |
| 138 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 139 | return Py_BuildValue("(Oi)", newmfssobject(&result), (int)changed); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 142 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 143 | mfsa_GetInfo(mfsaobject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 144 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 145 | Str63 value; |
| 146 | int i; |
| 147 | OSErr err; |
| 148 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 149 | if (!PyArg_ParseTuple(args, "i", &i)) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | err = GetAliasInfo(self->alias, (AliasInfoType)i, value); |
| 152 | if ( err ) { |
| 153 | PyErr_Mac(ErrorObject, err); |
| 154 | return 0; |
| 155 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 156 | return PyString_FromStringAndSize((char *)&value[1], value[0]); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 159 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 160 | mfsa_Update(mfsaobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 161 | { |
| 162 | FSSpec target, fromfile, *fromfilep; |
| 163 | OSErr err; |
| 164 | Boolean changed; |
| 165 | |
| 166 | fromfile.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 167 | if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetFSSpec, &target, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 168 | PyMac_GetFSSpec, &fromfile)) |
| 169 | return NULL; |
| 170 | if ( fromfile.name[0] ) |
| 171 | fromfilep = &fromfile; |
| 172 | else |
| 173 | fromfilep = NULL; |
| 174 | err = UpdateAlias(fromfilep, &target, self->alias, &changed); |
| 175 | if ( err ) { |
| 176 | PyErr_Mac(ErrorObject, err); |
| 177 | return 0; |
| 178 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 179 | return Py_BuildValue("i", (int)changed); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 182 | static struct PyMethodDef mfsa_methods[] = { |
| 183 | {"Resolve", (PyCFunction)mfsa_Resolve, 1}, |
| 184 | {"GetInfo", (PyCFunction)mfsa_GetInfo, 1}, |
| 185 | {"Update", (PyCFunction)mfsa_Update, 1}, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 186 | |
| 187 | {NULL, NULL} /* sentinel */ |
| 188 | }; |
| 189 | |
| 190 | /* ---------- */ |
| 191 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 192 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 193 | mfsa_getattr(mfsaobject *self, char *name) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 194 | { |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 195 | if ( strcmp(name, "data") == 0 ) { |
| 196 | int size; |
| 197 | PyObject *rv; |
| 198 | |
| 199 | size = GetHandleSize((Handle)self->alias); |
| 200 | HLock((Handle)self->alias); |
| 201 | rv = PyString_FromStringAndSize(*(Handle)self->alias, size); |
| 202 | HUnlock((Handle)self->alias); |
| 203 | return rv; |
| 204 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 205 | return Py_FindMethod(mfsa_methods, (PyObject *)self, name); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 208 | static mfsaobject * |
| 209 | newmfsaobject(AliasHandle alias) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 210 | { |
| 211 | mfsaobject *self; |
| 212 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 213 | self = PyObject_NEW(mfsaobject, &Mfsatype); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 214 | if (self == NULL) |
| 215 | return NULL; |
| 216 | self->alias = alias; |
| 217 | return self; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 222 | mfsa_dealloc(mfsaobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 223 | { |
| 224 | #if 0 |
| 225 | if ( self->alias ) { |
| 226 | should we do something here? |
| 227 | } |
| 228 | #endif |
| 229 | |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame^] | 230 | PyObject_DEL(self); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 233 | statichere PyTypeObject Mfsatype = { |
| 234 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 235 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 236 | "macfs.Alias", /*tp_name*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 237 | sizeof(mfsaobject), /*tp_basicsize*/ |
| 238 | 0, /*tp_itemsize*/ |
| 239 | /* methods */ |
| 240 | (destructor)mfsa_dealloc, /*tp_dealloc*/ |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 241 | (printfunc)0, /*tp_print*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 242 | (getattrfunc)mfsa_getattr, /*tp_getattr*/ |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 243 | (setattrfunc)0, /*tp_setattr*/ |
| 244 | (cmpfunc)0, /*tp_compare*/ |
| 245 | (reprfunc)0, /*tp_repr*/ |
| 246 | 0, /*tp_as_number*/ |
| 247 | 0, /*tp_as_sequence*/ |
| 248 | 0, /*tp_as_mapping*/ |
| 249 | (hashfunc)0, /*tp_hash*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /* End of code for Alias objects */ |
| 253 | /* -------------------------------------------------------- */ |
| 254 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 255 | /* ---------------------------------------------------------------- */ |
| 256 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 257 | static struct PyMethodDef mfsi_methods[] = { |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 258 | |
| 259 | {NULL, NULL} /* sentinel */ |
| 260 | }; |
| 261 | |
| 262 | /* ---------- */ |
| 263 | |
| 264 | static mfsiobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 265 | newmfsiobject(void) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 266 | { |
| 267 | mfsiobject *self; |
| 268 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 269 | self = PyObject_NEW(mfsiobject, &Mfsitype); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 270 | if (self == NULL) |
| 271 | return NULL; |
| 272 | memset((char *)&self->finfo, '\0', sizeof(self->finfo)); |
| 273 | return self; |
| 274 | } |
| 275 | |
| 276 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 277 | mfsi_dealloc(mfsiobject *self) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 278 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame^] | 279 | PyObject_DEL(self); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 282 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 283 | mfsi_getattr(mfsiobject *self, char *name) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 284 | { |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 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); |
Just van Rossum | 14666cc | 2001-12-11 14:04:12 +0000 | [diff] [blame] | 295 | else if ( strcmp(name, "__members__") == 0 ) |
| 296 | return Py_BuildValue("[sssss]", "Type", "Creator", "Flags", "Location", "Fldr"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 297 | else |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 298 | return Py_FindMethod(mfsi_methods, (PyObject *)self, name); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 303 | mfsi_setattr(mfsiobject *self, char *name, PyObject *v) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 304 | { |
| 305 | int rv; |
| 306 | int i; |
| 307 | |
| 308 | if ( v == NULL ) { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 309 | PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 310 | return -1; |
| 311 | } |
| 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; |
| 324 | } else { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 325 | PyErr_SetString(PyExc_AttributeError, "No such attribute"); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | if (rv) |
| 329 | return 0; |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 334 | static PyTypeObject Mfsitype = { |
| 335 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 336 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 337 | "macfs.FInfo", /*tp_name*/ |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 338 | sizeof(mfsiobject), /*tp_basicsize*/ |
| 339 | 0, /*tp_itemsize*/ |
| 340 | /* methods */ |
| 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*/ |
| 347 | 0, /*tp_as_number*/ |
| 348 | 0, /*tp_as_sequence*/ |
| 349 | 0, /*tp_as_mapping*/ |
| 350 | (hashfunc)0, /*tp_hash*/ |
| 351 | }; |
| 352 | |
| 353 | /* End of code for FInfo object objects */ |
| 354 | /* -------------------------------------------------------- */ |
| 355 | |
| 356 | |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 357 | /* |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 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 |
| 362 | ** helper routines. |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 363 | */ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 364 | static int |
| 365 | _mfs_GetFSSpecFromFSSpec(PyObject *self, FSSpec *fssp) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 366 | { |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 367 | if ( is_mfssobject(self) ) { |
| 368 | *fssp = ((mfssobject *)self)->fsspec; |
| 369 | return 1; |
| 370 | } |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* Return an FSSpec if this is an FSref */ |
| 375 | static int |
| 376 | _mfs_GetFSSpecFromFSRef(PyObject *self, FSSpec *fssp) |
| 377 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 378 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 379 | static FSRef *fsrp; |
| 380 | |
| 381 | if ( is_mfsrobject(self) ) { |
| 382 | fsrp = &((mfsrobject *)self)->fsref; |
| 383 | if ( FSGetCatalogInfo(&((mfsrobject *)self)->fsref, kFSCatInfoNone, NULL, NULL, fssp, NULL) == noErr ) |
| 384 | return 1; |
| 385 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 386 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* Return an FSRef if this is an FSRef */ |
| 391 | static int |
| 392 | _mfs_GetFSRefFromFSRef(PyObject *self, FSRef *fsrp) |
| 393 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 394 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 395 | if ( is_mfsrobject(self) ) { |
| 396 | *fsrp = ((mfsrobject *)self)->fsref; |
| 397 | return 1; |
| 398 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 399 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* Return an FSRef if this is an FSSpec */ |
| 404 | static int |
| 405 | _mfs_GetFSRefFromFSSpec(PyObject *self, FSRef *fsrp) |
| 406 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 407 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 408 | if ( is_mfssobject(self) ) { |
| 409 | if ( FSpMakeFSRef(&((mfssobject *)self)->fsspec, fsrp) == noErr ) |
| 410 | return 1; |
| 411 | } |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 412 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 413 | return 0; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 416 | /* |
| 417 | ** Two generally useful routines |
| 418 | */ |
| 419 | static OSErr |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 420 | PyMac_GetFileDates(FSSpec *fss, unsigned long *crdat, unsigned long *mddat, |
| 421 | unsigned long *bkdat) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 422 | { |
| 423 | CInfoPBRec pb; |
| 424 | OSErr error; |
| 425 | |
| 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; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static OSErr |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 439 | PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat, |
| 440 | unsigned long bkdat) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 441 | { |
| 442 | CInfoPBRec pb; |
| 443 | OSErr error; |
| 444 | |
| 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); |
| 459 | return error; |
| 460 | } |
| 461 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 462 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 463 | mfss_as_pathname(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 464 | { |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 465 | char strbuf[PATHNAMELEN]; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 466 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 467 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 468 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 469 | return NULL; |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 470 | err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 471 | if ( err ) { |
| 472 | PyErr_Mac(ErrorObject, err); |
| 473 | return NULL; |
| 474 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 475 | return PyString_FromString(strbuf); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 478 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 479 | mfss_as_tuple(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 480 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 481 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 482 | return NULL; |
| 483 | return Py_BuildValue("(iis#)", self->fsspec.vRefNum, self->fsspec.parID, |
| 484 | &self->fsspec.name[1], self->fsspec.name[0]); |
| 485 | } |
| 486 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 487 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 488 | mfss_NewAlias(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 489 | { |
| 490 | FSSpec src, *srcp; |
| 491 | OSErr err; |
| 492 | AliasHandle alias; |
| 493 | |
| 494 | src.name[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 495 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &src)) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 496 | return NULL; |
| 497 | if ( src.name[0] ) |
| 498 | srcp = &src; |
| 499 | else |
| 500 | srcp = NULL; |
| 501 | err = NewAlias(srcp, &self->fsspec, &alias); |
| 502 | if ( err ) { |
| 503 | PyErr_Mac(ErrorObject, err); |
| 504 | return NULL; |
| 505 | } |
| 506 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 507 | return (PyObject *)newmfsaobject(alias); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 510 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 511 | mfss_NewAliasMinimal(mfssobject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 512 | { |
| 513 | OSErr err; |
| 514 | AliasHandle alias; |
| 515 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 516 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 517 | return NULL; |
| 518 | err = NewAliasMinimal(&self->fsspec, &alias); |
| 519 | if ( err ) { |
| 520 | PyErr_Mac(ErrorObject, err); |
| 521 | return NULL; |
| 522 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 523 | return (PyObject *)newmfsaobject(alias); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 526 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 527 | mfss_FSpMakeFSRef(mfssobject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 528 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 529 | #if TARGET_API_MAC_OS8 |
| 530 | PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform"); |
| 531 | return 0; |
| 532 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 533 | OSErr err; |
| 534 | FSRef fsref; |
| 535 | |
| 536 | if (!PyArg_ParseTuple(args, "")) |
| 537 | return NULL; |
| 538 | err = FSpMakeFSRef(&self->fsspec, &fsref); |
| 539 | if ( err ) { |
| 540 | PyErr_Mac(ErrorObject, err); |
| 541 | return NULL; |
| 542 | } |
| 543 | return (PyObject *)newmfsrobject(&fsref); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 544 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 547 | /* 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] | 548 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 549 | mfss_GetCreatorType(mfssobject *self, PyObject *args) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 550 | { |
| 551 | OSErr err; |
| 552 | FInfo info; |
| 553 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 554 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 555 | return NULL; |
| 556 | err = FSpGetFInfo(&self->fsspec, &info); |
| 557 | if ( err ) { |
| 558 | PyErr_Mac(ErrorObject, err); |
| 559 | return NULL; |
| 560 | } |
| 561 | return Py_BuildValue("(O&O&)", |
| 562 | PyMac_BuildOSType, info.fdCreator, PyMac_BuildOSType, info.fdType); |
| 563 | } |
| 564 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 565 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 566 | mfss_SetCreatorType(mfssobject *self, PyObject *args) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 567 | { |
| 568 | OSErr err; |
| 569 | OSType creator, type; |
| 570 | FInfo info; |
| 571 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 572 | if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 573 | return NULL; |
| 574 | err = FSpGetFInfo(&self->fsspec, &info); |
| 575 | if ( err ) { |
| 576 | PyErr_Mac(ErrorObject, err); |
| 577 | return NULL; |
| 578 | } |
| 579 | info.fdType = type; |
| 580 | info.fdCreator = creator; |
| 581 | err = FSpSetFInfo(&self->fsspec, &info); |
| 582 | if ( err ) { |
| 583 | PyErr_Mac(ErrorObject, err); |
| 584 | return NULL; |
| 585 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 586 | Py_INCREF(Py_None); |
| 587 | return Py_None; |
Jack Jansen | 8828fcf | 1995-02-02 14:23:52 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 590 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 591 | mfss_GetFInfo(mfssobject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 592 | { |
| 593 | OSErr err; |
| 594 | mfsiobject *fip; |
| 595 | |
| 596 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 597 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 598 | return NULL; |
| 599 | if ( (fip=newmfsiobject()) == NULL ) |
| 600 | return NULL; |
| 601 | err = FSpGetFInfo(&self->fsspec, &fip->finfo); |
| 602 | if ( err ) { |
| 603 | PyErr_Mac(ErrorObject, err); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 604 | Py_DECREF(fip); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 607 | return (PyObject *)fip; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 610 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 611 | mfss_SetFInfo(mfssobject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 612 | { |
| 613 | OSErr err; |
| 614 | mfsiobject *fip; |
| 615 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 616 | if (!PyArg_ParseTuple(args, "O!", &Mfsitype, &fip)) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 617 | return NULL; |
| 618 | err = FSpSetFInfo(&self->fsspec, &fip->finfo); |
| 619 | if ( err ) { |
| 620 | PyErr_Mac(ErrorObject, err); |
| 621 | return NULL; |
| 622 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 623 | Py_INCREF(Py_None); |
| 624 | return Py_None; |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 627 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 628 | mfss_GetDates(mfssobject *self, PyObject *args) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 629 | { |
| 630 | OSErr err; |
| 631 | unsigned long crdat, mddat, bkdat; |
| 632 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 633 | if (!PyArg_ParseTuple(args, "")) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 634 | return NULL; |
| 635 | err = PyMac_GetFileDates(&self->fsspec, &crdat, &mddat, &bkdat); |
| 636 | if ( err ) { |
| 637 | PyErr_Mac(ErrorObject, err); |
| 638 | return NULL; |
| 639 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 640 | return Py_BuildValue("ddd", (double)crdat, (double)mddat, (double)bkdat); |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 643 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 644 | mfss_SetDates(mfssobject *self, PyObject *args) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 645 | { |
| 646 | OSErr err; |
| 647 | double crdat, mddat, bkdat; |
| 648 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 649 | if (!PyArg_ParseTuple(args, "ddd", &crdat, &mddat, &bkdat)) |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 650 | return NULL; |
| 651 | err = PyMac_SetFileDates(&self->fsspec, (unsigned long)crdat, |
| 652 | (unsigned long)mddat, (unsigned long)bkdat); |
| 653 | if ( err ) { |
| 654 | PyErr_Mac(ErrorObject, err); |
| 655 | return NULL; |
| 656 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 657 | Py_INCREF(Py_None); |
| 658 | return Py_None; |
Jack Jansen | 0bdf979 | 1996-09-15 22:11:25 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 661 | static struct PyMethodDef mfss_methods[] = { |
| 662 | {"as_pathname", (PyCFunction)mfss_as_pathname, 1}, |
| 663 | {"as_tuple", (PyCFunction)mfss_as_tuple, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 664 | {"as_fsref", (PyCFunction)mfss_FSpMakeFSRef, 1}, |
| 665 | {"FSpMakeFSRef", (PyCFunction)mfss_FSpMakeFSRef, 1}, |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 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}, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 674 | |
| 675 | {NULL, NULL} /* sentinel */ |
| 676 | }; |
| 677 | |
| 678 | /* ---------- */ |
| 679 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 680 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 681 | mfss_getattr(mfssobject *self, char *name) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 682 | { |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 683 | if ( strcmp(name, "data") == 0) |
| 684 | return PyString_FromStringAndSize((char *)&self->fsspec, sizeof(FSSpec)); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 685 | return Py_FindMethod(mfss_methods, (PyObject *)self, name); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | mfssobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 689 | newmfssobject(FSSpec *fss) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 690 | { |
| 691 | mfssobject *self; |
| 692 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 693 | self = PyObject_NEW(mfssobject, &Mfsstype); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 694 | if (self == NULL) |
| 695 | return NULL; |
| 696 | self->fsspec = *fss; |
| 697 | return self; |
| 698 | } |
| 699 | |
| 700 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 701 | mfss_dealloc(mfssobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 702 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame^] | 703 | PyObject_DEL(self); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 706 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 707 | mfss_repr(mfssobject *self) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 708 | { |
| 709 | char buf[512]; |
| 710 | |
Jack Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 711 | PyOS_snprintf(buf, sizeof(buf), "FSSpec((%d, %ld, '%.*s'))", |
Guido van Rossum | f45b53b | 1995-02-20 23:43:29 +0000 | [diff] [blame] | 712 | self->fsspec.vRefNum, |
| 713 | self->fsspec.parID, |
| 714 | self->fsspec.name[0], self->fsspec.name+1); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 715 | return PyString_FromString(buf); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 719 | mfss_compare(mfssobject *v, mfssobject *w) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 720 | { |
| 721 | int minlen; |
| 722 | int res; |
| 723 | |
| 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; |
| 734 | return res; |
| 735 | } |
| 736 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 737 | statichere PyTypeObject Mfsstype = { |
| 738 | PyObject_HEAD_INIT(&PyType_Type) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 739 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 740 | "macfs.FSSpec", /*tp_name*/ |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 741 | sizeof(mfssobject), /*tp_basicsize*/ |
| 742 | 0, /*tp_itemsize*/ |
| 743 | /* methods */ |
| 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*/ |
| 750 | 0, /*tp_as_number*/ |
| 751 | 0, /*tp_as_sequence*/ |
| 752 | 0, /*tp_as_mapping*/ |
| 753 | (hashfunc)0, /*tp_hash*/ |
| 754 | }; |
| 755 | |
| 756 | /* End of code for FSSpec objects */ |
| 757 | /* -------------------------------------------------------- */ |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 758 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 759 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 760 | mfsr_as_fsspec(mfsrobject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 761 | { |
| 762 | OSErr err; |
| 763 | FSSpec fss; |
| 764 | |
| 765 | if (!PyArg_ParseTuple(args, "")) |
| 766 | return NULL; |
| 767 | err = FSGetCatalogInfo(&self->fsref, kFSCatInfoNone, NULL, NULL, &fss, NULL); |
| 768 | if ( err ) { |
| 769 | PyErr_Mac(ErrorObject, err); |
| 770 | return NULL; |
| 771 | } |
| 772 | Py_INCREF(Py_None); |
| 773 | return (PyObject *)newmfssobject(&fss); |
| 774 | } |
| 775 | |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 776 | static PyObject * |
| 777 | mfsr_as_pathname(mfsrobject *self, PyObject *args) |
| 778 | { |
Jack Jansen | 5ae815a | 2002-02-26 21:36:19 +0000 | [diff] [blame] | 779 | unsigned char strbuf[PATHNAMELEN]; |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 780 | OSStatus err; |
| 781 | |
| 782 | if (!PyArg_ParseTuple(args, "")) |
| 783 | return NULL; |
| 784 | err = FSRefMakePath(&self->fsref, strbuf, PATHNAMELEN); |
| 785 | if ( err ) { |
| 786 | PyErr_Mac(ErrorObject, err); |
| 787 | return NULL; |
| 788 | } |
Jack Jansen | 5ae815a | 2002-02-26 21:36:19 +0000 | [diff] [blame] | 789 | return PyString_FromString((char *)strbuf); |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 792 | static struct PyMethodDef mfsr_methods[] = { |
| 793 | {"as_fsspec", (PyCFunction)mfsr_as_fsspec, 1}, |
Jack Jansen | ba4fe77 | 2002-01-18 16:13:15 +0000 | [diff] [blame] | 794 | {"as_pathname", (PyCFunction)mfsr_as_pathname, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 795 | #if 0 |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 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}, |
| 805 | #endif |
| 806 | |
| 807 | {NULL, NULL} /* sentinel */ |
| 808 | }; |
| 809 | |
| 810 | /* ---------- */ |
| 811 | |
| 812 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 813 | mfsr_getattr(mfsrobject *self, char *name) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 814 | { |
| 815 | if ( strcmp(name, "data") == 0) |
| 816 | return PyString_FromStringAndSize((char *)&self->fsref, sizeof(FSRef)); |
| 817 | return Py_FindMethod(mfsr_methods, (PyObject *)self, name); |
| 818 | } |
| 819 | |
| 820 | mfsrobject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 821 | newmfsrobject(FSRef *fsr) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 822 | { |
| 823 | mfsrobject *self; |
| 824 | |
| 825 | self = PyObject_NEW(mfsrobject, &Mfsrtype); |
| 826 | if (self == NULL) |
| 827 | return NULL; |
| 828 | self->fsref = *fsr; |
| 829 | return self; |
| 830 | } |
| 831 | |
| 832 | static int |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 833 | mfsr_compare(mfsrobject *v, mfsrobject *w) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 834 | { |
| 835 | OSErr err; |
| 836 | |
| 837 | if ( v == w ) return 0; |
| 838 | err = FSCompareFSRefs(&v->fsref, &w->fsref); |
| 839 | if ( err == 0 ) |
| 840 | return 0; |
| 841 | if (v < w ) |
| 842 | return -1; |
| 843 | return 1; |
| 844 | } |
| 845 | |
| 846 | static void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 847 | mfsr_dealloc(mfsrobject *self) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 848 | { |
Jack Jansen | 0e2f798 | 2002-05-22 14:31:48 +0000 | [diff] [blame^] | 849 | PyObject_DEL(self); |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | statichere PyTypeObject Mfsrtype = { |
| 853 | PyObject_HEAD_INIT(&PyType_Type) |
| 854 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 855 | "macfs.FSRef", /*tp_name*/ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 856 | sizeof(mfsrobject), /*tp_basicsize*/ |
| 857 | 0, /*tp_itemsize*/ |
| 858 | /* methods */ |
| 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*/ |
| 865 | 0, /*tp_as_number*/ |
| 866 | 0, /*tp_as_sequence*/ |
| 867 | 0, /*tp_as_mapping*/ |
| 868 | (hashfunc)0, /*tp_hash*/ |
| 869 | }; |
| 870 | |
| 871 | /* End of code for FSRef objects */ |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 872 | #endif /* !TARGET_API_MAC_OS8 */ |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 873 | /* -------------------------------------------------------- */ |
| 874 | |
| 875 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 876 | mfs_ResolveAliasFile(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 877 | { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 878 | FSSpec fss; |
| 879 | Boolean chain = 1, isfolder, wasaliased; |
| 880 | OSErr err; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 881 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 882 | if (!PyArg_ParseTuple(args, "O&|i", PyMac_GetFSSpec, &fss, &chain)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 883 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 884 | err = ResolveAliasFile(&fss, chain, &isfolder, &wasaliased); |
| 885 | if ( err ) { |
| 886 | PyErr_Mac(ErrorObject, err); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 887 | return NULL; |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 888 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 889 | return Py_BuildValue("Oii", newmfssobject(&fss), (int)isfolder, (int)wasaliased); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 892 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 893 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 894 | mfs_StandardGetFile(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 895 | { |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 896 | SFTypeList list; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 897 | short numtypes; |
| 898 | StandardFileReply reply; |
| 899 | |
| 900 | list[0] = list[1] = list[2] = list[3] = 0; |
| 901 | numtypes = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 902 | if (!PyArg_ParseTuple(args, "|O&O&O&O&", PyMac_GetOSType, &list[0], |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 903 | PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2], |
| 904 | PyMac_GetOSType, &list[3]) ) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 905 | return NULL; |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 906 | while ( numtypes < 4 && list[numtypes] ) { |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 907 | numtypes++; |
| 908 | } |
Jack Jansen | c263286 | 1995-06-03 21:15:50 +0000 | [diff] [blame] | 909 | if ( numtypes == 0 ) |
| 910 | numtypes = -1; |
Guido van Rossum | b2f524a | 1995-01-30 08:56:06 +0000 | [diff] [blame] | 911 | StandardGetFile((FileFilterUPP)0, numtypes, list, &reply); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 912 | return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 913 | } |
| 914 | |
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_PromptGetFile(PyObject *self, PyObject *args) |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 917 | { |
| 918 | SFTypeList list; |
| 919 | short numtypes; |
| 920 | StandardFileReply reply; |
| 921 | char *prompt = NULL; |
| 922 | |
| 923 | list[0] = list[1] = list[2] = list[3] = 0; |
| 924 | numtypes = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 925 | 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] | 926 | PyMac_GetOSType, &list[1], PyMac_GetOSType, &list[2], |
| 927 | PyMac_GetOSType, &list[3]) ) |
| 928 | return NULL; |
| 929 | while ( numtypes < 4 && list[numtypes] ) { |
| 930 | numtypes++; |
| 931 | } |
| 932 | if ( numtypes == 0 ) |
| 933 | numtypes = -1; |
| 934 | PyMac_PromptGetFile(numtypes, list, &reply, prompt); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 935 | return Py_BuildValue("(Oi)", newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 938 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 939 | mfs_StandardPutFile(PyObject *self, PyObject *args) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 940 | { |
| 941 | Str255 prompt, dft; |
| 942 | StandardFileReply reply; |
| 943 | |
| 944 | dft[0] = 0; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 945 | if (!PyArg_ParseTuple(args, "O&|O&", PyMac_GetStr255, &prompt, PyMac_GetStr255, &dft) ) |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 946 | return NULL; |
| 947 | StandardPutFile(prompt, dft, &reply); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 948 | return Py_BuildValue("(Oi)",newmfssobject(&reply.sfFile), reply.sfGood); |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 951 | /* |
| 952 | ** Set initial directory for file dialogs */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 953 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 954 | mfs_SetFolder(PyObject *self, PyObject *args) |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 955 | { |
| 956 | FSSpec spec; |
| 957 | FSSpec ospec; |
| 958 | short orefnum; |
| 959 | long oparid; |
| 960 | |
| 961 | /* Get old values */ |
| 962 | orefnum = -LMGetSFSaveDisk(); |
| 963 | oparid = LMGetCurDirStore(); |
| 964 | (void)FSMakeFSSpec(orefnum, oparid, "\pplaceholder", &ospec); |
| 965 | |
| 966 | /* Go to working directory by default */ |
| 967 | (void)FSMakeFSSpec(0, 0, "\p:placeholder", &spec); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 968 | if (!PyArg_ParseTuple(args, "|O&", PyMac_GetFSSpec, &spec)) |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 969 | return NULL; |
| 970 | /* Set standard-file working directory */ |
| 971 | LMSetSFSaveDisk(-spec.vRefNum); |
| 972 | LMSetCurDirStore(spec.parID); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 973 | return (PyObject *)newmfssobject(&ospec); |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 974 | } |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 975 | #endif |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 976 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 977 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 978 | mfs_FSSpec(PyObject *self, PyObject *args) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 979 | { |
| 980 | FSSpec fss; |
| 981 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 982 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss)) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 983 | return NULL; |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 984 | return (PyObject *)newmfssobject(&fss); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 987 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 988 | mfs_FSRef(PyObject *self, PyObject *args) |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 989 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 990 | #if TARGET_API_MAC_OS8 |
| 991 | PyErr_SetString(PyExc_NotImplementedError, "FSRef objects not supported on this platform"); |
| 992 | return 0; |
| 993 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 994 | FSRef fsr; |
| 995 | |
| 996 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &fsr)) |
| 997 | return NULL; |
| 998 | return (PyObject *)newmfsrobject(&fsr); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 999 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1003 | mfs_RawFSSpec(PyObject *self, PyObject *args) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1004 | { |
| 1005 | FSSpec *fssp; |
| 1006 | int size; |
| 1007 | |
Jack Jansen | eeccca9 | 1997-05-07 15:46:31 +0000 | [diff] [blame] | 1008 | if (!PyArg_ParseTuple(args, "s#", &fssp, &size)) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1009 | return NULL; |
| 1010 | if ( size != sizeof(FSSpec) ) { |
| 1011 | PyErr_SetString(PyExc_TypeError, "Incorrect size for FSSpec record"); |
| 1012 | return NULL; |
| 1013 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1014 | return (PyObject *)newmfssobject(fssp); |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1017 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1018 | mfs_RawAlias(PyObject *self, PyObject *args) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1019 | { |
| 1020 | char *dataptr; |
| 1021 | Handle h; |
| 1022 | int size; |
| 1023 | |
Jack Jansen | eeccca9 | 1997-05-07 15:46:31 +0000 | [diff] [blame] | 1024 | if (!PyArg_ParseTuple(args, "s#", &dataptr, &size)) |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1025 | return NULL; |
| 1026 | h = NewHandle(size); |
| 1027 | if ( h == NULL ) { |
| 1028 | PyErr_NoMemory(); |
| 1029 | return NULL; |
| 1030 | } |
| 1031 | HLock(h); |
| 1032 | memcpy((char *)*h, dataptr, size); |
| 1033 | HUnlock(h); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1034 | return (PyObject *)newmfsaobject((AliasHandle)h); |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 1037 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1038 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1039 | mfs_GetDirectory(PyObject *self, PyObject *args) |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1040 | { |
| 1041 | FSSpec fsdir; |
| 1042 | int ok; |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1043 | char *prompt = NULL; |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1044 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1045 | if (!PyArg_ParseTuple(args, "|s", &prompt) ) |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1046 | return NULL; |
| 1047 | |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1048 | ok = PyMac_GetDirectory(&fsdir, prompt); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1049 | return Py_BuildValue("(Oi)", newmfssobject(&fsdir), ok); |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1050 | } |
Jack Jansen | 9d8b96c | 2000-07-14 22:16:45 +0000 | [diff] [blame] | 1051 | #endif |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1052 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1053 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1054 | mfs_FindFolder(PyObject *self, PyObject *args) |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1055 | { |
| 1056 | OSErr err; |
| 1057 | short where; |
| 1058 | OSType which; |
| 1059 | int create; |
| 1060 | short refnum; |
| 1061 | long dirid; |
| 1062 | |
Jack Jansen | abd703d | 2001-03-15 14:38:10 +0000 | [diff] [blame] | 1063 | if (!PyArg_ParseTuple(args, "hO&i", &where, PyMac_GetOSType, &which, &create) ) |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1064 | return NULL; |
| 1065 | err = FindFolder(where, which, (Boolean)create, &refnum, &dirid); |
| 1066 | if ( err ) { |
| 1067 | PyErr_Mac(ErrorObject, err); |
| 1068 | return NULL; |
| 1069 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1070 | return Py_BuildValue("(ii)", refnum, dirid); |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1073 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1074 | mfs_FindApplication(PyObject *self, PyObject *args) |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1075 | { |
| 1076 | OSErr err; |
| 1077 | OSType which; |
| 1078 | FSSpec fss; |
| 1079 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1080 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &which) ) |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1081 | return NULL; |
| 1082 | err = FindApplicationFromCreator(which, &fss); |
| 1083 | if ( err ) { |
| 1084 | PyErr_Mac(ErrorObject, err); |
| 1085 | return NULL; |
| 1086 | } |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1087 | return (PyObject *)newmfssobject(&fss); |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1090 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1091 | mfs_FInfo(PyObject *self, PyObject *args) |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1092 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1093 | return (PyObject *)newmfsiobject(); |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1096 | static PyObject * |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1097 | mfs_NewAliasMinimalFromFullPath(PyObject *self, PyObject *args) |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1098 | { |
| 1099 | OSErr err; |
| 1100 | char *fullpath; |
| 1101 | int fullpathlen; |
| 1102 | AliasHandle alias; |
| 1103 | Str32 zonename; |
| 1104 | Str31 servername; |
| 1105 | |
| 1106 | if (!PyArg_ParseTuple(args, "s#", &fullpath, &fullpathlen) ) |
| 1107 | return NULL; |
| 1108 | zonename[0] = 0; |
| 1109 | servername[0] = 0; |
| 1110 | err = NewAliasMinimalFromFullPath(fullpathlen, (Ptr)fullpath, zonename, |
| 1111 | servername, &alias); |
| 1112 | if ( err ) { |
| 1113 | PyErr_Mac(ErrorObject, err); |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | return (PyObject *)newmfsaobject(alias); |
| 1117 | } |
| 1118 | |
| 1119 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1120 | /* List of methods defined in the module */ |
| 1121 | |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1122 | static struct PyMethodDef mfs_methods[] = { |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1123 | {"ResolveAliasFile", mfs_ResolveAliasFile, 1}, |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 1124 | #if !TARGET_API_MAC_CARBON |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1125 | {"StandardGetFile", mfs_StandardGetFile, 1}, |
Jack Jansen | d5d5f46 | 1995-08-14 12:22:56 +0000 | [diff] [blame] | 1126 | {"PromptGetFile", mfs_PromptGetFile, 1}, |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1127 | {"StandardPutFile", mfs_StandardPutFile, 1}, |
Jack Jansen | 81f51c7 | 1995-02-20 15:45:25 +0000 | [diff] [blame] | 1128 | {"GetDirectory", mfs_GetDirectory, 1}, |
Jack Jansen | d99d283 | 1996-07-22 15:26:01 +0000 | [diff] [blame] | 1129 | {"SetFolder", mfs_SetFolder, 1}, |
Jack Jansen | e79dc76 | 2000-06-02 21:35:07 +0000 | [diff] [blame] | 1130 | #endif |
Jack Jansen | 17ba43f | 1995-01-26 16:22:07 +0000 | [diff] [blame] | 1131 | {"FSSpec", mfs_FSSpec, 1}, |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1132 | {"FSRef", mfs_FSRef, 1}, |
Jack Jansen | c889b76 | 1995-02-13 12:00:46 +0000 | [diff] [blame] | 1133 | {"RawFSSpec", mfs_RawFSSpec, 1}, |
| 1134 | {"RawAlias", mfs_RawAlias, 1}, |
Jack Jansen | 2c67362 | 1995-06-18 20:05:14 +0000 | [diff] [blame] | 1135 | {"FindFolder", mfs_FindFolder, 1}, |
Jack Jansen | 924ca85 | 1996-09-20 15:25:16 +0000 | [diff] [blame] | 1136 | {"FindApplication", mfs_FindApplication, 1}, |
Jack Jansen | 3d18593 | 1995-08-07 14:04:10 +0000 | [diff] [blame] | 1137 | {"FInfo", mfs_FInfo, 1}, |
Jack Jansen | d993648 | 1997-06-16 14:31:38 +0000 | [diff] [blame] | 1138 | {"NewAliasMinimalFromFullPath", mfs_NewAliasMinimalFromFullPath, 1}, |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1139 | |
| 1140 | {NULL, NULL} /* sentinel */ |
| 1141 | }; |
| 1142 | |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1143 | /* |
| 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). |
| 1147 | */ |
| 1148 | int |
| 1149 | PyMac_GetFSRef(PyObject *v, FSRef *fsr) |
| 1150 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1151 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 2bf52da | 2001-11-06 15:57:26 +0000 | [diff] [blame] | 1152 | PyErr_SetString(PyExc_TypeError, "FSRef objects not supported on this platform"); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1153 | return 0; |
| 1154 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1155 | /* If it's an FSRef we're also okay. */ |
| 1156 | if (_mfs_GetFSRefFromFSRef(v, fsr)) |
| 1157 | return 1; |
| 1158 | /* first check whether it already is an FSSpec */ |
| 1159 | if ( _mfs_GetFSRefFromFSSpec(v, fsr) ) |
| 1160 | return 1; |
| 1161 | if ( PyString_Check(v) ) { |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1162 | #if TARGET_API_MAC_OSX |
| 1163 | OSStatus err; |
| 1164 | if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) { |
| 1165 | PyErr_Mac(ErrorObject, err); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | return 1; |
| 1169 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1170 | PyErr_SetString(PyExc_NotImplementedError, "Cannot create an FSRef from a pathname on this platform"); |
| 1171 | return 0; |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1172 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1173 | } |
| 1174 | PyErr_SetString(PyExc_TypeError, "FSRef argument should be existing FSRef, FSSpec or (OSX only) pathname"); |
| 1175 | return 0; |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1176 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /* Convert FSSpec to PyObject */ |
| 1180 | PyObject *PyMac_BuildFSRef(FSRef *v) |
| 1181 | { |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1182 | #if TARGET_API_MAC_OS8 |
| 1183 | return NULL; |
| 1184 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1185 | return (PyObject *)newmfsrobject(v); |
Jack Jansen | cbed91b | 2001-08-03 13:31:36 +0000 | [diff] [blame] | 1186 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | ** Convert a Python object to an FSRef. |
| 1191 | ** The object may either be a full pathname (OSX only), an FSSpec or an FSRef. |
| 1192 | */ |
| 1193 | int |
| 1194 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
| 1195 | { |
| 1196 | Str255 path; |
| 1197 | short refnum; |
| 1198 | long parid; |
| 1199 | OSErr err; |
| 1200 | |
| 1201 | /* first check whether it already is an FSSpec */ |
| 1202 | if ( _mfs_GetFSSpecFromFSSpec(v, fs) ) |
| 1203 | return 1; |
| 1204 | /* If it's an FSRef we're also okay. */ |
| 1205 | if (_mfs_GetFSSpecFromFSRef(v, fs)) |
| 1206 | return 1; |
| 1207 | if ( PyString_Check(v) ) { |
Jack Jansen | d48b106 | 2002-01-13 23:16:32 +0000 | [diff] [blame] | 1208 | #if TARGET_API_MAC_OSX |
| 1209 | FSRef fsr; |
| 1210 | |
| 1211 | if ( !PyMac_GetFSRef(v, &fsr) ) |
| 1212 | return 0; |
| 1213 | if ( FSGetCatalogInfo(&fsr, kFSCatInfoNone, NULL, NULL, fs, NULL) == noErr ) |
| 1214 | return 1; |
| 1215 | return 0; |
| 1216 | #else |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1217 | /* It's a pathname */ |
| 1218 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
| 1219 | return 0; |
| 1220 | refnum = 0; /* XXXX Should get CurWD here?? */ |
| 1221 | parid = 0; |
Jack Jansen | d48b106 | 2002-01-13 23:16:32 +0000 | [diff] [blame] | 1222 | #endif |
Jack Jansen | 4e566ab | 2001-07-08 22:07:23 +0000 | [diff] [blame] | 1223 | } else { |
| 1224 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be FSSpec, FSRef, fullpath or (vrefnum,dirid,path)", |
| 1225 | &refnum, &parid, PyMac_GetStr255, &path)) { |
| 1226 | return 0; |
| 1227 | } |
| 1228 | } |
| 1229 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 1230 | if ( err && err != fnfErr ) { |
| 1231 | PyMac_Error(err); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | return 1; |
| 1235 | } |
| 1236 | |
| 1237 | /* Convert FSSpec to PyObject */ |
| 1238 | PyObject *PyMac_BuildFSSpec(FSSpec *v) |
| 1239 | { |
| 1240 | return (PyObject *)newmfssobject(v); |
| 1241 | } |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1242 | |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 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. |
| 1248 | */ |
| 1249 | static void |
| 1250 | PyMac_InstallNavServicesForSF(void) |
| 1251 | { |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1252 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1253 | if ( !PyMac_options.nonavservice ) { |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1254 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1255 | PyObject *m = PyImport_ImportModule("macfsn"); |
| 1256 | |
| 1257 | if ( m == NULL ) { |
| 1258 | PySys_WriteStderr("'import macfsn' failed; "); |
| 1259 | if (Py_VerboseFlag) { |
| 1260 | PySys_WriteStderr("traceback:\n"); |
| 1261 | PyErr_Print(); |
| 1262 | } |
| 1263 | else { |
| 1264 | PySys_WriteStderr("use -v for traceback\n"); |
| 1265 | } |
| 1266 | PyErr_Clear(); |
| 1267 | } |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1268 | #ifndef TARGET_API_MAC_OSX |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1269 | } |
Jack Jansen | 01a9462 | 2001-11-01 14:00:19 +0000 | [diff] [blame] | 1270 | #endif |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1274 | /* Initialization function for the module (*must* be called initmacfs) */ |
| 1275 | |
| 1276 | void |
Jack Jansen | deefbe5 | 2001-08-08 13:46:49 +0000 | [diff] [blame] | 1277 | initmacfs(void) |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1278 | { |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1279 | PyObject *m, *d; |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1280 | |
Jack Jansen | 4590049 | 2001-08-06 15:32:30 +0000 | [diff] [blame] | 1281 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(FSSpec, PyMac_GetFSSpec); |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 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); |
Jack Jansen | a5bca57 | 2001-08-03 15:39:27 +0000 | [diff] [blame] | 1285 | |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1286 | /* Create the module and add the functions */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1287 | m = Py_InitModule("macfs", mfs_methods); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1288 | |
| 1289 | /* Add some symbolic constants to the module */ |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1290 | d = PyModule_GetDict(m); |
Jack Jansen | 4377a1a | 2000-01-24 10:37:59 +0000 | [diff] [blame] | 1291 | ErrorObject = PyMac_GetOSErrException(); |
Jack Jansen | f5c2057 | 1997-01-30 15:48:07 +0000 | [diff] [blame] | 1292 | PyDict_SetItemString(d, "error", ErrorObject); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1293 | |
Jack Jansen | a755e68 | 1997-09-20 17:40:22 +0000 | [diff] [blame] | 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); |
Just van Rossum | ab57c7d | 2001-10-31 22:55:08 +0000 | [diff] [blame] | 1303 | |
| 1304 | PyMac_InstallNavServicesForSF(); |
Jack Jansen | 84fa5ec | 1995-01-18 14:04:40 +0000 | [diff] [blame] | 1305 | } |