Created
October 23, 2017 17:07
-
-
Save zinid/ac3bdb27237f4df150af9401229c3a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Compile: | |
* $ ragel -G2 zinid.rl -o zinid.c | |
* $ gcc -O2 zinid.c -o zinid | |
*/ | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#define BUFSIZE 1024 | |
char **idx; | |
size_t size = 1024*1024; | |
size_t ptr = 0; | |
int pos = 0; | |
char *line_stop = NULL; | |
char *line_start = NULL; | |
char *key_stop = NULL; | |
char *val_start = NULL; | |
char *val_stop = NULL; | |
%%{ | |
machine foo; | |
action key_start {line_start = p;} | |
action key_stop {key_stop = p;} | |
action val_start {val_start = p;} | |
action val_stop {val_stop = p;} | |
action line_stop { | |
line_stop = p; | |
idx[pos] = line_start; | |
idx[pos+1] = key_stop; | |
idx[pos+2] = val_start; | |
idx[pos+3] = val_stop; | |
pos += 4; | |
} | |
action parse_error {error = 1;} | |
word = [a-z0-9]+; | |
main := ( | |
(word > key_start % key_stop) ' ' | |
(word > val_start % val_stop) | |
(('\r'?'\n') % line_stop) any* | |
) $! parse_error; | |
}%% | |
%% write data; | |
int parse(char *buf, char *pe) { | |
int error = 0, cs = 0; | |
char *space = NULL, *crlf = NULL; | |
char *p = buf; | |
char *eof = pe; | |
%% write init; | |
%% write exec; | |
return error; | |
} | |
void do_write(char *out, const char *in, size_t len) { | |
if ((ptr + len) >= size) { | |
write(STDOUT_FILENO, out, ptr); | |
ptr = 0; | |
} | |
memcpy(out + ptr, in, len); | |
ptr += len; | |
} | |
int main(int argc, char *argv[]) { | |
int i = 0, j = 0, got = 0; | |
idx = malloc(1024*1024); | |
char *in = NULL, *buf = NULL; | |
char *end = NULL; | |
in = malloc(1024*1024); | |
char *out = malloc(size); | |
line_start = in; | |
while (1) { | |
if ((got = read(STDIN_FILENO, in, BUFSIZE)) > 0) { | |
in = in + got; | |
while (!parse(line_start, in)) { | |
line_start = line_stop; | |
} | |
} else | |
break; | |
} | |
for (i = 0; i<pos; i += 4) { | |
int len_i = idx[i+1] - idx[i]; | |
for (j = 0; j<pos; j += 4) { | |
int len_j = idx[j+3] - idx[j+2]; | |
do_write(out, idx[i], len_i); | |
do_write(out, idx[j+2], len_j); | |
do_write(out, "\n", 1); | |
} | |
} | |
write(STDOUT_FILENO, out, ptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment