PostGIS  3.4.0dev-r@@SVN_REVISION@@
stringlist.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2021 Paul Ramsey <pramsey@cleverelephant.ca>
22  *
23  **********************************************************************/
24 
25 #include "liblwgeom_internal.h"
26 #include "stringlist.h"
27 
28 static size_t
30 {
31  return capacity * sizeof(char*);
32 }
33 
34 static void
36 {
37  s->capacity = size;
38  s->length = 0;
39  s->data = lwalloc(stringlist_capacity_in_bytes(s->capacity));
40  memset(s->data, 0, stringlist_capacity_in_bytes(s->capacity));
41 }
42 
43 void
45 {
47 }
48 
49 void
51 {
52  size_t i;
53  if (!s || !s->data) return;
54  for (i = 0; i < s->length; i++)
55  if (s->data[i]) lwfree(s->data[i]);
56  lwfree(s->data);
57  memset(s, 0, sizeof(stringlist_t));
58 }
59 
60 
63 {
64  stringlist_t *s = lwalloc(sizeof(stringlist_t));
65  memset(s, 0, sizeof(stringlist_t));
67  return s;
68 }
69 
72 {
74 }
75 
76 void
78 {
80  lwfree(s);
81 }
82 
83 static int
84 stringlist_cmp(const void *a, const void *b)
85 {
86  const char **ia = (const char **)a;
87  const char **ib = (const char **)b;
88  return strcmp(*ia, *ib);
89 }
90 
91 static void
92 stringlist_add_string_internal(stringlist_t *s, const char* string, int dosort)
93 {
94  if (!string) return;
95  if (s->capacity == 0)
96  {
98  }
99  if (s->length == s->capacity)
100  {
101  s->capacity *= 2;
102  s->data = lwrealloc(s->data, stringlist_capacity_in_bytes(s->capacity));
103  };
104  s->data[s->length++] = lwstrdup(string);
105  if (dosort)
107  return;
108 }
109 
110 void
111 stringlist_add_string(stringlist_t *s, const char* string)
112 {
113  stringlist_add_string_internal(s, string, 1);
114 }
115 
116 void
118 {
119  stringlist_add_string_internal(s, string, 0);
120 }
121 
122 void
124 {
125  qsort(s->data, s->length, sizeof(char*), stringlist_cmp);
126 }
127 
128 const char *
129 stringlist_find(stringlist_t *s, const char *key)
130 {
131  char ** rslt = bsearch(&key, s->data, s->length, sizeof(char*), stringlist_cmp);
132  if (! rslt) return NULL;
133  return *rslt;
134 }
135 
136 size_t
138 {
139  return s->length;
140 }
141 
142 const char *
144 {
145  if (i < s->length)
146  return s->data[i];
147  return NULL;
148 }
char * s
Definition: cu_in_wkt.c:23
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
char * lwstrdup(const char *a)
Definition: lwutil.c:248
static int stringlist_cmp(const void *a, const void *b)
Definition: stringlist.c:84
void stringlist_init(stringlist_t *s)
Definition: stringlist.c:44
size_t stringlist_length(stringlist_t *s)
Definition: stringlist.c:137
void stringlist_release(stringlist_t *s)
Definition: stringlist.c:50
const char * stringlist_find(stringlist_t *s, const char *key)
Definition: stringlist.c:129
static size_t stringlist_capacity_in_bytes(size_t capacity)
Definition: stringlist.c:29
void stringlist_add_string_nosort(stringlist_t *s, const char *string)
Definition: stringlist.c:117
const char * stringlist_get(stringlist_t *s, size_t i)
Definition: stringlist.c:143
static void stringlist_add_string_internal(stringlist_t *s, const char *string, int dosort)
Definition: stringlist.c:92
void stringlist_add_string(stringlist_t *s, const char *string)
Definition: stringlist.c:111
static void stringlist_init_with_size(stringlist_t *s, size_t size)
Definition: stringlist.c:35
void stringlist_sort(stringlist_t *s)
Definition: stringlist.c:123
void stringlist_destroy(stringlist_t *s)
Definition: stringlist.c:77
stringlist_t * stringlist_create_with_size(size_t size)
Definition: stringlist.c:62
stringlist_t * stringlist_create(void)
Definition: stringlist.c:71
#define STRINGLIST_STARTSIZE
Definition: stringlist.h:37