X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_strvec.h;h=8e14973b40123ee646ac9b882dc97840e8286114;hb=038508b2212449e82ff8ace9eae8742d99ed0186;hp=6d2a5b472e3e976fab0c3fe8ac03c25608b8c5fb;hpb=d3f82a7571b394d8e9b6f2791219eb7ad67d6597;p=protos%2Flibecoli.git diff --git a/lib/ecoli_strvec.h b/lib/ecoli_strvec.h index 6d2a5b4..8e14973 100644 --- a/lib/ecoli_strvec.h +++ b/lib/ecoli_strvec.h @@ -1,28 +1,5 @@ -/* - * Copyright (c) 2016, Olivier MATZ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the University of California, Berkeley nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ */ /** @@ -46,6 +23,40 @@ */ struct ec_strvec *ec_strvec(void); +#ifndef EC_COUNT_OF +#define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \ + ((size_t)(!(sizeof(x) % sizeof(0[x]))))) +#endif + +/** + * Allocate a new string vector + * + * The string vector is initialized with the list of const strings + * passed as arguments. + * + * @return + * The new strvec object, or NULL on error (errno is set). + */ +#define EC_STRVEC(args...) ({ \ + const char *_arr[] = {args}; \ + ec_strvec_from_array(_arr, EC_COUNT_OF(_arr)); \ + }) +/** + * Allocate a new string vector + * + * The string vector is initialized with the array of const strings + * passed as arguments. + * + * @param strarr + * The array of const strings. + * @param n + * The number of strings in the array. + * @return + * The new strvec object, or NULL on error (errno is set). + */ +struct ec_strvec *ec_strvec_from_array(const char * const *strarr, + size_t n); + /** * Add a string in a vector. * @@ -58,6 +69,18 @@ struct ec_strvec *ec_strvec(void); */ int ec_strvec_add(struct ec_strvec *strvec, const char *s); +/** + * Delete the last entry in the string vector. + * + * @param strvec + * The pointer to the string vector. + * @param s + * The string to be added at the end of the vector. + * @return + * 0 on success or -1 on error (errno is set). + */ +int ec_strvec_del_last(struct ec_strvec *strvec); + /** * Duplicate a string vector. * @@ -114,6 +137,30 @@ size_t ec_strvec_len(const struct ec_strvec *strvec); */ const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx); +/** + * Compare two string vectors + * + * @param strvec + * The pointer to the first string vector. + * @param strvec + * The pointer to the second string vector. + * @return + * 0 if the string vectors are equal. + */ +int ec_strvec_cmp(const struct ec_strvec *strvec1, + const struct ec_strvec *strvec2); + +/** + * Sort the string vector. + * + * @param strvec + * The pointer to the first string vector. + * @param str_cmp + * The sort function to use. If NULL, use strcmp. + */ +void ec_strvec_sort(struct ec_strvec *strvec, + int (*str_cmp)(const char *s1, const char *s2)); + /** * Dump a string vector. *