summaryrefslogtreecommitdiff
path: root/ext/satellite/namedvalue_to_zval.c
blob: 86fb2cb247b267400247bbbdf7f8bf4580931a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: David Eriksson <david@2good.com>                            |
   +----------------------------------------------------------------------+
 */

/*
 * $Id$
 * vim: syntax=c tabstop=2 shiftwidth=2
 */

/* -----------------------------------------------------------------------
 * 
 * Functions to convert a CORBA_NamedValue to a zval
 *
 * -----------------------------------------------------------------------
 */
#include <zend_API.h>
#include "namedvalue_to_zval.h"
#include "common.h"
#include "object.h"
#include "struct.h"
#include "corba.h"

#include <ORBitutil/util.h>		/* for ALIGN_ADDRESS() */

static zend_bool satellite_any_to_zval_boolean(
		const CORBA_any * pSource, zval * pDestination)
{
	CORBA_boolean * p_value = (CORBA_boolean *)pSource->_value;
	ZVAL_BOOL(pDestination, *p_value != 0);
	return TRUE;
}

static zend_bool satellite_any_to_zval_double(
		const CORBA_any * pSource, zval * pDestination)
{
	CORBA_double * p_value = (CORBA_double *)pSource->_value;
	ZVAL_DOUBLE(pDestination, *p_value);
	return TRUE;
}

static zend_bool satellite_any_to_zval_long(
		const CORBA_any * pSource, zval * pDestination)
{
	CORBA_long * p_value = (CORBA_long *)pSource->_value;
	ZVAL_LONG(pDestination, *p_value);
	return TRUE;
}

static zend_bool satellite_any_to_zval_short(
		const CORBA_any * pSource, zval * pDestination)
{
	CORBA_short * p_value = (CORBA_short *)pSource->_value;
	ZVAL_LONG(pDestination, *p_value);
	return TRUE;
}


static zend_bool satellite_any_to_zval_objref(
		const CORBA_any * pSource, zval * pDestination)
{
	CORBA_Object * p_value = (CORBA_Object *)pSource->_value;
	CORBA_Object dup = 
		CORBA_Object_duplicate(*p_value, orbit_get_environment());
	
	return OrbitObject_Create(dup, pDestination);
}

static zend_bool satellite_any_to_zval_sequence(
		const CORBA_any * pSource, zval * pDestination)
{
	int i;
	int length = 0, step;
	void ** pp_members = NULL;
	zend_bool success = FALSE;
	CORBA_NamedValue source_item;
	zval * p_destination_item = NULL;
	CORBA_TypeCode content_type = NULL;
	CORBA_sequence_octet * p_value = 
		(CORBA_sequence_octet *)pSource->_value;
	
	/* get array to items */
	pp_members = (void **)p_value->_buffer;

	/* get number of members */
	length = p_value->_length;

	/* get type of items */
	content_type = CORBA_TypeCode_content_type(
			pSource->_type, orbit_get_environment());
	if (content_type == NULL)
		goto error;

	/* set zval members */
#if 0
	pDestination->type = IS_ARRAY;
	pDestination->refcount = 1;

	pDestination->value.ht = orbit_new(HashTable);

	zend_hash_init(
			pDestination->value.ht,		/* hash table */
			0, 												/* size */
			NULL, 										/* hash function */
			ZVAL_PTR_DTOR, 						/* destructor */
			0);												/* persistent */
#else
	array_init(pDestination);
#endif

	step = content_type->length ? content_type->length : 1;

	for (i = 0; i < length; i++)
	{
		p_destination_item = NULL;
		memset(&source_item, 0, sizeof(CORBA_NamedValue));

		source_item.argument._type = content_type;
		source_item.argument._value = pp_members;

		pp_members += step;

		ALLOC_ZVAL(p_destination_item);
		
		/* convert item */
		success = orbit_namedvalue_to_zval(
			&source_item,
			p_destination_item);

		if (!success)
			goto error;

		/* add to hashtable */
		INIT_PZVAL(p_destination_item);	/* testing! */
		zend_hash_next_index_insert(
			pDestination->value.ht,
			&p_destination_item,
			sizeof(zval *),
			NULL);

/*		FREE_ZVAL(p_destination_item);*/
	}

	return TRUE;
	
error:
	return FALSE;
}

static zend_bool satellite_any_to_zval_string(
		const CORBA_any * pSource, zval * pDestination)
{
	char ** pp_value = (char **)pSource->_value;
	ZVAL_STRING(pDestination, (*pp_value), TRUE);
	return TRUE;
}

static zend_bool satellite_any_to_zval_struct(
		const CORBA_any * pSource, zval * pDestination)
{
	int member_count = 0;
	CORBA_TypeCode type = NULL;
	char * p_repository_id = NULL;
	OrbitStruct * p_struct = NULL;

	/* make type a shortcut to the typecode */
	type = pSource->_type;

	/* find out the repository id */
	p_repository_id = CORBA_TypeCode_id(type, orbit_get_environment());
	/* do not check for exception because we may be converting one! */
	if (p_repository_id == NULL)
		goto error;

	if (!OrbitStruct_Create(p_repository_id, pDestination))
		goto error;

	p_struct = OrbitStruct_RetrieveData(pDestination);
	if (p_struct == NULL)
		goto error;

	/* now we know the source and destination matches! */
	member_count = CORBA_TypeCode_member_count(type, orbit_get_environment());
	/* do not check for exception because we may be converting one! */

	if (member_count > 0)
	{
		int i;
		zend_bool success;
		zval destination_item;
		CORBA_NamedValue source_item;
		char * p_buffer = (char *)pSource->_value;
		
		for (i = 0; i < member_count; i++)
		{
			int alignment;
			char * p_name = 
				CORBA_TypeCode_member_name(type, i, orbit_get_environment());
			if (p_name == NULL)
				goto error;
			
			/* prepare destination item */
			ZVAL_NULL(&destination_item);

			/* prepare source item */
			memset(&source_item, 0, sizeof(CORBA_NamedValue));
			source_item.argument._type = 
					CORBA_TypeCode_member_type(type, i, orbit_get_environment());
			if (source_item.argument._type == NULL)
				goto error;

			/* align pointer and set value */
			alignment = ORBit_find_alignment(source_item.argument._type);
			p_buffer = ALIGN_ADDRESS(p_buffer, alignment);
			source_item.argument._value = p_buffer;
			
			/* convert item */
			if (!orbit_namedvalue_to_zval(&source_item, &destination_item))
				goto error;
			
			/* set attribute */
			success = OrbitStruct_PutProperty(p_struct, p_name, &destination_item);
			if (!success)	goto error;
			
			/* advance buffer */
			p_buffer += ORBit_gather_alloc_info(source_item.argument._type);
		}
	}

	return TRUE;

error:	
	return FALSE;
}

zend_bool satellite_any_to_zval(
		const CORBA_any * pSource, zval * pDestination)
{
	zend_bool success = FALSE;
	CORBA_TCKind kind;
  
	if (pSource->_type == NULL || pDestination == NULL)
		return FALSE;
		
	kind = CORBA_TypeCode_kind(
			pSource->_type, 
			orbit_get_environment());	/* does not throw exceptions */

	switch (kind)
	{
		case CORBA_tk_boolean:
			success = satellite_any_to_zval_boolean(pSource, pDestination);
			break;
		
		case CORBA_tk_double:
			success = satellite_any_to_zval_double(pSource, pDestination);
			break;

		case CORBA_tk_enum:
		case CORBA_tk_long:
			success = satellite_any_to_zval_long(pSource, pDestination);
			break;

		case CORBA_tk_objref:
			success = satellite_any_to_zval_objref(pSource, pDestination);
			break;

		case CORBA_tk_sequence:
			success = satellite_any_to_zval_sequence(pSource, pDestination);
			break;

		case CORBA_tk_short:
			success = satellite_any_to_zval_short(pSource, pDestination);
			break;

		case CORBA_tk_string:
			success = satellite_any_to_zval_string(pSource, pDestination);
			break;

		case CORBA_tk_struct:	/* 15 */
		case CORBA_tk_except: /* 22 */
			success = satellite_any_to_zval_struct(pSource, pDestination);
			break;

		default:
/*			printf("unsupported corba TCKind %i\n", kind);*/
			zend_error(E_WARNING, "(satellite) unsupported corba TCKind %i", kind);
	}

	return success;
}

zend_bool orbit_namedvalue_to_zval(
		const CORBA_NamedValue * pSource, zval * pDestination)
{
	if (pSource == NULL)
		return FALSE;

	return satellite_any_to_zval(&pSource->argument, pDestination);
}