X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=test%2Fbin%2Fjson_process.c;h=1ed0c578f1cc44a6b019955fc7655d34c3969db5;hb=5df7b7939794abc9e8a826919d84d27385a6679d;hp=cff820b7e6941af1bbb8ff1c6098ea3a3017ef95;hpb=bb5d4efb2ef9609bcd1163238bccd11a1df28095;p=jansson.git diff --git a/test/bin/json_process.c b/test/bin/json_process.c index cff820b..1ed0c57 100644 --- a/test/bin/json_process.c +++ b/test/bin/json_process.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2011 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -7,6 +7,8 @@ #include #include +#include +#include #include static int getenv_int(const char *name) @@ -25,6 +27,26 @@ static int getenv_int(const char *name) return (int)result; } +/* Return a pointer to the first non-whitespace character of str. + Modifies str so that all trailing whitespace characters are + replaced by '\0'. */ +static const char *strip(char *str) +{ + size_t length; + char *result = str; + while(*result && isspace(*result)) + result++; + + length = strlen(result); + if(length == 0) + return result; + + while(isspace(result[length - 1])) + result[--length] = '\0'; + + return result; +} + int main(int argc, char *argv[]) { int indent = 0; @@ -59,9 +81,39 @@ int main(int argc, char *argv[]) if(getenv_int("JSON_SORT_KEYS")) flags |= JSON_SORT_KEYS; - json = json_loadf(stdin, 0, &error); + if(getenv_int("STRIP")) { + /* Load to memory, strip leading and trailing whitespace */ + size_t size = 0, used = 0; + char *buffer = NULL; + + while(1) { + int count; + + size = (size == 0 ? 128 : size * 2); + buffer = realloc(buffer, size); + if(!buffer) { + fprintf(stderr, "Unable to allocate %d bytes\n", (int)size); + return 1; + } + + count = fread(buffer + used, 1, size - used, stdin); + if(count < size - used) { + buffer[used + count] = '\0'; + break; + } + used += count; + } + + json = json_loads(strip(buffer), 0, &error); + free(buffer); + } + else + json = json_loadf(stdin, 0, &error); + if(!json) { - fprintf(stderr, "%d\n%s\n", error.line, error.text); + fprintf(stderr, "%d %d %d\n%s\n", + error.line, error.column, error.position, + error.text); return 1; }