You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse_flags.c 1.0 kB

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