Update copyright for 2022
[pgsql.git] / src / fe_utils / simple_list.c
blobae6c69464cd3af62bcd4579276ef2fff5762f4dc
1 /*-------------------------------------------------------------------------
3 * Simple list facilities for frontend code
5 * Data structures for simple lists of OIDs and strings. The support for
6 * these is very primitive compared to the backend's List facilities, but
7 * it's all we need in, eg, pg_dump.
10 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/fe_utils/simple_list.c
15 *-------------------------------------------------------------------------
17 #include "postgres_fe.h"
19 #include "fe_utils/simple_list.h"
23 * Append an OID to the list.
25 void
26 simple_oid_list_append(SimpleOidList *list, Oid val)
28 SimpleOidListCell *cell;
30 cell = (SimpleOidListCell *) pg_malloc(sizeof(SimpleOidListCell));
31 cell->next = NULL;
32 cell->val = val;
34 if (list->tail)
35 list->tail->next = cell;
36 else
37 list->head = cell;
38 list->tail = cell;
42 * Is OID present in the list?
44 bool
45 simple_oid_list_member(SimpleOidList *list, Oid val)
47 SimpleOidListCell *cell;
49 for (cell = list->head; cell; cell = cell->next)
51 if (cell->val == val)
52 return true;
54 return false;
58 * Append a string to the list.
60 * The given string is copied, so it need not survive past the call.
62 void
63 simple_string_list_append(SimpleStringList *list, const char *val)
65 SimpleStringListCell *cell;
67 cell = (SimpleStringListCell *)
68 pg_malloc(offsetof(SimpleStringListCell, val) + strlen(val) + 1);
70 cell->next = NULL;
71 cell->touched = false;
72 strcpy(cell->val, val);
74 if (list->tail)
75 list->tail->next = cell;
76 else
77 list->head = cell;
78 list->tail = cell;
82 * Is string present in the list?
84 * If found, the "touched" field of the first match is set true.
86 bool
87 simple_string_list_member(SimpleStringList *list, const char *val)
89 SimpleStringListCell *cell;
91 for (cell = list->head; cell; cell = cell->next)
93 if (strcmp(cell->val, val) == 0)
95 cell->touched = true;
96 return true;
99 return false;
103 * Destroy an OID list
105 void
106 simple_oid_list_destroy(SimpleOidList *list)
108 SimpleOidListCell *cell;
110 cell = list->head;
111 while (cell != NULL)
113 SimpleOidListCell *next;
115 next = cell->next;
116 pg_free(cell);
117 cell = next;
122 * Destroy a string list
124 void
125 simple_string_list_destroy(SimpleStringList *list)
127 SimpleStringListCell *cell;
129 cell = list->head;
130 while (cell != NULL)
132 SimpleStringListCell *next;
134 next = cell->next;
135 pg_free(cell);
136 cell = next;
141 * Find first not-touched list entry, if there is one.
143 const char *
144 simple_string_list_not_touched(SimpleStringList *list)
146 SimpleStringListCell *cell;
148 for (cell = list->head; cell; cell = cell->next)
150 if (!cell->touched)
151 return cell->val;
153 return NULL;
157 * Append a pointer to the list.
159 * Caller must ensure that the pointer remains valid.
161 void
162 simple_ptr_list_append(SimplePtrList *list, void *ptr)
164 SimplePtrListCell *cell;
166 cell = (SimplePtrListCell *) pg_malloc(sizeof(SimplePtrListCell));
167 cell->next = NULL;
168 cell->ptr = ptr;
170 if (list->tail)
171 list->tail->next = cell;
172 else
173 list->head = cell;
174 list->tail = cell;