-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
257 lines (213 loc) · 6.25 KB
/
array.c
File metadata and controls
257 lines (213 loc) · 6.25 KB
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
/*
+----------------------------------------------------------------------+
| swoole_orm |
+----------------------------------------------------------------------+
| swoole_orm is a database ORM ext for mysql written in c language |
+----------------------------------------------------------------------+
| LICENSE: https://github.com/caohao0730/swoole_orm/blob/master/LICENSE|
+----------------------------------------------------------------------+
| Author: Cao Hao <649947921@qq.com> |
| CreateTime: 2018-11-19 |
+----------------------------------------------------------------------+
*/
#include "swoole_orm.h"
//根据数组下标从数组中获取字符串
char* sw_orm_get_string_from_array_index(zval *array, ulong index)
{
zval *pData = NULL;
sw_orm_zend_hash_index_find(Z_ARRVAL_P(array), index, (void**) &pData);
if(pData == NULL){
return NULL;
}
if(Z_TYPE_P(pData) != IS_STRING) {
return NULL;
}
char * str = Z_STRVAL_P(pData);
return str;
}
//获取数组的 array_keys ,注意不用了销毁返回的HashTable指针指向的内存地址
HashTable* sw_orm_get_array_keys(zval *p) {
if(SW_ORM_IS_NOT_ARRAY(p)) {
return NULL;
}
uint32_t array_size = zend_hash_num_elements(Z_ARRVAL_P(p));
if(array_size == 0) {
return NULL;
}
char * key;
zval *value;
uint32_t key_len;
int key_type;
ulong_t num = 0;
HashTable *new_hash;
ALLOC_HASHTABLE(new_hash);
zend_hash_init(new_hash, array_size, NULL, ZVAL_PTR_DTOR, 0);
SW_ORM_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(p), key, key_len, key_type, value)
if (HASH_KEY_IS_STRING != key_type) { //非字符串
continue;
}
zval *zval_key;
SW_ORM_MAKE_STD_ZVAL(zval_key);
SW_ORM_ZVAL_STRING(zval_key, key, 1);
sw_orm_zend_hash_index_update(new_hash, num, (void*) zval_key, sizeof(zval *), NULL);
sw_orm_zval_ptr_dtor(&zval_key);
num++;
SW_ORM_HASHTABLE_FOREACH_END();
if(zend_hash_num_elements(new_hash) == 0) {
sw_orm_free_hash(new_hash);
new_hash = NULL;
}
return new_hash;
}
//获取数组的第N个key
char* sw_orm_get_array_key_index(zval *p, uint32_t index) {
if(SW_ORM_IS_NOT_ARRAY(p)) {
return NULL;
}
uint32_t array_size = zend_hash_num_elements(Z_ARRVAL_P(p));
if(array_size < index) {
return NULL;
}
char * key;
zval *value;
uint32_t key_len;
int key_type;
ulong_t num = 0;
SW_ORM_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(p), key, key_len, key_type, value)
if (HASH_KEY_IS_STRING != key_type) { //非字符串
continue;
}
if(num == index) {
return key;
}
num++;
SW_ORM_HASHTABLE_FOREACH_END();
return NULL;
}
//根据 index 获取 hashtable 的元素
zval* sw_orm_get_element_by_hashtable_index(HashTable *ht, int index) {
if(ht == NULL) {
return NULL;
}
zval *value;
sw_orm_zend_hash_index_find(ht, index, (void**) &value);
return value;
}
//根据 key 获取 hashtable 的元素
zval* php_sw_orm_array_get_value(HashTable *ht, char *key) {
zval *v;
if( sw_orm_zend_hash_find(ht, key, strlen(key), (void **) &v) == SUCCESS ) {
if(ZVAL_IS_NULL(v)) {
return NULL;
} else {
return v;
}
} else {
return NULL;
}
}
//销毁 HashTable, 删除HashTable的数据, 并销毁释放 HashTable 内存
void sw_orm_destroy_hash(HashTable *ht) {
uint32_t array_size = zend_hash_num_elements(ht);
if(array_size == 0) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
return;
}
char *key;
zval *value;
uint32_t key_len;
int key_type;
SW_ORM_HASHTABLE_FOREACH_START2(ht, key, key_len, key_type, value)
if(SW_ORM_IS_ARRAY(value)) {
sw_orm_destroy_hash(Z_ARRVAL_P(value));
}
sw_orm_zval_ptr_dtor(&value);
SW_ORM_HASHTABLE_FOREACH_END();
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
}
//销毁数组, 删除其中HashTable的数据, 并销毁释放 HashTable 内存,并销毁数组内存,将数组指针置为NULL
void sw_orm_destroy_array(zval **array) {
sw_orm_destroy_hash(Z_ARRVAL_P(*array));
sw_orm_zval_ptr_dtor(array);
*array = NULL;
}
//清理 HashTable 内的数据元素。
void sw_orm_clean_hash(HashTable *ht) {
uint32_t array_size = zend_hash_num_elements(ht);
if(array_size == 0) {
return;
}
char *key;
zval *value;
uint32_t key_len;
int key_type;
SW_ORM_HASHTABLE_FOREACH_START2(ht, key, key_len, key_type, value)
if(SW_ORM_IS_ARRAY(value)) {
sw_orm_clean_hash(Z_ARRVAL_P(value));
}
sw_orm_zval_ptr_dtor(&value);
SW_ORM_HASHTABLE_FOREACH_END();
}
int is_set_array_index(HashTable *ht, int index) {
zval* p = sw_orm_get_element_by_hashtable_index(ht, index);
if(SW_ORM_IS_NOT_EMPTY(p)) {
return 1;
} else {
return 0;
}
}
char* sw_orm_get_string_from_hashtable_index(HashTable *ht, int index) {
zval* val = sw_orm_get_element_by_hashtable_index(ht, index);
if(SW_ORM_IS_EMPTY(val)) {
return NULL;
}
if(Z_TYPE_P(val) == IS_STRING) {
return Z_STRVAL_P(val);
} else {
return NULL;
}
}
char* sw_orm_get_string_from_hashtable(HashTable *ht, char* key) {
zval* val = php_sw_orm_array_get_value(ht, key);
if(SW_ORM_IS_EMPTY(val)) {
return NULL;
}
if(Z_TYPE_P(val) == IS_STRING) {
return Z_STRVAL_P(val);
} else {
return NULL;
}
}
//php implode 功能
char* sw_orm_implode(zval *arr, const char *delim_str, char** result)
{
zval *return_value = NULL;
SW_ORM_MAKE_STD_ZVAL(return_value);
zend_string *delim = zend_string_init(delim_str, strlen(delim_str), 0);
php_implode(delim, arr, return_value);
sw_orm_multi_memcpy_auto_realloc(result, 1, Z_STRVAL_P(return_value));
efree(delim);
sw_orm_zval_ptr_dtor(&return_value);
return *result;
}
//单元素数组重组
void sw_orm_array_single_columns(zval** return_single_column_result, zval* data) {
array_init(*return_single_column_result);
char *key;
zval *value;
uint32_t key_len;
int key_type;
SW_ORM_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(data), key, key_len, key_type, value)
char *key2;
zval *value2;
uint32_t key_len2;
int key_type2;
SW_ORM_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(value), key2, key_len2, key_type2, value2)
zval *copy = sw_orm_zval_copy(value2);
add_next_index_zval(*return_single_column_result, copy);
break;
SW_ORM_HASHTABLE_FOREACH_END();
SW_ORM_HASHTABLE_FOREACH_END();
}