1#include "config.h" 2 3#include <stdio.h> 4#include <string.h> 5 6#include "json.h" 7#include "parse_flags.h" 8 9#if !defined(HAVE_STRCASECMP) && defined(_MSC_VER) 10# define strcasecmp _stricmp 11#elif !defined(HAVE_STRCASECMP) 12# error You do not have strcasecmp on your system. 13#endif /* HAVE_STRNCASECMP */ 14 15static struct { 16 const char *arg; 17 int flag; 18} format_args[] = { 19 { "plain", JSON_C_TO_STRING_PLAIN }, 20 { "spaced", JSON_C_TO_STRING_SPACED }, 21 { "pretty", JSON_C_TO_STRING_PRETTY }, 22}; 23 24#ifndef NELEM 25#define NELEM(x) (sizeof(x) / sizeof(&x[0])) 26#endif 27 28int parse_flags(int argc, char **argv) 29{ 30 int arg_idx; 31 int sflags = 0; 32 for (arg_idx = 1; arg_idx < argc ; arg_idx++) 33 { 34 int jj; 35 for (jj = 0; jj < (int)NELEM(format_args); jj++) 36 { 37 if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0) 38 { 39 sflags |= format_args[jj].flag; 40 break; 41 } 42 } 43 if (jj == NELEM(format_args)) 44 { 45 printf("Unknown arg: %s\n", argv[arg_idx]); 46 exit(1); 47 } 48 } 49 return sflags; 50} 51