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
|
/*
+----------------------------------------------------------------------+
| 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
*/
/* -----------------------------------------------------------------------
*
* OrbitEnum class
*
* -----------------------------------------------------------------------
*/
#include "enum.h"
#include "typemanager.h"
#include "common.h"
#include "hashtable.h"
struct _OrbitEnum
{
EnumType * mpEnumType;
HashTable mMembers;
};
IMPLEMENT_CLASS(OrbitEnum, CONSTRUCTOR|DESTRUCTOR|GET_PROPERTY)
static zend_bool OrbitEnum_InitializeMembers(OrbitEnum * pEnum)
{
int enum_value = 0;
EnumMemberType * p_member = NULL;
p_member = EnumType_GetFirstMember(pEnum->mpEnumType);
zend_hash_init(
&pEnum->mMembers, /* hash table */
0, /* size */
NULL, /* hash function */
ZVAL_DESTRUCTOR, /* destructor */
0); /* persistent */
if (!EnumMemberType_IsValid(p_member))
return TRUE; /* no members */
do
{
zval * p_value = NULL;
char * p_name = EnumMemberType_GetName(p_member);
ALLOC_ZVAL(p_value);
ZVAL_LONG(p_value, enum_value);
zend_hash_add(
&pEnum->mMembers,
p_name,
strlen(p_name)+1,
p_value,
sizeof(zval),
NULL
);
/* set value for next member */
enum_value++;
} while (EnumMemberType_GetNext(p_member));
return TRUE;
}
zend_bool OrbitEnum_Constructor(OrbitEnum ** ppEnum, int parameterCount,
const zval ** ppParameters)
{
OrbitEnum * p_enum = orbit_new(OrbitEnum);
/* check parameter count */
if (parameterCount != 1)
{
zend_wrong_param_count(TSRMLS_C);
goto error;
}
/* validate parameter types */
if (Z_TYPE_P(ppParameters[0]) != IS_STRING)
goto error;
/* find type information */
p_enum->mpEnumType = TypeManager_FindEnum(Z_STRVAL_P(ppParameters[0]));
if (p_enum->mpEnumType == NULL)
{
zend_error(E_WARNING, "(Satellite) unknown enum '%s'", Z_STRVAL_P(ppParameters[0]));
goto error;
}
/* initialize members */
if (!OrbitEnum_InitializeMembers(p_enum))
goto error;
*ppEnum = p_enum;
return TRUE;
error:
/* printf("OrbitEnum_Constructor failed\n");*/
OrbitEnum_Destructor(p_enum);
*ppEnum = NULL;
return FALSE;
}
zend_bool OrbitEnum_Destructor(OrbitEnum * pEnum)
{
/* printf("OrbitEnum_Destructor\n");*/
if (pEnum != NULL)
{
orbit_delete(pEnum->mpEnumType);
}
zend_hash_destroy(&pEnum->mMembers);
orbit_delete(pEnum);
return TRUE;
}
/* not used */
zend_bool OrbitEnum_CallFunction(OrbitEnum * pEnum,
const char * pFunctionName, int parameterCount, const zval ** ppParameters, zval * pResult)
{
return FALSE;
}
/* not used */
zend_bool OrbitEnum_PutProperty(OrbitEnum * pEnum,
const char * pPropertyName, const zval * pValue)
{
return FALSE;
}
zend_bool OrbitEnum_GetProperty(OrbitEnum * pEnum,
const char * pPropertyName, zval * pReturnValue)
{
zval * p_value = orbit_find_by_key(&pEnum->mMembers, pPropertyName);
if (p_value == NULL)
{
zend_error(E_WARNING, "(Satellite) unknown member '%s' in enum '%s'",
pPropertyName, EnumType_GetRepositoryId(pEnum->mpEnumType));
ZVAL_NULL(pReturnValue);
return FALSE;
}
else
{
memcpy(pReturnValue, p_value, sizeof(zval)); /* raw data copy */
zval_copy_ctor(pReturnValue); /* smart data copy */
INIT_PZVAL(pReturnValue); /* set reference count */
return TRUE;
}
}
|