]> granicus.if.org Git - jq/commitdiff
locfile.h -> locfile.h + locfile.c
authorBrendan Macmillan <melbourne.research@gmail.com>
Sun, 26 May 2013 02:42:04 +0000 (12:42 +1000)
committerBrendan Macmillan <melbourne.research@gmail.com>
Wed, 29 May 2013 05:17:01 +0000 (15:17 +1000)
clean up includes of a few files

Makefile.am
builtin.c
locfile.c [new file with mode: 0644]
locfile.h
main.c

index 23e9d253dc1b57de39d59fe9792adf99456e968f..a4d5cd1b28465de81a53d6975c3b03a41d239abc 100644 (file)
@@ -5,7 +5,7 @@ JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h          \
   jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h parser.y    \
   jv_utf8_tables.h lexer.l
 
-JQ_SRC = opcode.c bytecode.c compile.c execute.c builtin.c jv.c                \
+JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c              \
   jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c     \
   jq_test.c ${JQ_INCS}
 
@@ -123,4 +123,4 @@ if ENABLE_DOCS
 # 'make clean' doesn't delete the manpage if it can't be rebuilt
 clean-local:
        rm -f jq.1
-endif
\ No newline at end of file
+endif 
index 071cb564618230281cd40dce8712c50e36cc1335..9b5daef68cbe861ec76bbb9b8985ad64f5ee9e1f 100644 (file)
--- a/builtin.c
+++ b/builtin.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <string.h>
 #include "builtin.h"
 #include "compile.h"
diff --git a/locfile.c b/locfile.c
new file mode 100644 (file)
index 0000000..1c73f32
--- /dev/null
+++ b/locfile.c
@@ -0,0 +1,68 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdarg.h>
+#include "jv_alloc.h"
+#include "locfile.h"
+
+
+void locfile_init(struct locfile* l, const char* data, int length) {
+  l->data = data;
+  l->length = length;
+  l->nlines = 1;
+  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') {
+      l->linemap[line] = i;
+      line++;
+    }
+  }
+  l->linemap[l->nlines] = length;
+}
+
+void locfile_free(struct locfile* l) {
+  jv_mem_free(l->linemap);
+}
+
+static int locfile_get_line(struct locfile* l, int pos) {
+  assert(pos < l->length);
+  int line = 0;
+  while (l->linemap[line+1] < pos) line++;
+  assert(line < l->nlines);
+  return line;
+}
+
+static int locfile_line_length(struct locfile* l, int line) {
+  assert(line < l->nlines);
+  return l->linemap[line+1] - l->linemap[line];
+}
+
+void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
+  va_list fmtargs;
+  va_start(fmtargs, fmt);
+  vfprintf(stderr, fmt, fmtargs);
+  va_end(fmtargs);
+  fprintf(stderr, "\n");
+  if (loc.start == -1) {
+    fprintf(stderr, "<unknown location>\n");
+    return;
+  }
+  int startline = locfile_get_line(l, loc.start);
+  int offset = l->linemap[startline];
+       fprintf(stderr, "HERE1\n%.*s\n", locfile_line_length(l, startline)-(startline!=0), l->data + offset +(startline!=0));           // if not first line, this starts at the '\n' in l
+  fprintf(stderr, "HERE2\n%*s", loc.start - offset -(startline!=0), "");               // space padding.  If not first line, offset is the '\n' at beginning of line, and one too many
+                       /* ASIDE: because all this code is in locfile.h instead of locfile.c, it recompiles everything, and takes forever */
+                       /* I've separated it out, into locfile.h;  removed static */
+                       /* Problem:  a few files include locfile.h to get the *.h it includes... this seems bad to me */
+  for (int i = loc.start; 
+       i < loc.end && i < offset + locfile_line_length(l, startline);
+       i++){
+    fprintf(stderr, "^");
+  }
+  fprintf(stderr, "\n");
+}
index c557b9878664100ed6f07a8fd14a75401efc1e11..35907974807d3f6a398efca085d422729f116407 100644 (file)
--- a/locfile.h
+++ b/locfile.h
@@ -1,10 +1,6 @@
 #ifndef _LOCFILE_H
 #define _LOCFILE_H
-#include <stdlib.h>
-#include <stdio.h>
-#include <assert.h>
-#include <stdarg.h>
-#include "jv_alloc.h"
+
 typedef struct {
   int start, end;
 } location;
@@ -18,62 +14,10 @@ struct locfile {
   int nlines;
 };
 
-static void locfile_init(struct locfile* l, const char* data, int length) {
-  l->data = data;
-  l->length = length;
-  l->nlines = 1;
-  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') {
-      l->linemap[line] = i;
-      line++;
-    }
-  }
-  l->linemap[l->nlines] = length;
-}
-
-static void locfile_free(struct locfile* l) {
-  jv_mem_free(l->linemap);
-}
-
-static int locfile_get_line(struct locfile* l, int pos) {
-  assert(pos < l->length);
-  int line = 0;
-  while (l->linemap[line+1] < pos) line++;
-  assert(line < l->nlines);
-  return line;
-}
+void locfile_init(struct locfile* l, const char* data, int length);
 
-static int locfile_line_length(struct locfile* l, int line) {
-  assert(line < l->nlines);
-  return l->linemap[line+1] - l->linemap[line];
-}
+void locfile_free(struct locfile* l);
 
-static void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
-  va_list fmtargs;
-  va_start(fmtargs, fmt);
-  vfprintf(stderr, fmt, fmtargs);
-  va_end(fmtargs);
-  fprintf(stderr, "\n");
-  if (loc.start == -1) {
-    fprintf(stderr, "<unknown location>\n");
-    return;
-  }
-  int startline = locfile_get_line(l, loc.start);
-  int offset = l->linemap[startline];
-       fprintf(stderr, "%.*s\n", locfile_line_length(l, startline)-(startline!=0), l->data + offset +(startline!=0));
-  fprintf(stderr, "%*s", loc.start - offset -(startline!=0), "");
-  for (int i = loc.start; 
-       i < loc.end && i < offset + locfile_line_length(l, startline);
-       i++){
-    fprintf(stderr, "^");
-  }
-  fprintf(stderr, "\n");
-}
+void locfile_locate(struct locfile* l, location loc, const char* fmt, ...);
 
 #endif
diff --git a/main.c b/main.c
index 1e951849f7555f0a3c80ca7fd623880793cfbe96..8321743c1d19da8723ad264d9bdff0f622758971 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>