2016-10-04 16:12:28 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
|
|
|
*
|
2018-10-26 03:33:58 +08:00
|
|
|
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2016-10-04 16:12:28 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
2018-10-26 03:33:58 +08:00
|
|
|
* strlist.c - list of unique, ordered strings
|
2016-10-04 16:12:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "strlist.h"
|
|
|
|
|
2018-10-26 03:33:58 +08:00
|
|
|
/*
|
|
|
|
* Create a string list
|
|
|
|
*/
|
2018-11-12 02:33:52 +08:00
|
|
|
struct strlist *strlist_alloc(void)
|
2016-10-04 16:12:28 +08:00
|
|
|
{
|
2018-11-12 02:33:52 +08:00
|
|
|
struct strlist *list = nasm_zalloc(sizeof(*list));
|
|
|
|
hash_init(&list->hash, HASH_MEDIUM);
|
|
|
|
list->tailp = &list->head;
|
|
|
|
return list;
|
2016-10-04 16:12:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-10-26 03:33:58 +08:00
|
|
|
* Append a string to a string list if and only if it isn't
|
2016-10-04 16:12:28 +08:00
|
|
|
* already there. Return true if it was added.
|
|
|
|
*/
|
2018-11-12 02:33:52 +08:00
|
|
|
bool strlist_add(struct strlist *list, const char *str)
|
2016-10-04 16:12:28 +08:00
|
|
|
{
|
2018-11-12 02:33:52 +08:00
|
|
|
struct strlist_entry *e;
|
|
|
|
struct hash_insert hi;
|
|
|
|
size_t len;
|
2016-10-04 16:12:28 +08:00
|
|
|
|
2018-11-12 02:33:52 +08:00
|
|
|
if (!list)
|
|
|
|
return false;
|
2016-10-04 16:12:28 +08:00
|
|
|
|
2018-11-12 02:33:52 +08:00
|
|
|
if (hash_find(&list->hash, str, &hi))
|
|
|
|
return false;
|
2018-10-26 03:33:58 +08:00
|
|
|
|
2018-11-12 02:33:52 +08:00
|
|
|
len = strlen(str);
|
2016-10-04 16:12:28 +08:00
|
|
|
|
2018-11-12 02:33:52 +08:00
|
|
|
/* Structure already has char[1] as EOS */
|
|
|
|
e = nasm_zalloc(sizeof(*e) + len);
|
|
|
|
e->len = len;
|
|
|
|
memcpy(e->str, str, len + 1);
|
2018-10-26 03:33:58 +08:00
|
|
|
|
2018-11-12 02:33:52 +08:00
|
|
|
*list->tailp = e;
|
|
|
|
list->tailp = &e->next;
|
|
|
|
|
|
|
|
hash_add(&hi, e->str, (void *)e);
|
|
|
|
return true;
|
2016-10-04 16:12:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-10-26 03:33:58 +08:00
|
|
|
* Free a string list
|
2016-10-04 16:12:28 +08:00
|
|
|
*/
|
2018-11-12 02:33:52 +08:00
|
|
|
void strlist_free(struct strlist *list)
|
2016-10-04 16:12:28 +08:00
|
|
|
{
|
2018-11-12 02:33:52 +08:00
|
|
|
if (list) {
|
|
|
|
hash_free_all(&list->hash, false);
|
|
|
|
nasm_free(list);
|
|
|
|
}
|
2016-10-04 16:12:28 +08:00
|
|
|
}
|