1/*
2 * Test support for the special support for BOOL, UniChar and "flavours of char"
3 * (using special metadata)
4 */
5#include "Python.h"
6#include "pyobjc-api.h"
7
8#import <Foundation/Foundation.h>
9
10
11typedef struct _EmbeddedBool {
12	int count;
13	BOOL isValid;
14} EmbeddedBoolStruct;
15
16typedef struct _EmbeddedBoolArray {
17	int count;
18	BOOL valid[4];
19} EmbeddedBoolArrayStruct;
20
21@interface OC_TestSpecialTypeCode : NSObject
22{
23  int _idx;
24}
25@end
26
27@implementation OC_TestSpecialTypeCode
28
29static BOOL gBOOLValues[] = { YES, NO };
30static UniChar gUniCharValues[] = { 'a', 55, 9243, 'b' };
31static char gTextCharValues[] = { 'a', 55, 'z' };
32static char gNumCharValues[] = { 1, 2, 3, 4 };
33
34-init
35{
36	[super init];
37	_idx = 0;
38	return self;
39}
40
41
42
43-(BOOL)BOOLValue
44{
45	BOOL result = gBOOLValues[_idx];
46	_idx = (_idx + 1) % (sizeof(gBOOLValues)/sizeof(BOOL));
47	return result;
48}
49
50-(UniChar)UniCharValue
51{
52	UniChar result = gUniCharValues[_idx];
53	_idx = (_idx + 1) % (sizeof(gUniCharValues)/sizeof(UniChar));
54	return result;
55}
56
57-(char)byteValue
58{
59	char result = gTextCharValues[_idx];
60	_idx = (_idx + 1) % (sizeof(gTextCharValues)/sizeof(char));
61	return result;
62}
63
64-(char)int8Value
65{
66	char result = gTextCharValues[_idx];
67	_idx = (_idx + 1) % (sizeof(gNumCharValues)/sizeof(char));
68	return result;
69}
70
71
72
73
74-(BOOL*)BOOLArray
75{
76	static BOOL gBOOLArray[] = { YES, NO, YES, NO };
77	return gBOOLArray;
78}
79
80
81-(UniChar*)UniCharArray
82{
83	static UniChar gUniCharArray[] = { 100, 400, 955, 40000 };
84	return gUniCharArray;
85}
86
87-(UniChar*)UniCharString
88{
89	static UniChar gUniCharArray[] = { 'h', 'e', 'l', 'p', 0 };
90	return gUniCharArray;
91}
92
93-(char*)byteArray
94{
95	static char gByteArray[] = { 100, 200, 150, 99 };
96	return gByteArray;
97}
98
99-(char*)byteString
100{
101	static char gByteString[] = "hello world";
102	return gByteString;
103}
104
105-(char*)int8Array
106{
107	static char gByteArray[] = { 100, 200, 150, 99 };
108	return gByteArray;
109}
110
111-(char*)int8String
112{
113	static char gByteString[] = "hello";
114	return gByteString;
115}
116
117-(NSObject*)BOOLArg:(BOOL)v1 andBOOLArg:(BOOL)v2
118{
119	return [NSArray arrayWithObjects:
120			[NSNumber numberWithInt:(int)v1],
121			[NSNumber numberWithInt:(int)v2],
122			nil];
123}
124
125-(NSObject*)BOOLArrayOf4In:(BOOL*)value
126{
127	return [NSArray arrayWithObjects:
128			[NSNumber numberWithInt:(int)value[0]],
129			[NSNumber numberWithInt:(int)value[1]],
130			[NSNumber numberWithInt:(int)value[2]],
131			[NSNumber numberWithInt:(int)value[3]],
132			nil];
133}
134
135-(void)BOOLArrayOf4Out:(BOOL*)value
136{
137	switch (_idx % 4) {
138	case 0:
139		value[0] = YES;
140		value[1] = NO;
141		value[2] = YES;
142		value[3] = NO;
143		break;
144	case 1:
145		value[0] = NO;
146		value[1] = YES;
147		value[2] = NO;
148		value[3] = YES;
149		break;
150	case 2:
151		value[0] = YES;
152		value[1] = YES;
153		value[2] = YES;
154		value[3] = YES;
155		break;
156	case 3:
157		value[0] = NO;
158		value[1] = NO;
159		value[2] = NO;
160		value[3] = NO;
161		break;
162	}
163
164	_idx+=1;
165}
166
167-(NSObject*)BOOLArrayOf4InOut:(BOOL*)value
168{
169	NSObject* result = [NSArray arrayWithObjects:
170			[NSNumber numberWithInt:(int)value[0]],
171			[NSNumber numberWithInt:(int)value[1]],
172			[NSNumber numberWithInt:(int)value[2]],
173			[NSNumber numberWithInt:(int)value[3]],
174			nil];
175
176	switch (_idx % 4) {
177	case 0:
178		value[0] = YES;
179		value[1] = YES;
180		value[2] = YES;
181		value[3] = YES;
182		break;
183	case 1:
184		value[0] = NO;
185		value[1] = NO;
186		value[2] = NO;
187		value[3] = NO;
188		break;
189	case 2:
190		value[0] = YES;
191		value[1] = NO;
192		value[2] = YES;
193		value[3] = NO;
194		break;
195	case 3:
196		value[0] = NO;
197		value[1] = YES;
198		value[2] = NO;
199		value[3] = YES;
200		break;
201	}
202
203	_idx+=1;
204
205	return result;
206}
207
208
209-(EmbeddedBoolArrayStruct)identicalEmbeddedBoolArrayStruct:(EmbeddedBoolArrayStruct)v
210{
211	return v;
212}
213
214-(EmbeddedBoolStruct)identicalEmbeddedBoolStruct:(EmbeddedBoolStruct)v
215{
216	return v;
217}
218
219-(NSObject*)UniCharStringArg:(UniChar*)value
220{
221	NSUInteger length = 0;
222	while (value[length] != 0) {
223		length++;
224	}
225	return [NSString stringWithCharacters:value length:length];
226}
227
228-(NSObject*)UniCharArg:(UniChar)a andUniCharArg:(UniChar)b
229{
230	return [NSArray arrayWithObjects:
231			[NSString stringWithCharacters:&a length:1],
232			[NSString stringWithCharacters:&b length:1],
233			nil];
234}
235
236-(void)UniCharArrayOf4Out:(UniChar*)buffer
237{
238	buffer[0] = 'b';
239	buffer[1] = 'o';
240	buffer[2] = 'a';
241	buffer[3] = 't';
242}
243-(NSObject*)UniCharArrayOf4InOut:(UniChar*)buffer
244{
245	NSObject* result = [NSString stringWithCharacters:buffer length:4];
246	buffer[0] = 'h';
247	buffer[1] = 'a';
248	buffer[2] = 'n';
249	buffer[3] = 'd';
250	return result;
251}
252-(NSObject*)UniCharArrayOf4In:(UniChar*)buffer
253{
254	return [NSString stringWithCharacters:buffer length:4];
255}
256
257-(NSObject*)byteStringArg:(char*)value
258{
259	return [NSString stringWithCString:value encoding:NSISOLatin1StringEncoding];
260}
261
262-(NSObject*)byteArg:(char)a andbyteArg:(char)b
263{
264	char abuf[2];
265	char bbuf[2];
266
267	abuf[0] = a; abuf[1] = 0;
268	bbuf[0] = b; bbuf[1] = 0;
269
270	return [NSArray arrayWithObjects:
271		     [NSString stringWithCString:abuf encoding:NSISOLatin1StringEncoding],
272		     [NSString stringWithCString:bbuf encoding:NSISOLatin1StringEncoding],
273		     nil];
274}
275
276-(void)byteArrayOf4Out:(char*)buffer
277{
278	buffer[0] = 'b';
279	buffer[1] = 'o';
280	buffer[2] = 'a';
281	buffer[3] = 't';
282}
283
284-(NSObject*)byteArrayOf4InOut:(char*)buffer
285{
286	char tmp[5];
287	int i;
288	for (i = 0; i < 4; i++) {
289		tmp[i] = buffer[i];
290	}
291	tmp[4] = 0;
292	NSObject* result = [NSString stringWithCString:tmp encoding:NSISOLatin1StringEncoding];
293	buffer[0] = 'h';
294	buffer[1] = 'a';
295	buffer[2] = 'n';
296	buffer[3] = 'd';
297	return result;
298}
299
300-(NSObject*)byteArrayOf4In:(char*)buffer
301{
302	char tmp[5];
303	int i;
304	for (i = 0; i < 4; i++) {
305		tmp[i] = buffer[i];
306	}
307	tmp[4] = 0;
308	return [NSString stringWithCString:tmp encoding:NSISOLatin1StringEncoding];
309}
310
311-(NSObject*)int8StringArg:(char*)value
312{
313	NSMutableArray* a = [[NSMutableArray alloc] init];
314	int i;
315
316	for (i = 0; value[i] != 0; i++) {
317		[a addObject:[NSNumber numberWithInt:value[i]]];
318	}
319	return a;
320}
321
322-(NSObject*)int8Arg:(char)a andint8Arg:(char)b
323{
324	return [NSArray arrayWithObjects:
325			[NSNumber numberWithInt:a],
326			[NSNumber numberWithInt:b],
327			nil];
328}
329
330-(void)int8ArrayOf4Out:(char*)buffer
331{
332	buffer[0] = 'b';
333	buffer[1] = 'o';
334	buffer[2] = 'a';
335	buffer[3] = 't';
336}
337
338-(NSObject*)int8ArrayOf4InOut:(char*)buffer
339{
340	NSObject* result =  [NSArray arrayWithObjects:
341			[NSNumber numberWithInt:buffer[0]],
342			[NSNumber numberWithInt:buffer[1]],
343			[NSNumber numberWithInt:buffer[2]],
344			[NSNumber numberWithInt:buffer[3]],
345			nil];
346	buffer[0] = 'h';
347	buffer[1] = 'a';
348	buffer[2] = 'n';
349	buffer[3] = 'd';
350	return result;
351}
352
353-(NSObject*)int8ArrayOf4In:(char*)buffer
354{
355	NSObject* result =  [NSArray arrayWithObjects:
356			[NSNumber numberWithInt:buffer[0]],
357			[NSNumber numberWithInt:buffer[1]],
358			[NSNumber numberWithInt:buffer[2]],
359			[NSNumber numberWithInt:buffer[3]],
360			nil];
361	return result;
362}
363
364@end
365
366
367static PyMethodDef mod_methods[] = {
368	        { 0, 0, 0, 0 }
369};
370
371#if PY_VERSION_HEX >= 0x03000000
372
373static struct PyModuleDef mod_module = {
374	PyModuleDef_HEAD_INIT,
375	"specialtypecodes",
376	NULL,
377	0,
378	mod_methods,
379	NULL,
380	NULL,
381	NULL,
382	NULL
383};
384
385#define INITERROR() return NULL
386#define INITDONE() return m
387
388PyObject* PyInit_specialtypecodes(void);
389
390PyObject*
391PyInit_specialtypecodes(void)
392
393#else
394
395#define INITERROR() return
396#define INITDONE() return
397
398void initspecialtypecodes(void);
399
400void
401initspecialtypecodes(void)
402#endif
403{
404	PyObject* m;
405
406
407#if PY_VERSION_HEX >= 0x03000000
408	m = PyModule_Create(&mod_module);
409#else
410	m = Py_InitModule4("specialtypecodes", mod_methods,
411		NULL, NULL, PYTHON_API_VERSION);
412#endif
413	if (!m) {
414		INITERROR();
415	}
416	if (PyObjC_ImportAPI(m) < 0) {
417		INITERROR();
418	}
419
420	if (PyModule_AddObject(m, "OC_TestSpecialTypeCode",
421		PyObjCClass_New([OC_TestSpecialTypeCode class])) < 0) {
422		INITERROR();
423	}
424
425	INITDONE();
426}
427