Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Modules / icgluemodule.c
blobb6174399ca9388d4d835eae74f23de2842c23ea9
1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 #include "Python.h"
33 #include "macglue.h"
35 extern int ResObj_Convert(PyObject *, Handle *); /* From Resmodule.c */
37 #if TARGET_API_MAC_CARBON
38 /* The Carbon headers define PRAGMA_ALIGN_SUPPORT to something illegal,
39 ** because you shouldn't use it for Carbon. All good and well, but portable
40 ** code still needs it. So, we undefine it here.
42 #undef PRAGMA_ALIGN_SUPPORTED
43 #define PRAGMA_ALIGN_SUPPORTED 0
44 #endif /* TARGET_API_MAC_CARBON */
46 #include "ICAPI.h"
48 static PyObject *ErrorObject;
50 static PyObject *
51 ici_error(ICError err)
53 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
54 return NULL;
57 /* ----------------------------------------------------- */
59 /* Declarations for objects of type ic_instance */
61 typedef struct {
62 PyObject_HEAD
63 ICInstance inst;
64 } iciobject;
66 staticforward PyTypeObject Icitype;
70 /* ---------------------------------------------------------------- */
72 #if !TARGET_API_MAC_CARBON
73 static char ici_ICFindConfigFile__doc__[] =
74 "()->None; Find config file in standard places"
77 static PyObject *
78 ici_ICFindConfigFile(self, args)
79 iciobject *self;
80 PyObject *args;
82 ICError err;
84 if (!PyArg_ParseTuple(args, ""))
85 return NULL;
86 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
87 return ici_error(err);
88 Py_INCREF(Py_None);
89 return Py_None;
93 static char ici_ICFindUserConfigFile__doc__[] =
94 "(vRefNum, dirID)->None; Find config file in specified place"
97 static PyObject *
98 ici_ICFindUserConfigFile(self, args)
99 iciobject *self;
100 PyObject *args;
102 ICError err;
103 ICDirSpec where;
105 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
106 return NULL;
107 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
108 return ici_error(err);
109 Py_INCREF(Py_None);
110 return Py_None;
114 static char ici_ICChooseConfig__doc__[] =
115 "()->None; Let the user choose a config file"
118 static PyObject *
119 ici_ICChooseConfig(self, args)
120 iciobject *self;
121 PyObject *args;
123 ICError err;
125 if (!PyArg_ParseTuple(args, ""))
126 return NULL;
127 if ((err=ICChooseConfig(self->inst)) != 0 )
128 return ici_error(err);
129 Py_INCREF(Py_None);
130 return Py_None;
132 #endif /* !TARGET_API_MAC_CARBON */
135 static char ici_ICChooseNewConfig__doc__[] =
136 "()->None; Let the user choose a new config file"
139 static PyObject *
140 ici_ICChooseNewConfig(self, args)
141 iciobject *self;
142 PyObject *args;
144 ICError err;
146 if (!PyArg_ParseTuple(args, ""))
147 return NULL;
148 if ((err=ICChooseNewConfig(self->inst)) != 0 )
149 return ici_error(err);
150 Py_INCREF(Py_None);
151 return Py_None;
155 static char ici_ICGetSeed__doc__[] =
156 "()->int; Returns int that changes when configuration does"
159 static PyObject *
160 ici_ICGetSeed(self, args)
161 iciobject *self;
162 PyObject *args;
164 ICError err;
165 long seed;
167 if (!PyArg_ParseTuple(args, ""))
168 return NULL;
169 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
170 return ici_error(err);
171 return Py_BuildValue("i", (int)seed);
175 static char ici_ICBegin__doc__[] =
176 "(perm)->None; Lock config file for read/write"
179 static PyObject *
180 ici_ICBegin(self, args)
181 iciobject *self;
182 PyObject *args;
184 ICError err;
185 int perm;
187 if (!PyArg_ParseTuple(args, "i", &perm))
188 return NULL;
189 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
190 return ici_error(err);
191 Py_INCREF(Py_None);
192 return Py_None;
196 static char ici_ICFindPrefHandle__doc__[] =
197 "(key, handle)->attrs; Lookup key, store result in handle, return attributes"
200 static PyObject *
201 ici_ICFindPrefHandle(self, args)
202 iciobject *self;
203 PyObject *args;
205 ICError err;
206 Str255 key;
207 ICAttr attr;
208 Handle h;
210 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
211 return NULL;
212 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
213 return ici_error(err);
214 return Py_BuildValue("i", (int)attr);
218 static char ici_ICSetPref__doc__[] =
219 "(key, attr, data)->None; Set preference key to data with attributes"
222 static PyObject *
223 ici_ICSetPref(self, args)
224 iciobject *self;
225 PyObject *args;
227 ICError err;
228 Str255 key;
229 int attr;
230 char *data;
231 int datalen;
233 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
234 &data, &datalen))
235 return NULL;
236 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
237 (long)datalen)) != 0)
238 return ici_error(err);
239 Py_INCREF(Py_None);
240 return Py_None;
244 static char ici_ICCountPref__doc__[] =
245 "()->int; Return number of preferences"
248 static PyObject *
249 ici_ICCountPref(self, args)
250 iciobject *self;
251 PyObject *args;
253 ICError err;
254 long count;
256 if (!PyArg_ParseTuple(args, ""))
257 return NULL;
258 if ((err=ICCountPref(self->inst, &count)) != 0 )
259 return ici_error(err);
260 return Py_BuildValue("i", (int)count);
264 static char ici_ICGetIndPref__doc__[] =
265 "(num)->key; Return key of preference with given index"
268 static PyObject *
269 ici_ICGetIndPref(self, args)
270 iciobject *self;
271 PyObject *args;
273 ICError err;
274 long num;
275 Str255 key;
277 if (!PyArg_ParseTuple(args, "l", &num))
278 return NULL;
279 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
280 return ici_error(err);
281 return Py_BuildValue("O&", PyMac_BuildStr255, key);
285 static char ici_ICDeletePref__doc__[] =
286 "(key)->None; Delete preference"
289 static PyObject *
290 ici_ICDeletePref(self, args)
291 iciobject *self;
292 PyObject *args;
294 ICError err;
295 Str255 key;
297 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
298 return NULL;
299 if ((err=ICDeletePref(self->inst, key)) != 0 )
300 return ici_error(err);
301 Py_INCREF(Py_None);
302 return Py_None;
306 static char ici_ICEnd__doc__[] =
307 "()->None; Unlock file after ICBegin call"
310 static PyObject *
311 ici_ICEnd(self, args)
312 iciobject *self;
313 PyObject *args;
315 ICError err;
317 if (!PyArg_ParseTuple(args, ""))
318 return NULL;
319 if ((err=ICEnd(self->inst)) != 0 )
320 return ici_error(err);
321 Py_INCREF(Py_None);
322 return Py_None;
326 static char ici_ICEditPreferences__doc__[] =
327 "(key)->None; Ask user to edit preferences, staring with key"
330 static PyObject *
331 ici_ICEditPreferences(self, args)
332 iciobject *self;
333 PyObject *args;
335 ICError err;
336 Str255 key;
338 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
339 return NULL;
340 if ((err=ICEditPreferences(self->inst, key)) != 0 )
341 return ici_error(err);
342 Py_INCREF(Py_None);
343 return Py_None;
347 static char ici_ICParseURL__doc__[] =
348 "(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
351 static PyObject *
352 ici_ICParseURL(self, args)
353 iciobject *self;
354 PyObject *args;
356 ICError err;
357 Str255 hint;
358 char *data;
359 int datalen;
360 long selStart, selEnd;
361 Handle h;
363 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
364 &selStart, &selEnd, ResObj_Convert, &h))
365 return NULL;
366 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
367 &selStart, &selEnd, h)) != 0 )
368 return ici_error(err);
369 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
373 static char ici_ICLaunchURL__doc__[] =
374 "(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
377 static PyObject *
378 ici_ICLaunchURL(self, args)
379 iciobject *self;
380 PyObject *args;
382 ICError err;
383 Str255 hint;
384 char *data;
385 int datalen;
386 long selStart, selEnd;
388 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
389 &selStart, &selEnd))
390 return NULL;
391 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
392 &selStart, &selEnd)) != 0 )
393 return ici_error(err);
394 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
398 static char ici_ICMapFilename__doc__[] =
399 "(filename)->mapinfo; Get filemap info for given filename"
402 static PyObject *
403 ici_ICMapFilename(self, args)
404 iciobject *self;
405 PyObject *args;
407 ICError err;
408 Str255 filename;
409 ICMapEntry entry;
411 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
412 return NULL;
413 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
414 return ici_error(err);
415 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
416 PyMac_BuildOSType, entry.file_type,
417 PyMac_BuildOSType, entry.file_creator,
418 PyMac_BuildOSType, entry.post_creator,
419 entry.flags,
420 PyMac_BuildStr255, entry.extension,
421 PyMac_BuildStr255, entry.creator_app_name,
422 PyMac_BuildStr255, entry.post_app_name,
423 PyMac_BuildStr255, entry.MIME_type,
424 PyMac_BuildStr255, entry.entry_name);
428 static char ici_ICMapTypeCreator__doc__[] =
429 "(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
432 static PyObject *
433 ici_ICMapTypeCreator(self, args)
434 iciobject *self;
435 PyObject *args;
437 ICError err;
438 OSType type, creator;
439 Str255 filename;
440 ICMapEntry entry;
442 if (!PyArg_ParseTuple(args, "O&O&O&",
443 PyMac_GetOSType, &type,
444 PyMac_GetOSType, &creator,
445 PyMac_GetStr255, filename))
446 return NULL;
447 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
448 return ici_error(err);
449 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
450 PyMac_BuildOSType, entry.file_type,
451 PyMac_BuildOSType, entry.file_creator,
452 PyMac_BuildOSType, entry.post_creator,
453 entry.flags,
454 PyMac_BuildStr255, entry.extension,
455 PyMac_BuildStr255, entry.creator_app_name,
456 PyMac_BuildStr255, entry.post_app_name,
457 PyMac_BuildStr255, entry.MIME_type,
458 PyMac_BuildStr255, entry.entry_name);
462 static struct PyMethodDef ici_methods[] = {
463 #if !TARGET_API_MAC_CARBON
464 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
465 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
466 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
467 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
468 #endif /* !TARGET_API_MAC_CARBON */
469 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
470 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
471 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
472 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
473 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
474 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
475 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
476 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
477 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
478 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
479 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
480 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
481 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
483 {NULL, NULL} /* sentinel */
486 /* ---------- */
489 static iciobject *
490 newiciobject(OSType creator)
492 iciobject *self;
493 ICError err;
495 self = PyObject_NEW(iciobject, &Icitype);
496 if (self == NULL)
497 return NULL;
498 if ((err=ICStart(&self->inst, creator)) != 0 ) {
499 (void)ici_error(err);
500 PyMem_DEL(self);
501 return NULL;
503 return self;
507 static void
508 ici_dealloc(self)
509 iciobject *self;
511 (void)ICStop(self->inst);
512 PyMem_DEL(self);
515 static PyObject *
516 ici_getattr(self, name)
517 iciobject *self;
518 char *name;
520 return Py_FindMethod(ici_methods, (PyObject *)self, name);
523 static char Icitype__doc__[] =
524 "Internet Config instance"
527 static PyTypeObject Icitype = {
528 PyObject_HEAD_INIT(&PyType_Type)
529 0, /*ob_size*/
530 "ic_instance", /*tp_name*/
531 sizeof(iciobject), /*tp_basicsize*/
532 0, /*tp_itemsize*/
533 /* methods */
534 (destructor)ici_dealloc, /*tp_dealloc*/
535 (printfunc)0, /*tp_print*/
536 (getattrfunc)ici_getattr, /*tp_getattr*/
537 (setattrfunc)0, /*tp_setattr*/
538 (cmpfunc)0, /*tp_compare*/
539 (reprfunc)0, /*tp_repr*/
540 0, /*tp_as_number*/
541 0, /*tp_as_sequence*/
542 0, /*tp_as_mapping*/
543 (hashfunc)0, /*tp_hash*/
544 (ternaryfunc)0, /*tp_call*/
545 (reprfunc)0, /*tp_str*/
547 /* Space for future expansion */
548 0L,0L,0L,0L,
549 Icitype__doc__ /* Documentation string */
552 /* End of code for ic_instance objects */
553 /* -------------------------------------------------------- */
556 static char ic_ICStart__doc__[] =
557 "(OSType)->ic_instance; Create an Internet Config instance"
560 static PyObject *
561 ic_ICStart(self, args)
562 PyObject *self; /* Not used */
563 PyObject *args;
565 OSType creator;
567 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
568 return NULL;
569 return (PyObject *)newiciobject(creator);
572 /* List of methods defined in the module */
574 static struct PyMethodDef ic_methods[] = {
575 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
577 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
581 /* Initialization function for the module (*must* be called initicglue) */
583 static char icglue_module_documentation[] =
584 "Implements low-level Internet Config interface"
587 void
588 initicglue()
590 PyObject *m, *d;
592 /* Create the module and add the functions */
593 m = Py_InitModule4("icglue", ic_methods,
594 icglue_module_documentation,
595 (PyObject*)NULL,PYTHON_API_VERSION);
597 /* Add some symbolic constants to the module */
598 d = PyModule_GetDict(m);
599 ErrorObject = PyString_FromString("icglue.error");
600 PyDict_SetItemString(d, "error", ErrorObject);
602 /* XXXX Add constants here */
604 /* Check for errors */
605 if (PyErr_Occurred())
606 Py_FatalError("can't initialize module icglue");