Merge pull request #267 from davidgiven/dtrg-fixes

More warning fixes.
This commit is contained in:
David Given 2023-01-08 13:02:19 +01:00 committed by GitHub
commit 278ed3cf26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 35 deletions

View file

@ -1,6 +1,9 @@
#ifndef PMAP_H #ifndef PMAP_H
#define PMAP_H #define PMAP_H
/* A bidirectional multimap, storing (left, right) tuples.
*/
struct pmap_node struct pmap_node
{ {
void* left; void* left;
@ -23,10 +26,33 @@ struct pmap
int max; \ int max; \
} }
/** Adds a new relation `(left -> right)` to the map
* if `left` is not present. Otherwise replaces the
* `right` associated with the first `left`.
*
* Use this in normal map mode.
*/
extern void pmap_put(void* map, void* left, void* right); extern void pmap_put(void* map, void* left, void* right);
/** Adds a new relation to the map if that relation does
* not already exist.
*
* Use this in multimap mode.
*/
extern void pmap_add(void* map, void* left, void* right); extern void pmap_add(void* map, void* left, void* right);
/** Removes a relation from the map.
*
* Use this in multimap mode.
*/
extern void pmap_remove(void* map, void* left, void* right); extern void pmap_remove(void* map, void* left, void* right);
/** Given a `left`, find the first matching `right`.
*/
extern void* pmap_findleft(void* map, void* left); extern void* pmap_findleft(void* map, void* left);
/** Given a `right`, find the first matching `left`.
*/
extern void* pmap_findright(void* map, void* right); extern void* pmap_findright(void* map, void* right);
#endif #endif

View file

@ -26,7 +26,7 @@ static void append(void* mapp, const char* key, void* value)
} }
void smap_put(struct smap *mapp, const char* key, void* value) void smap_put(void *mapp, const char* key, void* value)
{ {
struct smap* map = mapp; struct smap* map = mapp;
int i; int i;
@ -44,34 +44,36 @@ void smap_put(struct smap *mapp, const char* key, void* value)
append(map, key, value); append(map, key, value);
} }
void smap_init(struct smap *mapp) void smap_init(void *mapp)
{ {
mapp->count = 0; struct smap* map = mapp;
mapp->item = NULL; map->count = 0;
mapp->max = 0; map->item = NULL;
map->max = 0;
} }
void smap_free(struct smap *mapp, int free_key, int free_value) void smap_free(void *mapp, int free_key, int free_value)
{ {
struct smap* map = mapp;
int i; int i;
for (i=0; i<mapp->count; i++) for (i=0; i<map->count; i++)
{ {
struct smap_node* node = &mapp->item[i]; struct smap_node* node = &map->item[i];
if (free_key) if (free_key)
{ {
free(node->left); free((void*) node->left);
} }
if (free_value) if (free_value)
{ {
free(node->right); free(node->right);
} }
} }
mapp->count = 0; map->count = 0;
free(mapp->item); free(map->item);
} }
void smap_add(struct smap *mapp, const char* key, void* value) void smap_add(void *mapp, const char* key, void* value)
{ {
struct smap* map = mapp; struct smap* map = mapp;
int i; int i;
@ -86,7 +88,7 @@ void smap_add(struct smap *mapp, const char* key, void* value)
append(map, key, value); append(map, key, value);
} }
void* smap_get(struct smap *mapp, const char* left) void* smap_get(void *mapp, const char* left)
{ {
struct smap* map = mapp; struct smap* map = mapp;
int i; int i;

View file

@ -6,9 +6,11 @@
#ifndef SMAP_H #ifndef SMAP_H
#define SMAP_H #define SMAP_H
/* A string multimap (each key can have multiple values). */
struct smap_node struct smap_node
{ {
char* left; const char* left;
void* right; void* right;
}; };
@ -23,36 +25,45 @@ struct smap
#define SMAPOF(RIGHT) \ #define SMAPOF(RIGHT) \
struct { \ struct { \
struct { char* left; RIGHT* right; }* item; \ struct { const char* left; RIGHT* right; }* item; \
int count; \ int count; \
int max; \ int max; \
} }
/** Initializes a string map and returns the initialized /** Initializes a string map. Optional; a zero-initialised map
* handle in `mapp`. * structure is a valid empty map.
*/ */
extern void smap_init(struct smap *mapp); extern void smap_init(void *mapp);
/** Adds a new item with name `key` in the string map if
* it does not already exist, otherwise replaces the /** Adds a new item with name `key` in the string map if it does not already
* value `value` associated with the existing `key`. * exist. Otherwise replaces the value `value` associated with the existing
*/ * `key`.
extern void smap_put(struct smap *mapp, const 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, const char* key, void* value);
/** Returns the value associated with the specified `key`, returns
* NULL if `key` is not present in the string map.
* *
* Use this for normal map mode.
*/ */
extern void* smap_get(struct smap* mapp, const char* key); extern void smap_put(void *mapp, const char* key, void* value);
/** Adds a new item with name `key` and value `value`. If that relation already
* exists, nothing happens.
*
* Use this for multimap mode.
*/
extern void smap_add(void *mapp, const char* key, void* value);
/** Returns the first value associated with the specified `key`. Returns NULL
* if `key` is not present in the string map.
*
* Use this in normal map mode.
*/
extern void* smap_get(void* mapp, const char* key);
/** Frees the data structure associated with the string map. /** Frees the data structure associated with the string map.
* Also frees the memory associated with the key if * Also frees the memory associated with the key if
* `free_key` is non-zero, and free the memory associated * `free_key` is non-zero, and free the memory associated
* with the value if `free_value` is non-zero. * with the value if `free_value` is non-zero.
* *
*/ */
extern void smap_free(struct smap *mapp, int free_key, int free_value); extern void smap_free(void *mapp, int free_key, int free_value);
#endif #endif

View file

@ -39,7 +39,7 @@ void stringlist_free(struct stringlist* list, int freedata)
struct stringfragment* next = f->next; struct stringfragment* next = f->next;
if (freedata) if (freedata)
{ {
free(f->data); free((void*) f->data);
f->data = NULL; f->data = NULL;
} }
free(f); free(f);
@ -54,7 +54,7 @@ void stringlist_init(struct stringlist* list)
} }
char* stringlist_get(struct stringlist *list, int index) const char* stringlist_get(struct stringlist *list, int index)
{ {
struct stringfragment* f = list->first; struct stringfragment* f = list->first;
int i = 0; int i = 0;

View file

@ -3,7 +3,7 @@
struct stringfragment struct stringfragment
{ {
char* data; const char* data;
struct stringfragment* next; struct stringfragment* next;
}; };
@ -18,7 +18,7 @@ extern void stringlist_addall(struct stringlist* list, struct stringlist* src);
extern void stringlist_free(struct stringlist* list, int freedata); extern void stringlist_free(struct stringlist* list, int freedata);
extern void stringlist_init(struct stringlist* list); extern void stringlist_init(struct stringlist* list);
extern int stringlist_count(struct stringlist *list); extern int stringlist_count(struct stringlist *list);
extern char* stringlist_get(struct stringlist *list, int index); extern const char* stringlist_get(struct stringlist *list, int index);
#endif #endif