I'd like to propose a slight change of dbmfetch
adding support for arrays.

The best solution to this that I found was taking
the values of the array sent in as keys and returning
a associative array with the keys as keys and the
values from the dbm file as values.

Below is my little hack for this.

What do you think?

  - Gustaf

=========================

PHP_FUNCTION(dbmfetch)
{
   pval *id, *key;
   pval **current_key, *current_data;
   dbm_info *info;
   char *ret, *result;

   if (ZEND_NUM_ARGS()!=2||zend_get_parameters(ht,2,&id,&key)==FAILURE) {
     WRONG_PARAM_COUNT;
   }

   info = php_find_dbm(id);
   if (!info) {
     php_error(E_WARNING, "not a valid database identifier %d", Z_LVAL_P(id));
     RETURN_FALSE;
   }

   /* Check that key isn't an array */
   if (Z_TYPE_P(key) != IS_ARRAY) {
     convert_to_string(key);
     ret = php_dbm_fetch(info, Z_STRVAL_P(key));
     if (ret) {
       RETVAL_STRING(ret, 0);
     } else {
       RETURN_FALSE;
     }
   }
   /* Key was an array, loop and fetch */
   else {
     if(array_init(return_value) != SUCCESS) {
       zend_error(E_ERROR, "unable to create array");
       RETURN_FALSE;
     }
     zend_hash_internal_pointer_reset(Z_ARRVAL_P(key));
     /* Get value for each entry and try to find that "key"
      * in the dbm file, add to return_value with "key" as
      * key and dbm_value as value.
      *
      * It may seem backwards, but once you think about it
      * it's logical since it's just about the only way to
      * store the connection between the earlier value and
      * the fetched value. */
     while(zend_hash_get_current_data(Z_ARRVAL_P(key), (void 
**)&current_key) == SUCCESS) {
       /* Key is always considered a string for the dbm */
       convert_to_string_ex(current_key);
       ret = php_dbm_fetch(info, Z_STRVAL_PP(current_key));
       /* Create new current_data ZVAL for this key.*/
       MAKE_STD_ZVAL(current_data);
       if(ret) {
         ZVAL_STRING(current_data, ret, 1);
         zend_printf("ret valid: %s<br>\n", Z_STRVAL_P(current_data));
         add_assoc_zval_ex(return_value,
                   Z_STRVAL_PP(current_key),
                   Z_STRLEN_PP(current_key),
                   current_data);
       } else {
         ZVAL_STRING(current_data, empty_string, 1);
         add_assoc_zval_ex(return_value,
                   Z_STRVAL_PP(current_key),
                   Z_STRLEN_PP(current_key),
                   current_data);
       }
       zend_hash_move_forward(Z_ARRVAL_P(key));
       /* Shouldn't we free current_key's string pointer here?  */
     }
   }
}


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to