1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Marcus Boerger <helly@php.net>                              |
16   +----------------------------------------------------------------------+
17 */
18
19/* $Id$ */
20
21#ifdef HAVE_CONFIG_H
22# include "config.h"
23#endif
24
25#include "php.h"
26#include "php_ini.h"
27#include "ext/standard/info.h"
28#include "zend_exceptions.h"
29#include "zend_interfaces.h"
30
31#include "php_spl.h"
32#include "spl_functions.h"
33#include "spl_engine.h"
34#include "spl_iterators.h"
35#include "spl_directory.h"
36#include "spl_array.h"
37#include "spl_exceptions.h"
38#include "ext/standard/php_smart_str.h"
39
40#ifdef accept
41#undef accept
42#endif
43
44PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
45PHPAPI zend_class_entry *spl_ce_RecursiveIteratorIterator;
46PHPAPI zend_class_entry *spl_ce_FilterIterator;
47PHPAPI zend_class_entry *spl_ce_CallbackFilterIterator;
48PHPAPI zend_class_entry *spl_ce_RecursiveFilterIterator;
49PHPAPI zend_class_entry *spl_ce_RecursiveCallbackFilterIterator;
50PHPAPI zend_class_entry *spl_ce_ParentIterator;
51PHPAPI zend_class_entry *spl_ce_SeekableIterator;
52PHPAPI zend_class_entry *spl_ce_LimitIterator;
53PHPAPI zend_class_entry *spl_ce_CachingIterator;
54PHPAPI zend_class_entry *spl_ce_RecursiveCachingIterator;
55PHPAPI zend_class_entry *spl_ce_OuterIterator;
56PHPAPI zend_class_entry *spl_ce_IteratorIterator;
57PHPAPI zend_class_entry *spl_ce_NoRewindIterator;
58PHPAPI zend_class_entry *spl_ce_InfiniteIterator;
59PHPAPI zend_class_entry *spl_ce_EmptyIterator;
60PHPAPI zend_class_entry *spl_ce_AppendIterator;
61PHPAPI zend_class_entry *spl_ce_RegexIterator;
62PHPAPI zend_class_entry *spl_ce_RecursiveRegexIterator;
63PHPAPI zend_class_entry *spl_ce_Countable;
64PHPAPI zend_class_entry *spl_ce_RecursiveTreeIterator;
65
66ZEND_BEGIN_ARG_INFO(arginfo_recursive_it_void, 0)
67ZEND_END_ARG_INFO()
68
69const zend_function_entry spl_funcs_RecursiveIterator[] = {
70    SPL_ABSTRACT_ME(RecursiveIterator, hasChildren,  arginfo_recursive_it_void)
71    SPL_ABSTRACT_ME(RecursiveIterator, getChildren,  arginfo_recursive_it_void)
72    PHP_FE_END
73};
74
75typedef enum {
76    RIT_LEAVES_ONLY = 0,
77    RIT_SELF_FIRST  = 1,
78    RIT_CHILD_FIRST = 2
79} RecursiveIteratorMode;
80
81#define RIT_CATCH_GET_CHILD CIT_CATCH_GET_CHILD
82
83typedef enum {
84    RTIT_BYPASS_CURRENT = 4,
85    RTIT_BYPASS_KEY     = 8
86} RecursiveTreeIteratorFlags;
87
88typedef enum {
89    RS_NEXT  = 0,
90    RS_TEST  = 1,
91    RS_SELF  = 2,
92    RS_CHILD = 3,
93    RS_START = 4
94} RecursiveIteratorState;
95
96typedef struct _spl_sub_iterator {
97    zend_object_iterator    *iterator;
98    zval                    *zobject;
99    zend_class_entry        *ce;
100    RecursiveIteratorState  state;
101} spl_sub_iterator;
102
103typedef struct _spl_recursive_it_object {
104    zend_object              std;
105    spl_sub_iterator         *iterators;
106    int                      level;
107    RecursiveIteratorMode    mode;
108    int                      flags;
109    int                      max_depth;
110    zend_bool                in_iteration;
111    zend_function            *beginIteration;
112    zend_function            *endIteration;
113    zend_function            *callHasChildren;
114    zend_function            *callGetChildren;
115    zend_function            *beginChildren;
116    zend_function            *endChildren;
117    zend_function            *nextElement;
118    zend_class_entry         *ce;
119    smart_str                prefix[6];
120    smart_str                postfix[1];
121} spl_recursive_it_object;
122
123typedef struct _spl_recursive_it_iterator {
124    zend_object_iterator   intern;
125    zval                   *zobject;
126} spl_recursive_it_iterator;
127
128static zend_object_handlers spl_handlers_rec_it_it;
129static zend_object_handlers spl_handlers_dual_it;
130
131#define SPL_FETCH_AND_CHECK_DUAL_IT(var, objzval) \
132    do { \
133        spl_dual_it_object *it = zend_object_store_get_object((objzval) TSRMLS_CC); \
134        if (it->dit_type == DIT_Unknown) { \
135            zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, \
136                "The object is in an invalid state as the parent constructor was not called"); \
137            return; \
138        } \
139        (var) = it; \
140    } while (0)
141
142#define SPL_FETCH_SUB_ELEMENT(var, object, element) \
143    do { \
144        if(!(object)->iterators) { \
145            zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, \
146                "The object is in an invalid state as the parent constructor was not called"); \
147            return; \
148        } \
149        (var) = (object)->iterators[(object)->level].element; \
150    } while (0)
151
152#define SPL_FETCH_SUB_ITERATOR(var, object) SPL_FETCH_SUB_ELEMENT(var, object, iterator)
153
154
155static void spl_recursive_it_dtor(zend_object_iterator *_iter TSRMLS_DC)
156{
157    spl_recursive_it_iterator *iter   = (spl_recursive_it_iterator*)_iter;
158    spl_recursive_it_object   *object = (spl_recursive_it_object*)_iter->data;
159    zend_object_iterator      *sub_iter;
160
161    while (object->level > 0) {
162        sub_iter = object->iterators[object->level].iterator;
163        sub_iter->funcs->dtor(sub_iter TSRMLS_CC);
164        zval_ptr_dtor(&object->iterators[object->level--].zobject);
165    }
166    object->iterators = erealloc(object->iterators, sizeof(spl_sub_iterator));
167    object->level = 0;
168
169    zval_ptr_dtor(&iter->zobject);
170    efree(iter);
171}
172
173static int spl_recursive_it_valid_ex(spl_recursive_it_object *object, zval *zthis TSRMLS_DC)
174{
175    zend_object_iterator      *sub_iter;
176    int                       level = object->level;
177
178    if(!object->iterators) {
179        return FAILURE;
180    }
181    while (level >=0) {
182        sub_iter = object->iterators[level].iterator;
183        if (sub_iter->funcs->valid(sub_iter TSRMLS_CC) == SUCCESS) {
184            return SUCCESS;
185        }
186        level--;
187    }
188    if (object->endIteration && object->in_iteration) {
189        zend_call_method_with_0_params(&zthis, object->ce, &object->endIteration, "endIteration", NULL);
190    }
191    object->in_iteration = 0;
192    return FAILURE;
193}
194
195static int spl_recursive_it_valid(zend_object_iterator *iter TSRMLS_DC)
196{
197    spl_recursive_it_object   *object = (spl_recursive_it_object*)iter->data;
198
199    return spl_recursive_it_valid_ex(object, ((spl_recursive_it_iterator*)iter)->zobject TSRMLS_CC);
200}
201
202static void spl_recursive_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
203{
204    spl_recursive_it_object   *object = (spl_recursive_it_object*)iter->data;
205    zend_object_iterator      *sub_iter = object->iterators[object->level].iterator;
206
207    sub_iter->funcs->get_current_data(sub_iter, data TSRMLS_CC);
208}
209
210static void spl_recursive_it_get_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC)
211{
212    spl_recursive_it_object   *object = (spl_recursive_it_object*)iter->data;
213    zend_object_iterator      *sub_iter = object->iterators[object->level].iterator;
214
215    if (sub_iter->funcs->get_current_key) {
216        sub_iter->funcs->get_current_key(sub_iter, key TSRMLS_CC);
217    } else {
218        ZVAL_LONG(key, iter->index);
219    }
220}
221
222static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zval *zthis TSRMLS_DC)
223{
224    zend_object_iterator      *iterator;
225    zval                      *zobject;
226    zend_class_entry          *ce;
227    zval                      *retval, *child;
228    zend_object_iterator      *sub_iter;
229    int                       has_children;
230
231    SPL_FETCH_SUB_ITERATOR(iterator, object);
232
233    while (!EG(exception)) {
234next_step:
235        iterator = object->iterators[object->level].iterator;
236        switch (object->iterators[object->level].state) {
237            case RS_NEXT:
238                iterator->funcs->move_forward(iterator TSRMLS_CC);
239                if (EG(exception)) {
240                    if (!(object->flags & RIT_CATCH_GET_CHILD)) {
241                        return;
242                    } else {
243                        zend_clear_exception(TSRMLS_C);
244                    }
245                }
246                /* fall through */
247            case RS_START:
248                if (iterator->funcs->valid(iterator TSRMLS_CC) == FAILURE) {
249                    break;
250                }
251                object->iterators[object->level].state = RS_TEST;
252                /* break; */
253            case RS_TEST:
254                ce = object->iterators[object->level].ce;
255                zobject = object->iterators[object->level].zobject;
256                if (object->callHasChildren) {
257                    zend_call_method_with_0_params(&zthis, object->ce, &object->callHasChildren, "callHasChildren", &retval);
258                } else {
259                    zend_call_method_with_0_params(&zobject, ce, NULL, "haschildren", &retval);
260                }
261                if (EG(exception)) {
262                    if (!(object->flags & RIT_CATCH_GET_CHILD)) {
263                        object->iterators[object->level].state = RS_NEXT;
264                        return;
265                    } else {
266                        zend_clear_exception(TSRMLS_C);
267                    }
268                }
269                if (retval) {
270                    has_children = zend_is_true(retval);
271                    zval_ptr_dtor(&retval);
272                    if (has_children) {
273                        if (object->max_depth == -1 || object->max_depth > object->level) {
274                            switch (object->mode) {
275                            case RIT_LEAVES_ONLY:
276                            case RIT_CHILD_FIRST:
277                                object->iterators[object->level].state = RS_CHILD;
278                                goto next_step;
279                            case RIT_SELF_FIRST:
280                                object->iterators[object->level].state = RS_SELF;
281                                goto next_step;
282                            }
283                        } else {
284                            /* do not recurse into */
285                            if (object->mode == RIT_LEAVES_ONLY) {
286                                /* this is not a leave, so skip it */
287                                object->iterators[object->level].state = RS_NEXT;
288                                goto next_step;
289                            }
290                        }
291                    }
292                }
293                if (object->nextElement) {
294                    zend_call_method_with_0_params(&zthis, object->ce, &object->nextElement, "nextelement", NULL);
295                }
296                object->iterators[object->level].state = RS_NEXT;
297                if (EG(exception)) {
298                    if (!(object->flags & RIT_CATCH_GET_CHILD)) {
299                        return;
300                    } else {
301                        zend_clear_exception(TSRMLS_C);
302                    }
303                }
304                return /* self */;
305            case RS_SELF:
306                if (object->nextElement && (object->mode == RIT_SELF_FIRST || object->mode == RIT_CHILD_FIRST)) {
307                    zend_call_method_with_0_params(&zthis, object->ce, &object->nextElement, "nextelement", NULL);
308                }
309                if (object->mode == RIT_SELF_FIRST) {
310                    object->iterators[object->level].state = RS_CHILD;
311                } else {
312                    object->iterators[object->level].state = RS_NEXT;
313                }
314                return /* self */;
315            case RS_CHILD:
316                ce = object->iterators[object->level].ce;
317                zobject = object->iterators[object->level].zobject;
318                if (object->callGetChildren) {
319                    zend_call_method_with_0_params(&zthis, object->ce, &object->callGetChildren, "callGetChildren", &child);
320                } else {
321                    zend_call_method_with_0_params(&zobject, ce, NULL, "getchildren", &child);
322                }
323
324                if (EG(exception)) {
325                    if (!(object->flags & RIT_CATCH_GET_CHILD)) {
326                        return;
327                    } else {
328                        zend_clear_exception(TSRMLS_C);
329                        if (child) {
330                            zval_ptr_dtor(&child);
331                        }
332                        object->iterators[object->level].state = RS_NEXT;
333                        goto next_step;
334                    }
335                }
336
337                ce = child && Z_TYPE_P(child) == IS_OBJECT ? Z_OBJCE_P(child) : NULL;
338                if (!ce || !instanceof_function(ce, spl_ce_RecursiveIterator TSRMLS_CC)) {
339                    if (child) {
340                        zval_ptr_dtor(&child);
341                    }
342                    zend_throw_exception(spl_ce_UnexpectedValueException, "Objects returned by RecursiveIterator::getChildren() must implement RecursiveIterator", 0 TSRMLS_CC);
343                    return;
344                }
345                if (object->mode == RIT_CHILD_FIRST) {
346                    object->iterators[object->level].state = RS_SELF;
347                } else {
348                    object->iterators[object->level].state = RS_NEXT;
349                }
350                object->iterators = erealloc(object->iterators, sizeof(spl_sub_iterator) * (++object->level+1));
351                sub_iter = ce->get_iterator(ce, child, 0 TSRMLS_CC);
352                object->iterators[object->level].iterator = sub_iter;
353                object->iterators[object->level].zobject = child;
354                object->iterators[object->level].ce = ce;
355                object->iterators[object->level].state = RS_START;
356                if (sub_iter->funcs->rewind) {
357                    sub_iter->funcs->rewind(sub_iter TSRMLS_CC);
358                }
359                if (object->beginChildren) {
360                    zend_call_method_with_0_params(&zthis, object->ce, &object->beginChildren, "beginchildren", NULL);
361                    if (EG(exception)) {
362                        if (!(object->flags & RIT_CATCH_GET_CHILD)) {
363                            return;
364                        } else {
365                            zend_clear_exception(TSRMLS_C);
366                        }
367                    }
368                }
369                goto next_step;
370        }
371        /* no more elements */
372        if (object->level > 0) {
373            if (object->endChildren) {
374                zend_call_method_with_0_params(&zthis, object->ce, &object->endChildren, "endchildren", NULL);
375                if (EG(exception)) {
376                    if (!(object->flags & RIT_CATCH_GET_CHILD)) {
377                        return;
378                    } else {
379                        zend_clear_exception(TSRMLS_C);
380                    }
381                }
382            }
383            iterator->funcs->dtor(iterator TSRMLS_CC);
384            zval_ptr_dtor(&object->iterators[object->level].zobject);
385            object->level--;
386        } else {
387            return; /* done completeley */
388        }
389    }
390}
391
392static void spl_recursive_it_rewind_ex(spl_recursive_it_object *object, zval *zthis TSRMLS_DC)
393{
394    zend_object_iterator      *sub_iter;
395
396    SPL_FETCH_SUB_ITERATOR(sub_iter, object);
397
398    while (object->level) {
399        sub_iter = object->iterators[object->level].iterator;
400        sub_iter->funcs->dtor(sub_iter TSRMLS_CC);
401        zval_ptr_dtor(&object->iterators[object->level--].zobject);
402        if (!EG(exception) && (!object->endChildren || object->endChildren->common.scope != spl_ce_RecursiveIteratorIterator)) {
403            zend_call_method_with_0_params(&zthis, object->ce, &object->endChildren, "endchildren", NULL);
404        }
405    }
406    object->iterators = erealloc(object->iterators, sizeof(spl_sub_iterator));
407    object->iterators[0].state = RS_START;
408    sub_iter = object->iterators[0].iterator;
409    if (sub_iter->funcs->rewind) {
410        sub_iter->funcs->rewind(sub_iter TSRMLS_CC);
411    }
412    if (!EG(exception) && object->beginIteration && !object->in_iteration) {
413        zend_call_method_with_0_params(&zthis, object->ce, &object->beginIteration, "beginIteration", NULL);
414    }
415    object->in_iteration = 1;
416    spl_recursive_it_move_forward_ex(object, zthis TSRMLS_CC);
417}
418
419static void spl_recursive_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
420{
421    spl_recursive_it_move_forward_ex((spl_recursive_it_object*)iter->data, ((spl_recursive_it_iterator*)iter)->zobject TSRMLS_CC);
422}
423
424static void spl_recursive_it_rewind(zend_object_iterator *iter TSRMLS_DC)
425{
426    spl_recursive_it_rewind_ex((spl_recursive_it_object*)iter->data, ((spl_recursive_it_iterator*)iter)->zobject TSRMLS_CC);
427}
428
429static zend_object_iterator *spl_recursive_it_get_iterator(zend_class_entry *ce, zval *zobject, int by_ref TSRMLS_DC)
430{
431    spl_recursive_it_iterator *iterator;
432    spl_recursive_it_object   *object;
433
434    if (by_ref) {
435        zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
436    }
437    iterator = emalloc(sizeof(spl_recursive_it_iterator));
438    object   = (spl_recursive_it_object*)zend_object_store_get_object(zobject TSRMLS_CC);
439    if (object->iterators == NULL) {
440        zend_error(E_ERROR, "The object to be iterated is in an invalid state: "
441                "the parent constructor has not been called");
442    }
443
444    Z_ADDREF_P(zobject);
445    iterator->intern.data = (void*)object;
446    iterator->intern.funcs = ce->iterator_funcs.funcs;
447    iterator->zobject = zobject;
448    return (zend_object_iterator*)iterator;
449}
450
451zend_object_iterator_funcs spl_recursive_it_iterator_funcs = {
452    spl_recursive_it_dtor,
453    spl_recursive_it_valid,
454    spl_recursive_it_get_current_data,
455    spl_recursive_it_get_current_key,
456    spl_recursive_it_move_forward,
457    spl_recursive_it_rewind
458};
459
460static void spl_recursive_it_it_construct(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *ce_base, zend_class_entry *ce_inner, recursive_it_it_type rit_type)
461{
462    zval                      *object = getThis();
463    spl_recursive_it_object   *intern;
464    zval                      *iterator;
465    zend_class_entry          *ce_iterator;
466    long                       mode, flags;
467    int                        inc_refcount = 1;
468    zend_error_handling        error_handling;
469
470    zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling TSRMLS_CC);
471
472    switch(rit_type) {
473        case RIT_RecursiveTreeIterator: {
474
475            zval *caching_it, *caching_it_flags, *user_caching_it_flags = NULL;
476            mode = RIT_SELF_FIRST;
477            flags = RTIT_BYPASS_KEY;
478
479            if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "o|lzl", &iterator, &flags, &user_caching_it_flags, &mode) == SUCCESS) {
480                if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate TSRMLS_CC)) {
481                    zval *aggregate = iterator;
482                    zend_call_method_with_0_params(&aggregate, Z_OBJCE_P(aggregate), &Z_OBJCE_P(aggregate)->iterator_funcs.zf_new_iterator, "getiterator", &iterator);
483                    inc_refcount = 0;
484                }
485
486                MAKE_STD_ZVAL(caching_it_flags);
487                if (user_caching_it_flags) {
488                    ZVAL_ZVAL(caching_it_flags, user_caching_it_flags, 1, 0);
489                } else {
490                    ZVAL_LONG(caching_it_flags, CIT_CATCH_GET_CHILD);
491                }
492                spl_instantiate_arg_ex2(spl_ce_RecursiveCachingIterator, &caching_it, 1, iterator, caching_it_flags TSRMLS_CC);
493                zval_ptr_dtor(&caching_it_flags);
494                if (inc_refcount == 0 && iterator) {
495                    zval_ptr_dtor(&iterator);
496                }
497                iterator = caching_it;
498                inc_refcount = 0;
499            } else {
500                iterator = NULL;
501            }
502            break;
503        }
504        case RIT_RecursiveIteratorIterator:
505        default: {
506            mode = RIT_LEAVES_ONLY;
507            flags = 0;
508
509            if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "o|ll", &iterator, &mode, &flags) == SUCCESS) {
510                if (instanceof_function(Z_OBJCE_P(iterator), zend_ce_aggregate TSRMLS_CC)) {
511                    zval *aggregate = iterator;
512                    zend_call_method_with_0_params(&aggregate, Z_OBJCE_P(aggregate), &Z_OBJCE_P(aggregate)->iterator_funcs.zf_new_iterator, "getiterator", &iterator);
513                    inc_refcount = 0;
514                }
515            } else {
516                iterator = NULL;
517            }
518            break;
519        }
520    }
521    if (!iterator || !instanceof_function(Z_OBJCE_P(iterator), spl_ce_RecursiveIterator TSRMLS_CC)) {
522        if (iterator && !inc_refcount) {
523            zval_ptr_dtor(&iterator);
524        }
525        zend_throw_exception(spl_ce_InvalidArgumentException, "An instance of RecursiveIterator or IteratorAggregate creating it is required", 0 TSRMLS_CC);
526        zend_restore_error_handling(&error_handling TSRMLS_CC);
527        return;
528    }
529
530    intern = (spl_recursive_it_object*)zend_object_store_get_object(object TSRMLS_CC);
531    intern->iterators = emalloc(sizeof(spl_sub_iterator));
532    intern->level = 0;
533    intern->mode = mode;
534    intern->flags = flags;
535    intern->max_depth = -1;
536    intern->in_iteration = 0;
537    intern->ce = Z_OBJCE_P(object);
538
539    zend_hash_find(&intern->ce->function_table, "beginiteration", sizeof("beginiteration"), (void **) &intern->beginIteration);
540    if (intern->beginIteration->common.scope == ce_base) {
541        intern->beginIteration = NULL;
542    }
543    zend_hash_find(&intern->ce->function_table, "enditeration", sizeof("enditeration"), (void **) &intern->endIteration);
544    if (intern->endIteration->common.scope == ce_base) {
545        intern->endIteration = NULL;
546    }
547    zend_hash_find(&intern->ce->function_table, "callhaschildren", sizeof("callHasChildren"), (void **) &intern->callHasChildren);
548    if (intern->callHasChildren->common.scope == ce_base) {
549        intern->callHasChildren = NULL;
550    }
551    zend_hash_find(&intern->ce->function_table, "callgetchildren", sizeof("callGetChildren"), (void **) &intern->callGetChildren);
552    if (intern->callGetChildren->common.scope == ce_base) {
553        intern->callGetChildren = NULL;
554    }
555    zend_hash_find(&intern->ce->function_table, "beginchildren", sizeof("beginchildren"), (void **) &intern->beginChildren);
556    if (intern->beginChildren->common.scope == ce_base) {
557        intern->beginChildren = NULL;
558    }
559    zend_hash_find(&intern->ce->function_table, "endchildren", sizeof("endchildren"), (void **) &intern->endChildren);
560    if (intern->endChildren->common.scope == ce_base) {
561        intern->endChildren = NULL;
562    }
563    zend_hash_find(&intern->ce->function_table, "nextelement", sizeof("nextElement"), (void **) &intern->nextElement);
564    if (intern->nextElement->common.scope == ce_base) {
565        intern->nextElement = NULL;
566    }
567    ce_iterator = Z_OBJCE_P(iterator); /* respect inheritance, don't use spl_ce_RecursiveIterator */
568    intern->iterators[0].iterator = ce_iterator->get_iterator(ce_iterator, iterator, 0 TSRMLS_CC);
569    if (inc_refcount) {
570        Z_ADDREF_P(iterator);
571    }
572    intern->iterators[0].zobject = iterator;
573    intern->iterators[0].ce = ce_iterator;
574    intern->iterators[0].state = RS_START;
575
576    zend_restore_error_handling(&error_handling TSRMLS_CC);
577
578    if (EG(exception)) {
579        zend_object_iterator *sub_iter;
580
581        while (intern->level >= 0) {
582            sub_iter = intern->iterators[intern->level].iterator;
583            sub_iter->funcs->dtor(sub_iter TSRMLS_CC);
584            zval_ptr_dtor(&intern->iterators[intern->level--].zobject);
585        }
586        efree(intern->iterators);
587        intern->iterators = NULL;
588    }
589}
590
591/* {{{ proto void RecursiveIteratorIterator::__construct(RecursiveIterator|IteratorAggregate it [, int mode = RIT_LEAVES_ONLY [, int flags = 0]]) throws InvalidArgumentException
592   Creates a RecursiveIteratorIterator from a RecursiveIterator. */
593SPL_METHOD(RecursiveIteratorIterator, __construct)
594{
595    spl_recursive_it_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveIteratorIterator, zend_ce_iterator, RIT_RecursiveIteratorIterator);
596} /* }}} */
597
598/* {{{ proto void RecursiveIteratorIterator::rewind()
599   Rewind the iterator to the first element of the top level inner iterator. */
600SPL_METHOD(RecursiveIteratorIterator, rewind)
601{
602    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
603
604    if (zend_parse_parameters_none() == FAILURE) {
605        return;
606    }
607
608    spl_recursive_it_rewind_ex(object, getThis() TSRMLS_CC);
609} /* }}} */
610
611/* {{{ proto bool RecursiveIteratorIterator::valid()
612   Check whether the current position is valid */
613SPL_METHOD(RecursiveIteratorIterator, valid)
614{
615    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
616
617    if (zend_parse_parameters_none() == FAILURE) {
618        return;
619    }
620
621    RETURN_BOOL(spl_recursive_it_valid_ex(object, getThis() TSRMLS_CC) == SUCCESS);
622} /* }}} */
623
624/* {{{ proto mixed RecursiveIteratorIterator::key()
625   Access the current key */
626SPL_METHOD(RecursiveIteratorIterator, key)
627{
628    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
629    zend_object_iterator      *iterator;
630
631    if (zend_parse_parameters_none() == FAILURE) {
632        return;
633    }
634
635    SPL_FETCH_SUB_ITERATOR(iterator, object);
636
637    if (iterator->funcs->get_current_key) {
638        iterator->funcs->get_current_key(iterator, return_value TSRMLS_CC);
639    } else {
640        RETURN_NULL();
641    }
642} /* }}} */
643
644/* {{{ proto mixed RecursiveIteratorIterator::current()
645   Access the current element value */
646SPL_METHOD(RecursiveIteratorIterator, current)
647{
648    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
649    zend_object_iterator      *iterator;
650    zval                      **data;
651
652    if (zend_parse_parameters_none() == FAILURE) {
653        return;
654    }
655
656    SPL_FETCH_SUB_ITERATOR(iterator, object);
657
658    iterator->funcs->get_current_data(iterator, &data TSRMLS_CC);
659    if (data && *data) {
660        RETURN_ZVAL(*data, 1, 0);
661    }
662} /* }}} */
663
664/* {{{ proto void RecursiveIteratorIterator::next()
665   Move forward to the next element */
666SPL_METHOD(RecursiveIteratorIterator, next)
667{
668    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
669
670    if (zend_parse_parameters_none() == FAILURE) {
671        return;
672    }
673
674    spl_recursive_it_move_forward_ex(object, getThis() TSRMLS_CC);
675} /* }}} */
676
677/* {{{ proto int RecursiveIteratorIterator::getDepth()
678   Get the current depth of the recursive iteration */
679SPL_METHOD(RecursiveIteratorIterator, getDepth)
680{
681    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
682
683    if (zend_parse_parameters_none() == FAILURE) {
684        return;
685    }
686
687    RETURN_LONG(object->level);
688} /* }}} */
689
690/* {{{ proto RecursiveIterator RecursiveIteratorIterator::getSubIterator([int level])
691   The current active sub iterator or the iterator at specified level */
692SPL_METHOD(RecursiveIteratorIterator, getSubIterator)
693{
694    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
695    long  level = object->level;
696    zval      *zobject;
697
698    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &level) == FAILURE) {
699        return;
700    }
701    if (level < 0 || level > object->level) {
702        RETURN_NULL();
703    }
704
705    if(!object->iterators) {
706        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
707            "The object is in an invalid state as the parent constructor was not called");
708        return;
709    }
710
711    RETURN_ZVAL(object->iterators[level].zobject, 1, 0);
712} /* }}} */
713
714/* {{{ proto RecursiveIterator RecursiveIteratorIterator::getInnerIterator()
715   The current active sub iterator */
716SPL_METHOD(RecursiveIteratorIterator, getInnerIterator)
717{
718    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
719    zval      *zobject;
720
721    if (zend_parse_parameters_none() == FAILURE) {
722        return;
723    }
724
725    SPL_FETCH_SUB_ELEMENT(zobject, object, zobject);
726
727    RETURN_ZVAL(zobject, 1, 0);
728} /* }}} */
729
730/* {{{ proto RecursiveIterator RecursiveIteratorIterator::beginIteration()
731   Called when iteration begins (after first rewind() call) */
732SPL_METHOD(RecursiveIteratorIterator, beginIteration)
733{
734    if (zend_parse_parameters_none() == FAILURE) {
735        return;
736    }
737    /* nothing to do */
738} /* }}} */
739
740/* {{{ proto RecursiveIterator RecursiveIteratorIterator::endIteration()
741   Called when iteration ends (when valid() first returns false */
742SPL_METHOD(RecursiveIteratorIterator, endIteration)
743{
744    if (zend_parse_parameters_none() == FAILURE) {
745        return;
746    }
747    /* nothing to do */
748} /* }}} */
749
750/* {{{ proto bool RecursiveIteratorIterator::callHasChildren()
751   Called for each element to test whether it has children */
752SPL_METHOD(RecursiveIteratorIterator, callHasChildren)
753{
754    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
755    zend_class_entry *ce;
756    zval *retval, *zobject;
757
758    if (zend_parse_parameters_none() == FAILURE) {
759        return;
760    }
761
762    if (!object->iterators) {
763        RETURN_NULL();
764    }
765
766    SPL_FETCH_SUB_ELEMENT(ce, object, ce);
767
768    zobject = object->iterators[object->level].zobject;
769    if (!zobject) {
770        RETURN_FALSE;
771    } else {
772        zend_call_method_with_0_params(&zobject, ce, NULL, "haschildren", &retval);
773        if (retval) {
774            RETURN_ZVAL(retval, 0, 1);
775        } else {
776            RETURN_FALSE;
777        }
778    }
779} /* }}} */
780
781/* {{{ proto RecursiveIterator RecursiveIteratorIterator::callGetChildren()
782   Return children of current element */
783SPL_METHOD(RecursiveIteratorIterator, callGetChildren)
784{
785    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
786    zend_class_entry *ce;
787    zval *retval, *zobject;
788
789    if (zend_parse_parameters_none() == FAILURE) {
790        return;
791    }
792
793    SPL_FETCH_SUB_ELEMENT(ce, object, ce);
794
795    zobject = object->iterators[object->level].zobject;
796    if (!zobject) {
797        return;
798    } else {
799        zend_call_method_with_0_params(&zobject, ce, NULL, "getchildren", &retval);
800        if (retval) {
801            RETURN_ZVAL(retval, 0, 1);
802        }
803    }
804} /* }}} */
805
806/* {{{ proto void RecursiveIteratorIterator::beginChildren()
807   Called when recursing one level down */
808SPL_METHOD(RecursiveIteratorIterator, beginChildren)
809{
810    if (zend_parse_parameters_none() == FAILURE) {
811        return;
812    }
813    /* nothing to do */
814} /* }}} */
815
816/* {{{ proto void RecursiveIteratorIterator::endChildren()
817   Called when end recursing one level */
818SPL_METHOD(RecursiveIteratorIterator, endChildren)
819{
820    if (zend_parse_parameters_none() == FAILURE) {
821        return;
822    }
823    /* nothing to do */
824} /* }}} */
825
826/* {{{ proto void RecursiveIteratorIterator::nextElement()
827   Called when the next element is available */
828SPL_METHOD(RecursiveIteratorIterator, nextElement)
829{
830    if (zend_parse_parameters_none() == FAILURE) {
831        return;
832    }
833    /* nothing to do */
834} /* }}} */
835
836/* {{{ proto void RecursiveIteratorIterator::setMaxDepth([$max_depth = -1])
837   Set the maximum allowed depth (or any depth if pmax_depth = -1] */
838SPL_METHOD(RecursiveIteratorIterator, setMaxDepth)
839{
840    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
841    long  max_depth = -1;
842
843    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &max_depth) == FAILURE) {
844        return;
845    }
846    if (max_depth < -1) {
847        zend_throw_exception(spl_ce_OutOfRangeException, "Parameter max_depth must be >= -1", 0 TSRMLS_CC);
848        return;
849    }
850    object->max_depth = max_depth;
851} /* }}} */
852
853/* {{{ proto int|false RecursiveIteratorIterator::getMaxDepth()
854   Return the maximum accepted depth or false if any depth is allowed */
855SPL_METHOD(RecursiveIteratorIterator, getMaxDepth)
856{
857    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
858
859    if (zend_parse_parameters_none() == FAILURE) {
860        return;
861    }
862
863    if (object->max_depth == -1) {
864        RETURN_FALSE;
865    } else {
866        RETURN_LONG(object->max_depth);
867    }
868} /* }}} */
869
870static union _zend_function *spl_recursive_it_get_method(zval **object_ptr, char *method, int method_len, const zend_literal *key TSRMLS_DC)
871{
872    union _zend_function    *function_handler;
873    spl_recursive_it_object *object = (spl_recursive_it_object*)zend_object_store_get_object(*object_ptr TSRMLS_CC);
874    long                     level = object->level;
875    zval                    *zobj;
876
877    if (!object->iterators) {
878        php_error_docref(NULL TSRMLS_CC, E_ERROR, "The %s instance wasn't initialized properly", Z_OBJCE_PP(object_ptr)->name);
879    }
880    zobj = object->iterators[level].zobject;
881
882    function_handler = std_object_handlers.get_method(object_ptr, method, method_len, key TSRMLS_CC);
883    if (!function_handler) {
884        if (zend_hash_find(&Z_OBJCE_P(zobj)->function_table, method, method_len+1, (void **) &function_handler) == FAILURE) {
885            if (Z_OBJ_HT_P(zobj)->get_method) {
886                *object_ptr = zobj;
887                function_handler = Z_OBJ_HT_P(*object_ptr)->get_method(object_ptr, method, method_len, key TSRMLS_CC);
888            }
889        } else {
890            *object_ptr = zobj;
891        }
892    }
893    return function_handler;
894}
895
896/* {{{ spl_RecursiveIteratorIterator_dtor */
897static void spl_RecursiveIteratorIterator_dtor(zend_object *_object, zend_object_handle handle TSRMLS_DC)
898{
899    spl_recursive_it_object   *object = (spl_recursive_it_object *)_object;
900    zend_object_iterator      *sub_iter;
901
902    /* call standard dtor */
903    zend_objects_destroy_object(_object, handle TSRMLS_CC);
904
905    if (object->iterators) {
906        while (object->level >= 0) {
907            sub_iter = object->iterators[object->level].iterator;
908            sub_iter->funcs->dtor(sub_iter TSRMLS_CC);
909            zval_ptr_dtor(&object->iterators[object->level--].zobject);
910        }
911        efree(object->iterators);
912        object->iterators = NULL;
913    }
914}
915/* }}} */
916
917/* {{{ spl_RecursiveIteratorIterator_free_storage */
918static void spl_RecursiveIteratorIterator_free_storage(void *_object TSRMLS_DC)
919{
920    spl_recursive_it_object   *object = (spl_recursive_it_object *)_object;
921
922    zend_object_std_dtor(&object->std TSRMLS_CC);
923    smart_str_free(&object->prefix[0]);
924    smart_str_free(&object->prefix[1]);
925    smart_str_free(&object->prefix[2]);
926    smart_str_free(&object->prefix[3]);
927    smart_str_free(&object->prefix[4]);
928    smart_str_free(&object->prefix[5]);
929
930    smart_str_free(&object->postfix[0]);
931
932    efree(object);
933}
934/* }}} */
935
936/* {{{ spl_RecursiveIteratorIterator_new_ex */
937static zend_object_value spl_RecursiveIteratorIterator_new_ex(zend_class_entry *class_type, int init_prefix TSRMLS_DC)
938{
939    zend_object_value retval;
940    spl_recursive_it_object *intern;
941
942    intern = emalloc(sizeof(spl_recursive_it_object));
943    memset(intern, 0, sizeof(spl_recursive_it_object));
944
945    if (init_prefix) {
946        smart_str_appendl(&intern->prefix[0], "",    0);
947        smart_str_appendl(&intern->prefix[1], "| ",  2);
948        smart_str_appendl(&intern->prefix[2], "  ",  2);
949        smart_str_appendl(&intern->prefix[3], "|-",  2);
950        smart_str_appendl(&intern->prefix[4], "\\-", 2);
951        smart_str_appendl(&intern->prefix[5], "",    0);
952
953        smart_str_appendl(&intern->postfix[0], "",    0);
954    }
955
956    zend_object_std_init(&intern->std, class_type TSRMLS_CC);
957    object_properties_init(&intern->std, class_type);
958
959    retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)spl_RecursiveIteratorIterator_dtor, (zend_objects_free_object_storage_t) spl_RecursiveIteratorIterator_free_storage, NULL TSRMLS_CC);
960    retval.handlers = &spl_handlers_rec_it_it;
961    return retval;
962}
963/* }}} */
964
965/* {{{ spl_RecursiveIteratorIterator_new */
966static zend_object_value spl_RecursiveIteratorIterator_new(zend_class_entry *class_type TSRMLS_DC)
967{
968    return spl_RecursiveIteratorIterator_new_ex(class_type, 0 TSRMLS_CC);
969}
970/* }}} */
971
972/* {{{ spl_RecursiveTreeIterator_new */
973static zend_object_value spl_RecursiveTreeIterator_new(zend_class_entry *class_type TSRMLS_DC)
974{
975    return spl_RecursiveIteratorIterator_new_ex(class_type, 1 TSRMLS_CC);
976}
977/* }}} */
978
979ZEND_BEGIN_ARG_INFO_EX(arginfo_recursive_it___construct, 0, 0, 1)
980    ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
981    ZEND_ARG_INFO(0, mode)
982    ZEND_ARG_INFO(0, flags)
983ZEND_END_ARG_INFO();
984
985ZEND_BEGIN_ARG_INFO_EX(arginfo_recursive_it_getSubIterator, 0, 0, 0)
986    ZEND_ARG_INFO(0, level)
987ZEND_END_ARG_INFO();
988
989ZEND_BEGIN_ARG_INFO_EX(arginfo_recursive_it_setMaxDepth, 0, 0, 0)
990    ZEND_ARG_INFO(0, max_depth)
991ZEND_END_ARG_INFO();
992
993static const zend_function_entry spl_funcs_RecursiveIteratorIterator[] = {
994    SPL_ME(RecursiveIteratorIterator, __construct,       arginfo_recursive_it___construct,    ZEND_ACC_PUBLIC)
995    SPL_ME(RecursiveIteratorIterator, rewind,            arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
996    SPL_ME(RecursiveIteratorIterator, valid,             arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
997    SPL_ME(RecursiveIteratorIterator, key,               arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
998    SPL_ME(RecursiveIteratorIterator, current,           arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
999    SPL_ME(RecursiveIteratorIterator, next,              arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1000    SPL_ME(RecursiveIteratorIterator, getDepth,          arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1001    SPL_ME(RecursiveIteratorIterator, getSubIterator,    arginfo_recursive_it_getSubIterator, ZEND_ACC_PUBLIC)
1002    SPL_ME(RecursiveIteratorIterator, getInnerIterator,  arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1003    SPL_ME(RecursiveIteratorIterator, beginIteration,    arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1004    SPL_ME(RecursiveIteratorIterator, endIteration,      arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1005    SPL_ME(RecursiveIteratorIterator, callHasChildren,   arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1006    SPL_ME(RecursiveIteratorIterator, callGetChildren,   arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1007    SPL_ME(RecursiveIteratorIterator, beginChildren,     arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1008    SPL_ME(RecursiveIteratorIterator, endChildren,       arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1009    SPL_ME(RecursiveIteratorIterator, nextElement,       arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1010    SPL_ME(RecursiveIteratorIterator, setMaxDepth,       arginfo_recursive_it_setMaxDepth,    ZEND_ACC_PUBLIC)
1011    SPL_ME(RecursiveIteratorIterator, getMaxDepth,       arginfo_recursive_it_void,           ZEND_ACC_PUBLIC)
1012    PHP_FE_END
1013};
1014
1015static void spl_recursive_tree_iterator_get_prefix(spl_recursive_it_object *object, zval *return_value TSRMLS_DC)
1016{
1017    smart_str  str = {0};
1018    zval      *has_next;
1019    int        level;
1020
1021    smart_str_appendl(&str, object->prefix[0].c, object->prefix[0].len);
1022
1023    for (level = 0; level < object->level; ++level) {
1024        zend_call_method_with_0_params(&object->iterators[level].zobject, object->iterators[level].ce, NULL, "hasnext", &has_next);
1025        if (has_next) {
1026            if (Z_LVAL_P(has_next)) {
1027                smart_str_appendl(&str, object->prefix[1].c, object->prefix[1].len);
1028            } else {
1029                smart_str_appendl(&str, object->prefix[2].c, object->prefix[2].len);
1030            }
1031            zval_ptr_dtor(&has_next);
1032        }
1033    }
1034    zend_call_method_with_0_params(&object->iterators[level].zobject, object->iterators[level].ce, NULL, "hasnext", &has_next);
1035    if (has_next) {
1036        if (Z_LVAL_P(has_next)) {
1037            smart_str_appendl(&str, object->prefix[3].c, object->prefix[3].len);
1038        } else {
1039            smart_str_appendl(&str, object->prefix[4].c, object->prefix[4].len);
1040        }
1041        zval_ptr_dtor(&has_next);
1042    }
1043
1044    smart_str_appendl(&str, object->prefix[5].c, object->prefix[5].len);
1045    smart_str_0(&str);
1046
1047    RETVAL_STRINGL(str.c, str.len, 0);
1048}
1049
1050static void spl_recursive_tree_iterator_get_entry(spl_recursive_it_object * object, zval * return_value TSRMLS_DC)
1051{
1052    zend_object_iterator      *iterator = object->iterators[object->level].iterator;
1053    zval                     **data;
1054    zend_error_handling        error_handling;
1055
1056    iterator->funcs->get_current_data(iterator, &data TSRMLS_CC);
1057
1058    zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
1059    if (data && *data) {
1060        RETVAL_ZVAL(*data, 1, 0);
1061        if (Z_TYPE_P(return_value) == IS_ARRAY) {
1062            zval_dtor(return_value);
1063            ZVAL_STRINGL(return_value, "Array", sizeof("Array")-1, 1);
1064        } else {
1065            convert_to_string(return_value);
1066        }
1067    }
1068    zend_restore_error_handling(&error_handling TSRMLS_CC);
1069}
1070
1071static void spl_recursive_tree_iterator_get_postfix(spl_recursive_it_object * object, zval * return_value TSRMLS_DC)
1072{
1073    RETVAL_STRINGL(object->postfix[0].c, object->postfix[0].len, 1);
1074}
1075
1076/* {{{ proto void RecursiveTreeIterator::__construct(RecursiveIterator|IteratorAggregate it [, int flags = RTIT_BYPASS_KEY [, int cit_flags = CIT_CATCH_GET_CHILD [, mode = RIT_SELF_FIRST ]]]) throws InvalidArgumentException
1077   RecursiveIteratorIterator to generate ASCII graphic trees for the entries in a RecursiveIterator */
1078SPL_METHOD(RecursiveTreeIterator, __construct)
1079{
1080    spl_recursive_it_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveTreeIterator, zend_ce_iterator, RIT_RecursiveTreeIterator);
1081} /* }}} */
1082
1083/* {{{ proto void RecursiveTreeIterator::setPrefixPart(int part, string prefix) throws OutOfRangeException
1084   Sets prefix parts as used in getPrefix() */
1085SPL_METHOD(RecursiveTreeIterator, setPrefixPart)
1086{
1087    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1088    long  part;
1089    char* prefix;
1090    int   prefix_len;
1091
1092    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &part, &prefix, &prefix_len) == FAILURE) {
1093        return;
1094    }
1095    if (0 > part || part > 5) {
1096        zend_throw_exception_ex(spl_ce_OutOfRangeException, 0 TSRMLS_CC, "Use RecursiveTreeIterator::PREFIX_* constant");
1097        return;
1098    }
1099
1100    smart_str_free(&object->prefix[part]);
1101    smart_str_appendl(&object->prefix[part], prefix, prefix_len);
1102} /* }}} */
1103
1104/* {{{ proto string RecursiveTreeIterator::getPrefix()
1105   Returns the string to place in front of current element */
1106SPL_METHOD(RecursiveTreeIterator, getPrefix)
1107{
1108    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1109
1110    if (zend_parse_parameters_none() == FAILURE) {
1111        return;
1112    }
1113
1114    if(!object->iterators) {
1115        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
1116            "The object is in an invalid state as the parent constructor was not called");
1117        return;
1118    }
1119
1120    spl_recursive_tree_iterator_get_prefix(object, return_value TSRMLS_CC);
1121} /* }}} */
1122
1123/* {{{ proto void RecursiveTreeIterator::setPostfix(string prefix)
1124   Sets postfix as used in getPostfix() */
1125SPL_METHOD(RecursiveTreeIterator, setPostfix)
1126{
1127    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1128    char* postfix;
1129    int   postfix_len;
1130
1131    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &postfix, &postfix_len) == FAILURE) {
1132        return;
1133    }
1134
1135    smart_str_free(&object->postfix[0]);
1136    smart_str_appendl(&object->postfix[0], postfix, postfix_len);
1137} /* }}} */
1138
1139/* {{{ proto string RecursiveTreeIterator::getEntry()
1140   Returns the string presentation built for current element */
1141SPL_METHOD(RecursiveTreeIterator, getEntry)
1142{
1143    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1144
1145    if (zend_parse_parameters_none() == FAILURE) {
1146        return;
1147    }
1148
1149    if(!object->iterators) {
1150        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
1151            "The object is in an invalid state as the parent constructor was not called");
1152        return;
1153    }
1154
1155    spl_recursive_tree_iterator_get_entry(object, return_value TSRMLS_CC);
1156} /* }}} */
1157
1158/* {{{ proto string RecursiveTreeIterator::getPostfix()
1159   Returns the string to place after the current element */
1160SPL_METHOD(RecursiveTreeIterator, getPostfix)
1161{
1162    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1163
1164    if (zend_parse_parameters_none() == FAILURE) {
1165        return;
1166    }
1167
1168    if(!object->iterators) {
1169        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
1170            "The object is in an invalid state as the parent constructor was not called");
1171        return;
1172    }
1173
1174    spl_recursive_tree_iterator_get_postfix(object, return_value TSRMLS_CC);
1175} /* }}} */
1176
1177/* {{{ proto mixed RecursiveTreeIterator::current()
1178   Returns the current element prefixed and postfixed */
1179SPL_METHOD(RecursiveTreeIterator, current)
1180{
1181    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1182    zval                       prefix, entry, postfix;
1183    char                      *str, *ptr;
1184    size_t                     str_len;
1185
1186    if (zend_parse_parameters_none() == FAILURE) {
1187        return;
1188    }
1189
1190    if(!object->iterators) {
1191        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
1192            "The object is in an invalid state as the parent constructor was not called");
1193        return;
1194    }
1195
1196    if (object->flags & RTIT_BYPASS_CURRENT) {
1197        zend_object_iterator      *iterator;
1198        zval                      **data;
1199
1200        SPL_FETCH_SUB_ITERATOR(iterator, object);
1201        iterator->funcs->get_current_data(iterator, &data TSRMLS_CC);
1202        if (data && *data) {
1203            RETURN_ZVAL(*data, 1, 0);
1204        } else {
1205            RETURN_NULL();
1206        }
1207    }
1208
1209    INIT_ZVAL(prefix);
1210    INIT_ZVAL(entry);
1211    spl_recursive_tree_iterator_get_prefix(object, &prefix TSRMLS_CC);
1212    spl_recursive_tree_iterator_get_entry(object, &entry TSRMLS_CC);
1213    if (Z_TYPE(entry) != IS_STRING) {
1214        zval_dtor(&prefix);
1215        zval_dtor(&entry);
1216        RETURN_NULL();
1217    }
1218    spl_recursive_tree_iterator_get_postfix(object, &postfix TSRMLS_CC);
1219
1220    str_len = Z_STRLEN(prefix) + Z_STRLEN(entry) + Z_STRLEN(postfix);
1221    str = (char *) emalloc(str_len + 1U);
1222    ptr = str;
1223
1224    memcpy(ptr, Z_STRVAL(prefix), Z_STRLEN(prefix));
1225    ptr += Z_STRLEN(prefix);
1226    memcpy(ptr, Z_STRVAL(entry), Z_STRLEN(entry));
1227    ptr += Z_STRLEN(entry);
1228    memcpy(ptr, Z_STRVAL(postfix), Z_STRLEN(postfix));
1229    ptr += Z_STRLEN(postfix);
1230    *ptr = 0;
1231
1232    zval_dtor(&prefix);
1233    zval_dtor(&entry);
1234    zval_dtor(&postfix);
1235
1236    RETURN_STRINGL(str, str_len, 0);
1237} /* }}} */
1238
1239/* {{{ proto mixed RecursiveTreeIterator::key()
1240   Returns the current key prefixed and postfixed */
1241SPL_METHOD(RecursiveTreeIterator, key)
1242{
1243    spl_recursive_it_object   *object = (spl_recursive_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1244    zend_object_iterator      *iterator;
1245    zval                       prefix, key, postfix, key_copy;
1246    char                      *str, *ptr;
1247    size_t                     str_len;
1248
1249    if (zend_parse_parameters_none() == FAILURE) {
1250        return;
1251    }
1252
1253    SPL_FETCH_SUB_ITERATOR(iterator, object);
1254
1255    if (iterator->funcs->get_current_key) {
1256        iterator->funcs->get_current_key(iterator, &key TSRMLS_CC);
1257    } else {
1258        ZVAL_NULL(&key);
1259    }
1260
1261    if (object->flags & RTIT_BYPASS_KEY) {
1262        zval *key_ptr = &key;
1263        RETVAL_ZVAL(key_ptr, 1, 0);
1264        zval_dtor(&key);
1265        return;
1266    }
1267
1268    if (Z_TYPE(key) != IS_STRING) {
1269        int use_copy;
1270        zend_make_printable_zval(&key, &key_copy, &use_copy);
1271        if (use_copy) {
1272            key = key_copy;
1273        }
1274    }
1275
1276    spl_recursive_tree_iterator_get_prefix(object, &prefix TSRMLS_CC);
1277    spl_recursive_tree_iterator_get_postfix(object, &postfix TSRMLS_CC);
1278
1279    str_len = Z_STRLEN(prefix) + Z_STRLEN(key) + Z_STRLEN(postfix);
1280    str = (char *) emalloc(str_len + 1U);
1281    ptr = str;
1282
1283    memcpy(ptr, Z_STRVAL(prefix), Z_STRLEN(prefix));
1284    ptr += Z_STRLEN(prefix);
1285    memcpy(ptr, Z_STRVAL(key), Z_STRLEN(key));
1286    ptr += Z_STRLEN(key);
1287    memcpy(ptr, Z_STRVAL(postfix), Z_STRLEN(postfix));
1288    ptr += Z_STRLEN(postfix);
1289    *ptr = 0;
1290
1291    zval_dtor(&prefix);
1292    zval_dtor(&key);
1293    zval_dtor(&postfix);
1294
1295    RETVAL_STRINGL(str, str_len, 0);
1296} /* }}} */
1297
1298ZEND_BEGIN_ARG_INFO_EX(arginfo_recursive_tree_it___construct, 0, 0, 1)
1299    ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
1300    ZEND_ARG_INFO(0, flags)
1301    ZEND_ARG_INFO(0, caching_it_flags)
1302    ZEND_ARG_INFO(0, mode)
1303ZEND_END_ARG_INFO();
1304
1305ZEND_BEGIN_ARG_INFO_EX(arginfo_recursive_tree_it_setPrefixPart, 0, 0, 2)
1306    ZEND_ARG_INFO(0, part)
1307    ZEND_ARG_INFO(0, value)
1308ZEND_END_ARG_INFO();
1309
1310static const zend_function_entry spl_funcs_RecursiveTreeIterator[] = {
1311    SPL_ME(RecursiveTreeIterator,     __construct,       arginfo_recursive_tree_it___construct,   ZEND_ACC_PUBLIC)
1312    SPL_ME(RecursiveIteratorIterator, rewind,            arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1313    SPL_ME(RecursiveIteratorIterator, valid,             arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1314    SPL_ME(RecursiveTreeIterator,     key,               arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1315    SPL_ME(RecursiveTreeIterator,     current,           arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1316    SPL_ME(RecursiveIteratorIterator, next,              arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1317    SPL_ME(RecursiveIteratorIterator, beginIteration,    arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1318    SPL_ME(RecursiveIteratorIterator, endIteration,      arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1319    SPL_ME(RecursiveIteratorIterator, callHasChildren,   arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1320    SPL_ME(RecursiveIteratorIterator, callGetChildren,   arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1321    SPL_ME(RecursiveIteratorIterator, beginChildren,     arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1322    SPL_ME(RecursiveIteratorIterator, endChildren,       arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1323    SPL_ME(RecursiveIteratorIterator, nextElement,       arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1324    SPL_ME(RecursiveTreeIterator,     getPrefix,         arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1325    SPL_ME(RecursiveTreeIterator,     setPrefixPart,     arginfo_recursive_tree_it_setPrefixPart, ZEND_ACC_PUBLIC)
1326    SPL_ME(RecursiveTreeIterator,     getEntry,          arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1327    SPL_ME(RecursiveTreeIterator,     setPostfix,        arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1328    SPL_ME(RecursiveTreeIterator,     getPostfix,        arginfo_recursive_it_void,               ZEND_ACC_PUBLIC)
1329    PHP_FE_END
1330};
1331
1332#if MBO_0
1333static int spl_dual_it_gets_implemented(zend_class_entry *interface, zend_class_entry *class_type TSRMLS_DC)
1334{
1335    class_type->iterator_funcs.zf_valid = NULL;
1336    class_type->iterator_funcs.zf_current = NULL;
1337    class_type->iterator_funcs.zf_key = NULL;
1338    class_type->iterator_funcs.zf_next = NULL;
1339    class_type->iterator_funcs.zf_rewind = NULL;
1340    if (!class_type->iterator_funcs.funcs) {
1341        class_type->iterator_funcs.funcs = &zend_interface_iterator_funcs_iterator;
1342    }
1343
1344    return SUCCESS;
1345}
1346#endif
1347
1348static union _zend_function *spl_dual_it_get_method(zval **object_ptr, char *method, int method_len, const zend_literal *key TSRMLS_DC)
1349{
1350    union _zend_function *function_handler;
1351    spl_dual_it_object   *intern;
1352
1353    intern = (spl_dual_it_object*)zend_object_store_get_object(*object_ptr TSRMLS_CC);
1354
1355    function_handler = std_object_handlers.get_method(object_ptr, method, method_len, key TSRMLS_CC);
1356    if (!function_handler && intern->inner.ce) {
1357        if (zend_hash_find(&intern->inner.ce->function_table, method, method_len+1, (void **) &function_handler) == FAILURE) {
1358            if (Z_OBJ_HT_P(intern->inner.zobject)->get_method) {
1359                *object_ptr = intern->inner.zobject;
1360                function_handler = Z_OBJ_HT_P(*object_ptr)->get_method(object_ptr, method, method_len, key TSRMLS_CC);
1361            }
1362        } else {
1363            *object_ptr = intern->inner.zobject;
1364        }
1365    }
1366    return function_handler;
1367}
1368
1369#if MBO_0
1370int spl_dual_it_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
1371{
1372    zval ***func_params, func;
1373    zval *retval_ptr;
1374    int arg_count;
1375    int current = 0;
1376    int success;
1377    void **p;
1378    spl_dual_it_object   *intern;
1379
1380    intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1381
1382    ZVAL_STRING(&func, method, 0);
1383    if (!zend_is_callable(&func, 0, &method TSRMLS_CC)) {
1384        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Method %s::%s() does not exist", intern->inner.ce->name, method);
1385        return FAILURE;
1386    }
1387
1388    p = EG(argument_stack).top_element-2;
1389    arg_count = (ulong) *p;
1390
1391    func_params = safe_emalloc(sizeof(zval **), arg_count, 0);
1392
1393    current = 0;
1394    while (arg_count-- > 0) {
1395        func_params[current] = (zval **) p - (arg_count-current);
1396        current++;
1397    }
1398    arg_count = current; /* restore */
1399
1400    if (call_user_function_ex(EG(function_table), NULL, &func, &retval_ptr, arg_count, func_params, 0, NULL TSRMLS_CC) == SUCCESS && retval_ptr) {
1401        RETURN_ZVAL(retval_ptr, 0, 1);
1402
1403        success = SUCCESS;
1404    } else {
1405        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to call %s::%s()", intern->inner.ce->name, method);
1406        success = FAILURE;
1407    }
1408
1409    efree(func_params);
1410    return success;
1411}
1412#endif
1413
1414#define SPL_CHECK_CTOR(intern, classname) \
1415    if (intern->dit_type == DIT_Unknown) { \
1416        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Classes derived from %s must call %s::__construct()", \
1417                (spl_ce_##classname)->name, (spl_ce_##classname)->name); \
1418        return; \
1419    }
1420
1421#define APPENDIT_CHECK_CTOR(intern) SPL_CHECK_CTOR(intern, AppendIterator)
1422
1423static inline int spl_dual_it_fetch(spl_dual_it_object *intern, int check_more TSRMLS_DC);
1424
1425static inline int spl_cit_check_flags(int flags)
1426{
1427    int cnt = 0;
1428
1429    cnt += (flags & CIT_CALL_TOSTRING) ? 1 : 0;
1430    cnt += (flags & CIT_TOSTRING_USE_KEY) ? 1 : 0;
1431    cnt += (flags & CIT_TOSTRING_USE_CURRENT) ? 1 : 0;
1432    cnt += (flags & CIT_TOSTRING_USE_INNER) ? 1 : 0;
1433
1434    return cnt <= 1 ? SUCCESS : FAILURE;
1435}
1436
1437static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *ce_base, zend_class_entry *ce_inner, dual_it_type dit_type)
1438{
1439    zval                 *zobject, *retval;
1440    spl_dual_it_object   *intern;
1441    zend_class_entry     *ce = NULL;
1442    int                   inc_refcount = 1;
1443    zend_error_handling   error_handling;
1444
1445    intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1446
1447    if (intern->dit_type != DIT_Unknown) {
1448        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s::getIterator() must be called exactly once per instance", ce_base->name);
1449        return NULL;
1450    }
1451
1452    zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling TSRMLS_CC);
1453
1454    intern->dit_type = dit_type;
1455    switch (dit_type) {
1456        case DIT_LimitIterator: {
1457            intern->u.limit.offset = 0; /* start at beginning */
1458            intern->u.limit.count = -1; /* get all */
1459            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|ll", &zobject, ce_inner, &intern->u.limit.offset, &intern->u.limit.count) == FAILURE) {
1460                zend_restore_error_handling(&error_handling TSRMLS_CC);
1461                return NULL;
1462            }
1463            if (intern->u.limit.offset < 0) {
1464                zend_throw_exception(spl_ce_OutOfRangeException, "Parameter offset must be >= 0", 0 TSRMLS_CC);
1465                zend_restore_error_handling(&error_handling TSRMLS_CC);
1466                return NULL;
1467            }
1468            if (intern->u.limit.count < 0 && intern->u.limit.count != -1) {
1469                zend_throw_exception(spl_ce_OutOfRangeException, "Parameter count must either be -1 or a value greater than or equal 0", 0 TSRMLS_CC);
1470                zend_restore_error_handling(&error_handling TSRMLS_CC);
1471                return NULL;
1472            }
1473            break;
1474        }
1475        case DIT_CachingIterator:
1476        case DIT_RecursiveCachingIterator: {
1477            long flags = CIT_CALL_TOSTRING;
1478            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|l", &zobject, ce_inner, &flags) == FAILURE) {
1479                zend_restore_error_handling(&error_handling TSRMLS_CC);
1480                return NULL;
1481            }
1482            if (spl_cit_check_flags(flags) != SUCCESS) {
1483                zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_CURRENT", 0 TSRMLS_CC);
1484                zend_restore_error_handling(&error_handling TSRMLS_CC);
1485                return NULL;
1486            }
1487            intern->u.caching.flags |= flags & CIT_PUBLIC;
1488            MAKE_STD_ZVAL(intern->u.caching.zcache);
1489            array_init(intern->u.caching.zcache);
1490            break;
1491        }
1492        case DIT_IteratorIterator: {
1493            zend_class_entry **pce_cast;
1494            char * class_name = NULL;
1495            int class_name_len = 0;
1496
1497            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|s", &zobject, ce_inner, &class_name, &class_name_len) == FAILURE) {
1498                zend_restore_error_handling(&error_handling TSRMLS_CC);
1499                return NULL;
1500            }
1501            ce = Z_OBJCE_P(zobject);
1502            if (!instanceof_function(ce, zend_ce_iterator TSRMLS_CC)) {
1503                if (ZEND_NUM_ARGS() > 1) {
1504                    if (zend_lookup_class(class_name, class_name_len, &pce_cast TSRMLS_CC) == FAILURE
1505                    || !instanceof_function(ce, *pce_cast TSRMLS_CC)
1506                    || !(*pce_cast)->get_iterator
1507                    ) {
1508                        zend_throw_exception(spl_ce_LogicException, "Class to downcast to not found or not base class or does not implement Traversable", 0 TSRMLS_CC);
1509                        zend_restore_error_handling(&error_handling TSRMLS_CC);
1510                        return NULL;
1511                    }
1512                    ce = *pce_cast;
1513                }
1514                if (instanceof_function(ce, zend_ce_aggregate TSRMLS_CC)) {
1515                    zend_call_method_with_0_params(&zobject, ce, &ce->iterator_funcs.zf_new_iterator, "getiterator", &retval);
1516                    if (EG(exception)) {
1517                        if (retval) {
1518                            zval_ptr_dtor(&retval);
1519                        }
1520                        zend_restore_error_handling(&error_handling TSRMLS_CC);
1521                        return NULL;
1522                    }
1523                    if (!retval || Z_TYPE_P(retval) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(retval), zend_ce_traversable TSRMLS_CC)) {
1524                        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "%s::getIterator() must return an object that implements Traversable", ce->name);
1525                        zend_restore_error_handling(&error_handling TSRMLS_CC);
1526                        return NULL;
1527                    }
1528                    zobject = retval;
1529                    ce = Z_OBJCE_P(zobject);
1530                    inc_refcount = 0;
1531                }
1532            }
1533            break;
1534        }
1535        case DIT_AppendIterator:
1536            spl_instantiate(spl_ce_ArrayIterator, &intern->u.append.zarrayit, 1 TSRMLS_CC);
1537            zend_call_method_with_0_params(&intern->u.append.zarrayit, spl_ce_ArrayIterator, &spl_ce_ArrayIterator->constructor, "__construct", NULL);
1538            intern->u.append.iterator = spl_ce_ArrayIterator->get_iterator(spl_ce_ArrayIterator, intern->u.append.zarrayit, 0 TSRMLS_CC);
1539            zend_restore_error_handling(&error_handling TSRMLS_CC);
1540            return intern;
1541#if HAVE_PCRE || HAVE_BUNDLED_PCRE
1542        case DIT_RegexIterator:
1543        case DIT_RecursiveRegexIterator: {
1544            char *regex;
1545            int regex_len;
1546            long mode = REGIT_MODE_MATCH;
1547
1548            intern->u.regex.use_flags = ZEND_NUM_ARGS() >= 5;
1549            intern->u.regex.flags = 0;
1550            intern->u.regex.preg_flags = 0;
1551            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os|lll", &zobject, ce_inner, &regex, &regex_len, &mode, &intern->u.regex.flags, &intern->u.regex.preg_flags) == FAILURE) {
1552                zend_restore_error_handling(&error_handling TSRMLS_CC);
1553                return NULL;
1554            }
1555            if (mode < 0 || mode >= REGIT_MODE_MAX) {
1556                zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Illegal mode %ld", mode);
1557                zend_restore_error_handling(&error_handling TSRMLS_CC);
1558                return NULL;
1559            }
1560            intern->u.regex.mode = mode;
1561            intern->u.regex.regex = estrndup(regex, regex_len);
1562            intern->u.regex.regex_len = regex_len;
1563            intern->u.regex.pce = pcre_get_compiled_regex_cache(regex, regex_len TSRMLS_CC);
1564            if (intern->u.regex.pce == NULL) {
1565                /* pcre_get_compiled_regex_cache has already sent error */
1566                zend_restore_error_handling(&error_handling TSRMLS_CC);
1567                return NULL;
1568            }
1569            intern->u.regex.pce->refcount++;
1570            break;
1571        }
1572#endif
1573        case DIT_CallbackFilterIterator:
1574        case DIT_RecursiveCallbackFilterIterator: {
1575            _spl_cbfilter_it_intern *cfi = emalloc(sizeof(*cfi));
1576            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Of", &zobject, ce_inner, &cfi->fci, &cfi->fcc) == FAILURE) {
1577                zend_restore_error_handling(&error_handling TSRMLS_CC);
1578                efree(cfi);
1579                return NULL;
1580            }
1581            if (cfi->fci.function_name) {
1582                Z_ADDREF_P(cfi->fci.function_name);
1583            }
1584            if (cfi->fci.object_ptr) {
1585                Z_ADDREF_P(cfi->fci.object_ptr);
1586            }
1587            intern->u.cbfilter = cfi;
1588            break;
1589        }
1590        default:
1591            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobject, ce_inner) == FAILURE) {
1592                zend_restore_error_handling(&error_handling TSRMLS_CC);
1593                return NULL;
1594            }
1595            break;
1596    }
1597
1598    zend_restore_error_handling(&error_handling TSRMLS_CC);
1599
1600    if (inc_refcount) {
1601        Z_ADDREF_P(zobject);
1602    }
1603    intern->inner.zobject = zobject;
1604    intern->inner.ce = dit_type == DIT_IteratorIterator ? ce : Z_OBJCE_P(zobject);
1605    intern->inner.object = zend_object_store_get_object(zobject TSRMLS_CC);
1606    intern->inner.iterator = intern->inner.ce->get_iterator(intern->inner.ce, zobject, 0 TSRMLS_CC);
1607
1608    return intern;
1609}
1610
1611/* {{{ proto void FilterIterator::__construct(Iterator it)
1612   Create an Iterator from another iterator */
1613SPL_METHOD(FilterIterator, __construct)
1614{
1615    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_FilterIterator, zend_ce_iterator, DIT_FilterIterator);
1616} /* }}} */
1617
1618/* {{{ proto void CallbackFilterIterator::__construct(Iterator it, callback)
1619   Create an Iterator from another iterator */
1620SPL_METHOD(CallbackFilterIterator, __construct)
1621{
1622    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_CallbackFilterIterator, zend_ce_iterator, DIT_CallbackFilterIterator);
1623} /* }}} */
1624
1625/* {{{ proto Iterator FilterIterator::getInnerIterator()
1626       proto Iterator CachingIterator::getInnerIterator()
1627       proto Iterator LimitIterator::getInnerIterator()
1628       proto Iterator ParentIterator::getInnerIterator()
1629   Get the inner iterator */
1630SPL_METHOD(dual_it, getInnerIterator)
1631{
1632    spl_dual_it_object   *intern;
1633
1634    if (zend_parse_parameters_none() == FAILURE) {
1635        return;
1636    }
1637
1638    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1639
1640    if (intern->inner.zobject) {
1641        RETVAL_ZVAL(intern->inner.zobject, 1, 0);
1642    } else {
1643        RETURN_NULL();
1644    }
1645} /* }}} */
1646
1647static inline void spl_dual_it_require(spl_dual_it_object *intern TSRMLS_DC)
1648{
1649    if (!intern->inner.iterator) {
1650        php_error_docref(NULL TSRMLS_CC, E_ERROR, "The inner constructor wasn't initialized with an iterator instance");
1651    }
1652}
1653
1654static inline void spl_dual_it_free(spl_dual_it_object *intern TSRMLS_DC)
1655{
1656    if (intern->inner.iterator && intern->inner.iterator->funcs->invalidate_current) {
1657        intern->inner.iterator->funcs->invalidate_current(intern->inner.iterator TSRMLS_CC);
1658    }
1659    if (intern->current.data) {
1660        zval_ptr_dtor(&intern->current.data);
1661        intern->current.data = NULL;
1662    }
1663    if (intern->current.key) {
1664        zval_ptr_dtor(&intern->current.key);
1665        intern->current.key = NULL;
1666    }
1667    if (intern->dit_type == DIT_CachingIterator || intern->dit_type == DIT_RecursiveCachingIterator) {
1668        if (intern->u.caching.zstr) {
1669            zval_ptr_dtor(&intern->u.caching.zstr);
1670            intern->u.caching.zstr = NULL;
1671        }
1672        if (intern->u.caching.zchildren) {
1673            zval_ptr_dtor(&intern->u.caching.zchildren);
1674            intern->u.caching.zchildren = NULL;
1675        }
1676    }
1677}
1678
1679static inline void spl_dual_it_rewind(spl_dual_it_object *intern TSRMLS_DC)
1680{
1681    spl_dual_it_free(intern TSRMLS_CC);
1682    intern->current.pos = 0;
1683    if (intern->inner.iterator->funcs->rewind) {
1684        intern->inner.iterator->funcs->rewind(intern->inner.iterator TSRMLS_CC);
1685    }
1686}
1687
1688static inline int spl_dual_it_valid(spl_dual_it_object *intern TSRMLS_DC)
1689{
1690    if (!intern->inner.iterator) {
1691        return FAILURE;
1692    }
1693    /* FAILURE / SUCCESS */
1694    return intern->inner.iterator->funcs->valid(intern->inner.iterator TSRMLS_CC);
1695}
1696
1697static inline int spl_dual_it_fetch(spl_dual_it_object *intern, int check_more TSRMLS_DC)
1698{
1699    zval **data;
1700
1701    spl_dual_it_free(intern TSRMLS_CC);
1702    if (!check_more || spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
1703        intern->inner.iterator->funcs->get_current_data(intern->inner.iterator, &data TSRMLS_CC);
1704        if (data && *data) {
1705            intern->current.data = *data;
1706            Z_ADDREF_P(intern->current.data);
1707        }
1708
1709        MAKE_STD_ZVAL(intern->current.key);
1710        if (intern->inner.iterator->funcs->get_current_key) {
1711            intern->inner.iterator->funcs->get_current_key(intern->inner.iterator, intern->current.key TSRMLS_CC);
1712            if (EG(exception)) {
1713                zval_ptr_dtor(&intern->current.key);
1714                intern->current.key = NULL;
1715            }
1716        } else {
1717            ZVAL_LONG(intern->current.key, intern->current.pos);
1718        }
1719        return EG(exception) ? FAILURE : SUCCESS;
1720    }
1721    return FAILURE;
1722}
1723
1724static inline void spl_dual_it_next(spl_dual_it_object *intern, int do_free TSRMLS_DC)
1725{
1726    if (do_free) {
1727        spl_dual_it_free(intern TSRMLS_CC);
1728    } else {
1729        spl_dual_it_require(intern TSRMLS_CC);
1730    }
1731    intern->inner.iterator->funcs->move_forward(intern->inner.iterator TSRMLS_CC);
1732    intern->current.pos++;
1733}
1734
1735/* {{{ proto void ParentIterator::rewind()
1736       proto void IteratorIterator::rewind()
1737   Rewind the iterator
1738   */
1739SPL_METHOD(dual_it, rewind)
1740{
1741    spl_dual_it_object   *intern;
1742
1743    if (zend_parse_parameters_none() == FAILURE) {
1744        return;
1745    }
1746
1747    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1748
1749    spl_dual_it_rewind(intern TSRMLS_CC);
1750    spl_dual_it_fetch(intern, 1 TSRMLS_CC);
1751} /* }}} */
1752
1753/* {{{ proto bool FilterIterator::valid()
1754       proto bool ParentIterator::valid()
1755       proto bool IteratorIterator::valid()
1756       proto bool NoRewindIterator::valid()
1757   Check whether the current element is valid */
1758SPL_METHOD(dual_it, valid)
1759{
1760    spl_dual_it_object   *intern;
1761
1762    if (zend_parse_parameters_none() == FAILURE) {
1763        return;
1764    }
1765
1766    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1767
1768    RETURN_BOOL(intern->current.data);
1769} /* }}} */
1770
1771/* {{{ proto mixed FilterIterator::key()
1772       proto mixed CachingIterator::key()
1773       proto mixed LimitIterator::key()
1774       proto mixed ParentIterator::key()
1775       proto mixed IteratorIterator::key()
1776       proto mixed NoRewindIterator::key()
1777       proto mixed AppendIterator::key()
1778   Get the current key */
1779SPL_METHOD(dual_it, key)
1780{
1781    spl_dual_it_object   *intern;
1782
1783    if (zend_parse_parameters_none() == FAILURE) {
1784        return;
1785    }
1786
1787    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1788
1789    if (intern->current.key) {
1790        RETURN_ZVAL(intern->current.key, 1, 0);
1791    }
1792    RETURN_NULL();
1793} /* }}} */
1794
1795/* {{{ proto mixed FilterIterator::current()
1796       proto mixed CachingIterator::current()
1797       proto mixed LimitIterator::current()
1798       proto mixed ParentIterator::current()
1799       proto mixed IteratorIterator::current()
1800       proto mixed NoRewindIterator::current()
1801       proto mixed AppendIterator::current()
1802   Get the current element value */
1803SPL_METHOD(dual_it, current)
1804{
1805    spl_dual_it_object   *intern;
1806
1807    if (zend_parse_parameters_none() == FAILURE) {
1808        return;
1809    }
1810
1811    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1812
1813    if (intern->current.data) {
1814        RETVAL_ZVAL(intern->current.data, 1, 0);
1815    } else {
1816        RETURN_NULL();
1817    }
1818} /* }}} */
1819
1820/* {{{ proto void ParentIterator::next()
1821       proto void IteratorIterator::next()
1822       proto void NoRewindIterator::next()
1823   Move the iterator forward */
1824SPL_METHOD(dual_it, next)
1825{
1826    spl_dual_it_object   *intern;
1827
1828    if (zend_parse_parameters_none() == FAILURE) {
1829        return;
1830    }
1831
1832    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1833
1834    spl_dual_it_next(intern, 1 TSRMLS_CC);
1835    spl_dual_it_fetch(intern, 1 TSRMLS_CC);
1836} /* }}} */
1837
1838static inline void spl_filter_it_fetch(zval *zthis, spl_dual_it_object *intern TSRMLS_DC)
1839{
1840    zval *retval;
1841
1842    while (spl_dual_it_fetch(intern, 1 TSRMLS_CC) == SUCCESS) {
1843        zend_call_method_with_0_params(&zthis, intern->std.ce, NULL, "accept", &retval);
1844        if (retval) {
1845            if (zend_is_true(retval)) {
1846                zval_ptr_dtor(&retval);
1847                return;
1848            }
1849            zval_ptr_dtor(&retval);
1850        }
1851        if (EG(exception)) {
1852            return;
1853        }
1854        intern->inner.iterator->funcs->move_forward(intern->inner.iterator TSRMLS_CC);
1855    }
1856    spl_dual_it_free(intern TSRMLS_CC);
1857}
1858
1859static inline void spl_filter_it_rewind(zval *zthis, spl_dual_it_object *intern TSRMLS_DC)
1860{
1861    spl_dual_it_rewind(intern TSRMLS_CC);
1862    spl_filter_it_fetch(zthis, intern TSRMLS_CC);
1863}
1864
1865static inline void spl_filter_it_next(zval *zthis, spl_dual_it_object *intern TSRMLS_DC)
1866{
1867    spl_dual_it_next(intern, 1 TSRMLS_CC);
1868    spl_filter_it_fetch(zthis, intern TSRMLS_CC);
1869}
1870
1871/* {{{ proto void FilterIterator::rewind()
1872   Rewind the iterator */
1873SPL_METHOD(FilterIterator, rewind)
1874{
1875    spl_dual_it_object   *intern;
1876
1877    if (zend_parse_parameters_none() == FAILURE) {
1878        return;
1879    }
1880
1881    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1882    spl_filter_it_rewind(getThis(), intern TSRMLS_CC);
1883} /* }}} */
1884
1885/* {{{ proto void FilterIterator::next()
1886   Move the iterator forward */
1887SPL_METHOD(FilterIterator, next)
1888{
1889    spl_dual_it_object   *intern;
1890
1891    if (zend_parse_parameters_none() == FAILURE) {
1892        return;
1893    }
1894
1895    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1896    spl_filter_it_next(getThis(), intern TSRMLS_CC);
1897} /* }}} */
1898
1899/* {{{ proto void RecursiveCallbackFilterIterator::__construct(RecursiveIterator it, callback)
1900   Create a RecursiveCallbackFilterIterator from a RecursiveIterator */
1901SPL_METHOD(RecursiveCallbackFilterIterator, __construct)
1902{
1903    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveCallbackFilterIterator, spl_ce_RecursiveIterator, DIT_RecursiveCallbackFilterIterator);
1904} /* }}} */
1905
1906
1907/* {{{ proto void RecursiveFilterIterator::__construct(RecursiveIterator it)
1908   Create a RecursiveFilterIterator from a RecursiveIterator */
1909SPL_METHOD(RecursiveFilterIterator, __construct)
1910{
1911    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveFilterIterator, spl_ce_RecursiveIterator, DIT_RecursiveFilterIterator);
1912} /* }}} */
1913
1914/* {{{ proto bool RecursiveFilterIterator::hasChildren()
1915   Check whether the inner iterator's current element has children */
1916SPL_METHOD(RecursiveFilterIterator, hasChildren)
1917{
1918    spl_dual_it_object   *intern;
1919    zval                 *retval;
1920
1921    if (zend_parse_parameters_none() == FAILURE) {
1922        return;
1923    }
1924
1925    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1926
1927    zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "haschildren", &retval);
1928    if (retval) {
1929        RETURN_ZVAL(retval, 0, 1);
1930    } else {
1931        RETURN_FALSE;
1932    }
1933} /* }}} */
1934
1935/* {{{ proto RecursiveFilterIterator RecursiveFilterIterator::getChildren()
1936   Return the inner iterator's children contained in a RecursiveFilterIterator */
1937SPL_METHOD(RecursiveFilterIterator, getChildren)
1938{
1939    spl_dual_it_object   *intern;
1940    zval                 *retval;
1941
1942    if (zend_parse_parameters_none() == FAILURE) {
1943        return;
1944    }
1945
1946    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1947
1948    zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "getchildren", &retval);
1949    if (!EG(exception) && retval) {
1950        spl_instantiate_arg_ex1(Z_OBJCE_P(getThis()), &return_value, 0, retval TSRMLS_CC);
1951    }
1952    if (retval) {
1953        zval_ptr_dtor(&retval);
1954    }
1955} /* }}} */
1956
1957/* {{{ proto RecursiveCallbackFilterIterator RecursiveCallbackFilterIterator::getChildren()
1958   Return the inner iterator's children contained in a RecursiveCallbackFilterIterator */
1959SPL_METHOD(RecursiveCallbackFilterIterator, getChildren)
1960{
1961    spl_dual_it_object   *intern;
1962    zval                 *retval;
1963
1964    if (zend_parse_parameters_none() == FAILURE) {
1965        return;
1966    }
1967
1968    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
1969
1970    zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "getchildren", &retval);
1971    if (!EG(exception) && retval) {
1972        spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), &return_value, 0, retval, intern->u.cbfilter->fci.function_name TSRMLS_CC);
1973    }
1974    if (retval) {
1975        zval_ptr_dtor(&retval);
1976    }
1977} /* }}} */
1978/* {{{ proto void ParentIterator::__construct(RecursiveIterator it)
1979   Create a ParentIterator from a RecursiveIterator */
1980SPL_METHOD(ParentIterator, __construct)
1981{
1982    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_ParentIterator, spl_ce_RecursiveIterator, DIT_ParentIterator);
1983} /* }}} */
1984
1985#if HAVE_PCRE || HAVE_BUNDLED_PCRE
1986/* {{{ proto void RegexIterator::__construct(Iterator it, string regex [, int mode [, int flags [, int preg_flags]]])
1987   Create an RegexIterator from another iterator and a regular expression */
1988SPL_METHOD(RegexIterator, __construct)
1989{
1990    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RegexIterator, zend_ce_iterator, DIT_RegexIterator);
1991} /* }}} */
1992
1993/* {{{ proto bool CallbackFilterIterator::accept()
1994   Calls the callback with the current value, the current key and the inner iterator as arguments */
1995SPL_METHOD(CallbackFilterIterator, accept)
1996{
1997    spl_dual_it_object     *intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1998    zend_fcall_info        *fci = &intern->u.cbfilter->fci;
1999    zend_fcall_info_cache  *fcc = &intern->u.cbfilter->fcc;
2000    zval                  **params[3];
2001    zval                   *result;
2002
2003    if (zend_parse_parameters_none() == FAILURE) {
2004        return;
2005    }
2006
2007    if (intern->current.data == NULL || intern->current.key == NULL) {
2008        RETURN_FALSE;
2009    }
2010
2011    params[0] = &intern->current.data;
2012    params[1] = &intern->current.key;
2013    params[2] = &intern->inner.zobject;
2014
2015    fci->retval_ptr_ptr = &result;
2016    fci->param_count = 3;
2017    fci->params = params;
2018    fci->no_separation = 0;
2019
2020    if (zend_call_function(fci, fcc TSRMLS_CC) != SUCCESS || !result) {
2021        RETURN_FALSE;
2022    }
2023    if (EG(exception)) {
2024        return;
2025    }
2026
2027    RETURN_ZVAL(result, 1, 1);
2028}
2029/* }}} */
2030
2031/* {{{ proto bool RegexIterator::accept()
2032   Match (string)current() against regular expression */
2033SPL_METHOD(RegexIterator, accept)
2034{
2035    spl_dual_it_object *intern;
2036    char       *subject, *result;
2037    int        subject_len, use_copy, count = 0, result_len;
2038    zval       *subject_ptr, subject_copy, zcount, *replacement, tmp_replacement;
2039
2040    if (zend_parse_parameters_none() == FAILURE) {
2041        return;
2042    }
2043
2044    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2045
2046    if (intern->current.data == NULL) {
2047        RETURN_FALSE;
2048    }
2049
2050    if (intern->u.regex.flags & REGIT_USE_KEY) {
2051        subject_ptr = intern->current.key;
2052    } else {
2053        subject_ptr = intern->current.data;
2054    }
2055
2056    zend_make_printable_zval(subject_ptr, &subject_copy, &use_copy);
2057    if (use_copy) {
2058        subject = Z_STRVAL(subject_copy);
2059        subject_len = Z_STRLEN(subject_copy);
2060    } else {
2061        subject = Z_STRVAL_P(subject_ptr);
2062        subject_len = Z_STRLEN_P(subject_ptr);
2063    }
2064
2065    switch (intern->u.regex.mode)
2066    {
2067    case REGIT_MODE_MAX: /* won't happen but makes compiler happy */
2068    case REGIT_MODE_MATCH:
2069        count = pcre_exec(intern->u.regex.pce->re, intern->u.regex.pce->extra, subject, subject_len, 0, 0, NULL, 0);
2070        RETVAL_BOOL(count >= 0);
2071        break;
2072
2073    case REGIT_MODE_ALL_MATCHES:
2074    case REGIT_MODE_GET_MATCH:
2075        if (!use_copy) {
2076            subject = estrndup(subject, subject_len);
2077            use_copy = 1;
2078        }
2079        zval_ptr_dtor(&intern->current.data);
2080        ALLOC_INIT_ZVAL(intern->current.data);
2081        php_pcre_match_impl(intern->u.regex.pce, subject, subject_len, &zcount,
2082            intern->current.data, intern->u.regex.mode == REGIT_MODE_ALL_MATCHES, intern->u.regex.use_flags, intern->u.regex.preg_flags, 0 TSRMLS_CC);
2083        count = zend_hash_num_elements(Z_ARRVAL_P(intern->current.data));
2084        RETVAL_BOOL(count > 0);
2085        break;
2086
2087    case REGIT_MODE_SPLIT:
2088        if (!use_copy) {
2089            subject = estrndup(subject, subject_len);
2090            use_copy = 1;
2091        }
2092        zval_ptr_dtor(&intern->current.data);
2093        ALLOC_INIT_ZVAL(intern->current.data);
2094        php_pcre_split_impl(intern->u.regex.pce, subject, subject_len, intern->current.data, -1, intern->u.regex.preg_flags TSRMLS_CC);
2095        count = zend_hash_num_elements(Z_ARRVAL_P(intern->current.data));
2096        RETVAL_BOOL(count > 1);
2097        break;
2098
2099    case REGIT_MODE_REPLACE:
2100        replacement = zend_read_property(intern->std.ce, getThis(), "replacement", sizeof("replacement")-1, 1 TSRMLS_CC);
2101        if (Z_TYPE_P(replacement) != IS_STRING) {
2102            tmp_replacement = *replacement;
2103            zval_copy_ctor(&tmp_replacement);
2104            convert_to_string(&tmp_replacement);
2105            replacement = &tmp_replacement;
2106        }
2107        result = php_pcre_replace_impl(intern->u.regex.pce, subject, subject_len, replacement, 0, &result_len, -1, &count TSRMLS_CC);
2108
2109        if (intern->u.regex.flags & REGIT_USE_KEY) {
2110            zval_ptr_dtor(&intern->current.key);
2111            MAKE_STD_ZVAL(intern->current.key);
2112            ZVAL_STRINGL(intern->current.key, result, result_len, 0);
2113        } else {
2114            zval_ptr_dtor(&intern->current.data);
2115            MAKE_STD_ZVAL(intern->current.data);
2116            ZVAL_STRINGL(intern->current.data, result, result_len, 0);
2117        }
2118
2119        if (replacement == &tmp_replacement) {
2120            zval_dtor(replacement);
2121        }
2122        RETVAL_BOOL(count > 0);
2123    }
2124
2125    if (intern->u.regex.flags & REGIT_INVERTED) {
2126        RETVAL_BOOL(! Z_LVAL_P(return_value));
2127    }
2128
2129    if (use_copy) {
2130        str_efree(subject);
2131    }
2132} /* }}} */
2133
2134/* {{{ proto string RegexIterator::getRegex()
2135   Returns current regular expression */
2136SPL_METHOD(RegexIterator, getRegex)
2137{
2138    spl_dual_it_object *intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
2139
2140    if (zend_parse_parameters_none() == FAILURE) {
2141        return;
2142    }
2143
2144    RETURN_STRINGL(intern->u.regex.regex, intern->u.regex.regex_len, 1);
2145} /* }}} */
2146
2147/* {{{ proto bool RegexIterator::getMode()
2148   Returns current operation mode */
2149SPL_METHOD(RegexIterator, getMode)
2150{
2151    spl_dual_it_object *intern;
2152
2153    if (zend_parse_parameters_none() == FAILURE) {
2154        return;
2155    }
2156
2157    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2158
2159    RETURN_LONG(intern->u.regex.mode);
2160} /* }}} */
2161
2162/* {{{ proto bool RegexIterator::setMode(int new_mode)
2163   Set new operation mode */
2164SPL_METHOD(RegexIterator, setMode)
2165{
2166    spl_dual_it_object *intern;
2167    long mode;
2168
2169    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mode) == FAILURE) {
2170        return;
2171    }
2172
2173    if (mode < 0 || mode >= REGIT_MODE_MAX) {
2174        zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Illegal mode %ld", mode);
2175        return;/* NULL */
2176    }
2177
2178    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2179
2180    intern->u.regex.mode = mode;
2181} /* }}} */
2182
2183/* {{{ proto bool RegexIterator::getFlags()
2184   Returns current operation flags */
2185SPL_METHOD(RegexIterator, getFlags)
2186{
2187    spl_dual_it_object *intern;
2188
2189    if (zend_parse_parameters_none() == FAILURE) {
2190        return;
2191    }
2192
2193    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2194
2195    RETURN_LONG(intern->u.regex.flags);
2196} /* }}} */
2197
2198/* {{{ proto bool RegexIterator::setFlags(int new_flags)
2199   Set operation flags */
2200SPL_METHOD(RegexIterator, setFlags)
2201{
2202    spl_dual_it_object *intern;
2203    long flags;
2204
2205    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &flags) == FAILURE) {
2206        return;
2207    }
2208
2209    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2210
2211    intern->u.regex.flags = flags;
2212} /* }}} */
2213
2214/* {{{ proto bool RegexIterator::getFlags()
2215   Returns current PREG flags (if in use or NULL) */
2216SPL_METHOD(RegexIterator, getPregFlags)
2217{
2218    spl_dual_it_object *intern;
2219
2220    if (zend_parse_parameters_none() == FAILURE) {
2221        return;
2222    }
2223
2224    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2225
2226    if (intern->u.regex.use_flags) {
2227        RETURN_LONG(intern->u.regex.preg_flags);
2228    } else {
2229        return;
2230    }
2231} /* }}} */
2232
2233/* {{{ proto bool RegexIterator::setPregFlags(int new_flags)
2234   Set PREG flags */
2235SPL_METHOD(RegexIterator, setPregFlags)
2236{
2237    spl_dual_it_object *intern;
2238    long preg_flags;
2239
2240    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &preg_flags) == FAILURE) {
2241        return;
2242    }
2243
2244    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2245
2246    intern->u.regex.preg_flags = preg_flags;
2247    intern->u.regex.use_flags = 1;
2248} /* }}} */
2249
2250/* {{{ proto void RecursiveRegexIterator::__construct(RecursiveIterator it, string regex [, int mode [, int flags [, int preg_flags]]])
2251   Create an RecursiveRegexIterator from another recursive iterator and a regular expression */
2252SPL_METHOD(RecursiveRegexIterator, __construct)
2253{
2254    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveRegexIterator, spl_ce_RecursiveIterator, DIT_RecursiveRegexIterator);
2255} /* }}} */
2256
2257/* {{{ proto RecursiveRegexIterator RecursiveRegexIterator::getChildren()
2258   Return the inner iterator's children contained in a RecursiveRegexIterator */
2259SPL_METHOD(RecursiveRegexIterator, getChildren)
2260{
2261    spl_dual_it_object   *intern;
2262    zval                 *retval, *regex;
2263
2264    if (zend_parse_parameters_none() == FAILURE) {
2265        return;
2266    }
2267
2268    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2269
2270    zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "getchildren", &retval);
2271    if (!EG(exception)) {
2272        MAKE_STD_ZVAL(regex);
2273        ZVAL_STRING(regex, intern->u.regex.regex, 1);
2274        spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), &return_value, 0, retval, regex TSRMLS_CC);
2275        zval_ptr_dtor(&regex);
2276    }
2277    if (retval) {
2278        zval_ptr_dtor(&retval);
2279    }
2280} /* }}} */
2281
2282#endif
2283
2284/* {{{ spl_dual_it_dtor */
2285static void spl_dual_it_dtor(zend_object *_object, zend_object_handle handle TSRMLS_DC)
2286{
2287    spl_dual_it_object        *object = (spl_dual_it_object *)_object;
2288
2289    /* call standard dtor */
2290    zend_objects_destroy_object(_object, handle TSRMLS_CC);
2291
2292    spl_dual_it_free(object TSRMLS_CC);
2293
2294    if (object->inner.iterator) {
2295        object->inner.iterator->funcs->dtor(object->inner.iterator TSRMLS_CC);
2296    }
2297}
2298/* }}} */
2299
2300/* {{{ spl_dual_it_free_storage */
2301static void spl_dual_it_free_storage(void *_object TSRMLS_DC)
2302{
2303    spl_dual_it_object        *object = (spl_dual_it_object *)_object;
2304
2305
2306    if (object->inner.zobject) {
2307        zval_ptr_dtor(&object->inner.zobject);
2308    }
2309
2310    if (object->dit_type == DIT_AppendIterator) {
2311        object->u.append.iterator->funcs->dtor(object->u.append.iterator TSRMLS_CC);
2312        if (object->u.append.zarrayit) {
2313            zval_ptr_dtor(&object->u.append.zarrayit);
2314        }
2315    }
2316
2317    if (object->dit_type == DIT_CachingIterator || object->dit_type == DIT_RecursiveCachingIterator) {
2318        if (object->u.caching.zcache) {
2319            zval_ptr_dtor(&object->u.caching.zcache);
2320            object->u.caching.zcache = NULL;
2321        }
2322    }
2323
2324#if HAVE_PCRE || HAVE_BUNDLED_PCRE
2325    if (object->dit_type == DIT_RegexIterator || object->dit_type == DIT_RecursiveRegexIterator) {
2326        if (object->u.regex.pce) {
2327            object->u.regex.pce->refcount--;
2328        }
2329        if (object->u.regex.regex) {
2330            efree(object->u.regex.regex);
2331        }
2332    }
2333#endif
2334
2335    if (object->dit_type == DIT_CallbackFilterIterator || object->dit_type == DIT_RecursiveCallbackFilterIterator) {
2336        if (object->u.cbfilter) {
2337            if (object->u.cbfilter->fci.function_name) {
2338                zval_ptr_dtor(&object->u.cbfilter->fci.function_name);
2339            }
2340            if (object->u.cbfilter->fci.object_ptr) {
2341                zval_ptr_dtor(&object->u.cbfilter->fci.object_ptr);
2342            }
2343            efree(object->u.cbfilter);
2344        }
2345    }
2346
2347    zend_object_std_dtor(&object->std TSRMLS_CC);
2348
2349    efree(object);
2350}
2351/* }}} */
2352
2353/* {{{ spl_dual_it_new */
2354static zend_object_value spl_dual_it_new(zend_class_entry *class_type TSRMLS_DC)
2355{
2356    zend_object_value retval;
2357    spl_dual_it_object *intern;
2358
2359    intern = emalloc(sizeof(spl_dual_it_object));
2360    memset(intern, 0, sizeof(spl_dual_it_object));
2361    intern->dit_type = DIT_Unknown;
2362
2363    zend_object_std_init(&intern->std, class_type TSRMLS_CC);
2364    object_properties_init(&intern->std, class_type);
2365
2366    retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)spl_dual_it_dtor, (zend_objects_free_object_storage_t) spl_dual_it_free_storage, NULL TSRMLS_CC);
2367    retval.handlers = &spl_handlers_dual_it;
2368    return retval;
2369}
2370/* }}} */
2371
2372ZEND_BEGIN_ARG_INFO(arginfo_filter_it___construct, 0)
2373    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
2374ZEND_END_ARG_INFO();
2375
2376static const zend_function_entry spl_funcs_FilterIterator[] = {
2377    SPL_ME(FilterIterator,  __construct,      arginfo_filter_it___construct, ZEND_ACC_PUBLIC)
2378    SPL_ME(FilterIterator,  rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2379    SPL_ME(dual_it,         valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2380    SPL_ME(dual_it,         key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2381    SPL_ME(dual_it,         current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2382    SPL_ME(FilterIterator,  next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2383    SPL_ME(dual_it,         getInnerIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2384    SPL_ABSTRACT_ME(FilterIterator, accept,   arginfo_recursive_it_void)
2385    PHP_FE_END
2386};
2387
2388ZEND_BEGIN_ARG_INFO(arginfo_callback_filter_it___construct, 0)
2389    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
2390    ZEND_ARG_INFO(0, callback)
2391ZEND_END_ARG_INFO();
2392
2393static const zend_function_entry spl_funcs_CallbackFilterIterator[] = {
2394    SPL_ME(CallbackFilterIterator, __construct, arginfo_callback_filter_it___construct, ZEND_ACC_PUBLIC)
2395    SPL_ME(CallbackFilterIterator, accept,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2396    PHP_FE_END
2397};
2398
2399ZEND_BEGIN_ARG_INFO(arginfo_recursive_callback_filter_it___construct, 0)
2400    ZEND_ARG_OBJ_INFO(0, iterator, RecursiveIterator, 0)
2401    ZEND_ARG_INFO(0, callback)
2402ZEND_END_ARG_INFO();
2403
2404static const zend_function_entry spl_funcs_RecursiveCallbackFilterIterator[] = {
2405    SPL_ME(RecursiveCallbackFilterIterator, __construct, arginfo_recursive_callback_filter_it___construct, ZEND_ACC_PUBLIC)
2406    SPL_ME(RecursiveFilterIterator,  hasChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2407    SPL_ME(RecursiveCallbackFilterIterator,  getChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2408    PHP_FE_END
2409};
2410
2411ZEND_BEGIN_ARG_INFO(arginfo_parent_it___construct, 0)
2412    ZEND_ARG_OBJ_INFO(0, iterator, RecursiveIterator, 0)
2413ZEND_END_ARG_INFO();
2414
2415static const zend_function_entry spl_funcs_RecursiveFilterIterator[] = {
2416    SPL_ME(RecursiveFilterIterator,  __construct,      arginfo_parent_it___construct, ZEND_ACC_PUBLIC)
2417    SPL_ME(RecursiveFilterIterator,  hasChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2418    SPL_ME(RecursiveFilterIterator,  getChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2419    PHP_FE_END
2420};
2421
2422static const zend_function_entry spl_funcs_ParentIterator[] = {
2423    SPL_ME(ParentIterator,  __construct,      arginfo_parent_it___construct, ZEND_ACC_PUBLIC)
2424    SPL_MA(ParentIterator,  accept,           RecursiveFilterIterator, hasChildren, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2425    PHP_FE_END
2426};
2427
2428#if HAVE_PCRE || HAVE_BUNDLED_PCRE
2429ZEND_BEGIN_ARG_INFO_EX(arginfo_regex_it___construct, 0, 0, 2)
2430    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
2431    ZEND_ARG_INFO(0, regex)
2432    ZEND_ARG_INFO(0, mode)
2433    ZEND_ARG_INFO(0, flags)
2434    ZEND_ARG_INFO(0, preg_flags)
2435ZEND_END_ARG_INFO();
2436
2437ZEND_BEGIN_ARG_INFO_EX(arginfo_regex_it_set_mode, 0, 0, 1)
2438    ZEND_ARG_INFO(0, mode)
2439ZEND_END_ARG_INFO();
2440
2441ZEND_BEGIN_ARG_INFO_EX(arginfo_regex_it_set_flags, 0, 0, 1)
2442    ZEND_ARG_INFO(0, flags)
2443ZEND_END_ARG_INFO();
2444
2445ZEND_BEGIN_ARG_INFO_EX(arginfo_regex_it_set_preg_flags, 0, 0, 1)
2446    ZEND_ARG_INFO(0, preg_flags)
2447ZEND_END_ARG_INFO();
2448
2449static const zend_function_entry spl_funcs_RegexIterator[] = {
2450    SPL_ME(RegexIterator,   __construct,      arginfo_regex_it___construct,    ZEND_ACC_PUBLIC)
2451    SPL_ME(RegexIterator,   accept,           arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
2452    SPL_ME(RegexIterator,   getMode,          arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
2453    SPL_ME(RegexIterator,   setMode,          arginfo_regex_it_set_mode,       ZEND_ACC_PUBLIC)
2454    SPL_ME(RegexIterator,   getFlags,         arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
2455    SPL_ME(RegexIterator,   setFlags,         arginfo_regex_it_set_flags,      ZEND_ACC_PUBLIC)
2456    SPL_ME(RegexIterator,   getPregFlags,     arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
2457    SPL_ME(RegexIterator,   setPregFlags,     arginfo_regex_it_set_preg_flags, ZEND_ACC_PUBLIC)
2458    SPL_ME(RegexIterator,   getRegex,         arginfo_recursive_it_void,       ZEND_ACC_PUBLIC)
2459    PHP_FE_END
2460};
2461
2462ZEND_BEGIN_ARG_INFO_EX(arginfo_rec_regex_it___construct, 0, 0, 2)
2463    ZEND_ARG_OBJ_INFO(0, iterator, RecursiveIterator, 0)
2464    ZEND_ARG_INFO(0, regex)
2465    ZEND_ARG_INFO(0, mode)
2466    ZEND_ARG_INFO(0, flags)
2467    ZEND_ARG_INFO(0, preg_flags)
2468ZEND_END_ARG_INFO();
2469
2470static const zend_function_entry spl_funcs_RecursiveRegexIterator[] = {
2471    SPL_ME(RecursiveRegexIterator,  __construct,      arginfo_rec_regex_it___construct, ZEND_ACC_PUBLIC)
2472    SPL_ME(RecursiveFilterIterator, hasChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2473    SPL_ME(RecursiveRegexIterator,  getChildren,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2474    PHP_FE_END
2475};
2476#endif
2477
2478static inline int spl_limit_it_valid(spl_dual_it_object *intern TSRMLS_DC)
2479{
2480    /* FAILURE / SUCCESS */
2481    if (intern->u.limit.count != -1 && intern->current.pos >= intern->u.limit.offset + intern->u.limit.count) {
2482        return FAILURE;
2483    } else {
2484        return spl_dual_it_valid(intern TSRMLS_CC);
2485    }
2486}
2487
2488static inline void spl_limit_it_seek(spl_dual_it_object *intern, long pos TSRMLS_DC)
2489{
2490    zval  *zpos;
2491
2492    spl_dual_it_free(intern TSRMLS_CC);
2493    if (pos < intern->u.limit.offset) {
2494        zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Cannot seek to %ld which is below the offset %ld", pos, intern->u.limit.offset);
2495        return;
2496    }
2497    if (pos >= intern->u.limit.offset + intern->u.limit.count && intern->u.limit.count != -1) {
2498        zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Cannot seek to %ld which is behind offset %ld plus count %ld", pos, intern->u.limit.offset, intern->u.limit.count);
2499        return;
2500    }
2501    if (pos != intern->current.pos && instanceof_function(intern->inner.ce, spl_ce_SeekableIterator TSRMLS_CC)) {
2502        MAKE_STD_ZVAL(zpos);
2503        ZVAL_LONG(zpos, pos);
2504        spl_dual_it_free(intern TSRMLS_CC);
2505        zend_call_method_with_1_params(&intern->inner.zobject, intern->inner.ce, NULL, "seek", NULL, zpos);
2506        zval_ptr_dtor(&zpos);
2507        if (!EG(exception)) {
2508            intern->current.pos = pos;
2509            if (spl_limit_it_valid(intern TSRMLS_CC) == SUCCESS) {
2510                spl_dual_it_fetch(intern, 0 TSRMLS_CC);
2511            }
2512        }
2513    } else {
2514        /* emulate the forward seek, by next() calls */
2515        /* a back ward seek is done by a previous rewind() */
2516        if (pos < intern->current.pos) {
2517            spl_dual_it_rewind(intern TSRMLS_CC);
2518        }
2519        while (pos > intern->current.pos && spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
2520            spl_dual_it_next(intern, 1 TSRMLS_CC);
2521        }
2522        if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
2523            spl_dual_it_fetch(intern, 1 TSRMLS_CC);
2524        }
2525    }
2526}
2527
2528/* {{{ proto LimitIterator::__construct(Iterator it [, int offset, int count])
2529   Construct a LimitIterator from an Iterator with a given starting offset and optionally a maximum count */
2530SPL_METHOD(LimitIterator, __construct)
2531{
2532    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_LimitIterator, zend_ce_iterator, DIT_LimitIterator);
2533} /* }}} */
2534
2535/* {{{ proto void LimitIterator::rewind()
2536   Rewind the iterator to the specified starting offset */
2537SPL_METHOD(LimitIterator, rewind)
2538{
2539    spl_dual_it_object   *intern;
2540
2541    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2542    spl_dual_it_rewind(intern TSRMLS_CC);
2543    spl_limit_it_seek(intern, intern->u.limit.offset TSRMLS_CC);
2544} /* }}} */
2545
2546/* {{{ proto bool LimitIterator::valid()
2547   Check whether the current element is valid */
2548SPL_METHOD(LimitIterator, valid)
2549{
2550    spl_dual_it_object   *intern;
2551
2552    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2553
2554/*  RETURN_BOOL(spl_limit_it_valid(intern TSRMLS_CC) == SUCCESS);*/
2555    RETURN_BOOL((intern->u.limit.count == -1 || intern->current.pos < intern->u.limit.offset + intern->u.limit.count) && intern->current.data);
2556} /* }}} */
2557
2558/* {{{ proto void LimitIterator::next()
2559   Move the iterator forward */
2560SPL_METHOD(LimitIterator, next)
2561{
2562    spl_dual_it_object   *intern;
2563
2564    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2565
2566    spl_dual_it_next(intern, 1 TSRMLS_CC);
2567    if (intern->u.limit.count == -1 || intern->current.pos < intern->u.limit.offset + intern->u.limit.count) {
2568        spl_dual_it_fetch(intern, 1 TSRMLS_CC);
2569    }
2570} /* }}} */
2571
2572/* {{{ proto void LimitIterator::seek(int position)
2573   Seek to the given position */
2574SPL_METHOD(LimitIterator, seek)
2575{
2576    spl_dual_it_object   *intern;
2577    long                 pos;
2578
2579    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &pos) == FAILURE) {
2580        return;
2581    }
2582
2583    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2584    spl_limit_it_seek(intern, pos TSRMLS_CC);
2585    RETURN_LONG(intern->current.pos);
2586} /* }}} */
2587
2588/* {{{ proto int LimitIterator::getPosition()
2589   Return the current position */
2590SPL_METHOD(LimitIterator, getPosition)
2591{
2592    spl_dual_it_object   *intern;
2593    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2594    RETURN_LONG(intern->current.pos);
2595} /* }}} */
2596
2597ZEND_BEGIN_ARG_INFO(arginfo_seekable_it_seek, 0)
2598    ZEND_ARG_INFO(0, position)
2599ZEND_END_ARG_INFO();
2600
2601static const zend_function_entry spl_funcs_SeekableIterator[] = {
2602    SPL_ABSTRACT_ME(SeekableIterator, seek, arginfo_seekable_it_seek)
2603    PHP_FE_END
2604};
2605
2606ZEND_BEGIN_ARG_INFO_EX(arginfo_limit_it___construct, 0, 0, 1)
2607    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
2608    ZEND_ARG_INFO(0, offset)
2609    ZEND_ARG_INFO(0, count)
2610ZEND_END_ARG_INFO();
2611
2612ZEND_BEGIN_ARG_INFO(arginfo_limit_it_seek, 0)
2613    ZEND_ARG_INFO(0, position)
2614ZEND_END_ARG_INFO();
2615
2616static const zend_function_entry spl_funcs_LimitIterator[] = {
2617    SPL_ME(LimitIterator,   __construct,      arginfo_limit_it___construct, ZEND_ACC_PUBLIC)
2618    SPL_ME(LimitIterator,   rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2619    SPL_ME(LimitIterator,   valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2620    SPL_ME(dual_it,         key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2621    SPL_ME(dual_it,         current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2622    SPL_ME(LimitIterator,   next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2623    SPL_ME(LimitIterator,   seek,             arginfo_limit_it_seek, ZEND_ACC_PUBLIC)
2624    SPL_ME(LimitIterator,   getPosition,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2625    SPL_ME(dual_it,         getInnerIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
2626    PHP_FE_END
2627};
2628
2629static inline int spl_caching_it_valid(spl_dual_it_object *intern TSRMLS_DC)
2630{
2631    return intern->u.caching.flags & CIT_VALID ? SUCCESS : FAILURE;
2632}
2633
2634static inline int spl_caching_it_has_next(spl_dual_it_object *intern TSRMLS_DC)
2635{
2636    return spl_dual_it_valid(intern TSRMLS_CC);
2637}
2638
2639static inline void spl_caching_it_next(spl_dual_it_object *intern TSRMLS_DC)
2640{
2641    if (spl_dual_it_fetch(intern, 1 TSRMLS_CC) == SUCCESS) {
2642        intern->u.caching.flags |= CIT_VALID;
2643        /* Full cache ? */
2644        if (intern->u.caching.flags & CIT_FULL_CACHE) {
2645            zval *zcacheval;
2646            zval *key = intern->current.key;
2647
2648            MAKE_STD_ZVAL(zcacheval);
2649            ZVAL_ZVAL(zcacheval, intern->current.data, 1, 0);
2650
2651            array_set_zval_key(HASH_OF(intern->u.caching.zcache), key, zcacheval);
2652
2653            zval_ptr_dtor(&zcacheval);
2654        }
2655        /* Recursion ? */
2656        if (intern->dit_type == DIT_RecursiveCachingIterator) {
2657            zval *retval, *zchildren, zflags;
2658            zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "haschildren", &retval);
2659            if (EG(exception)) {
2660                if (retval) {
2661                    zval_ptr_dtor(&retval);
2662                }
2663                if (intern->u.caching.flags & CIT_CATCH_GET_CHILD) {
2664                    zend_clear_exception(TSRMLS_C);
2665                } else {
2666                    return;
2667                }
2668            } else {
2669                if (zend_is_true(retval)) {
2670                    zend_call_method_with_0_params(&intern->inner.zobject, intern->inner.ce, NULL, "getchildren", &zchildren);
2671                    if (EG(exception)) {
2672                        if (zchildren) {
2673                            zval_ptr_dtor(&zchildren);
2674                        }
2675                        if (intern->u.caching.flags & CIT_CATCH_GET_CHILD) {
2676                            zend_clear_exception(TSRMLS_C);
2677                        } else {
2678                            zval_ptr_dtor(&retval);
2679                            return;
2680                        }
2681                    } else {
2682                        INIT_PZVAL(&zflags);
2683                        ZVAL_LONG(&zflags, intern->u.caching.flags & CIT_PUBLIC);
2684                        spl_instantiate_arg_ex2(spl_ce_RecursiveCachingIterator, &intern->u.caching.zchildren, 1, zchildren, &zflags TSRMLS_CC);
2685                        zval_ptr_dtor(&zchildren);
2686                    }
2687                }
2688                zval_ptr_dtor(&retval);
2689                if (EG(exception)) {
2690                    if (intern->u.caching.flags & CIT_CATCH_GET_CHILD) {
2691                        zend_clear_exception(TSRMLS_C);
2692                    } else {
2693                        return;
2694                    }
2695                }
2696            }
2697        }
2698        if (intern->u.caching.flags & (CIT_TOSTRING_USE_INNER|CIT_CALL_TOSTRING)) {
2699            int  use_copy;
2700            zval expr_copy;
2701            ALLOC_ZVAL(intern->u.caching.zstr);
2702            if (intern->u.caching.flags & CIT_TOSTRING_USE_INNER) {
2703                *intern->u.caching.zstr = *intern->inner.zobject;
2704            } else {
2705                *intern->u.caching.zstr = *intern->current.data;
2706            }
2707            zend_make_printable_zval(intern->u.caching.zstr, &expr_copy, &use_copy);
2708            if (use_copy) {
2709                *intern->u.caching.zstr = expr_copy;
2710                INIT_PZVAL(intern->u.caching.zstr);
2711                zval_copy_ctor(intern->u.caching.zstr);
2712                zval_dtor(&expr_copy);
2713            } else {
2714                INIT_PZVAL(intern->u.caching.zstr);
2715                zval_copy_ctor(intern->u.caching.zstr);
2716            }
2717        }
2718        spl_dual_it_next(intern, 0 TSRMLS_CC);
2719    } else {
2720        intern->u.caching.flags &= ~CIT_VALID;
2721    }
2722}
2723
2724static inline void spl_caching_it_rewind(spl_dual_it_object *intern TSRMLS_DC)
2725{
2726    spl_dual_it_rewind(intern TSRMLS_CC);
2727    zend_hash_clean(HASH_OF(intern->u.caching.zcache));
2728    spl_caching_it_next(intern TSRMLS_CC);
2729}
2730
2731/* {{{ proto void CachingIterator::__construct(Iterator it [, flags = CIT_CALL_TOSTRING])
2732   Construct a CachingIterator from an Iterator */
2733SPL_METHOD(CachingIterator, __construct)
2734{
2735    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_CachingIterator, zend_ce_iterator, DIT_CachingIterator);
2736} /* }}} */
2737
2738/* {{{ proto void CachingIterator::rewind()
2739   Rewind the iterator */
2740SPL_METHOD(CachingIterator, rewind)
2741{
2742    spl_dual_it_object   *intern;
2743
2744    if (zend_parse_parameters_none() == FAILURE) {
2745        return;
2746    }
2747
2748    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2749
2750    spl_caching_it_rewind(intern TSRMLS_CC);
2751} /* }}} */
2752
2753/* {{{ proto bool CachingIterator::valid()
2754   Check whether the current element is valid */
2755SPL_METHOD(CachingIterator, valid)
2756{
2757    spl_dual_it_object   *intern;
2758
2759    if (zend_parse_parameters_none() == FAILURE) {
2760        return;
2761    }
2762
2763    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2764
2765    RETURN_BOOL(spl_caching_it_valid(intern TSRMLS_CC) == SUCCESS);
2766} /* }}} */
2767
2768/* {{{ proto void CachingIterator::next()
2769   Move the iterator forward */
2770SPL_METHOD(CachingIterator, next)
2771{
2772    spl_dual_it_object   *intern;
2773
2774    if (zend_parse_parameters_none() == FAILURE) {
2775        return;
2776    }
2777
2778    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2779
2780    spl_caching_it_next(intern TSRMLS_CC);
2781} /* }}} */
2782
2783/* {{{ proto bool CachingIterator::hasNext()
2784   Check whether the inner iterator has a valid next element */
2785SPL_METHOD(CachingIterator, hasNext)
2786{
2787    spl_dual_it_object   *intern;
2788
2789    if (zend_parse_parameters_none() == FAILURE) {
2790        return;
2791    }
2792
2793    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2794
2795    RETURN_BOOL(spl_caching_it_has_next(intern TSRMLS_CC) == SUCCESS);
2796} /* }}} */
2797
2798/* {{{ proto string CachingIterator::__toString()
2799   Return the string representation of the current element */
2800SPL_METHOD(CachingIterator, __toString)
2801{
2802    spl_dual_it_object   *intern;
2803
2804    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2805
2806    if (!(intern->u.caching.flags & (CIT_CALL_TOSTRING|CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT|CIT_TOSTRING_USE_INNER)))  {
2807        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not fetch string value (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2808        return;
2809    }
2810    if (intern->u.caching.flags & CIT_TOSTRING_USE_KEY) {
2811        MAKE_COPY_ZVAL(&intern->current.key, return_value);
2812        convert_to_string(return_value);
2813        return;
2814    } else if (intern->u.caching.flags & CIT_TOSTRING_USE_CURRENT) {
2815        MAKE_COPY_ZVAL(&intern->current.data, return_value);
2816        convert_to_string(return_value);
2817        return;
2818    }
2819    if (intern->u.caching.zstr) {
2820        RETURN_STRINGL(Z_STRVAL_P(intern->u.caching.zstr), Z_STRLEN_P(intern->u.caching.zstr), 1);
2821    } else {
2822        RETURN_NULL();
2823    }
2824} /* }}} */
2825
2826/* {{{ proto void CachingIterator::offsetSet(mixed index, mixed newval)
2827   Set given index in cache */
2828SPL_METHOD(CachingIterator, offsetSet)
2829{
2830    spl_dual_it_object   *intern;
2831    char *arKey;
2832    uint nKeyLength;
2833    zval *value;
2834
2835    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2836
2837    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
2838        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2839        return;
2840    }
2841
2842    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &arKey, &nKeyLength, &value) == FAILURE) {
2843        return;
2844    }
2845
2846    Z_ADDREF_P(value);
2847    zend_symtable_update(HASH_OF(intern->u.caching.zcache), arKey, nKeyLength+1, &value, sizeof(value), NULL);
2848}
2849/* }}} */
2850
2851/* {{{ proto string CachingIterator::offsetGet(mixed index)
2852   Return the internal cache if used */
2853SPL_METHOD(CachingIterator, offsetGet)
2854{
2855    spl_dual_it_object   *intern;
2856    char *arKey;
2857    uint nKeyLength;
2858    zval **value;
2859
2860    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2861
2862    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
2863        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2864        return;
2865    }
2866
2867    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arKey, &nKeyLength) == FAILURE) {
2868        return;
2869    }
2870
2871    if (zend_symtable_find(HASH_OF(intern->u.caching.zcache), arKey, nKeyLength+1, (void**)&value) == FAILURE) {
2872        zend_error(E_NOTICE, "Undefined index: %s", arKey);
2873        return;
2874    }
2875
2876    RETURN_ZVAL(*value, 1, 0);
2877}
2878/* }}} */
2879
2880/* {{{ proto void CachingIterator::offsetUnset(mixed index)
2881   Unset given index in cache */
2882SPL_METHOD(CachingIterator, offsetUnset)
2883{
2884    spl_dual_it_object   *intern;
2885    char *arKey;
2886    uint nKeyLength;
2887
2888    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2889
2890    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
2891        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2892        return;
2893    }
2894
2895    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arKey, &nKeyLength) == FAILURE) {
2896        return;
2897    }
2898
2899    zend_symtable_del(HASH_OF(intern->u.caching.zcache), arKey, nKeyLength+1);
2900}
2901/* }}} */
2902
2903/* {{{ proto bool CachingIterator::offsetExists(mixed index)
2904   Return whether the requested index exists */
2905SPL_METHOD(CachingIterator, offsetExists)
2906{
2907    spl_dual_it_object   *intern;
2908    char *arKey;
2909    uint nKeyLength;
2910
2911    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2912
2913    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
2914        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2915        return;
2916    }
2917
2918    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arKey, &nKeyLength) == FAILURE) {
2919        return;
2920    }
2921
2922    RETURN_BOOL(zend_symtable_exists(HASH_OF(intern->u.caching.zcache), arKey, nKeyLength+1));
2923}
2924/* }}} */
2925
2926/* {{{ proto bool CachingIterator::getCache()
2927   Return the cache */
2928SPL_METHOD(CachingIterator, getCache)
2929{
2930    spl_dual_it_object   *intern;
2931
2932    if (zend_parse_parameters_none() == FAILURE) {
2933        return;
2934    }
2935
2936    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2937
2938    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
2939        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%v does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
2940        return;
2941    }
2942
2943    RETURN_ZVAL(intern->u.caching.zcache, 1, 0);
2944}
2945/* }}} */
2946
2947/* {{{ proto int CachingIterator::getFlags()
2948   Return the internal flags */
2949SPL_METHOD(CachingIterator, getFlags)
2950{
2951    spl_dual_it_object   *intern;
2952
2953    if (zend_parse_parameters_none() == FAILURE) {
2954        return;
2955    }
2956
2957    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2958
2959    RETURN_LONG(intern->u.caching.flags);
2960}
2961/* }}} */
2962
2963/* {{{ proto void CachingIterator::setFlags(int flags)
2964   Set the internal flags */
2965SPL_METHOD(CachingIterator, setFlags)
2966{
2967    spl_dual_it_object   *intern;
2968    long flags;
2969
2970    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
2971
2972    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &flags) == FAILURE) {
2973        return;
2974    }
2975
2976    if (spl_cit_check_flags(flags) != SUCCESS) {
2977        zend_throw_exception(spl_ce_InvalidArgumentException , "Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, TOSTRING_USE_INNER", 0 TSRMLS_CC);
2978        return;
2979    }
2980    if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & CIT_CALL_TOSTRING) == 0) {
2981        zend_throw_exception(spl_ce_InvalidArgumentException, "Unsetting flag CALL_TO_STRING is not possible", 0 TSRMLS_CC);
2982        return;
2983    }
2984    if ((intern->u.caching.flags & CIT_TOSTRING_USE_INNER) != 0 && (flags & CIT_TOSTRING_USE_INNER) == 0) {
2985        zend_throw_exception(spl_ce_InvalidArgumentException, "Unsetting flag TOSTRING_USE_INNER is not possible", 0 TSRMLS_CC);
2986        return;
2987    }
2988    if ((flags & CIT_FULL_CACHE) != 0 && (intern->u.caching.flags & CIT_FULL_CACHE) == 0) {
2989        /* clear on (re)enable */
2990        zend_hash_clean(HASH_OF(intern->u.caching.zcache));
2991    }
2992    intern->u.caching.flags = (intern->u.caching.flags & ~CIT_PUBLIC) | (flags & CIT_PUBLIC);
2993}
2994/* }}} */
2995
2996/* {{{ proto void CachingIterator::count()
2997   Number of cached elements */
2998SPL_METHOD(CachingIterator, count)
2999{
3000    spl_dual_it_object   *intern;
3001
3002    if (zend_parse_parameters_none() == FAILURE) {
3003        return;
3004    }
3005
3006    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3007
3008    if (!(intern->u.caching.flags & CIT_FULL_CACHE))    {
3009        zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%v does not use a full cache (see CachingIterator::__construct)", Z_OBJCE_P(getThis())->name);
3010        return;
3011    }
3012
3013    RETURN_LONG(zend_hash_num_elements(HASH_OF(intern->u.caching.zcache)));
3014}
3015/* }}} */
3016
3017ZEND_BEGIN_ARG_INFO_EX(arginfo_caching_it___construct, 0, 0, 1)
3018    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
3019    ZEND_ARG_INFO(0, flags)
3020ZEND_END_ARG_INFO();
3021
3022ZEND_BEGIN_ARG_INFO(arginfo_caching_it_setFlags, 0)
3023    ZEND_ARG_INFO(0, flags)
3024ZEND_END_ARG_INFO();
3025
3026ZEND_BEGIN_ARG_INFO(arginfo_caching_it_offsetGet, 0)
3027    ZEND_ARG_INFO(0, index)
3028ZEND_END_ARG_INFO();
3029
3030ZEND_BEGIN_ARG_INFO(arginfo_caching_it_offsetSet, 0)
3031    ZEND_ARG_INFO(0, index)
3032    ZEND_ARG_INFO(0, newval)
3033ZEND_END_ARG_INFO();
3034
3035static const zend_function_entry spl_funcs_CachingIterator[] = {
3036    SPL_ME(CachingIterator, __construct,      arginfo_caching_it___construct, ZEND_ACC_PUBLIC)
3037    SPL_ME(CachingIterator, rewind,           arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3038    SPL_ME(CachingIterator, valid,            arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3039    SPL_ME(dual_it,         key,              arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3040    SPL_ME(dual_it,         current,          arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3041    SPL_ME(CachingIterator, next,             arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3042    SPL_ME(CachingIterator, hasNext,          arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3043    SPL_ME(CachingIterator, __toString,       arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3044    SPL_ME(dual_it,         getInnerIterator, arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3045    SPL_ME(CachingIterator, getFlags,         arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3046    SPL_ME(CachingIterator, setFlags,         arginfo_caching_it_setFlags,    ZEND_ACC_PUBLIC)
3047    SPL_ME(CachingIterator, offsetGet,        arginfo_caching_it_offsetGet,   ZEND_ACC_PUBLIC)
3048    SPL_ME(CachingIterator, offsetSet,        arginfo_caching_it_offsetSet,   ZEND_ACC_PUBLIC)
3049    SPL_ME(CachingIterator, offsetUnset,      arginfo_caching_it_offsetGet,   ZEND_ACC_PUBLIC)
3050    SPL_ME(CachingIterator, offsetExists,     arginfo_caching_it_offsetGet,   ZEND_ACC_PUBLIC)
3051    SPL_ME(CachingIterator, getCache,         arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3052    SPL_ME(CachingIterator, count,            arginfo_recursive_it_void,      ZEND_ACC_PUBLIC)
3053    PHP_FE_END
3054};
3055
3056/* {{{ proto void RecursiveCachingIterator::__construct(RecursiveIterator it [, flags = CIT_CALL_TOSTRING])
3057   Create an iterator from a RecursiveIterator */
3058SPL_METHOD(RecursiveCachingIterator, __construct)
3059{
3060    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_RecursiveCachingIterator, spl_ce_RecursiveIterator, DIT_RecursiveCachingIterator);
3061} /* }}} */
3062
3063/* {{{ proto bool RecursiveCachingIterator::hasChildren()
3064   Check whether the current element of the inner iterator has children */
3065SPL_METHOD(RecursiveCachingIterator, hasChildren)
3066{
3067    spl_dual_it_object   *intern;
3068
3069    if (zend_parse_parameters_none() == FAILURE) {
3070        return;
3071    }
3072
3073    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3074
3075    RETURN_BOOL(intern->u.caching.zchildren);
3076} /* }}} */
3077
3078/* {{{ proto RecursiveCachingIterator RecursiveCachingIterator::getChildren()
3079  Return the inner iterator's children as a RecursiveCachingIterator */
3080SPL_METHOD(RecursiveCachingIterator, getChildren)
3081{
3082    spl_dual_it_object   *intern;
3083
3084    if (zend_parse_parameters_none() == FAILURE) {
3085        return;
3086    }
3087
3088    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3089
3090    if (intern->u.caching.zchildren) {
3091        RETURN_ZVAL(intern->u.caching.zchildren, 1, 0);
3092    } else {
3093        RETURN_NULL();
3094    }
3095} /* }}} */
3096
3097ZEND_BEGIN_ARG_INFO_EX(arginfo_caching_rec_it___construct, 0, ZEND_RETURN_VALUE, 1)
3098    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
3099    ZEND_ARG_INFO(0, flags)
3100ZEND_END_ARG_INFO();
3101
3102static const zend_function_entry spl_funcs_RecursiveCachingIterator[] = {
3103    SPL_ME(RecursiveCachingIterator, __construct,   arginfo_caching_rec_it___construct, ZEND_ACC_PUBLIC)
3104    SPL_ME(RecursiveCachingIterator, hasChildren,   arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3105    SPL_ME(RecursiveCachingIterator, getChildren,   arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3106    PHP_FE_END
3107};
3108
3109/* {{{ proto void IteratorIterator::__construct(Traversable it)
3110   Create an iterator from anything that is traversable */
3111SPL_METHOD(IteratorIterator, __construct)
3112{
3113    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_IteratorIterator, zend_ce_traversable, DIT_IteratorIterator);
3114} /* }}} */
3115
3116ZEND_BEGIN_ARG_INFO(arginfo_iterator_it___construct, 0)
3117    ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0)
3118ZEND_END_ARG_INFO();
3119
3120static const zend_function_entry spl_funcs_IteratorIterator[] = {
3121    SPL_ME(IteratorIterator, __construct,      arginfo_iterator_it___construct, ZEND_ACC_PUBLIC)
3122    SPL_ME(dual_it,          rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3123    SPL_ME(dual_it,          valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3124    SPL_ME(dual_it,          key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3125    SPL_ME(dual_it,          current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3126    SPL_ME(dual_it,          next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3127    SPL_ME(dual_it,          getInnerIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3128    PHP_FE_END
3129};
3130
3131/* {{{ proto void NoRewindIterator::__construct(Iterator it)
3132   Create an iterator from another iterator */
3133SPL_METHOD(NoRewindIterator, __construct)
3134{
3135    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_NoRewindIterator, zend_ce_iterator, DIT_NoRewindIterator);
3136} /* }}} */
3137
3138/* {{{ proto void NoRewindIterator::rewind()
3139   Prevent a call to inner iterators rewind() */
3140SPL_METHOD(NoRewindIterator, rewind)
3141{
3142    if (zend_parse_parameters_none() == FAILURE) {
3143        return;
3144    }
3145    /* nothing to do */
3146} /* }}} */
3147
3148/* {{{ proto bool NoRewindIterator::valid()
3149   Return inner iterators valid() */
3150SPL_METHOD(NoRewindIterator, valid)
3151{
3152    spl_dual_it_object   *intern;
3153
3154    if (zend_parse_parameters_none() == FAILURE) {
3155        return;
3156    }
3157
3158    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3159    RETURN_BOOL(intern->inner.iterator->funcs->valid(intern->inner.iterator TSRMLS_CC) == SUCCESS);
3160} /* }}} */
3161
3162/* {{{ proto mixed NoRewindIterator::key()
3163   Return inner iterators key() */
3164SPL_METHOD(NoRewindIterator, key)
3165{
3166    spl_dual_it_object   *intern;
3167
3168    if (zend_parse_parameters_none() == FAILURE) {
3169        return;
3170    }
3171
3172    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3173
3174    if (intern->inner.iterator->funcs->get_current_key) {
3175        intern->inner.iterator->funcs->get_current_key(intern->inner.iterator, return_value TSRMLS_CC);
3176    } else {
3177        RETURN_NULL();
3178    }
3179} /* }}} */
3180
3181/* {{{ proto mixed NoRewindIterator::current()
3182   Return inner iterators current() */
3183SPL_METHOD(NoRewindIterator, current)
3184{
3185    spl_dual_it_object   *intern;
3186    zval **data;
3187
3188    if (zend_parse_parameters_none() == FAILURE) {
3189        return;
3190    }
3191
3192    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3193    intern->inner.iterator->funcs->get_current_data(intern->inner.iterator, &data TSRMLS_CC);
3194    if (data && *data) {
3195        RETURN_ZVAL(*data, 1, 0);
3196    }
3197} /* }}} */
3198
3199/* {{{ proto void NoRewindIterator::next()
3200   Return inner iterators next() */
3201SPL_METHOD(NoRewindIterator, next)
3202{
3203    spl_dual_it_object   *intern;
3204
3205    if (zend_parse_parameters_none() == FAILURE) {
3206        return;
3207    }
3208
3209    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3210    intern->inner.iterator->funcs->move_forward(intern->inner.iterator TSRMLS_CC);
3211} /* }}} */
3212
3213ZEND_BEGIN_ARG_INFO(arginfo_norewind_it___construct, 0)
3214    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
3215ZEND_END_ARG_INFO();
3216
3217static const zend_function_entry spl_funcs_NoRewindIterator[] = {
3218    SPL_ME(NoRewindIterator, __construct,      arginfo_norewind_it___construct, ZEND_ACC_PUBLIC)
3219    SPL_ME(NoRewindIterator, rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3220    SPL_ME(NoRewindIterator, valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3221    SPL_ME(NoRewindIterator, key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3222    SPL_ME(NoRewindIterator, current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3223    SPL_ME(NoRewindIterator, next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3224    SPL_ME(dual_it,          getInnerIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3225    PHP_FE_END
3226};
3227
3228/* {{{ proto void InfiniteIterator::__construct(Iterator it)
3229   Create an iterator from another iterator */
3230SPL_METHOD(InfiniteIterator, __construct)
3231{
3232    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_InfiniteIterator, zend_ce_iterator, DIT_InfiniteIterator);
3233} /* }}} */
3234
3235/* {{{ proto void InfiniteIterator::next()
3236   Prevent a call to inner iterators rewind() (internally the current data will be fetched if valid()) */
3237SPL_METHOD(InfiniteIterator, next)
3238{
3239    spl_dual_it_object   *intern;
3240
3241    if (zend_parse_parameters_none() == FAILURE) {
3242        return;
3243    }
3244
3245    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3246
3247    spl_dual_it_next(intern, 1 TSRMLS_CC);
3248    if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
3249        spl_dual_it_fetch(intern, 0 TSRMLS_CC);
3250    } else {
3251        spl_dual_it_rewind(intern TSRMLS_CC);
3252        if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
3253            spl_dual_it_fetch(intern, 0 TSRMLS_CC);
3254        }
3255    }
3256} /* }}} */
3257
3258static const zend_function_entry spl_funcs_InfiniteIterator[] = {
3259    SPL_ME(InfiniteIterator, __construct,      arginfo_norewind_it___construct, ZEND_ACC_PUBLIC)
3260    SPL_ME(InfiniteIterator, next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3261    PHP_FE_END
3262};
3263
3264/* {{{ proto void EmptyIterator::rewind()
3265   Does nothing  */
3266SPL_METHOD(EmptyIterator, rewind)
3267{
3268    if (zend_parse_parameters_none() == FAILURE) {
3269        return;
3270    }
3271} /* }}} */
3272
3273/* {{{ proto false EmptyIterator::valid()
3274   Return false */
3275SPL_METHOD(EmptyIterator, valid)
3276{
3277    if (zend_parse_parameters_none() == FAILURE) {
3278        return;
3279    }
3280    RETURN_FALSE;
3281} /* }}} */
3282
3283/* {{{ proto void EmptyIterator::key()
3284   Throws exception BadMethodCallException */
3285SPL_METHOD(EmptyIterator, key)
3286{
3287    if (zend_parse_parameters_none() == FAILURE) {
3288        return;
3289    }
3290    zend_throw_exception(spl_ce_BadMethodCallException, "Accessing the key of an EmptyIterator", 0 TSRMLS_CC);
3291} /* }}} */
3292
3293/* {{{ proto void EmptyIterator::current()
3294   Throws exception BadMethodCallException */
3295SPL_METHOD(EmptyIterator, current)
3296{
3297    if (zend_parse_parameters_none() == FAILURE) {
3298        return;
3299    }
3300    zend_throw_exception(spl_ce_BadMethodCallException, "Accessing the value of an EmptyIterator", 0 TSRMLS_CC);
3301} /* }}} */
3302
3303/* {{{ proto void EmptyIterator::next()
3304   Does nothing */
3305SPL_METHOD(EmptyIterator, next)
3306{
3307    if (zend_parse_parameters_none() == FAILURE) {
3308        return;
3309    }
3310} /* }}} */
3311
3312static const zend_function_entry spl_funcs_EmptyIterator[] = {
3313    SPL_ME(EmptyIterator, rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3314    SPL_ME(EmptyIterator, valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3315    SPL_ME(EmptyIterator, key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3316    SPL_ME(EmptyIterator, current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3317    SPL_ME(EmptyIterator, next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3318    PHP_FE_END
3319};
3320
3321int spl_append_it_next_iterator(spl_dual_it_object *intern TSRMLS_DC) /* {{{*/
3322{
3323    spl_dual_it_free(intern TSRMLS_CC);
3324
3325    if (intern->inner.zobject) {
3326        zval_ptr_dtor(&intern->inner.zobject);
3327        intern->inner.zobject = NULL;
3328        intern->inner.ce = NULL;
3329        intern->inner.object = NULL;
3330        if (intern->inner.iterator) {
3331            intern->inner.iterator->funcs->dtor(intern->inner.iterator TSRMLS_CC);
3332            intern->inner.iterator = NULL;
3333        }
3334    }
3335    if (intern->u.append.iterator->funcs->valid(intern->u.append.iterator TSRMLS_CC) == SUCCESS) {
3336        zval **it;
3337
3338        intern->u.append.iterator->funcs->get_current_data(intern->u.append.iterator, &it TSRMLS_CC);
3339        Z_ADDREF_PP(it);
3340        intern->inner.zobject = *it;
3341        intern->inner.ce = Z_OBJCE_PP(it);
3342        intern->inner.object = zend_object_store_get_object(*it TSRMLS_CC);
3343        intern->inner.iterator = intern->inner.ce->get_iterator(intern->inner.ce, *it, 0 TSRMLS_CC);
3344        spl_dual_it_rewind(intern TSRMLS_CC);
3345        return SUCCESS;
3346    } else {
3347        return FAILURE;
3348    }
3349} /* }}} */
3350
3351void spl_append_it_fetch(spl_dual_it_object *intern TSRMLS_DC) /* {{{*/
3352{
3353    while (spl_dual_it_valid(intern TSRMLS_CC) != SUCCESS) {
3354        intern->u.append.iterator->funcs->move_forward(intern->u.append.iterator TSRMLS_CC);
3355        if (spl_append_it_next_iterator(intern TSRMLS_CC) != SUCCESS) {
3356            return;
3357        }
3358    }
3359    spl_dual_it_fetch(intern, 0 TSRMLS_CC);
3360} /* }}} */
3361
3362void spl_append_it_next(spl_dual_it_object *intern TSRMLS_DC) /* {{{ */
3363{
3364    if (spl_dual_it_valid(intern TSRMLS_CC) == SUCCESS) {
3365        spl_dual_it_next(intern, 1 TSRMLS_CC);
3366    }
3367    spl_append_it_fetch(intern TSRMLS_CC);
3368} /* }}} */
3369
3370/* {{{ proto void AppendIterator::__construct()
3371   Create an AppendIterator */
3372SPL_METHOD(AppendIterator, __construct)
3373{
3374    spl_dual_it_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, spl_ce_AppendIterator, zend_ce_iterator, DIT_AppendIterator);
3375} /* }}} */
3376
3377/* {{{ proto void AppendIterator::append(Iterator it)
3378   Append an iterator */
3379SPL_METHOD(AppendIterator, append)
3380{
3381    spl_dual_it_object   *intern;
3382    zval *it;
3383
3384    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3385
3386    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "O", &it, zend_ce_iterator) == FAILURE) {
3387        return;
3388    }
3389    spl_array_iterator_append(intern->u.append.zarrayit, it TSRMLS_CC);
3390
3391    if (!intern->inner.iterator || spl_dual_it_valid(intern TSRMLS_CC) != SUCCESS) {
3392        if (intern->u.append.iterator->funcs->valid(intern->u.append.iterator TSRMLS_CC) != SUCCESS) {
3393            intern->u.append.iterator->funcs->rewind(intern->u.append.iterator TSRMLS_CC);
3394        }
3395        do {
3396            spl_append_it_next_iterator(intern TSRMLS_CC);
3397        } while (intern->inner.zobject != it);
3398        spl_append_it_fetch(intern TSRMLS_CC);
3399    }
3400} /* }}} */
3401
3402/* {{{ proto void AppendIterator::rewind()
3403   Rewind to the first iterator and rewind the first iterator, too */
3404SPL_METHOD(AppendIterator, rewind)
3405{
3406    spl_dual_it_object   *intern;
3407
3408    if (zend_parse_parameters_none() == FAILURE) {
3409        return;
3410    }
3411
3412    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3413
3414    intern->u.append.iterator->funcs->rewind(intern->u.append.iterator TSRMLS_CC);
3415    if (spl_append_it_next_iterator(intern TSRMLS_CC) == SUCCESS) {
3416        spl_append_it_fetch(intern TSRMLS_CC);
3417    }
3418} /* }}} */
3419
3420/* {{{ proto bool AppendIterator::valid()
3421   Check if the current state is valid */
3422SPL_METHOD(AppendIterator, valid)
3423{
3424    spl_dual_it_object   *intern;
3425
3426    if (zend_parse_parameters_none() == FAILURE) {
3427        return;
3428    }
3429
3430    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3431
3432    RETURN_BOOL(intern->current.data);
3433} /* }}} */
3434
3435/* {{{ proto void AppendIterator::next()
3436   Forward to next element */
3437SPL_METHOD(AppendIterator, next)
3438{
3439    spl_dual_it_object   *intern;
3440
3441    if (zend_parse_parameters_none() == FAILURE) {
3442        return;
3443    }
3444
3445    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3446
3447    spl_append_it_next(intern TSRMLS_CC);
3448} /* }}} */
3449
3450/* {{{ proto int AppendIterator::getIteratorIndex()
3451   Get index of iterator */
3452SPL_METHOD(AppendIterator, getIteratorIndex)
3453{
3454    spl_dual_it_object   *intern;
3455
3456    if (zend_parse_parameters_none() == FAILURE) {
3457        return;
3458    }
3459
3460    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3461
3462    APPENDIT_CHECK_CTOR(intern);
3463    spl_array_iterator_key(intern->u.append.zarrayit, return_value TSRMLS_CC);
3464} /* }}} */
3465
3466/* {{{ proto ArrayIterator AppendIterator::getArrayIterator()
3467   Get access to inner ArrayIterator */
3468SPL_METHOD(AppendIterator, getArrayIterator)
3469{
3470    spl_dual_it_object   *intern;
3471
3472    if (zend_parse_parameters_none() == FAILURE) {
3473        return;
3474    }
3475
3476    SPL_FETCH_AND_CHECK_DUAL_IT(intern, getThis());
3477
3478    RETURN_ZVAL(intern->u.append.zarrayit, 1, 0);
3479} /* }}} */
3480
3481ZEND_BEGIN_ARG_INFO(arginfo_append_it_append, 0)
3482    ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
3483ZEND_END_ARG_INFO();
3484
3485static const zend_function_entry spl_funcs_AppendIterator[] = {
3486    SPL_ME(AppendIterator, __construct,      arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3487    SPL_ME(AppendIterator, append,           arginfo_append_it_append, ZEND_ACC_PUBLIC)
3488    SPL_ME(AppendIterator, rewind,           arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3489    SPL_ME(AppendIterator, valid,            arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3490    SPL_ME(dual_it,        key,              arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3491    SPL_ME(dual_it,        current,          arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3492    SPL_ME(AppendIterator, next,             arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3493    SPL_ME(dual_it,        getInnerIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3494    SPL_ME(AppendIterator, getIteratorIndex, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3495    SPL_ME(AppendIterator, getArrayIterator, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3496    PHP_FE_END
3497};
3498
3499PHPAPI int spl_iterator_apply(zval *obj, spl_iterator_apply_func_t apply_func, void *puser TSRMLS_DC)
3500{
3501    zend_object_iterator   *iter;
3502    zend_class_entry       *ce = Z_OBJCE_P(obj);
3503
3504    iter = ce->get_iterator(ce, obj, 0 TSRMLS_CC);
3505
3506    if (EG(exception)) {
3507        goto done;
3508    }
3509
3510    iter->index = 0;
3511    if (iter->funcs->rewind) {
3512        iter->funcs->rewind(iter TSRMLS_CC);
3513        if (EG(exception)) {
3514            goto done;
3515        }
3516    }
3517
3518    while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
3519        if (EG(exception)) {
3520            goto done;
3521        }
3522        if (apply_func(iter, puser TSRMLS_CC) == ZEND_HASH_APPLY_STOP || EG(exception)) {
3523            goto done;
3524        }
3525        iter->index++;
3526        iter->funcs->move_forward(iter TSRMLS_CC);
3527        if (EG(exception)) {
3528            goto done;
3529        }
3530    }
3531
3532done:
3533    if (iter) {
3534        iter->funcs->dtor(iter TSRMLS_CC);
3535    }
3536    return EG(exception) ? FAILURE : SUCCESS;
3537}
3538/* }}} */
3539
3540static int spl_iterator_to_array_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
3541{
3542    zval **data, *return_value = (zval*)puser;
3543
3544    iter->funcs->get_current_data(iter, &data TSRMLS_CC);
3545    if (EG(exception)) {
3546        return ZEND_HASH_APPLY_STOP;
3547    }
3548    if (data == NULL || *data == NULL) {
3549        return ZEND_HASH_APPLY_STOP;
3550    }
3551    if (iter->funcs->get_current_key) {
3552        zval key;
3553        iter->funcs->get_current_key(iter, &key TSRMLS_CC);
3554        if (EG(exception)) {
3555            return ZEND_HASH_APPLY_STOP;
3556        }
3557        array_set_zval_key(Z_ARRVAL_P(return_value), &key, *data);
3558        zval_dtor(&key);
3559    } else {
3560        Z_ADDREF_PP(data);
3561        add_next_index_zval(return_value, *data);
3562    }
3563    return ZEND_HASH_APPLY_KEEP;
3564}
3565/* }}} */
3566
3567static int spl_iterator_to_values_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
3568{
3569    zval **data, *return_value = (zval*)puser;
3570
3571    iter->funcs->get_current_data(iter, &data TSRMLS_CC);
3572    if (EG(exception)) {
3573        return ZEND_HASH_APPLY_STOP;
3574    }
3575    if (data == NULL || *data == NULL) {
3576        return ZEND_HASH_APPLY_STOP;
3577    }
3578    Z_ADDREF_PP(data);
3579    add_next_index_zval(return_value, *data);
3580    return ZEND_HASH_APPLY_KEEP;
3581}
3582/* }}} */
3583
3584/* {{{ proto array iterator_to_array(Traversable it [, bool use_keys = true])
3585   Copy the iterator into an array */
3586PHP_FUNCTION(iterator_to_array)
3587{
3588    zval  *obj;
3589    zend_bool use_keys = 1;
3590
3591    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &obj, zend_ce_traversable, &use_keys) == FAILURE) {
3592        RETURN_FALSE;
3593    }
3594
3595    array_init(return_value);
3596
3597    if (spl_iterator_apply(obj, use_keys ? spl_iterator_to_array_apply : spl_iterator_to_values_apply, (void*)return_value TSRMLS_CC) != SUCCESS) {
3598        zval_dtor(return_value);
3599        RETURN_NULL();
3600    }
3601} /* }}} */
3602
3603static int spl_iterator_count_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
3604{
3605    (*(long*)puser)++;
3606    return ZEND_HASH_APPLY_KEEP;
3607}
3608/* }}} */
3609
3610/* {{{ proto int iterator_count(Traversable it)
3611   Count the elements in an iterator */
3612PHP_FUNCTION(iterator_count)
3613{
3614    zval  *obj;
3615    long  count = 0;
3616
3617    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, zend_ce_traversable) == FAILURE) {
3618        RETURN_FALSE;
3619    }
3620
3621    if (spl_iterator_apply(obj, spl_iterator_count_apply, (void*)&count TSRMLS_CC) == SUCCESS) {
3622        RETURN_LONG(count);
3623    }
3624}
3625/* }}} */
3626
3627typedef struct {
3628    zval                   *obj;
3629    zval                   *args;
3630    long                   count;
3631    zend_fcall_info        fci;
3632    zend_fcall_info_cache  fcc;
3633} spl_iterator_apply_info;
3634
3635static int spl_iterator_func_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
3636{
3637    zval *retval;
3638    spl_iterator_apply_info  *apply_info = (spl_iterator_apply_info*)puser;
3639    int result;
3640
3641    apply_info->count++;
3642    zend_fcall_info_call(&apply_info->fci, &apply_info->fcc, &retval, NULL TSRMLS_CC);
3643    if (retval) {
3644        result = zend_is_true(retval) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_STOP;
3645        zval_ptr_dtor(&retval);
3646    } else {
3647        result = ZEND_HASH_APPLY_STOP;
3648    }
3649    return result;
3650}
3651/* }}} */
3652
3653/* {{{ proto int iterator_apply(Traversable it, mixed function [, mixed params])
3654   Calls a function for every element in an iterator */
3655PHP_FUNCTION(iterator_apply)
3656{
3657    spl_iterator_apply_info  apply_info;
3658
3659    apply_info.args = NULL;
3660    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Of|a!", &apply_info.obj, zend_ce_traversable, &apply_info.fci, &apply_info.fcc, &apply_info.args) == FAILURE) {
3661        return;
3662    }
3663
3664    apply_info.count = 0;
3665    zend_fcall_info_args(&apply_info.fci, apply_info.args TSRMLS_CC);
3666    if (spl_iterator_apply(apply_info.obj, spl_iterator_func_apply, (void*)&apply_info TSRMLS_CC) == SUCCESS) {
3667        RETVAL_LONG(apply_info.count);
3668    } else {
3669        RETVAL_FALSE;
3670    }
3671    zend_fcall_info_args(&apply_info.fci, NULL TSRMLS_CC);
3672}
3673/* }}} */
3674
3675static const zend_function_entry spl_funcs_OuterIterator[] = {
3676    SPL_ABSTRACT_ME(OuterIterator, getInnerIterator,   arginfo_recursive_it_void)
3677    PHP_FE_END
3678};
3679
3680static const zend_function_entry spl_funcs_Countable[] = {
3681    SPL_ABSTRACT_ME(Countable, count,   arginfo_recursive_it_void)
3682    PHP_FE_END
3683};
3684
3685/* {{{ PHP_MINIT_FUNCTION(spl_iterators)
3686 */
3687PHP_MINIT_FUNCTION(spl_iterators)
3688{
3689    REGISTER_SPL_INTERFACE(RecursiveIterator);
3690    REGISTER_SPL_ITERATOR(RecursiveIterator);
3691
3692    REGISTER_SPL_STD_CLASS_EX(RecursiveIteratorIterator, spl_RecursiveIteratorIterator_new, spl_funcs_RecursiveIteratorIterator);
3693    REGISTER_SPL_ITERATOR(RecursiveIteratorIterator);
3694
3695    memcpy(&spl_handlers_rec_it_it, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
3696    spl_handlers_rec_it_it.get_method = spl_recursive_it_get_method;
3697    spl_handlers_rec_it_it.clone_obj = NULL;
3698
3699    memcpy(&spl_handlers_dual_it, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
3700    spl_handlers_dual_it.get_method = spl_dual_it_get_method;
3701    /*spl_handlers_dual_it.call_method = spl_dual_it_call_method;*/
3702    spl_handlers_dual_it.clone_obj = NULL;
3703
3704    spl_ce_RecursiveIteratorIterator->get_iterator = spl_recursive_it_get_iterator;
3705    spl_ce_RecursiveIteratorIterator->iterator_funcs.funcs = &spl_recursive_it_iterator_funcs;
3706
3707    REGISTER_SPL_CLASS_CONST_LONG(RecursiveIteratorIterator, "LEAVES_ONLY",     RIT_LEAVES_ONLY);
3708    REGISTER_SPL_CLASS_CONST_LONG(RecursiveIteratorIterator, "SELF_FIRST",      RIT_SELF_FIRST);
3709    REGISTER_SPL_CLASS_CONST_LONG(RecursiveIteratorIterator, "CHILD_FIRST",     RIT_CHILD_FIRST);
3710    REGISTER_SPL_CLASS_CONST_LONG(RecursiveIteratorIterator, "CATCH_GET_CHILD", RIT_CATCH_GET_CHILD);
3711
3712    REGISTER_SPL_INTERFACE(OuterIterator);
3713    REGISTER_SPL_ITERATOR(OuterIterator);
3714
3715    REGISTER_SPL_STD_CLASS_EX(IteratorIterator, spl_dual_it_new, spl_funcs_IteratorIterator);
3716    REGISTER_SPL_ITERATOR(IteratorIterator);
3717    REGISTER_SPL_IMPLEMENTS(IteratorIterator, OuterIterator);
3718
3719    REGISTER_SPL_SUB_CLASS_EX(FilterIterator, IteratorIterator, spl_dual_it_new, spl_funcs_FilterIterator);
3720    spl_ce_FilterIterator->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3721
3722    REGISTER_SPL_SUB_CLASS_EX(RecursiveFilterIterator, FilterIterator, spl_dual_it_new, spl_funcs_RecursiveFilterIterator);
3723    REGISTER_SPL_IMPLEMENTS(RecursiveFilterIterator, RecursiveIterator);
3724
3725    REGISTER_SPL_SUB_CLASS_EX(CallbackFilterIterator, FilterIterator, spl_dual_it_new, spl_funcs_CallbackFilterIterator);
3726
3727    REGISTER_SPL_SUB_CLASS_EX(RecursiveCallbackFilterIterator, CallbackFilterIterator, spl_dual_it_new, spl_funcs_RecursiveCallbackFilterIterator);
3728    REGISTER_SPL_IMPLEMENTS(RecursiveCallbackFilterIterator, RecursiveIterator);
3729
3730
3731    REGISTER_SPL_SUB_CLASS_EX(ParentIterator, RecursiveFilterIterator, spl_dual_it_new, spl_funcs_ParentIterator);
3732
3733    REGISTER_SPL_INTERFACE(Countable);
3734    REGISTER_SPL_INTERFACE(SeekableIterator);
3735    REGISTER_SPL_ITERATOR(SeekableIterator);
3736
3737    REGISTER_SPL_SUB_CLASS_EX(LimitIterator, IteratorIterator, spl_dual_it_new, spl_funcs_LimitIterator);
3738
3739    REGISTER_SPL_SUB_CLASS_EX(CachingIterator, IteratorIterator, spl_dual_it_new, spl_funcs_CachingIterator);
3740    REGISTER_SPL_IMPLEMENTS(CachingIterator, ArrayAccess);
3741    REGISTER_SPL_IMPLEMENTS(CachingIterator, Countable);
3742
3743    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CALL_TOSTRING",        CIT_CALL_TOSTRING);
3744    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CATCH_GET_CHILD",      CIT_CATCH_GET_CHILD);
3745    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "TOSTRING_USE_KEY",     CIT_TOSTRING_USE_KEY);
3746    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "TOSTRING_USE_CURRENT", CIT_TOSTRING_USE_CURRENT);
3747    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "TOSTRING_USE_INNER",   CIT_TOSTRING_USE_INNER);
3748    REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "FULL_CACHE",           CIT_FULL_CACHE);
3749
3750    REGISTER_SPL_SUB_CLASS_EX(RecursiveCachingIterator, CachingIterator, spl_dual_it_new, spl_funcs_RecursiveCachingIterator);
3751    REGISTER_SPL_IMPLEMENTS(RecursiveCachingIterator, RecursiveIterator);
3752
3753    REGISTER_SPL_SUB_CLASS_EX(NoRewindIterator, IteratorIterator, spl_dual_it_new, spl_funcs_NoRewindIterator);
3754
3755    REGISTER_SPL_SUB_CLASS_EX(AppendIterator, IteratorIterator, spl_dual_it_new, spl_funcs_AppendIterator);
3756
3757    REGISTER_SPL_IMPLEMENTS(RecursiveIteratorIterator, OuterIterator);
3758
3759    REGISTER_SPL_SUB_CLASS_EX(InfiniteIterator, IteratorIterator, spl_dual_it_new, spl_funcs_InfiniteIterator);
3760#if HAVE_PCRE || HAVE_BUNDLED_PCRE
3761    REGISTER_SPL_SUB_CLASS_EX(RegexIterator, FilterIterator, spl_dual_it_new, spl_funcs_RegexIterator);
3762    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "USE_KEY",     REGIT_USE_KEY);
3763    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "INVERT_MATCH",REGIT_INVERTED);
3764    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "MATCH",       REGIT_MODE_MATCH);
3765    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "GET_MATCH",   REGIT_MODE_GET_MATCH);
3766    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "ALL_MATCHES", REGIT_MODE_ALL_MATCHES);
3767    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "SPLIT",       REGIT_MODE_SPLIT);
3768    REGISTER_SPL_CLASS_CONST_LONG(RegexIterator, "REPLACE",     REGIT_MODE_REPLACE);
3769    REGISTER_SPL_PROPERTY(RegexIterator, "replacement", 0);
3770    REGISTER_SPL_SUB_CLASS_EX(RecursiveRegexIterator, RegexIterator, spl_dual_it_new, spl_funcs_RecursiveRegexIterator);
3771    REGISTER_SPL_IMPLEMENTS(RecursiveRegexIterator, RecursiveIterator);
3772#else
3773    spl_ce_RegexIterator = NULL;
3774    spl_ce_RecursiveRegexIterator = NULL;
3775#endif
3776
3777    REGISTER_SPL_STD_CLASS_EX(EmptyIterator, NULL, spl_funcs_EmptyIterator);
3778    REGISTER_SPL_ITERATOR(EmptyIterator);
3779
3780    REGISTER_SPL_SUB_CLASS_EX(RecursiveTreeIterator, RecursiveIteratorIterator, spl_RecursiveTreeIterator_new, spl_funcs_RecursiveTreeIterator);
3781    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "BYPASS_CURRENT",      RTIT_BYPASS_CURRENT);
3782    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "BYPASS_KEY",          RTIT_BYPASS_KEY);
3783    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_LEFT",         0);
3784    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_MID_HAS_NEXT", 1);
3785    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_MID_LAST",     2);
3786    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_END_HAS_NEXT", 3);
3787    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_END_LAST",     4);
3788    REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "PREFIX_RIGHT",        5);
3789
3790    return SUCCESS;
3791}
3792/* }}} */
3793
3794/*
3795 * Local variables:
3796 * tab-width: 4
3797 * c-basic-offset: 4
3798 * End:
3799 * vim600: fdm=marker
3800 * vim: noet sw=4 ts=4
3801 */
3802