diff --git a/jsoncat.c b/jsoncat.c index f04a6dc7..a5c339ec 100644 --- a/jsoncat.c +++ b/jsoncat.c @@ -88,7 +88,7 @@ void process(FILE *f) { } if (jp->error != NULL) { - fprintf(stderr, "%s\n", jp->error); + fprintf(stderr, "%d: %s\n", jp->line, jp->error); } } diff --git a/jsonpull.c b/jsonpull.c index bcf1d43d..227bca42 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -9,19 +9,24 @@ typedef enum json_expect { JSON_ITEM, JSON_COMMA, JSON_COLON, JSON_KEY, JSON_VALUE, } json_expect; -static int read_file(json_pull *p) { - return fgetc(p->source); +static int read_file(json_pull *j) { + int c = fgetc(j->source); + if (c == '\n') { + j->line++; + } + return c; } -static int peek_file(json_pull *p) { - int c = getc(p->source); - ungetc(c, p->source); +static int peek_file(json_pull *j) { + int c = getc(j->source); + ungetc(c, j->source); return c; } json_pull *json_begin_file(FILE *f) { json_pull *j = malloc(sizeof(json_pull)); j->container = NULL; + j->line = 1; j->read = read_file; j->peek = peek_file; @@ -30,14 +35,17 @@ json_pull *json_begin_file(FILE *f) { return j; } -static int read_string(json_pull *p) { - char *cp = p->source; +static int read_string(json_pull *j) { + char *cp = j->source; if (*cp == '\0') { return EOF; } int c = (unsigned char) *cp; cp++; - p->source = cp; + j->source = cp; + if (c == '\n') { + j->line++; + } return c; } @@ -52,6 +60,7 @@ static int peek_string(json_pull *p) { json_pull *json_begin_string(char *s) { json_pull *j = malloc(sizeof(json_pull)); j->container = NULL; + j->line = 1; j->read = read_string; j->peek = peek_string; diff --git a/jsonpull.h b/jsonpull.h index 428916b7..b4f315c6 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -24,6 +24,7 @@ struct json_pull { int (*read)(struct json_pull *); int (*peek)(struct json_pull *); void *source; + int line; }; typedef struct json_pull json_pull;