2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "dynamic-string.h"
26 #define THIS_MODULE VLM_svec
30 svec_init(struct svec *svec)
38 svec_clone(struct svec *svec, const struct svec *other)
41 svec_append(svec, other);
45 svec_destroy(struct svec *svec)
52 svec_clear(struct svec *svec)
56 for (i = 0; i < svec->n; i++) {
63 svec_add(struct svec *svec, const char *name)
65 svec_add_nocopy(svec, xstrdup(name));
69 svec_del(struct svec *svec, const char *name)
73 offset = svec_find(svec, name);
74 if (offset != SIZE_MAX) {
75 free(svec->names[offset]);
76 memmove(&svec->names[offset], &svec->names[offset + 1],
77 sizeof *svec->names * (svec->n - offset - 1));
83 svec_expand(struct svec *svec)
85 if (svec->n >= svec->allocated) {
86 svec->names = x2nrealloc(svec->names, &svec->allocated,
92 svec_add_nocopy(struct svec *svec, char *name)
95 svec->names[svec->n++] = name;
99 svec_append(struct svec *svec, const struct svec *other)
102 for (i = 0; i < other->n; i++) {
103 svec_add(svec, other->names[i]);
108 svec_terminate(struct svec *svec)
111 svec->names[svec->n] = NULL;
115 compare_strings(const void *a_, const void *b_)
119 return strcmp(*a, *b);
123 svec_sort(struct svec *svec)
125 qsort(svec->names, svec->n, sizeof *svec->names, compare_strings);
129 svec_sort_unique(struct svec *svec)
136 svec_unique(struct svec *svec)
138 assert(svec_is_sorted(svec));
140 /* This algorithm is lazy and sub-optimal, but it's "obviously correct"
141 * and asymptotically optimal . */
146 svec_add(&tmp, svec->names[0]);
147 for (i = 1; i < svec->n; i++) {
148 if (strcmp(svec->names[i - 1], svec->names[i])) {
149 svec_add(&tmp, svec->names[i]);
152 svec_swap(&tmp, svec);
158 svec_compact(struct svec *svec)
162 for (i = j = 0; i < svec->n; i++) {
163 if (svec->names[i] != NULL) {
164 svec->names[j++] = svec->names[i];
171 svec_diff(const struct svec *a, const struct svec *b,
172 struct svec *a_only, struct svec *both, struct svec *b_only)
176 assert(svec_is_sorted(a));
177 assert(svec_is_sorted(b));
187 for (i = j = 0; i < a->n && j < b->n; ) {
188 int cmp = strcmp(a->names[i], b->names[j]);
191 svec_add(a_only, a->names[i]);
194 } else if (cmp > 0) {
196 svec_add(b_only, b->names[j]);
201 svec_add(both, a->names[i]);
208 for (; i < a->n; i++) {
209 svec_add(a_only, a->names[i]);
213 for (; j < b->n; j++) {
214 svec_add(b_only, b->names[j]);
220 svec_contains(const struct svec *svec, const char *name)
222 return svec_find(svec, name) != SIZE_MAX;
226 svec_find(const struct svec *svec, const char *name)
230 assert(svec_is_sorted(svec));
231 p = bsearch(&name, svec->names, svec->n, sizeof *svec->names,
233 return p ? p - svec->names : SIZE_MAX;
237 svec_is_sorted(const struct svec *svec)
241 for (i = 1; i < svec->n; i++) {
242 if (strcmp(svec->names[i - 1], svec->names[i]) > 0) {
250 svec_is_unique(const struct svec *svec)
252 return svec_get_duplicate(svec) == NULL;
256 svec_get_duplicate(const struct svec *svec)
258 assert(svec_is_sorted(svec));
261 for (i = 1; i < svec->n; i++) {
262 if (!strcmp(svec->names[i - 1], svec->names[i])) {
263 return svec->names[i];
271 svec_swap(struct svec *a, struct svec *b)
273 struct svec tmp = *a;
279 svec_print(const struct svec *svec, const char *title)
283 printf("%s:\n", title);
284 for (i = 0; i < svec->n; i++) {
285 printf("\"%s\"\n", svec->names[i]);
289 /* Breaks 'words' into words at white space, respecting shell-like quoting
290 * conventions, and appends the words to 'svec'. */
292 svec_parse_words(struct svec *svec, const char *words)
294 struct ds word = DS_EMPTY_INITIALIZER;
297 for (p = words; *p != '\0'; p = q) {
300 while (isspace((unsigned char) *p)) {
308 for (q = p; *q != '\0'; q++) {
311 } else if (*q == '\'' || *q == '"') {
313 } else if (*q == '\\' && (!quote || quote == '"')) {
316 VLOG_WARN("%s: ends in trailing backslash", words);
319 ds_put_char(&word, *q);
320 } else if (isspace((unsigned char) *q) && !quote) {
324 ds_put_char(&word, *q);
327 svec_add(svec, ds_cstr(&word));
329 VLOG_WARN("%s: word ends inside quoted string", words);
336 svec_equal(const struct svec *a, const struct svec *b)
343 for (i = 0; i < a->n; i++) {
344 if (strcmp(a->names[i], b->names[i])) {
352 svec_join(const struct svec *svec,
353 const char *delimiter, const char *terminator)
359 for (i = 0; i < svec->n; i++) {
361 ds_put_cstr(&ds, delimiter);
363 ds_put_cstr(&ds, svec->names[i]);
365 ds_put_cstr(&ds, terminator);
369 /* Breaks 's' into tokens at any character in 'delimiters', and appends each
370 * token to 'svec'. Empty tokens are not added. */
372 svec_split(struct svec *svec, const char *s_, const char *delimiters)
374 char *s = xstrdup(s_);
375 char *save_ptr = NULL;
378 for (token = strtok_r(s, delimiters, &save_ptr); token != NULL;
379 token = strtok_r(NULL, delimiters, &save_ptr)) {
380 svec_add(svec, token);
386 svec_back(const struct svec *svec)
389 return svec->names[svec->n - 1];
393 svec_pop_back(struct svec *svec)
396 free(svec->names[--svec->n]);