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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. {"compact_array", JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY},
  24. };
  25. #ifndef NELEM
  26. #define NELEM(x) (sizeof(x) / sizeof(x[0]))
  27. #endif
  28. int 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. }