since apr is supposed to be kept out of the axis2 code , i wrote a wrapper to apr_hash
which has been used in the axis2 code
I have atttached the axis2_hash.h and axis2_hash.c files and a test.
Thanks
Nandika
/* * Copyright 2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licaxiander the License. */
#include <apr_pools.h>
#include <apr_hash.h>
#include "axis2_hash.h"
static apr_pool_t *hashpool;
void axis2_hash_ops_set(axis2_hash_t *ht,void *key,size_t klen,const void *val)
{
if( !ht || !key)
return;
(apr_hash_t*)(ht->hashtable);
apr_hash_set((apr_hash_t*)(ht->hashtable),key,klen,val);
}
void *axis2_hash_ops_get(axis2_hash_t *ht,void *key,size_t klen)
{
if(!ht || !key)
return NULL;
return apr_hash_get((apr_hash_t*)ht->hashtable,key,klen);
}
int axis2_hash_ops_first(axis2_hash_t *ht)
{
ht->hashindex = apr_hash_first(hashpool,ht->hashtable);
return (ht->hashindex) != NULL ? 1 : 0 ;
}
int axis2_hash_ops_has_next(axis2_hash_t *ht)
{
if(!(ht->hashindex))
return 0;
ht->hashindex = apr_hash_next((apr_hash_index_t*)ht->hashindex);
return (ht->hashindex)!= NULL ? 1 : 0;
}
void axis2_hash_ops_this(axis2_hash_t *ht,void **key,size_t klen,void **val)
{
if(!ht)
return;
apr_hash_this((apr_hash_index_t *)(ht->hashindex),key,klen,val);
}
axis2_hash_t *axis2_hash_create()
{
axis2_hash_t *ht = (axis2_hash_t*)malloc(sizeof(axis2_hash_t));
apr_status_t status;
if(!ht)
return NULL;
if(!hashpool)
{
status = apr_pool_create (&hashpool, NULL);
//if(!status)
// return NULL;
}
ht->hashtable = apr_hash_make (hashpool);
ht->hashindex = NULL;
ht->ops = (axis2_hash_ops_t*)malloc(sizeof(axis2_hash_ops_t));
ht->ops->set = axis2_hash_ops_set;
ht->ops->get = axis2_hash_ops_get;
ht->ops->first = axis2_hash_ops_first;
ht->ops->has_next = axis2_hash_ops_has_next;
ht->ops->this = axis2_hash_ops_this;
return ht;
}
/* * Copyright 2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AXIS2_HASH_H #define AXIS2_HASH_H #include <stdlib.h> struct axis2_hash; struct axis2_hash_ops; struct axis2_hash_index; typedef struct axis2_hash axis2_hash_t; typedef struct axis2_hash_ops axis2_hash_ops_t; typedef struct axis2_hash_index axis2_hash_index_t; struct axis2_hash_ops { void (*set)(axis2_hash_t *ht,void *key,size_t klen,const void *val); void *(*get)(axis2_hash_t *ht,void *key,size_t klen); int (*first)(axis2_hash_t *ht); int (*has_next)(axis2_hash_t *ht); void (*this)(axis2_hash_t *ht,void **key,size_t klen,void **val); }; struct axis2_hash { struct axis2_hash_ops *ops; void *hashindex; void *hashtable; }; axis2_hash_t *axis2_hash_create(); #define axis2_hash_set(hashtable,key,klen,value) ((hashtable)->ops->set((hashtable),(key),(klen),(value))) #define axis2_hash_get(hashtable,key,klen) ((hashtable)->ops->get((hashtable),(key),(klen))) #define axis2_hash_first(hashtable) ((hashtable)->ops->first((hashtable))) #define axis2_hash_has_next(hashtable) ((hashtable)->ops->has_next(hashtable)) /** This macro contains out parameters hashtable - pointer to axis2_hash_t key - ** pointer to key klen - val - ** pointer to value */ #define axis2_hash_this(hashtable,key,klen,value) ((hashtable)->ops->this((hashtable),(key),(klen),(value))) #define AXIS2_HASH_KEY_STRING (-1) #endif // AXIS2_HASH_H
#include <stdio.h>
#include <apr.h>
#include "axis2_hash.h"
#include <apr_pools.h>
#include <apr_hash.h>
typedef struct a
{
char *value;
}a;
int main()
{
axis2_hash_t *ht;
a *a1,*a2 ,*a3,*a4;
int i=0;
void *v =NULL;
char *key1 = "key1";
char *key2 = "key2";
char *key3 = "key3";
char *key4 = "key4";
a1 = (a*)malloc(sizeof(a));
a2 = (a*)malloc(sizeof(a));
a3 = (a*)malloc(sizeof(a));
a4 = (a*)malloc(sizeof(a));
a1->value = strdup("key1");
a2->value = strdup("key2");
a3->value = strdup("key3");
a4->value = strdup("key4");
if(apr_initialize()!= APR_SUCCESS)
{
return;
}
ht = axis2_hash_create();
axis2_hash_set(ht,key1,AXIS2_HASH_KEY_STRING,a1);
axis2_hash_set(ht,key2,AXIS2_HASH_KEY_STRING,a2);
axis2_hash_set(ht,key3,AXIS2_HASH_KEY_STRING,a3);
axis2_hash_set(ht,key4,AXIS2_HASH_KEY_STRING,a4);
for(i = axis2_hash_first(ht); i > 0 ; i = axis2_hash_has_next(ht))
{
axis2_hash_this(ht,NULL,NULL,&v);
printf("\n %s \n",((a*)v)->value);
}
printf("\n demo get %s
",((a*)axis2_hash_get(ht,key1,AXIS2_HASH_KEY_STRING))->value);
printf("\n demo get %s
",((a*)axis2_hash_get(ht,key2,AXIS2_HASH_KEY_STRING))->value);
printf("\n demo get %s
",((a*)axis2_hash_get(ht,key3,AXIS2_HASH_KEY_STRING))->value);
printf("\n demo get %s
",((a*)axis2_hash_get(ht,key4,AXIS2_HASH_KEY_STRING))->value);
getchar();
}
