-
Notifications
You must be signed in to change notification settings - Fork 1
/
PHPArray.cpp
147 lines (117 loc) · 2.86 KB
/
PHPArray.cpp
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
#include "StdAfx.h"
#include <zend.h>
#include <zend_list.h>
#include <zend_exceptions.h>
#include <zend_interfaces.h>
#include "PHPArray.h"
bool get_next_num_item(zval* arr, HashPosition* pos)
{
int key_type = zend_hash_get_current_key_type_ex(Z_ARRVAL_P(arr), pos);
while(key_type != HASH_KEY_IS_LONG && key_type != HASH_KEY_NON_EXISTANT)
{
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), pos);
key_type = zend_hash_get_current_key_type_ex(Z_ARRVAL_P(arr), pos);
};
return key_type == HASH_KEY_IS_LONG;
};
CPHPArray::CPHPArray(void)
{
}
CPHPArray::CPHPArray(zval* arr)
{
m_arr = arr;
ZVAL_ADDREF(m_arr);
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(m_arr), &m_pos);
get_next_num_item(m_arr, &m_pos);
};
CPHPArray::~CPHPArray(void)
{
ZVAL_DELREF(m_arr);
};
void CPHPArray::my_free()
{
delete this;
};
int CPHPArray::get_count()
{
return zend_hash_num_elements(Z_ARRVAL_P(m_arr));
};
inline void get_item_helper(zval* zvar, CSimpleTypeVar* var)
{
if(var)
{
switch(Z_TYPE_P(zvar))
{
case IS_STRING:
var->type = CSimpleTypeVar::VAR_STRING;
var->sval.str = Z_STRVAL_P(zvar);
var->sval.str_l = Z_STRLEN_P(zvar);
break;
case IS_DOUBLE:
var->type = CSimpleTypeVar::VAR_FLOAT;
var->fval = Z_DVAL_P(zvar);
break;
case IS_LONG:
var->type = CSimpleTypeVar::VAR_LONG;
var->lval = Z_LVAL_P(zvar);
break;
case IS_ARRAY:
var->type = CSimpleTypeVar::VAR_ARRAY;
var->aval = new CPHPArray(zvar);
break;
default:
var->type = CSimpleTypeVar::VAR_NULL;
};
};
};
bool CPHPArray::get_item(int i, CSimpleTypeVar* var)
{
zval **tmp;
bool retval = false;
if(zend_hash_index_find(Z_ARRVAL_P(m_arr), i, (void**)&tmp) == SUCCESS)
{
retval = true;
get_item_helper(*tmp, var);
};
return retval;
};
int CPHPArray::get_key()
{
ulong retval;
zend_hash_get_current_key_ex(Z_ARRVAL_P(m_arr), NULL, NULL, &retval, 0, &m_pos);
return retval;
};
bool CPHPArray::get_value(CSimpleTypeVar* var)
{
zval **tmp;
bool retval = false;
if(zend_hash_get_current_data_ex(Z_ARRVAL_P(m_arr), (void**) &tmp, &m_pos) == SUCCESS)
{
get_item_helper(*tmp, var);
retval = true;
};
return retval;
};
bool CPHPArray::get_valid()
{
zval **tmp;
return (zend_hash_get_current_data_ex(Z_ARRVAL_P(m_arr), (void **) &tmp, &m_pos) == SUCCESS);
};
void CPHPArray::reset()
{
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(m_arr), &m_pos);
get_next_num_item(m_arr, &m_pos);
};
bool CPHPArray::next(CSimpleTypeVar* var)
{
zval **tmp;
bool retval = false;
if(zend_hash_get_current_data_ex(Z_ARRVAL_P(m_arr), (void **) &tmp, &m_pos) == SUCCESS)
{
retval = true;
get_item_helper(*tmp, var);
zend_hash_move_forward_ex(Z_ARRVAL_P(m_arr), &m_pos);
get_next_num_item(m_arr, &m_pos);
};
return retval;
};