PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/resourcebundle - resourcebundle_iterator.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 63 66 95.5 %
Date: 2014-10-22 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Authors: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch>      |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : #include <php.h>
      18             : #include <zend.h>
      19             : #include <zend_API.h>
      20             : 
      21             : #include "resourcebundle/resourcebundle.h"
      22             : #include "resourcebundle/resourcebundle_class.h"
      23             : #include "resourcebundle/resourcebundle_iterator.h"
      24             : 
      25             : /*
      26             :  * Although libicu offers iterator functions, they are not used here: libicu does iterate
      27             :  * irrespective of array indices. Those cannot be recreated afterwards. Arrays as well as tables
      28             :  * can however be accessed by numerical index, with table keys readable ex post.
      29             :  */
      30             : 
      31             : /* {{{ resourcebundle_iterator_read */
      32          12 : static void resourcebundle_iterator_read( ResourceBundle_iterator *iterator TSRMLS_DC ) 
      33             : {
      34          12 :         UErrorCode icuerror = U_ZERO_ERROR;
      35          12 :         ResourceBundle_object *rb = iterator->subject;
      36             : 
      37          12 :         rb->child = ures_getByIndex( rb->me, iterator->i, rb->child, &icuerror );
      38             : 
      39          12 :         if (U_SUCCESS(icuerror)) {
      40             :                 /* ATTN: key extraction must be the first thing to do... rb->child might be reset in read! */
      41          12 :                 if (iterator->is_table) {
      42           6 :                         iterator->currentkey = estrdup( ures_getKey( rb->child ) );
      43             :                 }
      44          12 :                 resourcebundle_extract_value( &iterator->current, rb TSRMLS_CC );
      45             :         }
      46             :         else {
      47             :                 // zend_throw_exception( spl_ce_OutOfRangeException, "Running past end of ResourceBundle", 0 TSRMLS_CC);
      48           0 :                 ZVAL_UNDEF(&iterator->current);
      49             :         }
      50          12 : }
      51             : /* }}} */
      52             : 
      53             : /* {{{ resourcebundle_iterator_invalidate */
      54          18 : static void resourcebundle_iterator_invalidate( zend_object_iterator *iter TSRMLS_DC ) 
      55             : {
      56          18 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
      57             : 
      58          36 :         if (!Z_ISUNDEF(iterator->current)) {
      59          12 :                 zval_ptr_dtor( &iterator->current );
      60          12 :                 ZVAL_UNDEF(&iterator->current);
      61             :         }
      62          18 :         if (iterator->currentkey) {
      63           6 :                 efree( iterator->currentkey );
      64           6 :                 iterator->currentkey = NULL;
      65             :         }
      66          18 : }
      67             : /* }}} */
      68             : 
      69             : /* {{{ resourcebundle_iterator_dtor */
      70           3 : static void resourcebundle_iterator_dtor( zend_object_iterator *iter TSRMLS_DC )
      71             : {
      72           3 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
      73           3 :         zval                    *object = &iterator->intern.data;
      74             : 
      75           3 :         resourcebundle_iterator_invalidate( iter TSRMLS_CC );
      76             : 
      77           3 :         zval_ptr_dtor(object);
      78           3 : }
      79             : /* }}} */
      80             : 
      81             : /* {{{ resourcebundle_iterator_has_more */
      82          15 : static int resourcebundle_iterator_has_more( zend_object_iterator *iter TSRMLS_DC )
      83             : {
      84          15 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
      85          15 :         return (iterator->i < iterator->length) ? SUCCESS : FAILURE;
      86             : }
      87             : /* }}} */
      88             : 
      89             : /* {{{ resourcebundle_iterator_current */
      90          12 : static zval *resourcebundle_iterator_current( zend_object_iterator *iter TSRMLS_DC )
      91             : {
      92          12 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
      93          24 :         if (Z_ISUNDEF(iterator->current)) {
      94          12 :                 resourcebundle_iterator_read( iterator TSRMLS_CC);
      95             :         }
      96          12 :         return &iterator->current;
      97             : }
      98             : /* }}} */
      99             : 
     100             : /* {{{ resourcebundle_iterator_key */
     101          12 : static void resourcebundle_iterator_key( zend_object_iterator *iter, zval *key TSRMLS_DC )
     102             : {
     103          12 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
     104             : 
     105          24 :         if (Z_ISUNDEF(iterator->current)) {
     106           0 :                 resourcebundle_iterator_read( iterator TSRMLS_CC);
     107             :         }
     108             : 
     109          12 :         if (iterator->is_table) {
     110          12 :                 ZVAL_STRING(key, iterator->currentkey);
     111             :         } else {
     112           6 :                 ZVAL_LONG(key, iterator->i);
     113             :         }
     114          12 : }
     115             : /* }}} */
     116             : 
     117             : /* {{{ resourcebundle_iterator_step */
     118          12 : static void resourcebundle_iterator_step( zend_object_iterator *iter TSRMLS_DC )
     119             : {
     120          12 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
     121             : 
     122          12 :         iterator->i++;
     123          12 :         resourcebundle_iterator_invalidate( iter TSRMLS_CC );
     124          12 : }
     125             : /* }}} */
     126             : 
     127             : /* {{{ resourcebundle_iterator_has_reset */
     128           3 : static void resourcebundle_iterator_reset( zend_object_iterator *iter TSRMLS_DC )
     129             : {
     130           3 :         ResourceBundle_iterator *iterator = (ResourceBundle_iterator *) iter;
     131             : 
     132           3 :         iterator->i = 0;
     133           3 :         resourcebundle_iterator_invalidate( iter TSRMLS_CC );
     134           3 : }
     135             : /* }}} */
     136             : 
     137             : /* {{{ resourcebundle_iterator_funcs */
     138             : static zend_object_iterator_funcs resourcebundle_iterator_funcs = {
     139             :         resourcebundle_iterator_dtor,
     140             :         resourcebundle_iterator_has_more,
     141             :         resourcebundle_iterator_current,
     142             :         resourcebundle_iterator_key,
     143             :         resourcebundle_iterator_step,
     144             :         resourcebundle_iterator_reset,
     145             :         resourcebundle_iterator_invalidate
     146             : };
     147             : /* }}} */
     148             : 
     149             : /* {{{ resourcebundle_get_iterator */
     150           3 : zend_object_iterator *resourcebundle_get_iterator( zend_class_entry *ce, zval *object, int byref TSRMLS_DC )
     151             : {
     152           3 :         ResourceBundle_object   *rb = Z_INTL_RESOURCEBUNDLE_P(object );
     153           3 :         ResourceBundle_iterator *iterator = emalloc( sizeof( ResourceBundle_iterator ) );
     154             : 
     155           3 :         if (byref) {
     156           0 :              php_error( E_ERROR, "ResourceBundle does not support writable iterators" );
     157             :         }
     158             : 
     159           3 :         zend_iterator_init(&iterator->intern TSRMLS_CC);
     160           3 :         ZVAL_COPY(&iterator->intern.data, object);
     161           3 :         iterator->intern.funcs = &resourcebundle_iterator_funcs;
     162             : 
     163           3 :         iterator->subject = rb;
     164             : 
     165             :         /* The iterated rb can only be either URES_TABLE or URES_ARRAY
     166             :          * All other types are returned as php primitives!
     167             :          */
     168           3 :         iterator->is_table = (ures_getType( rb->me ) == URES_TABLE);
     169           3 :         iterator->length = ures_getSize( rb->me );
     170             : 
     171           3 :         ZVAL_UNDEF(&iterator->current);
     172           3 :         iterator->currentkey = NULL;
     173           3 :         iterator->i = 0;
     174             : 
     175           3 :         return (zend_object_iterator *) iterator;
     176             : }
     177             : /* }}} */
     178             : 
     179             : /*
     180             :  * Local variables:
     181             :  * tab-width: 4
     182             :  * c-basic-offset: 4
     183             :  * End:
     184             :  * vim600: noet sw=4 ts=4 fdm=marker
     185             :  * vim<600: noet sw=4 ts=4
     186             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:50 +0000 (4 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.