1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <ini.h>
#include "parseargs.h"
bool ini_handler_lineno = false;
int parseargs(int argc, char **argv)
{
while (1) {
int option_index = 0;
static const struct option long_options[] = {
{"ini_allow_multiline", required_argument, NULL, 0},
{"ini_allow_bom", required_argument, NULL, 0},
{"ini_start_comment_prefixes", required_argument, NULL, 0},
{"ini_allow_inline_comments", required_argument, NULL, 0},
{"ini_inline_comment_prefixes", required_argument, NULL, 0},
{"ini_use_stack", required_argument, NULL, 0},
{"ini_max_line", required_argument, NULL, 0},
{"ini_allow_realloc", required_argument, NULL, 0},
{"ini_initial_alloc", required_argument, NULL, 0},
{"ini_stop_on_first_error", required_argument, NULL, 0},
// {"ini_call_handler_on_new_section", required_argument, NULL, 0},
{"ini_handler_lineno", required_argument, NULL, 0},
{"ini_allow_no_value", required_argument, NULL, 0},
{0, 0, NULL, 0}
};
static const struct {
void *val;
char type;
} optargs[] = {
{&ini_allow_multiline, 'b'},
{&ini_allow_bom, 'b'},
{&ini_start_comment_prefixes, 's'},
{&ini_allow_inline_comments, 'b'},
{&ini_inline_comment_prefixes, 's'},
{&ini_use_stack, 'b'},
{&ini_max_line, 'i'},
{&ini_allow_realloc, 'b'},
{&ini_initial_alloc, 'i'},
{&ini_stop_on_first_error, 'b'},
// {&ini_call_handler_on_new_section, 'b'},
{&ini_handler_lineno, 'b'},
{&ini_allow_no_value, 'b'},
{0, 0}
};
int c = getopt_long(argc, argv, "", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
switch (optargs[option_index].type) {
case 's':
*((char **) optargs[option_index].val) = strdup(optarg);
break;
case 'i':
*((int *) optargs[option_index].val) = atoi(optarg);
break;
case 'b':
*((bool *) optargs[option_index].val) = atoi(optarg);
break;
default:
break;
}
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
return 1;
}
}
return 0;
}
|