cfsupport.py revision 234d0744466c17edb4440c766f02f6a6c955235b
1# This script generates a Python interface for an Apple Macintosh Manager.
2# It uses the "bgen" package to generate C code.
3# The function specifications are generated by scanning the mamager's header file,
4# using the "scantools" package (customized for this particular manager).
5
6#error missing SetActionFilter
7
8import string
9
10# Declarations that change for each manager
11MODNAME = '_CF'				# The name of the module
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = 'CF'			# The prefix for module-wide routines
15INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
16OUTPUTFILE = MODNAME + "module.c"	# The file generated by this program
17
18from macsupport import *
19
20# Special case generator for the functions that have an AllocatorRef first argument,
21# which we skip anyway, and the object as the second arg.
22class MethodSkipArg1(MethodGenerator):
23	"""Similar to MethodGenerator, but has self as last argument"""
24
25	def parseArgumentList(self, args):
26		if len(args) < 2:
27			raise ValueError, "MethodSkipArg1 expects at least 2 args"
28		a0, a1, args = args[0], args[1], args[2:]
29		t0, n0, m0 = a0
30		if t0 != "CFAllocatorRef" and m0 != InMode:
31			raise ValueError, "MethodSkipArg1 should have dummy AllocatorRef first arg"
32		t1, n1, m1 = a1
33		if m1 != InMode:
34			raise ValueError, "method's 'self' must be 'InMode'"
35		dummy = Variable(t0, n0, m0)
36		self.argumentList.append(dummy)
37		self.itself = Variable(t1, "_self->ob_itself", SelfMode)
38		self.argumentList.append(self.itself)
39		FunctionGenerator.parseArgumentList(self, args)
40
41
42# Create the type objects
43
44includestuff = includestuff + """
45#ifdef WITHOUT_FRAMEWORKS
46#include <CFBase.h>
47#include <CFArray.h>
48#include <CFData.h>
49#include <CFDictionary.h>
50#include <CFString.h>
51#include <CFURL.h>
52#include <CFPropertyList.h>
53#include <CFPreferences.h>
54#else
55#include <CoreServices/CoreServices.h>
56#endif
57
58#include "pycfbridge.h"
59
60#ifdef USE_TOOLBOX_OBJECT_GLUE
61extern PyObject *_CFTypeRefObj_New(CFTypeRef);
62extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
63#define CFTypeRefObj_New _CFTypeRefObj_New
64#define CFTypeRefObj_Convert _CFTypeRefObj_Convert
65
66extern PyObject *_CFStringRefObj_New(CFStringRef);
67extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
68#define CFStringRefObj_New _CFStringRefObj_New
69#define CFStringRefObj_Convert _CFStringRefObj_Convert
70
71extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
72extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
73#define CFMutableStringRefObj_New _CFMutableStringRefObj_New
74#define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
75
76extern PyObject *_CFArrayRefObj_New(CFArrayRef);
77extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
78#define CFArrayRefObj_New _CFArrayRefObj_New
79#define CFArrayRefObj_Convert _CFArrayRefObj_Convert
80
81extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
82extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
83#define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
84#define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
85
86extern PyObject *_CFDataRefObj_New(CFDataRef);
87extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
88#define CFDataRefObj_New _CFDataRefObj_New
89#define CFDataRefObj_Convert _CFDataRefObj_Convert
90
91extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
92extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
93#define CFMutableDataRefObj_New _CFMutableDataRefObj_New
94#define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
95
96extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
97extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
98#define CFDictionaryRefObj_New _CFDictionaryRefObj_New
99#define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
100
101extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
102extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
103#define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
104#define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
105
106extern PyObject *_CFURLRefObj_New(CFURLRef);
107extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
108extern int _OptionalCFURLRefObj_Convert(PyObject *, CFURLRef *);
109#define CFURLRefObj_New _CFURLRefObj_New
110#define CFURLRefObj_Convert _CFURLRefObj_Convert
111#define OptionalCFURLRefObj_Convert _OptionalCFURLRefObj_Convert
112#endif
113
114/*
115** Parse/generate CFRange records
116*/
117PyObject *CFRange_New(CFRange *itself)
118{
119
120	return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
121}
122
123int
124CFRange_Convert(PyObject *v, CFRange *p_itself)
125{
126	long location, length;
127
128	if( !PyArg_ParseTuple(v, "ll", &location, &length) )
129		return 0;
130	p_itself->location = (CFIndex)location;
131	p_itself->length = (CFIndex)length;
132	return 1;
133}
134
135/* Optional CFURL argument or None (passed as NULL) */
136int
137OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself)
138{
139    if ( v == Py_None ) {
140    	p_itself = NULL;
141    	return 1;
142    }
143    return CFURLRefObj_Convert(v, p_itself);
144}
145
146"""
147
148initstuff = initstuff + """
149PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
150PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
151PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
152PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
153PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
154PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
155
156PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
157PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
158PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
159PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
160PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
161PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
162PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
163PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
164PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
165PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
166PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
167"""
168
169variablestuff="""
170#define _STRINGCONST(name) PyModule_AddObject(m, #name, CFStringRefObj_New(name))
171_STRINGCONST(kCFPreferencesAnyApplication);
172_STRINGCONST(kCFPreferencesCurrentApplication);
173_STRINGCONST(kCFPreferencesAnyHost);
174_STRINGCONST(kCFPreferencesCurrentHost);
175_STRINGCONST(kCFPreferencesAnyUser);
176_STRINGCONST(kCFPreferencesCurrentUser);
177
178"""
179
180Boolean = Type("Boolean", "l")
181CFTypeID = Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
182CFHashCode = Type("CFHashCode", "l")
183CFIndex = Type("CFIndex", "l")
184CFRange = OpaqueByValueType('CFRange', 'CFRange')
185CFOptionFlags = Type("CFOptionFlags", "l")
186CFStringEncoding = Type("CFStringEncoding", "l")
187CFComparisonResult = Type("CFComparisonResult", "l")  # a bit dangerous, it's an enum
188CFURLPathStyle = Type("CFURLPathStyle", "l") #  a bit dangerous, it's an enum
189
190char_ptr = stringptr
191return_stringptr = Type("char *", "s")	# ONLY FOR RETURN VALUES!!
192
193CFAllocatorRef = FakeType("(CFAllocatorRef)NULL")
194CFArrayCallBacks_ptr = FakeType("&kCFTypeArrayCallBacks")
195CFDictionaryKeyCallBacks_ptr = FakeType("&kCFTypeDictionaryKeyCallBacks")
196CFDictionaryValueCallBacks_ptr = FakeType("&kCFTypeDictionaryValueCallBacks")
197# The real objects
198CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
199CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
200CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
201CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
202CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
203CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
204CFMutableDataRef = OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
205CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
206CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
207CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
208CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
209CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
210OptionalCFURLRef  = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
211##CFPropertyListRef = OpaqueByValueType("CFPropertyListRef", "CFTypeRefObj")
212# ADD object type here
213
214# Our (opaque) objects
215
216class MyGlobalObjectDefinition(GlobalObjectDefinition):
217	def outputCheckNewArg(self):
218		Output('if (itself == NULL)')
219		OutLbrace()
220		Output('PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");')
221		Output('return NULL;')
222		OutRbrace()
223	def outputStructMembers(self):
224		GlobalObjectDefinition.outputStructMembers(self)
225		Output("void (*ob_freeit)(CFTypeRef ptr);")
226	def outputInitStructMembers(self):
227		GlobalObjectDefinition.outputInitStructMembers(self)
228##		Output("it->ob_freeit = NULL;")
229		Output("it->ob_freeit = CFRelease;")
230	def outputCheckConvertArg(self):
231		Out("""
232		if (v == Py_None) { *p_itself = NULL; return 1; }
233		/* Check for other CF objects here */
234		""")
235	def outputCleanupStructMembers(self):
236		Output("if (self->ob_freeit && self->ob_itself)")
237		OutLbrace()
238		Output("self->ob_freeit((CFTypeRef)self->ob_itself);")
239		OutRbrace()
240
241	def outputCompare(self):
242		Output()
243		Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
244		OutLbrace()
245		Output("/* XXXX Or should we use CFEqual?? */")
246		Output("if ( self->ob_itself > other->ob_itself ) return 1;")
247		Output("if ( self->ob_itself < other->ob_itself ) return -1;")
248		Output("return 0;")
249		OutRbrace()
250
251	def outputHash(self):
252		Output()
253		Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
254		OutLbrace()
255		Output("/* XXXX Or should we use CFHash?? */")
256		Output("return (int)self->ob_itself;")
257		OutRbrace()
258
259	def outputRepr(self):
260		Output()
261		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
262		OutLbrace()
263		Output("char buf[100];")
264		Output("""sprintf(buf, "<CFTypeRef type-%%d object at 0x%%8.8x for 0x%%8.8x>", (int)CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself);""")
265		Output("return PyString_FromString(buf);")
266		OutRbrace()
267
268class CFTypeRefObjectDefinition(MyGlobalObjectDefinition):
269	pass
270
271class CFArrayRefObjectDefinition(MyGlobalObjectDefinition):
272	basechain = "&CFTypeRefObj_chain"
273
274	def outputRepr(self):
275		Output()
276		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
277		OutLbrace()
278		Output("char buf[100];")
279		Output("""sprintf(buf, "<CFArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
280		Output("return PyString_FromString(buf);")
281		OutRbrace()
282
283class CFMutableArrayRefObjectDefinition(MyGlobalObjectDefinition):
284	basechain = "&CFArrayRefObj_chain"
285
286	def outputRepr(self):
287		Output()
288		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
289		OutLbrace()
290		Output("char buf[100];")
291		Output("""sprintf(buf, "<CFMutableArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
292		Output("return PyString_FromString(buf);")
293		OutRbrace()
294
295class CFDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
296	basechain = "&CFTypeRefObj_chain"
297
298	def outputRepr(self):
299		Output()
300		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
301		OutLbrace()
302		Output("char buf[100];")
303		Output("""sprintf(buf, "<CFDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
304		Output("return PyString_FromString(buf);")
305		OutRbrace()
306
307class CFMutableDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
308	basechain = "&CFDictionaryRefObj_chain"
309
310	def outputRepr(self):
311		Output()
312		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
313		OutLbrace()
314		Output("char buf[100];")
315		Output("""sprintf(buf, "<CFMutableDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
316		Output("return PyString_FromString(buf);")
317		OutRbrace()
318
319class CFDataRefObjectDefinition(MyGlobalObjectDefinition):
320	basechain = "&CFTypeRefObj_chain"
321
322	def outputCheckConvertArg(self):
323		Out("""
324		if (v == Py_None) { *p_itself = NULL; return 1; }
325		if (PyString_Check(v)) {
326		    char *cStr;
327		    int cLen;
328		    if( PyString_AsStringAndSize(v, &cStr, &cLen) < 0 ) return 0;
329		    *p_itself = CFDataCreate((CFAllocatorRef)NULL, (unsigned char *)cStr, cLen);
330		    return 1;
331		}
332		""")
333
334	def outputRepr(self):
335		Output()
336		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
337		OutLbrace()
338		Output("char buf[100];")
339		Output("""sprintf(buf, "<CFDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
340		Output("return PyString_FromString(buf);")
341		OutRbrace()
342
343class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition):
344	basechain = "&CFDataRefObj_chain"
345
346	def outputRepr(self):
347		Output()
348		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
349		OutLbrace()
350		Output("char buf[100];")
351		Output("""sprintf(buf, "<CFMutableDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
352		Output("return PyString_FromString(buf);")
353		OutRbrace()
354
355class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
356	basechain = "&CFTypeRefObj_chain"
357
358	def outputCheckConvertArg(self):
359		Out("""
360		if (v == Py_None) { *p_itself = NULL; return 1; }
361		if (PyString_Check(v)) {
362		    char *cStr = PyString_AsString(v);
363			*p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, 0);
364			return 1;
365		}
366		if (PyUnicode_Check(v)) {
367			/* We use the CF types here, if Python was configured differently that will give an error */
368			CFIndex size = PyUnicode_GetSize(v);
369			UniChar *unichars = PyUnicode_AsUnicode(v);
370			if (!unichars) return 0;
371			*p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
372			return 1;
373		}
374
375		""")
376
377	def outputRepr(self):
378		Output()
379		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
380		OutLbrace()
381		Output("char buf[100];")
382		Output("""sprintf(buf, "<CFStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
383		Output("return PyString_FromString(buf);")
384		OutRbrace()
385
386class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition):
387	basechain = "&CFStringRefObj_chain"
388
389	def outputCheckConvertArg(self):
390		# Mutable, don't allow Python strings
391		return MyGlobalObjectDefinition.outputCheckConvertArg(self)
392
393	def outputRepr(self):
394		Output()
395		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
396		OutLbrace()
397		Output("char buf[100];")
398		Output("""sprintf(buf, "<CFMutableStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
399		Output("return PyString_FromString(buf);")
400		OutRbrace()
401
402class CFURLRefObjectDefinition(MyGlobalObjectDefinition):
403	basechain = "&CFTypeRefObj_chain"
404
405	def outputRepr(self):
406		Output()
407		Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
408		OutLbrace()
409		Output("char buf[100];")
410		Output("""sprintf(buf, "<CFURL object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
411		Output("return PyString_FromString(buf);")
412		OutRbrace()
413
414
415# ADD object class here
416
417# From here on it's basically all boiler plate...
418
419# Create the generator groups and link them
420module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
421CFTypeRef_object = CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
422CFArrayRef_object = CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
423CFMutableArrayRef_object = CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
424CFDictionaryRef_object = CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
425CFMutableDictionaryRef_object = CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
426CFDataRef_object = CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
427CFMutableDataRef_object = CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
428CFStringRef_object = CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
429CFMutableStringRef_object = CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
430CFURLRef_object = CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
431
432# ADD object here
433
434module.addobject(CFTypeRef_object)
435module.addobject(CFArrayRef_object)
436module.addobject(CFMutableArrayRef_object)
437module.addobject(CFDictionaryRef_object)
438module.addobject(CFMutableDictionaryRef_object)
439module.addobject(CFDataRef_object)
440module.addobject(CFMutableDataRef_object)
441module.addobject(CFStringRef_object)
442module.addobject(CFMutableStringRef_object)
443module.addobject(CFURLRef_object)
444# ADD addobject call here
445
446# Create the generator classes used to populate the lists
447Function = OSErrWeakLinkFunctionGenerator
448Method = OSErrWeakLinkMethodGenerator
449
450# Create and populate the lists
451functions = []
452CFTypeRef_methods = []
453CFArrayRef_methods = []
454CFMutableArrayRef_methods = []
455CFDictionaryRef_methods = []
456CFMutableDictionaryRef_methods = []
457CFDataRef_methods = []
458CFMutableDataRef_methods = []
459CFStringRef_methods = []
460CFMutableStringRef_methods = []
461CFURLRef_methods = []
462
463# ADD _methods initializer here
464execfile(INPUTFILE)
465
466
467# add the populated lists to the generator groups
468# (in a different wordl the scan program would generate this)
469for f in functions: module.add(f)
470for f in CFTypeRef_methods: CFTypeRef_object.add(f)
471for f in CFArrayRef_methods: CFArrayRef_object.add(f)
472for f in CFMutableArrayRef_methods: CFMutableArrayRef_object.add(f)
473for f in CFDictionaryRef_methods: CFDictionaryRef_object.add(f)
474for f in CFMutableDictionaryRef_methods: CFMutableDictionaryRef_object.add(f)
475for f in CFDataRef_methods: CFDataRef_object.add(f)
476for f in CFMutableDataRef_methods: CFMutableDataRef_object.add(f)
477for f in CFStringRef_methods: CFStringRef_object.add(f)
478for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
479for f in CFURLRef_methods: CFURLRef_object.add(f)
480
481# Manual generators for getting data out of strings
482
483getasstring_body = """
484int size = CFStringGetLength(_self->ob_itself)+1;
485char *data = malloc(size);
486
487if( data == NULL ) return PyErr_NoMemory();
488if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
489	_res = (PyObject *)PyString_FromString(data);
490} else {
491	PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
492	_res = NULL;
493}
494free(data);
495return _res;
496"""
497
498f = ManualGenerator("CFStringGetString", getasstring_body);
499f.docstring = lambda: "() -> (string _rv)"
500CFStringRef_object.add(f)
501
502getasunicode_body = """
503int size = CFStringGetLength(_self->ob_itself)+1;
504Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
505CFRange range;
506
507range.location = 0;
508range.length = size;
509if( data == NULL ) return PyErr_NoMemory();
510CFStringGetCharacters(_self->ob_itself, range, data);
511_res = (PyObject *)PyUnicode_FromUnicode(data, size);
512free(data);
513return _res;
514"""
515
516f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
517f.docstring = lambda: "() -> (unicode _rv)"
518CFStringRef_object.add(f)
519
520# Get data from CFDataRef
521getasdata_body = """
522int size = CFDataGetLength(_self->ob_itself);
523char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
524
525_res = (PyObject *)PyString_FromStringAndSize(data, size);
526return _res;
527"""
528
529f = ManualGenerator("CFDataGetData", getasdata_body);
530f.docstring = lambda: "() -> (string _rv)"
531CFDataRef_object.add(f)
532
533# Manual generator for CFPropertyListCreateFromXMLData because of funny error return
534fromxml_body = """
535CFTypeRef _rv;
536CFOptionFlags mutabilityOption;
537CFStringRef errorString;
538if (!PyArg_ParseTuple(_args, "l",
539                      &mutabilityOption))
540	return NULL;
541_rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
542                                      _self->ob_itself,
543                                      mutabilityOption,
544                                      &errorString);
545if (errorString)
546	CFRelease(errorString);
547if (_rv == NULL) {
548	PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
549	return NULL;
550}
551_res = Py_BuildValue("O&",
552                     CFTypeRefObj_New, _rv);
553return _res;
554"""
555f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
556f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
557CFTypeRef_object.add(f)
558
559# Convert CF objects to Python objects
560toPython_body = """
561_res = PyCF_CF2Python(_self->ob_itself);
562return _res;
563"""
564
565f = ManualGenerator("toPython", toPython_body);
566f.docstring = lambda: "() -> (python_object)"
567CFTypeRef_object.add(f)
568
569toCF_body = """
570CFTypeRef rv;
571CFTypeID typeid;
572
573if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv))
574	return NULL;
575typeid = CFGetTypeID(rv);
576
577if (typeid == CFStringGetTypeID())
578	return Py_BuildValue("O&", CFStringRefObj_New, rv);
579if (typeid == CFArrayGetTypeID())
580	return Py_BuildValue("O&", CFArrayRefObj_New, rv);
581if (typeid == CFDictionaryGetTypeID())
582	return Py_BuildValue("O&", CFDictionaryRefObj_New, rv);
583if (typeid == CFURLGetTypeID())
584	return Py_BuildValue("O&", CFURLRefObj_New, rv);
585
586_res = Py_BuildValue("O&", CFTypeRefObj_New, rv);
587return _res;
588"""
589f = ManualGenerator("toCF", toCF_body);
590f.docstring = lambda: "(python_object) -> (CF_object)"
591module.add(f)
592
593# ADD add forloop here
594
595# generate output (open the output file as late as possible)
596SetOutputFileName(OUTPUTFILE)
597module.generate()
598
599