/*
   Module name: hash_util.c
   Created by: Ljubomir Buturovic
   Created: 11/29/2001
   Purpose: user-friendly API for the Kazlib hash library.
*/

/*
  Copyright 2004 Ljubomir J. Buturovic

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "hash.h"
#include "hash_util.h"

hash_t *hashCreate(void)
{
  hash_t *hashtable;

  hashtable = hash_create(HASHCOUNT_T_MAX, 0, 0);
  return hashtable;
}

/*
  Return number of elements in 'hashtable'.
*/
int hashCount(hash_t *hashtable)
{
  int count = 0;

  if (hashtable != (hash_t *) 0)
    count = hash_count(hashtable);
  return count;
}

/*
  Put integer 'value' under 'key' into 'hashtable'. Return the stored
  value. NOTE: this function will return INT_MAX if the operation
  fails. Therefore it should not be used for hashtables where INT_MAX
  is a legal value.
*/
int hashPutInt(hash_t *hashtable, int key, int value)
{
  int status;
  int retval = INT_MAX;
  char *keyptr;
  char *data;
  
  keyptr = malloc(MAX_INT_LENGTH);
  sprintf(keyptr, "%d", key);
  data = malloc(MAX_INT_LENGTH);
  sprintf(data, "%d", value);
  status = hash_alloc_insert(hashtable, keyptr, data);
  if (status == 1)
    retval = value;
  return retval;
}

/*
  Get integer value corresponding to 'key' from 'hashtable'. Return
  INT_MAX if 'key' is not in the hashtable. NOTE: the function should
  not be used for hashtables where INT_MAX is a legal value.
*/
int hashGetInt(hash_t *hashtable, int key)
{
  int  value = INT_MAX;
  char *keyptr;
  hnode_t *hn;
  
  keyptr = malloc(MAX_INT_LENGTH);
  sprintf(keyptr, "%d", key);
  hn = hash_lookup(hashtable, keyptr);
  if (hn != ((hnode_t *) 0))
    value = atoi((char *) hnode_get(hn));
  free(keyptr);
  return value;
}

char *hashPutString(hash_t *hashtable, char *key, char *value)
{
  int  status;
  char *retval = (char *) 0;
  char *keyptr;
  char *valptr;

  keyptr = strdup(key);
  valptr = strdup(value);
  status = hash_alloc_insert(hashtable, keyptr, valptr);
  if (status == 1)
    retval = value;
  return retval;
}

char *hashGetString(hash_t *hashtable, char *key)
{
  char *value = (char *) 0;
  hnode_t *hn;

  hn = hash_lookup(hashtable, key);
  if (hn != ((hnode_t *) 0))
    value = (char *) hnode_get(hn);
  return value;
}

hash_t *hashPutHashtable(hash_t *hashtable, char *key, hash_t *value)
{
  int  status;
  char *keyptr;
  hash_t *valptr;
  hash_t *retval = (hash_t *) 0;

  if ((hashtable != (hash_t *) 0) && (key != (char *) 0) && (value != (hash_t *) 0))
    if (hash_lookup(hashtable, key) == (hnode_t *) 0)
      {
	keyptr = strdup(key);
	if (keyptr != (char *) 0)
	  {
	    valptr = malloc(sizeof(hash_t));
	    if (valptr != (hash_t *) 0)
	      {
		memcpy(valptr, value, sizeof(hash_t));
		status = hash_alloc_insert(hashtable, keyptr, valptr);
		if (status == 1)
		  retval = value;
	      }
	  }
      }
  return retval;
}

hash_t *hashGetHashtable(hash_t *hashtable, char *key)
{
  hash_t *value = (hash_t *) 0;
  hnode_t *hn;

  hn = hash_lookup(hashtable, key);
  if (hn != ((hnode_t *) 0))
    value = (hash_t *) hnode_get(hn);
  return value;
}

/*
  Put integer 'value' under string 'key' into 'hashtable'. Return the
  stored value. NOTE: this function will return INT_MAX if the
  operation fails. Therefore it should not be used for hashtables
  where INT_MAX is a legal value.
*/
int hashPutStrInt(hash_t *hashtable, char *key, int value)
{
  int status;
  int retval = INT_MAX;
  char *keyptr;
  char *data;

  if (hash_lookup(hashtable, key) == (hnode_t *) 0)
    {
      data = malloc(MAX_INT_LENGTH);
      if (data != (char *) 0)
	{
	  keyptr = strdup(key);
	  if (keyptr != (char *) 0)
	    {
	      sprintf(data, "%d", value);
	      status = hash_alloc_insert(hashtable, keyptr, data);
	      if (status == 1)
		retval = value;
	    }
	}
    }
  return retval;
}

/*
  Get integer value corresponding to string 'key' from
  'hashtable'. NOTE: this function will return INT_MAX if 'key' is not
  in the hashtable. That means that this function should not be used
  for hashtables where INT_MAX is a legal value.
*/
int hashGetStrInt(hash_t *hashtable, char *key)
{
  int  value = INT_MAX;
  hnode_t *hn;

  hn = hash_lookup(hashtable, key);
  if (hn != ((hnode_t *) 0))
    value = atoi((char *) hnode_get(hn));
  return value;
}

/*
  Remove 'key' from 'hashtable'.
*/
void *hashRemove(hash_t *hashtable, void *key)
{
  void *retval = (void *) 0;
  void *val;
  char *hash_key;
  hnode_t *hn;

  hn = hash_lookup(hashtable, key);
  if (hn) 
    {
      retval = (void *) hn;
      val = hnode_get(hn);
      hash_key = (void *) hnode_getkey(hn);
      hash_scan_delfree(hashtable, hn);
      free((void *) hash_key);
      free(val);
    }
  return retval;
}

/*
  Free all 'hashtable' elements and then free 'hashtable' itself.

  NOTE: hash.c source code says hash_free() is obsolete. Don't
  understand why.
*/
void hashDelete(hash_t *hashtable)
{
  hash_free(hashtable);
}



syntax highlighted by Code2HTML, v. 0.9.1