Back to index

php5  5.3.10
spl_fixedarray.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Antony Dovgal <tony@daylessday.org>                          |
00016   |         Etienne Kneuss <colder@php.net>                              |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: spl_fixedarray.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 #include "php_ini.h"
00028 #include "ext/standard/info.h"
00029 #include "zend_exceptions.h"
00030 
00031 #include "php_spl.h"
00032 #include "spl_functions.h"
00033 #include "spl_engine.h"
00034 #include "spl_fixedarray.h"
00035 #include "spl_exceptions.h"
00036 #include "spl_iterators.h"
00037 
00038 zend_object_handlers spl_handler_SplFixedArray;
00039 PHPAPI zend_class_entry *spl_ce_SplFixedArray;
00040 
00041 #ifdef COMPILE_DL_SPL_FIXEDARRAY
00042 ZEND_GET_MODULE(spl_fixedarray)
00043 #endif
00044 
00045 typedef struct _spl_fixedarray { /* {{{ */
00046        long size;
00047        zval **elements;
00048 } spl_fixedarray;
00049 /* }}} */
00050 
00051 typedef struct _spl_fixedarray_object { /* {{{ */
00052        zend_object            std;
00053        spl_fixedarray         *array;
00054        zval                  *retval;
00055        zend_function         *fptr_offset_get;
00056        zend_function         *fptr_offset_set;
00057        zend_function         *fptr_offset_has;
00058        zend_function         *fptr_offset_del;
00059        zend_function         *fptr_count;
00060        int                    current;
00061        int                    flags;
00062        zend_class_entry      *ce_get_iterator;
00063 } spl_fixedarray_object;
00064 /* }}} */
00065 
00066 typedef struct _spl_fixedarray_it { /* {{{ */
00067        zend_user_iterator     intern;
00068        spl_fixedarray_object  *object;
00069 } spl_fixedarray_it;
00070 /* }}} */
00071 
00072 #define SPL_FIXEDARRAY_OVERLOADED_REWIND  0x0001
00073 #define SPL_FIXEDARRAY_OVERLOADED_VALID   0x0002
00074 #define SPL_FIXEDARRAY_OVERLOADED_KEY     0x0004
00075 #define SPL_FIXEDARRAY_OVERLOADED_CURRENT 0x0008
00076 #define SPL_FIXEDARRAY_OVERLOADED_NEXT    0x0010
00077 
00078 static void spl_fixedarray_init(spl_fixedarray *array, long size TSRMLS_DC) /* {{{ */
00079 {
00080        if (size > 0) {
00081               array->size = 0; /* reset size in case ecalloc() fails */
00082               array->elements = ecalloc(size, sizeof(zval *));
00083               array->size = size;
00084        } else {
00085               array->elements = NULL;
00086               array->size = 0;
00087        }
00088 }
00089 /* }}} */
00090 
00091 static void spl_fixedarray_resize(spl_fixedarray *array, long size TSRMLS_DC) /* {{{ */
00092 {
00093        if (size == array->size) {
00094               /* nothing to do */
00095               return;
00096        }
00097 
00098        /* first initialization */
00099        if (array->size == 0) {
00100               spl_fixedarray_init(array, size TSRMLS_CC);
00101               return;
00102        }
00103 
00104        /* clearing the array */
00105        if (size == 0) {
00106               long i;
00107 
00108               for (i = 0; i < array->size; i++) {
00109                      if (array->elements[i]) {
00110                             zval_ptr_dtor(&(array->elements[i]));
00111                      }
00112               }
00113 
00114               if (array->elements) {
00115                      efree(array->elements);
00116                      array->elements = NULL;
00117               }
00118        } else if (size > array->size) {
00119               array->elements = erealloc(array->elements, sizeof(zval *) * size);
00120               memset(array->elements + array->size, '\0', sizeof(zval *) * (size - array->size));
00121        } else { /* size < array->size */
00122               long i;
00123 
00124               for (i = size; i < array->size; i++) {
00125                      if (array->elements[i]) {
00126                             zval_ptr_dtor(&(array->elements[i]));
00127                      }
00128               }
00129               array->elements = erealloc(array->elements, sizeof(zval *) * size);
00130        }
00131 
00132        array->size = size;
00133 }
00134 /* }}} */
00135 
00136 static void spl_fixedarray_copy(spl_fixedarray *to, spl_fixedarray *from TSRMLS_DC) /* {{{ */
00137 {
00138        int i;
00139        for (i = 0; i < from->size; i++) {
00140               if (from->elements[i]) {
00141                      Z_ADDREF_P(from->elements[i]);
00142                      to->elements[i] = from->elements[i];
00143               } else {
00144                      to->elements[i] = NULL;
00145               }
00146        }
00147 }
00148 /* }}} */
00149 
00150 static HashTable* spl_fixedarray_object_get_properties(zval *obj TSRMLS_DC) /* {{{{ */
00151 {
00152        spl_fixedarray_object *intern  = (spl_fixedarray_object*)zend_object_store_get_object(obj TSRMLS_CC);
00153        int  i = 0;
00154 
00155        if (intern->array && !GC_G(gc_active)) {
00156               int j = zend_hash_num_elements(intern->std.properties);
00157 
00158               for (i = 0; i < intern->array->size; i++) {
00159                      if (intern->array->elements[i]) {
00160                             zend_hash_index_update(intern->std.properties, i, (void *)&intern->array->elements[i], sizeof(zval *), NULL);
00161                             Z_ADDREF_P(intern->array->elements[i]);
00162                      } else {
00163                             zend_hash_index_update(intern->std.properties, i, (void *)&EG(uninitialized_zval_ptr), sizeof(zval *), NULL);
00164                             Z_ADDREF_P(EG(uninitialized_zval_ptr));
00165                      }
00166               }
00167               if (j > intern->array->size) {
00168                      for (i = intern->array->size; i < j; ++i) {
00169                             zend_hash_index_del(intern->std.properties, i);
00170                      }
00171               }
00172        }
00173 
00174        return intern->std.properties;
00175 }
00176 /* }}}} */
00177 
00178 static void spl_fixedarray_object_free_storage(void *object TSRMLS_DC) /* {{{ */
00179 {
00180        spl_fixedarray_object *intern = (spl_fixedarray_object *)object;
00181        long i;
00182 
00183        if (intern->array) {
00184               for (i = 0; i < intern->array->size; i++) {
00185                      if (intern->array->elements[i]) {
00186                             zval_ptr_dtor(&(intern->array->elements[i]));
00187                      }
00188               }
00189 
00190               if (intern->array->size > 0 && intern->array->elements) {
00191                      efree(intern->array->elements);
00192               }
00193               efree(intern->array);
00194        }
00195 
00196        zend_object_std_dtor(&intern->std TSRMLS_CC);
00197        zval_ptr_dtor(&intern->retval);
00198 
00199        efree(object);
00200 }
00201 /* }}} */
00202 
00203 zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
00204 
00205 static zend_object_value spl_fixedarray_object_new_ex(zend_class_entry *class_type, spl_fixedarray_object **obj, zval *orig, int clone_orig TSRMLS_DC) /* {{{ */
00206 {
00207        zend_object_value     retval;
00208        spl_fixedarray_object *intern;
00209        zval                 *tmp;
00210        zend_class_entry     *parent = class_type;
00211        int                   inherited = 0;
00212 
00213        intern = ecalloc(1, sizeof(spl_fixedarray_object));
00214        *obj = intern;
00215        ALLOC_INIT_ZVAL(intern->retval);
00216 
00217        zend_object_std_init(&intern->std, class_type TSRMLS_CC);
00218        zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
00219 
00220        intern->current = 0;
00221        intern->flags = 0;
00222 
00223        if (orig && clone_orig) {
00224               spl_fixedarray_object *other = (spl_fixedarray_object*)zend_object_store_get_object(orig TSRMLS_CC);
00225               intern->ce_get_iterator = other->ce_get_iterator;
00226 
00227               intern->array = emalloc(sizeof(spl_fixedarray));
00228               spl_fixedarray_init(intern->array, other->array->size TSRMLS_CC);
00229               spl_fixedarray_copy(intern->array, other->array TSRMLS_CC);
00230        }
00231 
00232        while (parent) {
00233               if (parent == spl_ce_SplFixedArray) {
00234                      retval.handlers = &spl_handler_SplFixedArray;
00235                      class_type->get_iterator = spl_fixedarray_get_iterator;
00236                      break;
00237               }
00238 
00239               parent = parent->parent;
00240               inherited = 1;
00241        }
00242 
00243        retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, spl_fixedarray_object_free_storage, NULL TSRMLS_CC);
00244 
00245        if (!parent) { /* this must never happen */
00246               php_error_docref(NULL TSRMLS_CC, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplFixedArray");
00247        }
00248        if (!class_type->iterator_funcs.zf_current) {
00249               zend_hash_find(&class_type->function_table, "rewind",  sizeof("rewind"),  (void **) &class_type->iterator_funcs.zf_rewind);
00250               zend_hash_find(&class_type->function_table, "valid",   sizeof("valid"),   (void **) &class_type->iterator_funcs.zf_valid);
00251               zend_hash_find(&class_type->function_table, "key",     sizeof("key"),     (void **) &class_type->iterator_funcs.zf_key);
00252               zend_hash_find(&class_type->function_table, "current", sizeof("current"), (void **) &class_type->iterator_funcs.zf_current);
00253               zend_hash_find(&class_type->function_table, "next",    sizeof("next"),    (void **) &class_type->iterator_funcs.zf_next);
00254        }
00255        if (inherited) {
00256               if (class_type->iterator_funcs.zf_rewind->common.scope  != parent) { 
00257                      intern->flags |= SPL_FIXEDARRAY_OVERLOADED_REWIND;
00258               }
00259               if (class_type->iterator_funcs.zf_valid->common.scope   != parent) { 
00260                      intern->flags |= SPL_FIXEDARRAY_OVERLOADED_VALID;
00261               }
00262               if (class_type->iterator_funcs.zf_key->common.scope     != parent) { 
00263                      intern->flags |= SPL_FIXEDARRAY_OVERLOADED_KEY;
00264               }
00265               if (class_type->iterator_funcs.zf_current->common.scope != parent) { 
00266                      intern->flags |= SPL_FIXEDARRAY_OVERLOADED_CURRENT;
00267               }
00268               if (class_type->iterator_funcs.zf_next->common.scope    != parent) { 
00269                      intern->flags |= SPL_FIXEDARRAY_OVERLOADED_NEXT;
00270               }
00271 
00272               zend_hash_find(&class_type->function_table, "offsetget",    sizeof("offsetget"),    (void **) &intern->fptr_offset_get);
00273               if (intern->fptr_offset_get->common.scope == parent) {
00274                      intern->fptr_offset_get = NULL;
00275               }
00276               zend_hash_find(&class_type->function_table, "offsetset",    sizeof("offsetset"),    (void **) &intern->fptr_offset_set);
00277               if (intern->fptr_offset_set->common.scope == parent) {
00278                      intern->fptr_offset_set = NULL;
00279               }
00280               zend_hash_find(&class_type->function_table, "offsetexists", sizeof("offsetexists"), (void **) &intern->fptr_offset_has);
00281               if (intern->fptr_offset_has->common.scope == parent) {
00282                      intern->fptr_offset_has = NULL;
00283               }
00284               zend_hash_find(&class_type->function_table, "offsetunset",  sizeof("offsetunset"),  (void **) &intern->fptr_offset_del);
00285               if (intern->fptr_offset_del->common.scope == parent) {
00286                      intern->fptr_offset_del = NULL;
00287               }
00288               zend_hash_find(&class_type->function_table, "count",        sizeof("count"),        (void **) &intern->fptr_count);
00289               if (intern->fptr_count->common.scope == parent) {
00290                      intern->fptr_count = NULL;
00291               }
00292        }
00293 
00294        return retval;
00295 }
00296 /* }}} */
00297 
00298 static zend_object_value spl_fixedarray_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
00299 {
00300        spl_fixedarray_object *tmp;
00301        return spl_fixedarray_object_new_ex(class_type, &tmp, NULL, 0 TSRMLS_CC);
00302 }
00303 /* }}} */
00304 
00305 static zend_object_value spl_fixedarray_object_clone(zval *zobject TSRMLS_DC) /* {{{ */
00306 {
00307        zend_object_value      new_obj_val;
00308        zend_object           *old_object;
00309        zend_object           *new_object;
00310        zend_object_handle     handle = Z_OBJ_HANDLE_P(zobject);
00311        spl_fixedarray_object  *intern;
00312 
00313        old_object  = zend_objects_get_address(zobject TSRMLS_CC);
00314        new_obj_val = spl_fixedarray_object_new_ex(old_object->ce, &intern, zobject, 1 TSRMLS_CC);
00315        new_object  = &intern->std;
00316 
00317        zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
00318 
00319        return new_obj_val;
00320 }
00321 /* }}} */
00322 
00323 static inline zval **spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset TSRMLS_DC) /* {{{ */
00324 {
00325        long index;
00326 
00327        /* we have to return NULL on error here to avoid memleak because of 
00328         * ZE duplicating uninitialized_zval_ptr */
00329        if (!offset) {
00330               zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
00331               return NULL;
00332        }
00333 
00334        if (Z_TYPE_P(offset) != IS_LONG) {
00335               index = spl_offset_convert_to_long(offset TSRMLS_CC);
00336        } else {
00337               index = Z_LVAL_P(offset);
00338        }
00339        
00340        if (index < 0 || intern->array == NULL || index >= intern->array->size) {
00341               zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
00342               return NULL;
00343        } else if(!intern->array->elements[index]) {
00344               return NULL;
00345        } else {
00346               return &intern->array->elements[index];
00347        }
00348 }
00349 /* }}} */
00350 
00351 static zval *spl_fixedarray_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
00352 {
00353        spl_fixedarray_object *intern;
00354        zval **retval;
00355 
00356        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00357 
00358        if (intern->fptr_offset_get) {
00359               zval *rv;
00360               SEPARATE_ARG_IF_REF(offset);
00361               zend_call_method_with_1_params(&object, intern->std.ce, &intern->fptr_offset_get, "offsetGet", &rv, offset);
00362               zval_ptr_dtor(&offset);
00363               if (rv) {
00364                      zval_ptr_dtor(&intern->retval);
00365                      MAKE_STD_ZVAL(intern->retval);
00366                      ZVAL_ZVAL(intern->retval, rv, 1, 1);
00367                      return intern->retval;
00368               }
00369               return EG(uninitialized_zval_ptr);
00370        }
00371 
00372        retval = spl_fixedarray_object_read_dimension_helper(intern, offset TSRMLS_CC);
00373        if (retval) {
00374               return *retval;
00375        }
00376        return NULL;
00377 }
00378 /* }}} */
00379 
00380 static inline void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value TSRMLS_DC) /* {{{ */
00381 {
00382        long index;
00383 
00384        if (!offset) {
00385               /* '$array[] = value' syntax is not supported */
00386               zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
00387               return;
00388        }
00389 
00390        if (Z_TYPE_P(offset) != IS_LONG) {
00391               index = spl_offset_convert_to_long(offset TSRMLS_CC);
00392        } else {
00393               index = Z_LVAL_P(offset);
00394        }
00395 
00396        if (index < 0 || intern->array == NULL || index >= intern->array->size) {
00397               zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
00398               return;
00399        } else {
00400               if (intern->array->elements[index]) {
00401                      zval_ptr_dtor(&(intern->array->elements[index]));
00402               }
00403               SEPARATE_ARG_IF_REF(value);
00404               intern->array->elements[index] = value;
00405        }
00406 }
00407 /* }}} */
00408 
00409 static void spl_fixedarray_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
00410 {
00411        spl_fixedarray_object *intern;
00412 
00413        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00414 
00415        if (intern->fptr_offset_set) {
00416               if (!offset) {
00417                      ALLOC_INIT_ZVAL(offset);
00418               } else {
00419                      SEPARATE_ARG_IF_REF(offset);
00420               }
00421               SEPARATE_ARG_IF_REF(value);
00422               zend_call_method_with_2_params(&object, intern->std.ce, &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
00423               zval_ptr_dtor(&value);
00424               zval_ptr_dtor(&offset);
00425               return;
00426        }
00427 
00428        spl_fixedarray_object_write_dimension_helper(intern, offset, value TSRMLS_CC);
00429 }
00430 /* }}} */
00431 
00432 static inline void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset TSRMLS_DC) /* {{{ */
00433 {
00434        long index;
00435        
00436        if (Z_TYPE_P(offset) != IS_LONG) {
00437               index = spl_offset_convert_to_long(offset TSRMLS_CC);
00438        } else {
00439               index = Z_LVAL_P(offset);
00440        }
00441        
00442        if (index < 0 || intern->array == NULL || index >= intern->array->size) {
00443               zend_throw_exception(spl_ce_RuntimeException, "Index invalid or out of range", 0 TSRMLS_CC);
00444               return;
00445        } else {
00446               if (intern->array->elements[index]) {
00447                      zval_ptr_dtor(&(intern->array->elements[index]));
00448               }
00449               intern->array->elements[index] = NULL;
00450        }
00451 }
00452 /* }}} */
00453 
00454 static void spl_fixedarray_object_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
00455 {
00456        spl_fixedarray_object *intern;
00457 
00458        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00459 
00460        if (intern->fptr_offset_del) {
00461               SEPARATE_ARG_IF_REF(offset);
00462               zend_call_method_with_1_params(&object, intern->std.ce, &intern->fptr_offset_del, "offsetUnset", NULL, offset);
00463               zval_ptr_dtor(&offset);
00464               return;
00465        }
00466 
00467        spl_fixedarray_object_unset_dimension_helper(intern, offset TSRMLS_CC);
00468 
00469 }
00470 /* }}} */
00471 
00472 static inline int spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
00473 {
00474        long index;
00475        int retval;
00476        
00477        if (Z_TYPE_P(offset) != IS_LONG) {
00478               index = spl_offset_convert_to_long(offset TSRMLS_CC);
00479        } else {
00480               index = Z_LVAL_P(offset);
00481        }
00482        
00483        if (index < 0 || intern->array == NULL || index >= intern->array->size) {
00484               retval = 0;
00485        } else {
00486               if (!intern->array->elements[index]) {
00487                      retval = 0;
00488               } else if (check_empty) {
00489                      if (zend_is_true(intern->array->elements[index])) {
00490                             retval = 1;
00491                      } else {
00492                             retval = 0;
00493                      }
00494               } else { /* != NULL and !check_empty */
00495                      retval = 1;
00496               }
00497        }
00498 
00499        return retval;
00500 }
00501 /* }}} */
00502 
00503 static int spl_fixedarray_object_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
00504 {
00505        spl_fixedarray_object *intern;
00506 
00507        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00508 
00509        if (intern->fptr_offset_get) {
00510               zval *rv;
00511               SEPARATE_ARG_IF_REF(offset);
00512               zend_call_method_with_1_params(&object, intern->std.ce, &intern->fptr_offset_has, "offsetExists", &rv, offset);
00513               zval_ptr_dtor(&offset);
00514               if (rv) {
00515                      zval_ptr_dtor(&intern->retval);
00516                      MAKE_STD_ZVAL(intern->retval);
00517                      ZVAL_ZVAL(intern->retval, rv, 1, 1);
00518                      return zend_is_true(intern->retval);
00519               }
00520               return 0;
00521        }
00522 
00523        return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty TSRMLS_CC);
00524 }
00525 /* }}} */
00526 
00527 static int spl_fixedarray_object_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */
00528 {
00529        spl_fixedarray_object *intern;
00530        
00531        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00532        if (intern->fptr_count) {
00533               zval *rv;
00534               zend_call_method_with_0_params(&object, intern->std.ce, &intern->fptr_count, "count", &rv);
00535               if (rv) {
00536                      zval_ptr_dtor(&intern->retval);
00537                      MAKE_STD_ZVAL(intern->retval);
00538                      ZVAL_ZVAL(intern->retval, rv, 1, 1);
00539                      convert_to_long(intern->retval);
00540                      *count = (long) Z_LVAL_P(intern->retval);
00541                      return SUCCESS;
00542               }
00543        } else if (intern->array) {
00544               *count = intern->array->size;
00545               return SUCCESS;
00546        }
00547 
00548        *count = 0;
00549        return SUCCESS;
00550 }
00551 /* }}} */
00552 
00553 /* {{{ proto void SplFixedArray::__construct([int size])
00554 */
00555 SPL_METHOD(SplFixedArray, __construct)
00556 {
00557        zval *object = getThis();
00558        spl_fixedarray_object *intern;
00559        long size = 0;
00560 
00561        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &size)) {
00562               return;
00563        }
00564 
00565        if (size < 0) {
00566               zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "array size cannot be less than zero");
00567               return;
00568        }
00569 
00570        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00571 
00572        if (intern->array) {
00573               /* called __construct() twice, bail out */
00574               return;
00575        }
00576 
00577        intern->array = emalloc(sizeof(spl_fixedarray));
00578        spl_fixedarray_init(intern->array, size TSRMLS_CC);
00579 }
00580 /* }}} */
00581 
00582 /* {{{ proto int SplFixedArray::count(void)
00583 */
00584 SPL_METHOD(SplFixedArray, count)
00585 {
00586        zval *object = getThis();
00587        spl_fixedarray_object *intern;
00588 
00589        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")) {
00590               return;
00591        }
00592 
00593        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00594        if (intern->array) {
00595               RETURN_LONG(intern->array->size);
00596        }
00597        RETURN_LONG(0);
00598 }
00599 /* }}} */
00600 
00601 /* {{{ proto object SplFixedArray::toArray()
00602 */
00603 SPL_METHOD(SplFixedArray, toArray)
00604 {
00605        spl_fixedarray_object *intern;
00606        zval *ret, *tmp;
00607        HashTable *ret_ht, *obj_ht;
00608 
00609        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")) {
00610               return;
00611        }
00612 
00613        intern = (spl_fixedarray_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
00614 
00615        ALLOC_HASHTABLE(ret_ht);
00616        zend_hash_init(ret_ht, 0, NULL, ZVAL_PTR_DTOR, 0);
00617        ALLOC_INIT_ZVAL(ret);
00618        Z_TYPE_P(ret) = IS_ARRAY;
00619        obj_ht = spl_fixedarray_object_get_properties(getThis() TSRMLS_CC);
00620        zend_hash_copy(ret_ht, obj_ht, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
00621        Z_ARRVAL_P(ret) = ret_ht;
00622 
00623        RETURN_ZVAL(ret, 1, 1);
00624 }
00625 /* }}} */
00626 
00627 /* {{{ proto object SplFixedArray::fromArray(array data[, bool save_indexes])
00628 */
00629 SPL_METHOD(SplFixedArray, fromArray)
00630 {
00631        zval *data;
00632        spl_fixedarray *array;
00633        spl_fixedarray_object *intern;
00634        int num;
00635        zend_bool save_indexes = 1;
00636 
00637        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &data, &save_indexes)) {
00638               return;
00639        }
00640 
00641        array = ecalloc(1, sizeof(*array));
00642        num = zend_hash_num_elements(Z_ARRVAL_P(data));
00643        
00644        if (num > 0 && save_indexes) {
00645               zval **element, *value;
00646               char *str_index;
00647               ulong num_index, max_index = 0;
00648               long tmp;
00649 
00650               for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(data));
00651                      zend_hash_get_current_data(Z_ARRVAL_P(data), (void **) &element) == SUCCESS;
00652                      zend_hash_move_forward(Z_ARRVAL_P(data))
00653                      ) {
00654                      if (zend_hash_get_current_key(Z_ARRVAL_P(data), &str_index, &num_index, 0) != HASH_KEY_IS_LONG || (long)num_index < 0) {
00655                             efree(array);
00656                             zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "array must contain only positive integer keys");
00657                             return;
00658                      }
00659 
00660                      if (num_index > max_index) {
00661                             max_index = num_index;
00662                      }
00663               }
00664 
00665               tmp = max_index + 1;
00666               if (tmp <= 0) {
00667                      efree(array);
00668                      zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "integer overflow detected");
00669                      return;
00670               }
00671               spl_fixedarray_init(array, tmp TSRMLS_CC);
00672 
00673               for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(data));
00674                      zend_hash_get_current_data(Z_ARRVAL_P(data), (void **) &element) == SUCCESS;
00675                      zend_hash_move_forward(Z_ARRVAL_P(data))
00676                      ) {
00677                      
00678                      zend_hash_get_current_key(Z_ARRVAL_P(data), &str_index, &num_index, 0);
00679                      value = *element;
00680 
00681                      SEPARATE_ARG_IF_REF(value);
00682                      array->elements[num_index] = value;
00683               }
00684 
00685        } else if (num > 0 && !save_indexes) {
00686               zval **element, *value;
00687               long i = 0;
00688               
00689               spl_fixedarray_init(array, num TSRMLS_CC);
00690               
00691               for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(data));
00692                      zend_hash_get_current_data(Z_ARRVAL_P(data), (void **) &element) == SUCCESS;
00693                      zend_hash_move_forward(Z_ARRVAL_P(data))
00694                      ) {
00695                      
00696                      value = *element;
00697 
00698                      SEPARATE_ARG_IF_REF(value);
00699                      array->elements[i] = value;
00700                      i++;
00701               }
00702        } else {
00703               spl_fixedarray_init(array, 0 TSRMLS_CC);
00704        }
00705 
00706        object_init_ex(return_value, spl_ce_SplFixedArray);
00707        Z_TYPE_P(return_value) = IS_OBJECT;
00708 
00709        intern = (spl_fixedarray_object *)zend_object_store_get_object(return_value TSRMLS_CC);
00710        intern->array = array;
00711 }
00712 /* }}} */
00713 
00714 /* {{{ proto int SplFixedArray::getSize(void)
00715 */
00716 SPL_METHOD(SplFixedArray, getSize)
00717 {
00718        zval *object = getThis();
00719        spl_fixedarray_object *intern;
00720 
00721        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")) {
00722               return;
00723        }
00724 
00725        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00726        if (intern->array) {
00727               RETURN_LONG(intern->array->size);
00728        }
00729        RETURN_LONG(0);
00730 }
00731 /* }}} */
00732 
00733 /* {{{ proto bool SplFixedArray::setSize(int size)
00734 */
00735 SPL_METHOD(SplFixedArray, setSize)
00736 {
00737        zval *object = getThis();
00738        spl_fixedarray_object *intern;
00739        long size;
00740 
00741        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &size)) {
00742               return;
00743        }
00744 
00745        if (size < 0) {
00746               zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "array size cannot be less than zero");
00747               return;
00748        }
00749 
00750        intern = (spl_fixedarray_object *)zend_object_store_get_object(object TSRMLS_CC);
00751        if (!intern->array) {
00752               intern->array = ecalloc(1, sizeof(spl_fixedarray));
00753        }
00754 
00755        spl_fixedarray_resize(intern->array, size TSRMLS_CC);
00756        RETURN_TRUE;
00757 }
00758 /* }}} */
00759 
00760 /* {{{ proto bool SplFixedArray::offsetExists(mixed $index) U
00761  Returns whether the requested $index exists. */
00762 SPL_METHOD(SplFixedArray, offsetExists)
00763 {
00764        zval                  *zindex;
00765        spl_fixedarray_object  *intern;
00766 
00767        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zindex) == FAILURE) {
00768               return;
00769        }
00770 
00771        intern = (spl_fixedarray_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
00772 
00773        RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0 TSRMLS_CC));
00774 } /* }}} */
00775 
00776 /* {{{ proto mixed SplFixedArray::offsetGet(mixed $index) U
00777  Returns the value at the specified $index. */
00778 SPL_METHOD(SplFixedArray, offsetGet)
00779 {
00780        zval                  *zindex, **value_pp;
00781        spl_fixedarray_object  *intern;
00782 
00783        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zindex) == FAILURE) {
00784               return;
00785        }
00786 
00787        intern    = (spl_fixedarray_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
00788        value_pp  = spl_fixedarray_object_read_dimension_helper(intern, zindex TSRMLS_CC);
00789 
00790        if (value_pp) {
00791               RETURN_ZVAL(*value_pp, 1, 0);
00792        }
00793        RETURN_NULL();
00794 } /* }}} */
00795 
00796 /* {{{ proto void SplFixedArray::offsetSet(mixed $index, mixed $newval) U
00797  Sets the value at the specified $index to $newval. */
00798 SPL_METHOD(SplFixedArray, offsetSet)
00799 {
00800        zval                  *zindex, *value;
00801        spl_fixedarray_object  *intern;
00802 
00803        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &zindex, &value) == FAILURE) {
00804               return;
00805        }
00806 
00807        intern = (spl_fixedarray_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
00808        spl_fixedarray_object_write_dimension_helper(intern, zindex, value TSRMLS_CC);
00809 
00810 } /* }}} */
00811 
00812 /* {{{ proto void SplFixedArray::offsetUnset(mixed $index) U
00813  Unsets the value at the specified $index. */
00814 SPL_METHOD(SplFixedArray, offsetUnset)
00815 {
00816        zval                  *zindex;
00817        spl_fixedarray_object  *intern;
00818 
00819        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zindex) == FAILURE) {
00820               return;
00821        }
00822 
00823        intern = (spl_fixedarray_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
00824        spl_fixedarray_object_unset_dimension_helper(intern, zindex TSRMLS_CC);
00825 
00826 } /* }}} */
00827 
00828 static void spl_fixedarray_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
00829 {
00830        spl_fixedarray_it  *iterator = (spl_fixedarray_it *)iter;
00831 
00832        zend_user_it_invalidate_current(iter TSRMLS_CC);
00833        zval_ptr_dtor((zval**)&iterator->intern.it.data);
00834 
00835        efree(iterator);
00836 }
00837 /* }}} */
00838 
00839 static void spl_fixedarray_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
00840 {
00841        spl_fixedarray_it     *iterator = (spl_fixedarray_it *)iter;
00842        spl_fixedarray_object *intern   = iterator->object;
00843 
00844        if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_REWIND) {
00845               zend_user_it_rewind(iter TSRMLS_CC);
00846        } else {
00847               iterator->object->current = 0;
00848        }
00849 }
00850 /* }}} */
00851 
00852 static int spl_fixedarray_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
00853 {
00854        spl_fixedarray_it     *iterator = (spl_fixedarray_it *)iter;
00855        spl_fixedarray_object *intern   = iterator->object;
00856 
00857        if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_VALID) {
00858               return zend_user_it_valid(iter TSRMLS_CC);
00859        }
00860 
00861        if (iterator->object->current >= 0 && iterator->object->array && iterator->object->current < iterator->object->array->size) {
00862               return SUCCESS;
00863        }
00864 
00865        return FAILURE;
00866 }
00867 /* }}} */
00868 
00869 static void spl_fixedarray_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
00870 {
00871        zval                  *zindex;
00872        spl_fixedarray_it     *iterator = (spl_fixedarray_it *)iter;
00873        spl_fixedarray_object *intern   = iterator->object;
00874 
00875        if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_CURRENT) {
00876               zend_user_it_get_current_data(iter, data TSRMLS_CC);
00877        } else {
00878               ALLOC_INIT_ZVAL(zindex);
00879               ZVAL_LONG(zindex, iterator->object->current);
00880 
00881               *data = spl_fixedarray_object_read_dimension_helper(intern, zindex TSRMLS_CC);
00882 
00883               if (*data == NULL) {
00884                      *data = &EG(uninitialized_zval_ptr);
00885               }
00886 
00887               zval_ptr_dtor(&zindex);
00888        }
00889 }
00890 /* }}} */
00891 
00892 static int spl_fixedarray_it_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) /* {{{ */
00893 {
00894        spl_fixedarray_it     *iterator = (spl_fixedarray_it *)iter;
00895        spl_fixedarray_object *intern   = iterator->object;
00896 
00897        if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_KEY) {
00898               return zend_user_it_get_current_key(iter, str_key, str_key_len, int_key TSRMLS_CC);
00899        } else {
00900               *int_key = (ulong) iterator->object->current;
00901               return HASH_KEY_IS_LONG;
00902        }
00903 
00904 }
00905 /* }}} */
00906 
00907 static void spl_fixedarray_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
00908 {
00909        spl_fixedarray_it     *iterator = (spl_fixedarray_it *)iter;
00910        spl_fixedarray_object *intern   = iterator->object;
00911 
00912        if (intern->flags & SPL_FIXEDARRAY_OVERLOADED_NEXT) {
00913               zend_user_it_move_forward(iter TSRMLS_CC);
00914        } else {
00915               zend_user_it_invalidate_current(iter TSRMLS_CC);
00916               iterator->object->current++;
00917        }
00918 }
00919 /* }}} */
00920 
00921 /* {{{  proto int SplFixedArray::key() U
00922    Return current array key */
00923 SPL_METHOD(SplFixedArray, key)
00924 {
00925        spl_fixedarray_object *intern = (spl_fixedarray_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
00926        
00927        if (zend_parse_parameters_none() == FAILURE) {
00928               return;
00929        }
00930 
00931        RETURN_LONG(intern->current);
00932 }
00933 /* }}} */
00934 
00935 /* {{{ proto void SplFixedArray::next() U
00936    Move to next entry */
00937 SPL_METHOD(SplFixedArray, next)
00938 {
00939        spl_fixedarray_object *intern = (spl_fixedarray_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
00940        
00941        if (zend_parse_parameters_none() == FAILURE) {
00942               return;
00943        }
00944 
00945        intern->current++;
00946 }
00947 /* }}} */
00948 
00949 /* {{{ proto bool SplFixedArray::valid() U
00950    Check whether the datastructure contains more entries */
00951 SPL_METHOD(SplFixedArray, valid)
00952 {
00953        spl_fixedarray_object *intern = (spl_fixedarray_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
00954        
00955        if (zend_parse_parameters_none() == FAILURE) {
00956               return;
00957        }
00958 
00959        RETURN_BOOL(intern->current >= 0 && intern->array && intern->current < intern->array->size);
00960 }
00961 /* }}} */
00962 
00963 /* {{{ proto void SplFixedArray::rewind() U
00964    Rewind the datastructure back to the start */
00965 SPL_METHOD(SplFixedArray, rewind)
00966 {
00967        spl_fixedarray_object *intern = (spl_fixedarray_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
00968        
00969        if (zend_parse_parameters_none() == FAILURE) {
00970               return;
00971        }
00972 
00973        intern->current = 0;
00974 }
00975 /* }}} */
00976 
00977 /* {{{ proto mixed|NULL SplFixedArray::current() U
00978    Return current datastructure entry */
00979 SPL_METHOD(SplFixedArray, current)
00980 {
00981        zval                 *zindex, **value_pp;
00982        spl_fixedarray_object *intern  = (spl_fixedarray_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
00983        
00984        if (zend_parse_parameters_none() == FAILURE) {
00985               return;
00986        }
00987 
00988        ALLOC_INIT_ZVAL(zindex);
00989        ZVAL_LONG(zindex, intern->current);
00990 
00991        value_pp  = spl_fixedarray_object_read_dimension_helper(intern, zindex TSRMLS_CC);
00992 
00993        zval_ptr_dtor(&zindex);
00994 
00995        if (value_pp) {
00996               RETURN_ZVAL(*value_pp, 1, 0);
00997        }
00998        RETURN_NULL();
00999 }
01000 /* }}} */
01001 
01002 /* iterator handler table */
01003 zend_object_iterator_funcs spl_fixedarray_it_funcs = {
01004        spl_fixedarray_it_dtor,
01005        spl_fixedarray_it_valid,
01006        spl_fixedarray_it_get_current_data,
01007        spl_fixedarray_it_get_current_key,
01008        spl_fixedarray_it_move_forward,
01009        spl_fixedarray_it_rewind
01010 };
01011 
01012 zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
01013 {
01014        spl_fixedarray_it      *iterator;
01015        spl_fixedarray_object  *fixedarray_object = (spl_fixedarray_object*)zend_object_store_get_object(object TSRMLS_CC);
01016 
01017        if (by_ref) {
01018               zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0 TSRMLS_CC);
01019               return NULL;
01020        }
01021 
01022        Z_ADDREF_P(object);
01023 
01024        iterator                     = emalloc(sizeof(spl_fixedarray_it));
01025        iterator->intern.it.data     = (void*)object;
01026        iterator->intern.it.funcs    = &spl_fixedarray_it_funcs;
01027        iterator->intern.ce          = ce;
01028        iterator->intern.value       = NULL;
01029        iterator->object             = fixedarray_object;
01030 
01031        return (zend_object_iterator*)iterator;
01032 }
01033 /* }}} */
01034 
01035 ZEND_BEGIN_ARG_INFO_EX(arginfo_splfixedarray_construct, 0, 0, 0)
01036        ZEND_ARG_INFO(0, size)
01037 ZEND_END_ARG_INFO()
01038 
01039 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_offsetGet, 0, 0, 1)
01040        ZEND_ARG_INFO(0, index)
01041 ZEND_END_ARG_INFO()
01042 
01043 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_offsetSet, 0, 0, 2)
01044        ZEND_ARG_INFO(0, index)
01045        ZEND_ARG_INFO(0, newval)
01046 ZEND_END_ARG_INFO()
01047 
01048 ZEND_BEGIN_ARG_INFO(arginfo_fixedarray_setSize, 0)
01049        ZEND_ARG_INFO(0, value)
01050 ZEND_END_ARG_INFO()
01051 
01052 ZEND_BEGIN_ARG_INFO_EX(arginfo_fixedarray_fromArray, 0, 0, 1)
01053        ZEND_ARG_INFO(0, data)
01054        ZEND_ARG_INFO(0, save_indexes)
01055 ZEND_END_ARG_INFO()
01056 
01057 ZEND_BEGIN_ARG_INFO(arginfo_splfixedarray_void, 0)
01058 ZEND_END_ARG_INFO()
01059 
01060 static zend_function_entry spl_funcs_SplFixedArray[] = { /* {{{ */
01061        SPL_ME(SplFixedArray, __construct,     arginfo_splfixedarray_construct,ZEND_ACC_PUBLIC)
01062        SPL_ME(SplFixedArray, count,           arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01063        SPL_ME(SplFixedArray, toArray,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01064        SPL_ME(SplFixedArray, fromArray,       arginfo_fixedarray_fromArray,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
01065        SPL_ME(SplFixedArray, getSize,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01066        SPL_ME(SplFixedArray, setSize,         arginfo_fixedarray_setSize,     ZEND_ACC_PUBLIC)
01067        SPL_ME(SplFixedArray, offsetExists,    arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
01068        SPL_ME(SplFixedArray, offsetGet,       arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
01069        SPL_ME(SplFixedArray, offsetSet,       arginfo_fixedarray_offsetSet,   ZEND_ACC_PUBLIC)
01070        SPL_ME(SplFixedArray, offsetUnset,     arginfo_fixedarray_offsetGet,   ZEND_ACC_PUBLIC)
01071        SPL_ME(SplFixedArray, rewind,          arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01072        SPL_ME(SplFixedArray, current,         arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01073        SPL_ME(SplFixedArray, key,             arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01074        SPL_ME(SplFixedArray, next,            arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01075        SPL_ME(SplFixedArray, valid,           arginfo_splfixedarray_void,     ZEND_ACC_PUBLIC)
01076        PHP_FE_END
01077 };
01078 /* }}} */
01079 
01080 /* {{{ PHP_MINIT_FUNCTION */
01081 PHP_MINIT_FUNCTION(spl_fixedarray)
01082 {
01083        REGISTER_SPL_STD_CLASS_EX(SplFixedArray, spl_fixedarray_new, spl_funcs_SplFixedArray);
01084        memcpy(&spl_handler_SplFixedArray, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
01085 
01086        spl_handler_SplFixedArray.clone_obj       = spl_fixedarray_object_clone;
01087        spl_handler_SplFixedArray.read_dimension  = spl_fixedarray_object_read_dimension;
01088        spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
01089        spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
01090        spl_handler_SplFixedArray.has_dimension   = spl_fixedarray_object_has_dimension;
01091        spl_handler_SplFixedArray.count_elements  = spl_fixedarray_object_count_elements;
01092        spl_handler_SplFixedArray.get_properties  = spl_fixedarray_object_get_properties;
01093 
01094        REGISTER_SPL_IMPLEMENTS(SplFixedArray, Iterator);
01095        REGISTER_SPL_IMPLEMENTS(SplFixedArray, ArrayAccess);
01096        REGISTER_SPL_IMPLEMENTS(SplFixedArray, Countable);
01097 
01098        spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
01099 
01100        return SUCCESS;
01101 }
01102 /* }}} */
01103 
01104 
01105 /*
01106  * Local variables:
01107  * tab-width: 4
01108  * c-basic-offset: 4
01109  * End:
01110  * vim600: noet sw=4 ts=4 fdm=marker
01111  * vim<600: noet sw=4 ts=4
01112  */