2008-10-02 19:34:42 +02:00
|
|
|
/*
|
2019-01-15 18:28:24 +01:00
|
|
|
SPDX-License-Identifier: GPL-2.0-only
|
2008-10-02 19:34:42 +02:00
|
|
|
|
2019-01-15 18:28:24 +01:00
|
|
|
Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>
|
2008-10-02 19:34:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "strings.h"
|
2008-10-02 22:06:01 +02:00
|
|
|
#include "gobuffer.h"
|
2008-10-02 19:34:42 +02:00
|
|
|
|
|
|
|
#include <search.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include "dutil.h"
|
|
|
|
|
|
|
|
struct strings *strings__new(void)
|
|
|
|
{
|
2012-08-17 23:47:15 +02:00
|
|
|
struct strings *strs = malloc(sizeof(*strs));
|
2008-10-02 19:34:42 +02:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
if (strs != NULL) {
|
|
|
|
strs->tree = NULL;
|
|
|
|
gobuffer__init(&strs->gb);
|
2008-10-02 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
return strs;
|
2008-10-02 19:34:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_nothing(void *ptr __unused)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
void strings__delete(struct strings *strs)
|
2008-10-02 19:34:42 +02:00
|
|
|
{
|
2012-08-17 23:47:15 +02:00
|
|
|
if (strs == NULL)
|
2009-03-20 14:35:57 +01:00
|
|
|
return;
|
2012-08-17 23:47:15 +02:00
|
|
|
tdestroy(strs->tree, do_nothing);
|
|
|
|
__gobuffer__delete(&strs->gb);
|
|
|
|
free(strs);
|
2008-10-02 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
static strings_t strings__insert(struct strings *strs, const char *s)
|
2008-10-02 19:34:42 +02:00
|
|
|
{
|
2012-08-17 23:47:15 +02:00
|
|
|
return gobuffer__add(&strs->gb, s, strlen(s) + 1);
|
2008-10-02 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct search_key {
|
2012-08-17 23:47:15 +02:00
|
|
|
struct strings *strs;
|
2008-10-02 19:34:42 +02:00
|
|
|
const char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int strings__compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct search_key *key = a;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
return strcmp(key->str, key->strs->gb.entries + (unsigned long)b);
|
2008-10-02 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
strings_t strings__add(struct strings *strs, const char *str)
|
2008-10-02 19:34:42 +02:00
|
|
|
{
|
2009-05-04 20:40:44 +02:00
|
|
|
unsigned long *s;
|
2008-10-02 19:34:42 +02:00
|
|
|
strings_t index;
|
|
|
|
struct search_key key = {
|
2012-08-17 23:47:15 +02:00
|
|
|
.strs = strs,
|
2008-10-02 19:34:42 +02:00
|
|
|
.str = str,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
s = tsearch(&key, &strs->tree, strings__compare);
|
2008-10-02 19:34:42 +02:00
|
|
|
if (s != NULL) {
|
|
|
|
if (*(struct search_key **)s == (void *)&key) { /* Not found, replace with the right key */
|
2012-08-17 23:47:15 +02:00
|
|
|
index = strings__insert(strs, str);
|
2008-10-02 19:34:42 +02:00
|
|
|
if (index != 0)
|
2009-05-04 20:40:44 +02:00
|
|
|
*s = (unsigned long)index;
|
2008-10-02 19:34:42 +02:00
|
|
|
else {
|
2012-08-17 23:47:15 +02:00
|
|
|
tdelete(&key, &strs->tree, strings__compare);
|
2008-10-02 19:34:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else /* Found! */
|
|
|
|
index = *s;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
2008-10-24 18:20:37 +02:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
strings_t strings__find(struct strings *strs, const char *str)
|
2008-10-24 18:20:37 +02:00
|
|
|
{
|
|
|
|
strings_t *s;
|
|
|
|
struct search_key key = {
|
2012-08-17 23:47:15 +02:00
|
|
|
.strs = strs,
|
2008-10-24 18:20:37 +02:00
|
|
|
.str = str,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
s = tfind(&key, &strs->tree, strings__compare);
|
2008-10-24 18:20:37 +02:00
|
|
|
return s ? *s : 0;
|
|
|
|
}
|
2009-03-14 17:50:36 +01:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
int strings__cmp(const struct strings *strs, strings_t a, strings_t b)
|
2008-10-24 18:20:37 +02:00
|
|
|
{
|
2012-08-17 23:47:15 +02:00
|
|
|
return a == b ? 0 : strcmp(strings__ptr(strs, a),
|
|
|
|
strings__ptr(strs, b));
|
2008-10-24 18:20:37 +02:00
|
|
|
}
|