mirror of
https://github.com/bol-van/zapret.git
synced 2025-04-19 21:42:59 +03:00
51 lines
862 B
C
51 lines
862 B
C
#include "strpool.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#undef uthash_nonfatal_oom
|
|
#define uthash_nonfatal_oom(elt) ut_oom_recover(elt)
|
|
|
|
static bool oom=false;
|
|
static void ut_oom_recover(strpool *elem)
|
|
{
|
|
oom=true;
|
|
}
|
|
|
|
bool StrPoolAddStr(strpool **pp,const char *s)
|
|
{
|
|
strpool *elem;
|
|
if (!(elem = (strpool*)malloc(sizeof(strpool))))
|
|
return false;
|
|
if (!(elem->str = strdup(s)))
|
|
{
|
|
free(elem);
|
|
return false;
|
|
}
|
|
oom = false;
|
|
HASH_ADD_KEYPTR( hh, *pp, elem->str, strlen(elem->str), elem );
|
|
if (oom)
|
|
{
|
|
free(elem->str);
|
|
free(elem);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool StrPoolCheckStr(strpool *p,const char *s)
|
|
{
|
|
strpool *elem;
|
|
HASH_FIND_STR( p, s, elem);
|
|
return elem!=NULL;
|
|
}
|
|
|
|
void StrPoolDestroy(strpool **p)
|
|
{
|
|
strpool *elem,*tmp;
|
|
HASH_ITER(hh, *p, elem, tmp) {
|
|
free(elem->str);
|
|
HASH_DEL(*p, elem);
|
|
free(elem);
|
|
}
|
|
*p = NULL;
|
|
}
|