Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
b8956e9e | 20 | /** @file lib/vector.h @brief Dynamic array template */ |
460b9539 | 21 | |
22 | #ifndef VECTOR_H | |
23 | #define VECTOR_H | |
24 | ||
05b75f8d RK |
25 | #include "mem.h" |
26 | ||
b8956e9e RK |
27 | /** @brief Dynamic array template |
28 | * @param NAME type name | |
29 | * @param ETYPE element type | |
30 | * @param REALLOC realloc function | |
31 | * | |
32 | * Defines @c struct @p NAME as a dynamic array with element type @p | |
33 | * ETYPE. @p REALLOC should have the same signature as realloc() and | |
34 | * will be used for all memory allocation. Typically it would be | |
35 | * xrealloc() for pointer-containing element types and | |
36 | * xrealloc_noptr() for pointer-free element types. | |
37 | * | |
38 | * Clients are inspected to read the @p vec member of the structure, | |
39 | * which points to the first element, and the @p nvec member, which is | |
40 | * the number of elements. It is safe to reduce @p nvec. Do not | |
41 | * touch any other members. | |
42 | * | |
43 | * The functions defined are: | |
44 | * - NAME_init(struct NAME *v) which initializes @p v | |
45 | * - NAME_append(struct NAME *v, ETYPE value) which appends @p value to @p v | |
46 | * - NAME_terminate(struct NAME *v) which zeroes out the element beyond the last | |
47 | */ | |
460b9539 | 48 | #define VECTOR_TYPE(NAME,ETYPE,REALLOC) \ |
49 | \ | |
50 | struct NAME { \ | |
ff8ddcef | 51 | /** @brief Pointer to elements */ \ |
460b9539 | 52 | ETYPE *vec; \ |
ff8ddcef RK |
53 | /** @brief Number of elements */ \ |
54 | int nvec; \ | |
55 | /** @brief Number of slots */ \ | |
56 | int nslots; \ | |
460b9539 | 57 | }; \ |
58 | \ | |
59 | static inline void NAME##_init(struct NAME *v) { \ | |
60 | memset(v, 0, sizeof *v); \ | |
61 | } \ | |
62 | \ | |
63 | static inline void NAME##_append(struct NAME *v, ETYPE val) { \ | |
64 | if(v->nvec >= v->nslots) { \ | |
65 | v->nslots = v->nslots ? 2 * v->nslots : 16; \ | |
66 | v->vec = REALLOC(v->vec, v->nslots * sizeof(ETYPE)); \ | |
67 | } \ | |
68 | v->vec[v->nvec++] = val; \ | |
69 | } \ | |
70 | \ | |
71 | static inline void NAME##_terminate(struct NAME *v) { \ | |
72 | if(v->nvec >= v->nslots) \ | |
73 | v->vec = REALLOC(v->vec, ++v->nslots * sizeof(ETYPE)); \ | |
74 | memset(&v->vec[v->nvec], 0, sizeof (ETYPE)); \ | |
75 | } \ | |
b8956e9e RK |
76 | \ |
77 | struct vector_swallow_semicolon | |
460b9539 | 78 | |
b8956e9e RK |
79 | /** @brief A dynamic array of pointers to strings */ |
80 | VECTOR_TYPE(vector, char *, xrealloc); | |
81 | /** @brief A dynamic string */ | |
82 | VECTOR_TYPE(dynstr, char, xrealloc_noptr); | |
83 | /** @brief A dynamic unicode string */ | |
84 | VECTOR_TYPE(dynstr_ucs4, uint32_t, xrealloc_noptr); | |
8818b7fc RK |
85 | /** @brief A dynamic array of pointers to unicode string */ |
86 | VECTOR_TYPE(vector32, uint32_t *, xrealloc); | |
460b9539 | 87 | |
b8956e9e | 88 | /** @brief Append many strings to a @ref vector */ |
460b9539 | 89 | void vector_append_many(struct vector *v, char **vec, int nvec); |
90 | ||
b8956e9e | 91 | /** @brief Append @p n bytes to a @ref dynstr */ |
460b9539 | 92 | void dynstr_append_bytes(struct dynstr *v, const char *ptr, size_t n); |
93 | ||
b8956e9e | 94 | /** @brief Append a string to a @ref dynstr */ |
460b9539 | 95 | static inline void dynstr_append_string(struct dynstr *v, const char *ptr) { |
96 | dynstr_append_bytes(v, ptr, strlen(ptr)); | |
97 | } | |
98 | ||
99 | #endif /* VECTOR_H */ | |
100 | ||
101 | /* | |
102 | Local Variables: | |
103 | c-basic-offset:2 | |
104 | comment-column:40 | |
105 | End: | |
106 | */ |