php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
resourcebundle_iterator.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | https://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch> |
12 +----------------------------------------------------------------------+
13 */
14
15#include <php.h>
16#include <zend.h>
17#include <zend_API.h>
18
22
23/*
24 * Although libicu offers iterator functions, they are not used here: libicu does iterate
25 * irrespective of array indices. Those cannot be recreated afterwards. Arrays as well as tables
26 * can however be accessed by numerical index, with table keys readable ex post.
27 */
28
29/* {{{ resourcebundle_iterator_read */
30static void resourcebundle_iterator_read( ResourceBundle_iterator *iterator )
31{
32 UErrorCode icuerror = U_ZERO_ERROR;
33 ResourceBundle_object *rb = iterator->subject;
34
35 rb->child = ures_getByIndex( rb->me, iterator->i, rb->child, &icuerror );
36
37 if (U_SUCCESS(icuerror)) {
38 /* ATTN: key extraction must be the first thing to do... rb->child might be reset in read! */
39 if (iterator->is_table) {
40 iterator->currentkey = estrdup( ures_getKey( rb->child ) );
41 }
42 resourcebundle_extract_value( &iterator->current, rb );
43 }
44 else {
45 ZVAL_UNDEF(&iterator->current);
46 }
47}
48/* }}} */
49
50/* {{{ resourcebundle_iterator_invalidate */
51static void resourcebundle_iterator_invalidate( zend_object_iterator *iter )
52{
54
55 if (!Z_ISUNDEF(iterator->current)) {
56 zval_ptr_dtor( &iterator->current );
57 ZVAL_UNDEF(&iterator->current);
58 }
59 if (iterator->currentkey) {
60 efree( iterator->currentkey );
61 iterator->currentkey = NULL;
62 }
63}
64/* }}} */
65
66/* {{{ resourcebundle_iterator_dtor */
67static void resourcebundle_iterator_dtor( zend_object_iterator *iter )
68{
70 zval *object = &iterator->intern.data;
71
72 resourcebundle_iterator_invalidate( iter );
73
74 zval_ptr_dtor(object);
75}
76/* }}} */
77
78/* {{{ resourcebundle_iterator_has_more */
79static zend_result resourcebundle_iterator_has_more( zend_object_iterator *iter )
80{
82 return (iterator->i < iterator->length) ? SUCCESS : FAILURE;
83}
84/* }}} */
85
86/* {{{ resourcebundle_iterator_current */
87static zval *resourcebundle_iterator_current( zend_object_iterator *iter )
88{
90 if (Z_ISUNDEF(iterator->current)) {
91 resourcebundle_iterator_read( iterator);
92 }
93 return &iterator->current;
94}
95/* }}} */
96
97/* {{{ resourcebundle_iterator_key */
98static void resourcebundle_iterator_key( zend_object_iterator *iter, zval *key )
99{
101
102 if (Z_ISUNDEF(iterator->current)) {
103 resourcebundle_iterator_read( iterator);
104 }
105
106 if (iterator->is_table) {
107 ZVAL_STRING(key, iterator->currentkey);
108 } else {
109 ZVAL_LONG(key, iterator->i);
110 }
111}
112/* }}} */
113
114/* {{{ resourcebundle_iterator_step */
115static void resourcebundle_iterator_step( zend_object_iterator *iter )
116{
118
119 iterator->i++;
120 resourcebundle_iterator_invalidate( iter );
121}
122/* }}} */
123
124/* {{{ resourcebundle_iterator_has_reset */
125static void resourcebundle_iterator_reset( zend_object_iterator *iter )
126{
128
129 iterator->i = 0;
130 resourcebundle_iterator_invalidate( iter );
131}
132/* }}} */
133
134/* {{{ resourcebundle_iterator_funcs */
135static const zend_object_iterator_funcs resourcebundle_iterator_funcs = {
136 resourcebundle_iterator_dtor,
137 resourcebundle_iterator_has_more,
138 resourcebundle_iterator_current,
139 resourcebundle_iterator_key,
140 resourcebundle_iterator_step,
141 resourcebundle_iterator_reset,
142 resourcebundle_iterator_invalidate,
143 NULL, /* get_gc */
144};
145/* }}} */
146
147/* {{{ resourcebundle_get_iterator */
149{
150 if (byref) {
151 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
152 return NULL;
153 }
154
157
158 zend_iterator_init(&iterator->intern);
159 Z_ADDREF_P(object);
160 ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
161 iterator->intern.funcs = &resourcebundle_iterator_funcs;
162
163 iterator->subject = rb;
164
165 /* The iterated rb can only be either URES_TABLE or URES_ARRAY
166 * All other types are returned as php primitives!
167 */
168 iterator->is_table = (ures_getType( rb->me ) == URES_TABLE);
169 iterator->length = ures_getSize( rb->me );
170
171 ZVAL_UNDEF(&iterator->current);
172 iterator->currentkey = NULL;
173 iterator->i = 0;
174
175 return (zend_object_iterator *) iterator;
176}
177/* }}} */
const U_ZERO_ERROR
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
unsigned char key[REFLECTION_KEY_LEN]
void resourcebundle_extract_value(zval *return_value, ResourceBundle_object *source)
#define Z_INTL_RESOURCEBUNDLE_P(zv)
zend_object_iterator * resourcebundle_get_iterator(zend_class_entry *ce, zval *object, int byref)
ResourceBundle_object * subject
const zend_object_iterator_funcs * funcs
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
ZEND_API void zend_iterator_init(zend_object_iterator *iter)
struct _zend_object_iterator zend_object_iterator
struct _zend_object_iterator_funcs zend_object_iterator_funcs
struct _zend_class_entry zend_class_entry
#define ZVAL_UNDEF(z)
#define ZVAL_LONG(z, l)
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_ADDREF_P(pz)
#define ZVAL_OBJ(z, o)
@ FAILURE
Definition zend_types.h:61
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
ZEND_API void zval_ptr_dtor(zval *zval_ptr)