1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * strlist.c - list of ordered strings, optionally made unique
41 * Create a string list. The list can be uniqizing or not.
43 struct strlist
*strlist_alloc(bool uniq
)
45 struct strlist
*list
= nasm_zalloc(sizeof(*list
));
46 list
->tailp
= &list
->head
;
52 * Append a string to a string list. Return the entry pointer, which
53 * may be a pre-existing entry for a uniqizing list.
56 static const struct strlist_entry
*
57 strlist_add_common(struct strlist
*list
, struct strlist_entry
*e
,
58 struct hash_insert
*hi
)
60 e
->offset
= list
->size
;
64 list
->tailp
= &e
->next
;
66 list
->size
+= e
->size
;
69 hash_add(hi
, e
->str
, (void *)e
);
74 const struct strlist_entry
*
75 strlist_add(struct strlist
*list
, const char *str
)
77 struct strlist_entry
*e
;
78 struct hash_insert hi
;
84 size
= strlen(str
) + 1;
86 void **dp
= hash_findb(&list
->hash
, str
, size
, &hi
);
91 /* Structure already has char[1] as EOS */
92 e
= nasm_malloc(sizeof(*e
) - 1 + size
);
94 memcpy(e
->str
, str
, size
);
96 return strlist_add_common(list
, e
, &hi
);
100 * printf() to a string list
102 const struct strlist_entry
*
103 strlist_vprintf(struct strlist
*list
, const char *fmt
, va_list ap
)
105 /* clang miscompiles offsetin() unless e is initialized here */
106 struct strlist_entry
*e
= NULL
;
107 struct hash_insert hi
;
112 e
= nasm_vaxprintf(offsetin(*e
, str
), fmt
, ap
);
113 e
->size
= nasm_last_string_size();
116 void **dp
= hash_findb(&list
->hash
, e
->str
, e
->size
, &hi
);
123 return strlist_add_common(list
, e
, &hi
);
126 const struct strlist_entry
*
127 strlist_printf(struct strlist
*list
, const char *fmt
, ...)
130 const struct strlist_entry
*e
;
133 e
= strlist_vprintf(list
, fmt
, ap
);
140 * Free a string list. Sets the pointed to pointer to NULL.
142 void strlist_free(struct strlist
**listp
)
144 struct strlist
*list
= *listp
;
145 struct strlist_entry
*e
, *tmp
;
151 hash_free(&list
->hash
);
153 list_for_each_safe(e
, tmp
, list
->head
)
161 * Search the string list for an entry. If found, return the entry pointer.
162 * Only possible on a uniqizing list.
164 const struct strlist_entry
*
165 strlist_find(const struct strlist
*list
, const char *str
)
169 nasm_assert(list
->uniq
);
171 hf
= hash_find((struct hash_table
*)&list
->hash
, str
, NULL
);
172 return hf
? *hf
: NULL
;
176 * Produce a linearized buffer containing the whole list, in order;
177 * The character "sep" is the separator between strings; this is
178 * typically either 0 or '\n'. strlist_size() will give the size of
179 * the returned buffer.
181 void *strlist_linearize(const struct strlist
*list
, char sep
)
183 const struct strlist_entry
*sl
;
184 char *buf
= nasm_malloc(list
->size
);
187 strlist_for_each(sl
, list
) {
188 p
= mempcpy(p
, sl
->str
, sl
->size
);
196 * Output a string list to a file. The separator can be any string.
198 void strlist_write(const struct strlist
*list
, const char *sep
, FILE *f
)
200 const struct strlist_entry
*sl
;
201 size_t seplen
= strlen(sep
);
203 strlist_for_each(sl
, list
) {
204 fwrite(sl
->str
, 1, sl
->size
- 1, f
);
205 fwrite(sep
, 1, seplen
, f
);