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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. {
  13. const char *arg;
  14. int flag;
  15. } format_args[] = {
  16. {"plain", JSON_C_TO_STRING_PLAIN},
  17. {"spaced", JSON_C_TO_STRING_SPACED},
  18. {"pretty", JSON_C_TO_STRING_PRETTY},
  19. {"pretty_tab", JSON_C_TO_STRING_PRETTY_TAB},
  20. };
  21. #ifndef NELEM
  22. #define NELEM(x) (sizeof(x) / sizeof(x[0]))
  23. #endif
  24. int parse_flags(int argc, char **argv)
  25. {
  26. int arg_idx;
  27. int sflags = 0;
  28. for (arg_idx = 1; arg_idx < argc; arg_idx++)
  29. {
  30. int jj;
  31. for (jj = 0; jj < (int)NELEM(format_args); jj++)
  32. {
  33. if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0)
  34. {
  35. sflags |= format_args[jj].flag;
  36. break;
  37. }
  38. }
  39. if (jj == NELEM(format_args))
  40. {
  41. printf("Unknown arg: %s\n", argv[arg_idx]);
  42. exit(1);
  43. }
  44. }
  45. return sflags;
  46. }