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
19 #include "resourcebundle/resourcebundle.h"
20 #include "resourcebundle/resourcebundle_class.h"
21 #include "resourcebundle/resourcebundle_iterator.h"
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 */
resourcebundle_iterator_read(ResourceBundle_iterator * iterator)30 static 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 */
resourcebundle_iterator_invalidate(zend_object_iterator * iter)51 static void resourcebundle_iterator_invalidate( zend_object_iterator *iter )
52 {
53 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
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 */
resourcebundle_iterator_dtor(zend_object_iterator * iter)67 static void resourcebundle_iterator_dtor( zend_object_iterator *iter )
68 {
69 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
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 */
resourcebundle_iterator_has_more(zend_object_iterator * iter)79 static zend_result resourcebundle_iterator_has_more( zend_object_iterator *iter )
80 {
81 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
82 return (iterator->i < iterator->length) ? SUCCESS : FAILURE;
83 }
84 /* }}} */
85
86 /* {{{ resourcebundle_iterator_current */
resourcebundle_iterator_current(zend_object_iterator * iter)87 static zval *resourcebundle_iterator_current( zend_object_iterator *iter )
88 {
89 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
90 if (Z_ISUNDEF(iterator->current)) {
91 resourcebundle_iterator_read( iterator);
92 }
93 return &iterator->current;
94 }
95 /* }}} */
96
97 /* {{{ resourcebundle_iterator_key */
resourcebundle_iterator_key(zend_object_iterator * iter,zval * key)98 static void resourcebundle_iterator_key( zend_object_iterator *iter, zval *key )
99 {
100 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
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 */
resourcebundle_iterator_step(zend_object_iterator * iter)115 static void resourcebundle_iterator_step( zend_object_iterator *iter )
116 {
117 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
118
119 iterator->i++;
120 resourcebundle_iterator_invalidate( iter );
121 }
122 /* }}} */
123
124 /* {{{ resourcebundle_iterator_has_reset */
resourcebundle_iterator_reset(zend_object_iterator * iter)125 static void resourcebundle_iterator_reset( zend_object_iterator *iter )
126 {
127 ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
128
129 iterator->i = 0;
130 resourcebundle_iterator_invalidate( iter );
131 }
132 /* }}} */
133
134 /* {{{ resourcebundle_iterator_funcs */
135 static 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 */
resourcebundle_get_iterator(zend_class_entry * ce,zval * object,int byref)148 zend_object_iterator *resourcebundle_get_iterator( zend_class_entry *ce, zval *object, int byref )
149 {
150 if (byref) {
151 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
152 return NULL;
153 }
154
155 ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P(object );
156 ResourceBundle_iterator *iterator = emalloc( sizeof( ResourceBundle_iterator ) );
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 /* }}} */
178