int sfatom_reset() { atom_first = 1; sfghash_delete( g_atom ); if( sfatom_init() ) { return SFGHASH_ERR; } return SFGHASH_OK; }
int sfatom_add(char * str, void * data) { if( atom_first ) { if( sfatom_init() ) { return SFGHASH_ERR; } } if( !g_atom ) { return SFGHASH_ERR; } sfghash_add( g_atom, strdup(str), data ); return SFGHASH_OK; }
/* * Hash test program */ int main ( int argc, char ** argv ) { int i; SFGHASH * t; SFGHASH_NODE * n, *m; char str[256],*p; int num=100; if( argc > 1 ) num = atoi(argv[1]); sfatom_init(); /* Create a Hash Table */ t = sfghash_new( 1000, 0 , GH_COPYKEYS , myfree ); /* Add Nodes to the Hash Table */ for(i=0;i<num;i++) { snprintf(str, sizeof(str), "KeyWord%d",i+1); str[sizeof(str) - 1] = '\0'; sfghash_add( t, str, strupr(strdup(str)) ); sfatom_add( str, strupr(strdup(str)) ); } /* Find and Display Nodes in the Hash Table */ printf("\n** FIND KEY TEST\n"); for(i=0;i<num;i++) { snprintf(str, sizeof(str), "KeyWord%d",i+1); str[sizeof(str) - 1] = '\0'; p = (char*) sfghash_find( t, str ); printf("Hash-key=%*s, data=%*s\n", strlen(str),str, strlen(str), p ); p = (char*) sfatom_find( str ); printf("Atom-key=%*s, data=%*s\n", strlen(str),str, strlen(str), p ); } /* Display All Nodes in the Hash Table */ printf("\n** FINDFIRST / FINDNEXT TEST\n"); for( n = sfghash_findfirst(t); n; n = sfghash_findnext(t) ) { printf("hash-findfirst/next: key=%s, data=%s\n", n->key, n->data ); // hashing code frees user data using 'myfree' above .... if( sfghash_remove(t,n->key) ) printf("Could not remove the key node\n"); else printf("key node removed\n"); } for( n = sfatom_findfirst(); n; n = sfatom_findnext() ) { printf("atom-findfirst/next: key=%s, data=%s\n", n->key, n->data ); free( n->data ); //since atom data is not freed automatically } /* Free the table and it's user data */ printf("****sfghash_delete\n"); sfghash_delete( t ); printf("****sfatom_reset\n"); sfatom_reset(); printf("\nnormal pgm finish\n\n"); return 0; }