resourcebundle_iterator.c [plain text]
#include <php.h>
#include <zend.h>
#include <zend_API.h>
#include "resourcebundle/resourcebundle.h"
#include "resourcebundle/resourcebundle_class.h"
#include "resourcebundle/resourcebundle_iterator.h"
static void resourcebundle_iterator_read( ResourceBundle_iterator *iterator )
{
UErrorCode icuerror = U_ZERO_ERROR;
ResourceBundle_object *rb = iterator->subject;
rb->child = ures_getByIndex( rb->me, iterator->i, rb->child, &icuerror );
if (U_SUCCESS(icuerror)) {
if (iterator->is_table) {
iterator->currentkey = estrdup( ures_getKey( rb->child ) );
}
resourcebundle_extract_value( &iterator->current, rb );
}
else {
ZVAL_UNDEF(&iterator->current);
}
}
static void resourcebundle_iterator_invalidate( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
if (!Z_ISUNDEF(iterator->current)) {
zval_ptr_dtor( &iterator->current );
ZVAL_UNDEF(&iterator->current);
}
if (iterator->currentkey) {
efree( iterator->currentkey );
iterator->currentkey = NULL;
}
}
static void resourcebundle_iterator_dtor( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
zval *object = &iterator->intern.data;
resourcebundle_iterator_invalidate( iter );
zval_ptr_dtor(object);
}
static int resourcebundle_iterator_has_more( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
return (iterator->i < iterator->length) ? SUCCESS : FAILURE;
}
static zval *resourcebundle_iterator_current( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
if (Z_ISUNDEF(iterator->current)) {
resourcebundle_iterator_read( iterator);
}
return &iterator->current;
}
static void resourcebundle_iterator_key( zend_object_iterator *iter, zval *key )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
if (Z_ISUNDEF(iterator->current)) {
resourcebundle_iterator_read( iterator);
}
if (iterator->is_table) {
ZVAL_STRING(key, iterator->currentkey);
} else {
ZVAL_LONG(key, iterator->i);
}
}
static void resourcebundle_iterator_step( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
iterator->i++;
resourcebundle_iterator_invalidate( iter );
}
static void resourcebundle_iterator_reset( zend_object_iterator *iter )
{
ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
iterator->i = 0;
resourcebundle_iterator_invalidate( iter );
}
static zend_object_iterator_funcs resourcebundle_iterator_funcs = {
resourcebundle_iterator_dtor,
resourcebundle_iterator_has_more,
resourcebundle_iterator_current,
resourcebundle_iterator_key,
resourcebundle_iterator_step,
resourcebundle_iterator_reset,
resourcebundle_iterator_invalidate
};
zend_object_iterator *resourcebundle_get_iterator( zend_class_entry *ce, zval *object, int byref )
{
ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P(object );
ResourceBundle_iterator *iterator = emalloc( sizeof( ResourceBundle_iterator ) );
if (byref) {
php_error( E_ERROR, "ResourceBundle does not support writable iterators" );
}
zend_iterator_init(&iterator->intern);
ZVAL_COPY(&iterator->intern.data, object);
iterator->intern.funcs = &resourcebundle_iterator_funcs;
iterator->subject = rb;
iterator->is_table = (ures_getType( rb->me ) == URES_TABLE);
iterator->length = ures_getSize( rb->me );
ZVAL_UNDEF(&iterator->current);
iterator->currentkey = NULL;
iterator->i = 0;
return (zend_object_iterator *) iterator;
}