From 96ade4ef56d18e9eaaccae4278e86f4dab96e157 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 10 Nov 2016 08:09:21 -0500 Subject: [PATCH] Added first pass at ctags file utility --- fetchtag.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 fetchtag.c diff --git a/fetchtag.c b/fetchtag.c new file mode 100644 index 0000000..8b0105a --- /dev/null +++ b/fetchtag.c @@ -0,0 +1,52 @@ +#include +#include +#include + +typedef struct { + char* file; + char* cmd; +} Choice; + +void add_choice(uint8_t* buf, size_t n) { + char* str = malloc(n); + memcpy(str, buf, n); + str[n] = '\0'; + /**/ + char* tag = strtok(str, "\t"); + char* file = strtok(NULL, "\t"); + char* cmd = file + strlen(file) + 1; + char* end = strrchr(cmd, ';'); + if (end) *end = '\0'; + + puts(tag); + puts(file); + puts(cmd); +} + +void fetchtags(char* tagfile, char* tag) { + size_t mlen = strlen(tag); + FMap file = fmap(tagfile); + for (size_t n, i = 0; i < file.len;) { + if (file.buf[i] > *tag) { + break; + } if ((file.buf[i + mlen] == '\t') && + (0 == strncmp(((char*)file.buf+i), tag, mlen))) { + for (n = 0; file.buf[i + n++] != '\n';); + add_choice(file.buf + i, n-1); + i += n; + } else { + while (file.buf[i++] != '\n'); + } + } +} + +int main(int argc, char** argv) { + /* usage message */ + if (argc != 3) { + printf("Usage: %s \n", argv[0]); + exit(1); + } + /* scan the file */ + fetchtags(argv[1], argv[2]); + return 0; +} -- 2.49.0