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.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include "config.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "json.h"
  8. #include "parse_flags.h"
  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. static struct
  15. {
  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. {"pretty_tab", JSON_C_TO_STRING_PRETTY_TAB},
  23. };
  24. #ifndef NELEM
  25. #define NELEM(x) (sizeof(x) / sizeof(x[0]))
  26. #endif
  27. int parse_flags(int argc, char **argv)
  28. {
  29. int arg_idx;
  30. int sflags = 0;
  31. for (arg_idx = 1; arg_idx < argc; arg_idx++)
  32. {
  33. int jj;
  34. for (jj = 0; jj < (int)NELEM(format_args); jj++)
  35. {
  36. if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0)
  37. {
  38. sflags |= format_args[jj].flag;
  39. break;
  40. }
  41. }
  42. if (jj == NELEM(format_args))
  43. {
  44. printf("Unknown arg: %s\n", argv[arg_idx]);
  45. exit(1);
  46. }
  47. }
  48. return sflags;
  49. }