json_process.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <jansson.h>
  15. #if HAVE_LOCALE_H
  16. #include <locale.h>
  17. #endif
  18. #if _WIN32
  19. #include <io.h> /* for _setmode() */
  20. #include <fcntl.h> /* for _O_BINARY */
  21. #endif
  22. #define l_isspace(c) ((c) == ' ' || (c) == '\n' || (c) == '\r' || (c) == '\t')
  23. static int getenv_int(const char *name)
  24. {
  25. char *value, *end;
  26. long result;
  27. value = getenv(name);
  28. if(!value)
  29. return 0;
  30. result = strtol(value, &end, 10);
  31. if(*end != '\0')
  32. return 0;
  33. return (int)result;
  34. }
  35. /* Return a pointer to the first non-whitespace character of str.
  36. Modifies str so that all trailing whitespace characters are
  37. replaced by '\0'. */
  38. static const char *strip(char *str)
  39. {
  40. size_t length;
  41. char *result = str;
  42. while(*result && l_isspace(*result))
  43. result++;
  44. length = strlen(result);
  45. if(length == 0)
  46. return result;
  47. while(l_isspace(result[length - 1]))
  48. result[--length] = '\0';
  49. return result;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. int indent = 0;
  54. size_t flags = 0;
  55. json_t *json;
  56. json_error_t error;
  57. #if HAVE_SETLOCALE
  58. setlocale(LC_ALL, "");
  59. #endif
  60. if(argc != 1) {
  61. fprintf(stderr, "usage: %s\n", argv[0]);
  62. return 2;
  63. }
  64. #ifdef _WIN32
  65. /* On Windows, set stdout and stderr to binary mode to avoid
  66. outputting DOS line terminators */
  67. _setmode(_fileno(stdout), _O_BINARY);
  68. _setmode(_fileno(stderr), _O_BINARY);
  69. #endif
  70. indent = getenv_int("JSON_INDENT");
  71. if(indent < 0 || indent > 255) {
  72. fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
  73. return 2;
  74. }
  75. if(indent > 0)
  76. flags |= JSON_INDENT(indent);
  77. if(getenv_int("JSON_COMPACT") > 0)
  78. flags |= JSON_COMPACT;
  79. if(getenv_int("JSON_ENSURE_ASCII"))
  80. flags |= JSON_ENSURE_ASCII;
  81. if(getenv_int("JSON_PRESERVE_ORDER"))
  82. flags |= JSON_PRESERVE_ORDER;
  83. if(getenv_int("JSON_SORT_KEYS"))
  84. flags |= JSON_SORT_KEYS;
  85. if(getenv_int("STRIP")) {
  86. /* Load to memory, strip leading and trailing whitespace */
  87. size_t size = 0, used = 0;
  88. char *buffer = NULL;
  89. while(1) {
  90. int count;
  91. size = (size == 0 ? 128 : size * 2);
  92. buffer = realloc(buffer, size);
  93. if(!buffer) {
  94. fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
  95. return 1;
  96. }
  97. count = fread(buffer + used, 1, size - used, stdin);
  98. if(count < size - used) {
  99. buffer[used + count] = '\0';
  100. break;
  101. }
  102. used += count;
  103. }
  104. json = json_loads(strip(buffer), 0, &error);
  105. free(buffer);
  106. }
  107. else
  108. json = json_loadf(stdin, 0, &error);
  109. if(!json) {
  110. fprintf(stderr, "%d %d %d\n%s\n",
  111. error.line, error.column, error.position,
  112. error.text);
  113. return 1;
  114. }
  115. json_dumpf(json, stdout, flags);
  116. json_decref(json);
  117. return 0;
  118. }
粤ICP备19079148号