Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Unnamed
Tuesday, October 31st, 2006 at 11:11:51am UTC 

  1. Reproduce code:
  2. ---------------
  3.  
  4. <?php
  5.  
  6.         $colour;
  7.         $num;
  8.  
  9.         echo "Test 1: zend_hash_apply with stop colour red \n";
  10.         init_array();
  11.         iterate_until_red($colour);
  12.         check_array(1);
  13.  
  14.         echo "Test 2: zend_hash_apply_with_argument with stop colour prurple \n";
  15.         init_array();
  16.         iterate_until($colour, "purple");
  17.         check_array(2);
  18.  
  19.         echo "Test 3: zend_hash_apply_with_arguments with stop colours yellow and black \n";
  20.         init_array();
  21.         iterate_until_either($colour, "yellow", "black");
  22.         check_array(3);
  23.  
  24.         function init_array()
  25.         {
  26.                 global $colour, $num;
  27.                 $colour = array("orange", "green", "blue", "red", "yellow", "purple", "indigo");
  28.                 $num = count($colour);
  29.         }
  30.  
  31.         function check_array($test)
  32.         {
  33.                 global $colour, $num;
  34.        
  35.                echo "Array contents after test:  ";
  36.                foreach ($colour as $val) {
  37.                       $i++;
  38.                       echo "$val ";
  39.                 }
  40.  
  41.                if ($num == count($colour)) {
  42.                       echo "\nTEST $test PASSED\n\n\n";
  43.                 } else {
  44.                       echo "\nTEST $test FAILED\n\n\n";
  45.                 }       
  46.         }
  47. ?>
  48.  
  49.  
  50.  
  51. which uses the following simple extension
  52.  
  53.  
  54. ----------------  hashapitest.h --------------
  55.  
  56. #ifndef PHP_HASHAPITEST_H
  57. /* Prevent double inclusion */
  58. #define PHP_HASHAPITEST_H
  59.  
  60. /* Define Extension Properties */
  61. #define PHP_HASHAPITEST_EXTNAME    "hashapitest"
  62. #define PHP_HASHAPITEST_EXTVER    "1.0"
  63.  
  64.  
  65. #define PHP_HASHAPITEST_DESCRIPTOR_RES_NAME "File Descriptor"
  66.  
  67. /* Import configure options
  68.    when building outside of
  69.    the PHP source tree */
  70. #ifdef HAVE_CONFIG_H
  71. #include "config.h"
  72. #endif
  73.  
  74. /* Include PHP Standard Header */
  75. #include "php.h"
  76. #include "stdio.h"
  77.  
  78. /* Define the entry point symbol
  79.  * Zend will use when loading this module
  80.  */
  81. extern zend_module_entry hashapitest_module_entry;
  82. #define phpext_hashapitest_ptr &hashapitest_module_entry
  83.  
  84. #endif /* PHP_HASHAPITEST_H */
  85.  
  86. -------------  end of hashapitest.h -------------------
  87.  
  88. and
  89.  
  90. --------------  hashapitest.c ---------------------
  91.  
  92.  
  93. #include "hashapitest.h"
  94.  
  95. int stop_on_red(zval **val TSRMLS_DC)
  96. {
  97.     int ret = ZEND_HASH_APPLY_KEEP;
  98.  
  99.     zval tmpcopy = **val;
  100.     zval_copy_ctor(&tmpcopy);
  101.     INIT_PZVAL(&tmpcopy);
  102.     convert_to_string(&tmpcopy);
  103.  
  104.     php_printf("Stop_on_red called for element: ");
  105.     PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy));
  106.     php_printf("\n");
  107.  
  108.    /* Check for STOP condition */
  109.     if (!strcmp(Z_STRVAL(tmpcopy), "red")) {
  110.        return ZEND_HASH_APPLY_STOP;
  111.     }
  112.  
  113.     zval_dtor(&tmpcopy);
  114.     return ret;
  115. }
  116.  
  117. int stop_on_colour(zval **val, char *colour TSRMLS_DC)
  118. {
  119.     int ret = ZEND_HASH_APPLY_KEEP;
  120.    
  121.     zval tmpcopy = **val;
  122.     zval_copy_ctor(&tmpcopy);
  123.     INIT_PZVAL(&tmpcopy);
  124.     convert_to_string(&tmpcopy);
  125.  
  126.  
  127.     php_printf("Stop_on_colour called for element:");
  128.     PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy));
  129.     php_printf("\n");
  130.  
  131.     /* Check for STOP condition */
  132.     if (!strcmp(Z_STRVAL(tmpcopy), colour)) {
  133.        ret = ZEND_HASH_APPLY_STOP;
  134.     }
  135.  
  136.     zval_dtor(&tmpcopy);
  137.     return ret;
  138. }
  139.  
  140. int stop_on_either_colour(zval **val, int num_args, va_list args, zend_hash_key *hash_key)
  141. {
  142.     int ret = ZEND_HASH_APPLY_KEEP;
  143.     char *colour1= va_arg(args, char*);
  144.     char *colour2= va_arg(args, char*);
  145.  
  146.     TSRMLS_FETCH();
  147.     zval tmpcopy = **val;
  148.     zval_copy_ctor(&tmpcopy);
  149.     INIT_PZVAL(&tmpcopy);
  150.     convert_to_string(&tmpcopy);
  151.  
  152.     php_printf("Stop_on_either_colour called for element: ");
  153.     PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy));
  154.     php_printf("\n");
  155.  
  156.     /* Check for STOP condition */
  157.     if (!strcmp(Z_STRVAL(tmpcopy), colour1)|| !strcmp(Z_STRVAL(tmpcopy), colour2)) {
  158.         ret = ZEND_HASH_APPLY_STOP;
  159.     }
  160.  
  161.     zval_dtor(&tmpcopy);
  162.     return ret;
  163. }
  164.  
  165. PHP_FUNCTION(iterate_until_red)
  166. {
  167.     zval *array;
  168.     HashTable *arrayht = NULL;
  169.     int arg1 = 99;
  170.     char *arg2 = "red";
  171.    
  172.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &array) == FAILURE) {
  173.         RETURN_NULL();
  174.     }
  175.     if (Z_TYPE_P(array) != IS_ARRAY) {
  176.         php_error_docref(NULL TSRMLS_CC, E_NOTICE,
  177.                "<p>iterate_until_red called on non-array zval<p/>");
  178.         return;
  179.     }
  180.  
  181.     arrayht =  Z_ARRVAL_P(array);
  182.     zend_hash_apply(arrayht, stop_on_red TSRMLS_CC);
  183.     return;
  184. }
  185.  
  186. PHP_FUNCTION(iterate_until)
  187. {
  188.     zval *array;
  189.     HashTable *arrayht = NULL;
  190.     char *stop_colour;
  191.     int stop_colour_len;
  192.    
  193.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &array, &stop_colour, &stop_colour_len) == FAILURE) {
  194.         RETURN_NULL();
  195.     }
  196.     if (Z_TYPE_P(array) != IS_ARRAY) {
  197.         php_error_docref(NULL TSRMLS_CC, E_NOTICE,
  198.                "<p>iterate_until called on non-array zval<p/>");
  199.         return;
  200.     }
  201.  
  202.     arrayht =  Z_ARRVAL_P(array);
  203.     zend_hash_apply_with_argument(arrayht, stop_on_colour, stop_colour TSRMLS_CC);
  204.     return;
  205. }
  206.  
  207. PHP_FUNCTION(iterate_until_either)
  208. {
  209.     zval *array;
  210.     HashTable *arrayht = NULL;
  211.     char *stop_colour1, *stop_colour2;
  212.     int stop_colour1_len, stop_colour2_len;
  213.  
  214.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zss", &array, &stop_colour1, &stop_colour1_len, &stop_colour2, &stop_colour2_len) == FAILURE) {
  215.         RETURN_NULL();
  216.     }
  217.     if (Z_TYPE_P(array) != IS_ARRAY) {
  218.         php_error_docref(NULL TSRMLS_CC, E_NOTICE,
  219.                "<p>iterate_until_either called on non-array zval<p/>");
  220.         return;
  221.     }
  222.  
  223.     arrayht =  Z_ARRVAL_P(array);
  224.     zend_hash_apply_with_arguments(arrayht, stop_on_either_colour, 2, stop_colour1, stop_colour2);
  225.     return;
  226. }
  227.  
  228.                    
  229. static function_entry php_hashapitest_functions[] = {
  230.     PHP_FE(iterate_until_red, NULL)
  231.     PHP_FE(iterate_until, NULL)
  232.     PHP_FE(iterate_until_either, NULL)
  233.     { NULL, NULL, NULL }
  234. };
  235.  
  236.  
  237. zend_module_entry hashapitest_module_entry = {
  238. #if ZEND_MODULE_API_NO >= 20010901
  239.      STANDARD_MODULE_HEADER,
  240. #endif
  241.     PHP_HASHAPITEST_EXTNAME,
  242.     php_hashapitest_functions, /* Functions */
  243.     NULL,  /* MINIT */
  244.     NULL, /* MSHUTDOWN */
  245.     NULL, /* RINIT */
  246.     NULL, /* RSHUTDOWN */
  247.     NULL, /* MINFO */
  248. #if ZEND_MODULE_API_NO >= 20010901
  249.     PHP_HASHAPITEST_EXTVER,
  250. #endif
  251.     STANDARD_MODULE_PROPERTIES
  252. };
  253.  
  254. #ifdef COMPILE_DL_HASHAPITEST
  255. ZEND_GET_MODULE(hashapitest)
  256. #endif
  257.  
  258. --------------  end of hashapitest.c ------------------

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right worth-right