Add init/free functions and rename parameters.
This commit is contained in:
		
							parent
							
								
									7cb2b3de7f
								
							
						
					
					
						commit
						5bb20bdb9e
					
				
					 4 changed files with 128 additions and 20 deletions
				
			
		| 
						 | 
				
			
			@ -3,14 +3,15 @@
 | 
			
		|||
#include <string.h>
 | 
			
		||||
#include "smap.h"
 | 
			
		||||
 | 
			
		||||
static void append(void* mapp, const char* left, void* right)
 | 
			
		||||
#define INCR_SIZE	8
 | 
			
		||||
 | 
			
		||||
static void append(void* mapp, char* key, void* value)
 | 
			
		||||
{
 | 
			
		||||
    struct smap* map = mapp;
 | 
			
		||||
    struct smap_node* node;
 | 
			
		||||
 | 
			
		||||
	if (map->count == map->max)
 | 
			
		||||
	{
 | 
			
		||||
		int newmax = (map->max == 0) ? 8 : (map->max * 2);
 | 
			
		||||
		int newmax = (map->max == 0) ? INCR_SIZE : (map->max * 2);
 | 
			
		||||
		struct smap_node* newarray = realloc(map->item, newmax * sizeof(*newarray));
 | 
			
		||||
 | 
			
		||||
		map->max = newmax;
 | 
			
		||||
| 
						 | 
				
			
			@ -20,11 +21,12 @@ static void append(void* mapp, const char* left, void* right)
 | 
			
		|||
    node = &map->item[map->count];
 | 
			
		||||
    map->count++;
 | 
			
		||||
 | 
			
		||||
    node->left = left;
 | 
			
		||||
    node->right = right;
 | 
			
		||||
    node->left = key;
 | 
			
		||||
    node->right = value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void smap_put(void* mapp, const char* left, void* right)
 | 
			
		||||
 | 
			
		||||
void smap_put(struct smap *mapp, char* key, void* value)
 | 
			
		||||
{
 | 
			
		||||
    struct smap* map = mapp;
 | 
			
		||||
    int i;
 | 
			
		||||
| 
						 | 
				
			
			@ -32,17 +34,44 @@ void smap_put(void* mapp, const char* left, void* right)
 | 
			
		|||
    for (i=0; i<map->count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        struct smap_node* node = &map->item[i];
 | 
			
		||||
        if (strcmp(node->left, left) == 0)
 | 
			
		||||
        if (strcmp(node->left, key) == 0)
 | 
			
		||||
        {
 | 
			
		||||
            node->right = right;
 | 
			
		||||
            node->right = value;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    append(map, left, right);
 | 
			
		||||
    append(map, key, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void smap_add(void* mapp, const char* left, void* right)
 | 
			
		||||
void smap_init(struct smap *mapp)
 | 
			
		||||
{
 | 
			
		||||
	mapp->count = 0;
 | 
			
		||||
	mapp->item = NULL;
 | 
			
		||||
	mapp->max = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void smap_free(struct smap *mapp, int free_key, int free_value)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
    for (i=0; i<mapp->count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        struct smap_node* node = &mapp->item[i];
 | 
			
		||||
        if (free_key)
 | 
			
		||||
        {
 | 
			
		||||
        	free(node->left);
 | 
			
		||||
        }
 | 
			
		||||
        if (free_value)
 | 
			
		||||
        {
 | 
			
		||||
        	free(node->right);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
	mapp->count = 0;
 | 
			
		||||
	free(mapp->item);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void smap_add(struct smap *mapp, char* key, void* value)
 | 
			
		||||
{
 | 
			
		||||
    struct smap* map = mapp;
 | 
			
		||||
    int i;
 | 
			
		||||
| 
						 | 
				
			
			@ -50,14 +79,14 @@ void smap_add(void* mapp, const char* left, void* right)
 | 
			
		|||
    for (i=0; i<map->count; i++)
 | 
			
		||||
    {
 | 
			
		||||
        struct smap_node* node = &map->item[i];
 | 
			
		||||
        if ((strcmp(node->left, left) == 0) && (node->right == right))
 | 
			
		||||
        if ((strcmp(node->left, key) == 0) && (node->right == value))
 | 
			
		||||
            return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    append(map, left, right);
 | 
			
		||||
    append(map, key, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* smap_get(void* mapp, const char* left)
 | 
			
		||||
void* smap_get(struct smap *mapp, const char* left)
 | 
			
		||||
{
 | 
			
		||||
    struct smap* map = mapp;
 | 
			
		||||
    int i;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,14 @@
 | 
			
		|||
/** @file
 | 
			
		||||
 * String map implementation.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef SMAP_H
 | 
			
		||||
#define SMAP_H
 | 
			
		||||
 | 
			
		||||
struct smap_node
 | 
			
		||||
{
 | 
			
		||||
	const char* left;
 | 
			
		||||
	char* left;
 | 
			
		||||
	void* right;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -18,14 +23,36 @@ struct smap
 | 
			
		|||
 | 
			
		||||
#define SMAPOF(RIGHT) \
 | 
			
		||||
	struct { \
 | 
			
		||||
		struct { const char* left; RIGHT* right; }* item; \
 | 
			
		||||
		struct { char* left; RIGHT* right; }* item; \
 | 
			
		||||
		int count; \
 | 
			
		||||
		int max; \
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
extern void smap_put(void* map, const char* left, void* right);
 | 
			
		||||
extern void smap_add(void* map, const char* left, void* right);
 | 
			
		||||
extern void* smap_get(void* map, const char* left);
 | 
			
		||||
/** Initializes a string map and returns the initialized
 | 
			
		||||
 *  handle in `mapp`.
 | 
			
		||||
 */
 | 
			
		||||
extern void smap_init(struct smap *mapp);
 | 
			
		||||
/** Adds a new item with name `key` in the string map if
 | 
			
		||||
 *  it does not already exist, otherwise replaces the
 | 
			
		||||
 *  value `value` associated with the existing `key`.
 | 
			
		||||
 */
 | 
			
		||||
extern void smap_put(struct smap *mapp, char* key, void* value);
 | 
			
		||||
/** Adds a new item in a string map only if `key` does
 | 
			
		||||
 *  not already exist in the string map.
 | 
			
		||||
 */
 | 
			
		||||
extern void smap_add(struct smap *mapp, char* key, void* value);
 | 
			
		||||
/** Returns the value associated with the specified `key`, returns
 | 
			
		||||
 *  NULL if `key` is not present in the string map.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
extern void* smap_get(struct smap* mapp, const char* key);
 | 
			
		||||
/** Frees the data structure associated with the string map.
 | 
			
		||||
 *  Also frees the memory associated with the key if
 | 
			
		||||
 *  `free_key` is non-zero, and free the memory associated
 | 
			
		||||
 *  with the value if `free_value` is non-zero.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
extern void smap_free(struct smap *mapp, int free_key, int free_value);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
/** @file
 | 
			
		||||
 * String list implementation.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include "stringlist.h"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -26,18 +30,62 @@ void stringlist_addall(struct stringlist* list, struct stringlist* src)
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void stringlist_free(struct stringlist* list)
 | 
			
		||||
void stringlist_free(struct stringlist* list, int freedata)
 | 
			
		||||
{
 | 
			
		||||
	struct stringfragment* f = list->first;
 | 
			
		||||
 | 
			
		||||
	while (f)
 | 
			
		||||
	{
 | 
			
		||||
		struct stringfragment* next = f->next;
 | 
			
		||||
		if (freedata)
 | 
			
		||||
		{
 | 
			
		||||
			free(f->data);
 | 
			
		||||
			f->data = NULL;
 | 
			
		||||
		}
 | 
			
		||||
		free(f);
 | 
			
		||||
		f = next;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void stringlist_init(struct stringlist* list)
 | 
			
		||||
{
 | 
			
		||||
	list->first = NULL;
 | 
			
		||||
	list->last = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
char* stringlist_get(struct stringlist *list, int index)
 | 
			
		||||
{
 | 
			
		||||
	struct stringfragment* f = list->first;
 | 
			
		||||
	int i = 0;
 | 
			
		||||
 | 
			
		||||
	while (f)
 | 
			
		||||
	{
 | 
			
		||||
		if (i == index)
 | 
			
		||||
		{
 | 
			
		||||
			return f->data;
 | 
			
		||||
		}
 | 
			
		||||
		f = f->next;
 | 
			
		||||
		i++;
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	return i;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int stringlist_count(struct stringlist *list)
 | 
			
		||||
{
 | 
			
		||||
	struct stringfragment* f = list->first;
 | 
			
		||||
	int i = 0;
 | 
			
		||||
 | 
			
		||||
	while (f)
 | 
			
		||||
	{
 | 
			
		||||
		f = f->next;
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* vim: set sw=4 ts=4 expandtab : */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,11 @@ struct stringlist
 | 
			
		|||
 | 
			
		||||
extern void stringlist_add(struct stringlist* list, const char* data);
 | 
			
		||||
extern void stringlist_addall(struct stringlist* list, struct stringlist* src);
 | 
			
		||||
extern void stringlist_free(struct stringlist* list);
 | 
			
		||||
extern void stringlist_free(struct stringlist* list, int freedata);
 | 
			
		||||
extern void stringlist_init(struct stringlist* list);
 | 
			
		||||
extern int stringlist_count(struct stringlist *list);
 | 
			
		||||
extern char* stringlist_get(struct stringlist *list, int index);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue