2013-05-26 12:42:04 +10:00
|
|
|
#include <assert.h>
|
2013-11-30 02:05:42 -06:00
|
|
|
#include <errno.h>
|
2013-05-26 12:42:04 +10:00
|
|
|
#include <stdarg.h>
|
2013-11-30 02:05:42 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "jq.h"
|
2013-05-26 12:42:04 +10:00
|
|
|
#include "jv_alloc.h"
|
|
|
|
#include "locfile.h"
|
|
|
|
|
|
|
|
|
2015-03-29 19:12:23 -05:00
|
|
|
struct locfile* locfile_init(jq_state *jq, const char *fname, const char* data, int length) {
|
2014-07-09 00:55:20 -04:00
|
|
|
struct locfile* l = jv_mem_alloc(sizeof(struct locfile));
|
2013-11-30 02:05:42 -06:00
|
|
|
l->jq = jq;
|
2015-03-29 19:12:23 -05:00
|
|
|
l->fname = jv_string(fname);
|
2014-07-09 00:55:20 -04:00
|
|
|
l->data = jv_mem_alloc(length);
|
|
|
|
memcpy((char*)l->data,data,length);
|
2013-05-26 12:42:04 +10:00
|
|
|
l->length = length;
|
|
|
|
l->nlines = 1;
|
2014-07-09 00:55:20 -04:00
|
|
|
l->refct = 1;
|
2013-05-26 12:42:04 +10:00
|
|
|
for (int i=0; i<length; i++) {
|
|
|
|
if (data[i] == '\n') l->nlines++;
|
|
|
|
}
|
|
|
|
l->linemap = jv_mem_alloc(sizeof(int) * (l->nlines + 1));
|
|
|
|
l->linemap[0] = 0;
|
|
|
|
int line = 1;
|
|
|
|
for (int i=0; i<length; i++) {
|
|
|
|
if (data[i] == '\n') {
|
2013-05-28 14:11:24 +10:00
|
|
|
l->linemap[line] = i+1; // at start of line, not of \n
|
2013-05-26 12:42:04 +10:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 14:11:24 +10:00
|
|
|
l->linemap[l->nlines] = length+1; // virtual last \n
|
2014-07-09 00:55:20 -04:00
|
|
|
return l;
|
2013-05-26 12:42:04 +10:00
|
|
|
}
|
|
|
|
|
2014-07-09 00:55:20 -04:00
|
|
|
struct locfile* locfile_retain(struct locfile* l) {
|
|
|
|
l->refct++;
|
|
|
|
return l;
|
|
|
|
}
|
2013-05-26 12:42:04 +10:00
|
|
|
void locfile_free(struct locfile* l) {
|
2014-07-09 00:55:20 -04:00
|
|
|
if (--(l->refct) == 0) {
|
2015-03-29 19:12:23 -05:00
|
|
|
jv_free(l->fname);
|
2014-07-09 00:55:20 -04:00
|
|
|
jv_mem_free(l->linemap);
|
|
|
|
jv_mem_free((char*)l->data);
|
|
|
|
jv_mem_free(l);
|
|
|
|
}
|
2013-05-26 12:42:04 +10:00
|
|
|
}
|
|
|
|
|
2015-03-30 15:55:54 -05:00
|
|
|
int locfile_get_line(struct locfile* l, int pos) {
|
2013-05-26 12:42:04 +10:00
|
|
|
assert(pos < l->length);
|
2013-05-28 14:11:24 +10:00
|
|
|
int line = 1;
|
|
|
|
while (l->linemap[line] <= pos) line++; // == if pos at start (before, never ==, because pos never on \n)
|
|
|
|
assert(line-1 < l->nlines);
|
|
|
|
return line-1;
|
2013-05-26 12:42:04 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static int locfile_line_length(struct locfile* l, int line) {
|
|
|
|
assert(line < l->nlines);
|
2013-05-28 14:11:24 +10:00
|
|
|
return l->linemap[line+1] - l->linemap[line] -1; // -1 to omit \n
|
2013-05-26 12:42:04 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
|
|
|
va_list fmtargs;
|
|
|
|
va_start(fmtargs, fmt);
|
2013-11-30 02:05:42 -06:00
|
|
|
int startline;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
if (loc.start != -1) {
|
|
|
|
startline = locfile_get_line(l, loc.start);
|
|
|
|
offset = l->linemap[startline];
|
|
|
|
}
|
|
|
|
|
|
|
|
jv m1 = jv_string_vfmt(fmt, fmtargs);
|
|
|
|
if (!jv_is_valid(m1)) {
|
2014-08-11 17:25:09 -05:00
|
|
|
jq_report_error(l->jq, m1);
|
|
|
|
return;
|
2013-11-30 02:05:42 -06:00
|
|
|
}
|
2013-05-26 12:42:04 +10:00
|
|
|
if (loc.start == -1) {
|
2014-08-11 17:25:09 -05:00
|
|
|
jq_report_error(l->jq, jv_string_fmt("jq: error: %s\n<unknown location>", jv_string_value(m1)));
|
2013-11-30 02:05:42 -06:00
|
|
|
jv_free(m1);
|
2013-05-26 12:42:04 +10:00
|
|
|
return;
|
|
|
|
}
|
2015-03-29 19:12:23 -05:00
|
|
|
jv m2 = jv_string_fmt("%s at %s, line %d:\n%.*s%*s", jv_string_value(m1),
|
|
|
|
jv_string_value(l->fname), startline + 1,
|
2014-08-11 17:25:09 -05:00
|
|
|
locfile_line_length(l, startline), l->data + offset,
|
|
|
|
loc.start - offset, "");
|
2013-11-30 02:05:42 -06:00
|
|
|
jv_free(m1);
|
2014-08-11 17:25:09 -05:00
|
|
|
jq_report_error(l->jq, m2);
|
2013-11-30 02:05:42 -06:00
|
|
|
return;
|
2013-05-26 12:42:04 +10:00
|
|
|
}
|