X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=argpar%2Fargpar.h;h=00334cd6fb9d6f0c14a7fc08f5a21d114e778e65;hp=85a663dc738d5a9401d5e3734a4fc4693e09b006;hb=03e1579f42a13b5e8ccf26bcc55fa5c23ba9f313;hpb=903a5b8ab5ab38d3b200b1d692ba0d29d080c92c diff --git a/argpar/argpar.h b/argpar/argpar.h index 85a663d..00334cd 100644 --- a/argpar/argpar.h +++ b/argpar/argpar.h @@ -1,38 +1,32 @@ -#ifndef BABELTRACE_ARGPAR_H -#define BABELTRACE_ARGPAR_H - /* - * Copyright 2019 Philippe Proulx + * SPDX-License-Identifier: MIT * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Copyright 2019 Philippe Proulx */ -#include -#include +#ifndef BABELTRACE_ARGPAR_H +#define BABELTRACE_ARGPAR_H -#include "common/macros.h" +#include /* Sentinel for an option descriptor array */ -#define BT_ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } +#define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } + +/* + * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't want them + * to be exported by that library, so mark them as "hidden". + * + * On Windows, symbols are local unless explicitly exported, + * see https://gcc.gnu.org/wiki/Visibility + */ +#if defined(_WIN32) || defined(__CYGWIN__) +#define ARGPAR_HIDDEN +#else +#define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) +#endif /* Option descriptor */ -struct bt_argpar_opt_descr { +struct argpar_opt_descr { /* Numeric ID for this option */ const int id; @@ -47,33 +41,33 @@ struct bt_argpar_opt_descr { }; /* Item type */ -enum bt_argpar_item_type { +enum argpar_item_type { /* Option */ - BT_ARGPAR_ITEM_TYPE_OPT, + ARGPAR_ITEM_TYPE_OPT, /* Non-option */ - BT_ARGPAR_ITEM_TYPE_NON_OPT, + ARGPAR_ITEM_TYPE_NON_OPT, }; /* Base item */ -struct bt_argpar_item { - enum bt_argpar_item_type type; +struct argpar_item { + enum argpar_item_type type; }; /* Option item */ -struct bt_argpar_item_opt { - struct bt_argpar_item base; +struct argpar_item_opt { + struct argpar_item base; /* Corresponding descriptor */ - const struct bt_argpar_opt_descr *descr; + const struct argpar_opt_descr *descr; /* Argument, or `NULL` if none */ const char *arg; }; /* Non-option item */ -struct bt_argpar_item_non_opt { - struct bt_argpar_item base; +struct argpar_item_non_opt { + struct argpar_item base; /* * Complete argument, pointing to one of the entries of the @@ -88,13 +82,24 @@ struct bt_argpar_item_non_opt { unsigned int non_opt_index; }; -/* What is returned by bt_argpar_parse() */ -struct bt_argpar_parse_ret { - /* Array of `struct bt_argpar_item *`, or `NULL` on error */ - GPtrArray *items; +struct argpar_item_array { + /* Array of `struct argpar_item *`, or `NULL` on error */ + struct argpar_item **items; + + /* Number of used slots in `items`. */ + unsigned int n_items; + + /* Number of allocated slots in `items`. */ + unsigned int n_alloc; +}; + +/* What is returned by argpar_parse() */ +struct argpar_parse_ret { + /* Array of `struct argpar_item *`, or `NULL` on error */ + struct argpar_item_array *items; /* Error string, or `NULL` if none */ - GString *error; + char *error; /* Number of original arguments (`argv`) ingested */ unsigned int ingested_orig_args; @@ -102,7 +107,7 @@ struct bt_argpar_parse_ret { /* * Parses the arguments `argv` of which the count is `argc` using the - * sentinel-terminated (use `BT_ARGPAR_OPT_DESCR_SENTINEL`) option + * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option * descriptor array `descrs`. * * This function considers ALL the elements of `argv`, including the @@ -142,9 +147,9 @@ struct bt_argpar_parse_ret { * contains one entry for each instance). * * On success, this function returns an array of items - * (`struct bt_argpar_item *`). Each item is to be casted to the - * appropriate type (`struct bt_argpar_item_opt *` or - * `struct bt_argpar_item_non_opt *`) depending on its type. + * (`struct argpar_item *`). Each item is to be casted to the + * appropriate type (`struct argpar_item_opt *` or + * `struct argpar_item_non_opt *`) depending on its type. * * The returned array contains the items in the same order that the * arguments were parsed, including non-option arguments. This means, @@ -192,21 +197,21 @@ struct bt_argpar_parse_ret { * the `error` string member contains details about the error. * * You can finalize the returned structure with - * bt_argpar_parse_ret_fini(). + * argpar_parse_ret_fini(). */ -BT_HIDDEN -struct bt_argpar_parse_ret bt_argpar_parse(unsigned int argc, +ARGPAR_HIDDEN +struct argpar_parse_ret argpar_parse(unsigned int argc, const char * const *argv, - const struct bt_argpar_opt_descr *descrs, + const struct argpar_opt_descr *descrs, bool fail_on_unknown_opt); /* - * Finalizes what is returned by bt_argpar_parse(). + * Finalizes what is returned by argpar_parse(). * - * It is safe to call bt_argpar_parse() multiple times with the same + * It is safe to call argpar_parse() multiple times with the same * structure. */ -BT_HIDDEN -void bt_argpar_parse_ret_fini(struct bt_argpar_parse_ret *ret); +ARGPAR_HIDDEN +void argpar_parse_ret_fini(struct argpar_parse_ret *ret); #endif /* BABELTRACE_ARGPAR_H */