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 761 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "json.h"
  4. #include "parse_flags.h"
  5. static struct {
  6. const char *arg;
  7. int flag;
  8. } format_args[] = {
  9. { "plain", JSON_C_TO_STRING_PLAIN },
  10. { "spaced", JSON_C_TO_STRING_SPACED },
  11. { "pretty", JSON_C_TO_STRING_PRETTY },
  12. };
  13. #ifndef NELEM
  14. #define NELEM(x) (sizeof(x) / sizeof(&x[0]))
  15. #endif
  16. int parse_flags(int argc, char **argv)
  17. {
  18. int arg_idx;
  19. int sflags = 0;
  20. for (arg_idx = 1; arg_idx < argc ; arg_idx++)
  21. {
  22. int jj;
  23. for (jj = 0; jj < NELEM(format_args); jj++)
  24. {
  25. if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0)
  26. {
  27. sflags |= format_args[jj].flag;
  28. break;
  29. }
  30. }
  31. if (jj == NELEM(format_args))
  32. {
  33. printf("Unknown arg: %s\n", argv[arg_idx]);
  34. exit(1);
  35. }
  36. }
  37. return sflags;
  38. }