2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
|
|
|
|
2008-05-24 16:05:49 +08:00
|
|
|
/* See 'hashmap.c' for detailed information. */
|
|
|
|
|
2002-04-08 05:30:02 +08:00
|
|
|
#ifndef _HASHMAP_H
|
|
|
|
#define _HASHMAP_H
|
|
|
|
|
|
|
|
/* Allow the use in C++ code. */
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're using a typedef here to "hide" the implementation details of the
|
|
|
|
* hash map. Sure, it's a pointer, but the struct is hidden in the C file.
|
|
|
|
* So, just use the hashmap_t like it's a cookie. :)
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
typedef struct hashmap_s *hashmap_t;
|
|
|
|
typedef int hashmap_iter;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hashmap_create() takes one argument, which is the number of buckets to
|
|
|
|
* use internally. hashmap_delete() is self explanatory.
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern hashmap_t hashmap_create(unsigned int nbuckets);
|
|
|
|
extern int hashmap_delete(hashmap_t map);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When the you insert a key/data pair into the hashmap it will the key
|
|
|
|
* and data are duplicated, so you must free your copy if it was created
|
|
|
|
* on the heap. The key must be a NULL terminated string. "data" must be
|
|
|
|
* non-NULL and length must be greater than zero.
|
|
|
|
*
|
|
|
|
* Returns: negative on error
|
|
|
|
* 0 upon successful insert
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern int hashmap_insert(hashmap_t map, const char *key,
|
|
|
|
const void *data, size_t len);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Get an iterator to the first entry.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Returns: an negative value upon error.
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern hashmap_iter hashmap_first(hashmap_t map);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Checks to see if the iterator is pointing at the "end" of the entries.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Returns: 1 if it is the end
|
|
|
|
* 0 otherwise
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern int hashmap_is_end(hashmap_t map, hashmap_iter iter);
|
2002-04-26 02:55:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a "pointer" to the first instance of the particular key. It can
|
|
|
|
* be tested against hashmap_is_end() to see if the key was not found.
|
|
|
|
*
|
|
|
|
* Returns: negative upon an error
|
|
|
|
* an "iterator" pointing at the first key
|
|
|
|
* an "end-iterator" if the key wasn't found
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern hashmap_iter hashmap_find(hashmap_t map, const char *key);
|
2002-04-26 02:55:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the key/data associated with a particular iterator.
|
|
|
|
* NOTE: These are pointers to the actual data, so don't mess around with them
|
|
|
|
* too much.
|
|
|
|
*
|
|
|
|
* Returns: the length of the data block upon success
|
|
|
|
* negative upon error
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern ssize_t hashmap_return_entry(hashmap_t map, hashmap_iter iter,
|
|
|
|
char **key, void **data);
|
2002-04-26 02:55:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the first entry (assuming there is more than one) for a particular
|
|
|
|
* key. The data MUST be non-NULL.
|
|
|
|
*
|
|
|
|
* Returns: negative upon error
|
|
|
|
* zero if no entry is found
|
|
|
|
* length of data for the entry
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern ssize_t hashmap_entry_by_key(hashmap_t map, const char *key,
|
|
|
|
void **data);
|
2002-04-26 02:55:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Searches for _any_ occurrances of "key" within the hashmap and returns the
|
|
|
|
* number of matching entries.
|
2008-05-24 16:05:49 +08:00
|
|
|
*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Returns: negative upon an error
|
|
|
|
* zero if no key is found
|
|
|
|
* count found (positive value)
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern ssize_t hashmap_search(hashmap_t map, const char *key);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the hashmap and remove the particular key.
|
2002-04-26 02:55:56 +08:00
|
|
|
* NOTE: This will invalidate any iterators which have been created.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
|
|
|
* Remove: negative upon error
|
|
|
|
* 0 if the key was not found
|
2002-04-26 02:55:56 +08:00
|
|
|
* positive count of entries deleted
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
extern ssize_t hashmap_remove(hashmap_t map, const char *key);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
2005-08-15 11:54:31 +08:00
|
|
|
#endif /* C++ */
|
|
|
|
#endif /* _HASHMAP_H */
|