]>
git.proxmox.com Git - ovs.git/blob - lib/svec.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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.
22 #include "dynamic-string.h"
24 #include "openvswitch/vlog.h"
26 VLOG_DEFINE_THIS_MODULE(svec
);
29 svec_init(struct svec
*svec
)
37 svec_clone(struct svec
*svec
, const struct svec
*other
)
40 svec_append(svec
, other
);
44 svec_destroy(struct svec
*svec
)
51 svec_clear(struct svec
*svec
)
55 for (i
= 0; i
< svec
->n
; i
++) {
62 svec_is_empty(const struct svec
*svec
)
68 svec_add(struct svec
*svec
, const char *name
)
70 svec_add_nocopy(svec
, xstrdup(name
));
74 svec_del(struct svec
*svec
, const char *name
)
78 offset
= svec_find(svec
, name
);
79 if (offset
!= SIZE_MAX
) {
80 free(svec
->names
[offset
]);
81 memmove(&svec
->names
[offset
], &svec
->names
[offset
+ 1],
82 sizeof *svec
->names
* (svec
->n
- offset
- 1));
88 svec_expand(struct svec
*svec
)
90 if (svec
->n
>= svec
->allocated
) {
91 svec
->names
= x2nrealloc(svec
->names
, &svec
->allocated
,
97 svec_add_nocopy(struct svec
*svec
, char *name
)
100 svec
->names
[svec
->n
++] = name
;
104 svec_append(struct svec
*svec
, const struct svec
*other
)
107 for (i
= 0; i
< other
->n
; i
++) {
108 svec_add(svec
, other
->names
[i
]);
113 svec_terminate(struct svec
*svec
)
116 svec
->names
[svec
->n
] = NULL
;
120 compare_strings(const void *a_
, const void *b_
)
124 return strcmp(*a
, *b
);
128 svec_sort(struct svec
*svec
)
130 qsort(svec
->names
, svec
->n
, sizeof *svec
->names
, compare_strings
);
134 svec_sort_unique(struct svec
*svec
)
141 svec_unique(struct svec
*svec
)
143 ovs_assert(svec_is_sorted(svec
));
145 /* This algorithm is lazy and sub-optimal, but it's "obviously correct"
146 * and asymptotically optimal . */
151 svec_add(&tmp
, svec
->names
[0]);
152 for (i
= 1; i
< svec
->n
; i
++) {
153 if (strcmp(svec
->names
[i
- 1], svec
->names
[i
])) {
154 svec_add(&tmp
, svec
->names
[i
]);
157 svec_swap(&tmp
, svec
);
163 svec_compact(struct svec
*svec
)
167 for (i
= j
= 0; i
< svec
->n
; i
++) {
168 if (svec
->names
[i
] != NULL
) {
169 svec
->names
[j
++] = svec
->names
[i
];
176 svec_diff(const struct svec
*a
, const struct svec
*b
,
177 struct svec
*a_only
, struct svec
*both
, struct svec
*b_only
)
181 ovs_assert(svec_is_sorted(a
));
182 ovs_assert(svec_is_sorted(b
));
192 for (i
= j
= 0; i
< a
->n
&& j
< b
->n
; ) {
193 int cmp
= strcmp(a
->names
[i
], b
->names
[j
]);
196 svec_add(a_only
, a
->names
[i
]);
199 } else if (cmp
> 0) {
201 svec_add(b_only
, b
->names
[j
]);
206 svec_add(both
, a
->names
[i
]);
213 for (; i
< a
->n
; i
++) {
214 svec_add(a_only
, a
->names
[i
]);
218 for (; j
< b
->n
; j
++) {
219 svec_add(b_only
, b
->names
[j
]);
225 svec_contains(const struct svec
*svec
, const char *name
)
227 return svec_find(svec
, name
) != SIZE_MAX
;
231 svec_find(const struct svec
*svec
, const char *name
)
235 ovs_assert(svec_is_sorted(svec
));
236 p
= bsearch(&name
, svec
->names
, svec
->n
, sizeof *svec
->names
,
238 return p
? p
- svec
->names
: SIZE_MAX
;
242 svec_is_sorted(const struct svec
*svec
)
246 for (i
= 1; i
< svec
->n
; i
++) {
247 if (strcmp(svec
->names
[i
- 1], svec
->names
[i
]) > 0) {
255 svec_is_unique(const struct svec
*svec
)
257 return svec_get_duplicate(svec
) == NULL
;
261 svec_get_duplicate(const struct svec
*svec
)
263 ovs_assert(svec_is_sorted(svec
));
266 for (i
= 1; i
< svec
->n
; i
++) {
267 if (!strcmp(svec
->names
[i
- 1], svec
->names
[i
])) {
268 return svec
->names
[i
];
276 svec_swap(struct svec
*a
, struct svec
*b
)
278 struct svec tmp
= *a
;
284 svec_print(const struct svec
*svec
, const char *title
)
288 printf("%s:\n", title
);
289 for (i
= 0; i
< svec
->n
; i
++) {
290 printf("\"%s\"\n", svec
->names
[i
]);
294 /* Breaks 'words' into words at white space, respecting shell-like quoting
295 * conventions, and appends the words to 'svec'. */
297 svec_parse_words(struct svec
*svec
, const char *words
)
299 struct ds word
= DS_EMPTY_INITIALIZER
;
302 for (p
= words
; *p
!= '\0'; p
= q
) {
305 while (isspace((unsigned char) *p
)) {
313 for (q
= p
; *q
!= '\0'; q
++) {
316 } else if (*q
== '\'' || *q
== '"') {
318 } else if (*q
== '\\' && (!quote
|| quote
== '"')) {
321 VLOG_WARN("%s: ends in trailing backslash", words
);
324 ds_put_char(&word
, *q
);
325 } else if (isspace((unsigned char) *q
) && !quote
) {
329 ds_put_char(&word
, *q
);
332 svec_add(svec
, ds_cstr(&word
));
334 VLOG_WARN("%s: word ends inside quoted string", words
);
341 svec_equal(const struct svec
*a
, const struct svec
*b
)
348 for (i
= 0; i
< a
->n
; i
++) {
349 if (strcmp(a
->names
[i
], b
->names
[i
])) {
357 svec_join(const struct svec
*svec
,
358 const char *delimiter
, const char *terminator
)
364 for (i
= 0; i
< svec
->n
; i
++) {
366 ds_put_cstr(&ds
, delimiter
);
368 ds_put_cstr(&ds
, svec
->names
[i
]);
370 ds_put_cstr(&ds
, terminator
);
375 svec_back(const struct svec
*svec
)
378 return svec
->names
[svec
->n
- 1];
382 svec_pop_back(struct svec
*svec
)
385 free(svec
->names
[--svec
->n
]);