Last active
October 22, 2017 21:13
-
-
Save aragaer/43300249f0c4ec4f175361d753001efe 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
#define _GNU_SOURCE | |
#include <ctype.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#define CHUNK 4096 | |
int main(int argc, char *argv[]) { | |
int bufsize = CHUNK; | |
int empty = bufsize; | |
char *data = malloc(bufsize); | |
int size; | |
while (1) { | |
char *ptr = data + bufsize - empty; | |
int result = read(0, ptr, empty); | |
if (result == empty) { | |
data = realloc(data, bufsize * 2); | |
if (data == NULL) { | |
fprintf(stderr, "not enough space\n"); | |
exit(-1); | |
} | |
empty = bufsize; | |
bufsize *= 2; | |
} else { | |
size = bufsize - empty + result; | |
break; | |
} | |
} | |
char *first = malloc(size); | |
char *second = malloc(size); | |
char *first_ptr = first, *second_ptr = second; | |
char *end = data + size - 1; | |
if (*end != '\n') | |
*++end = '\n'; | |
*++end = 'x'; | |
if (first == NULL || second == NULL) { | |
fprintf(stderr, "out of space\n"); | |
exit(-1); | |
} | |
int count = 0; | |
fprintf(stderr, "Got %d bytes\n", size); | |
char *ptr = data; | |
while (ptr < end) { | |
do | |
*first_ptr++ = *ptr++; | |
while (isalnum(*ptr)); | |
*first_ptr++ = ' '; | |
while (!isalnum(*ptr)) | |
ptr++; | |
while (isalnum(*ptr)) | |
*second_ptr++ = *ptr++; | |
*second_ptr++ = '\n'; | |
while (!isalnum(*ptr)) | |
ptr++; | |
count++; | |
} | |
free(data); | |
char out_buf[2][CHUNK]; | |
char *out_ptr = *out_buf; | |
char *head = first; | |
int line_number = 0; | |
for (line_number = 0; line_number < count; line_number++) { | |
//fprintf(stderr, "Line %d/%d\n", line_number+1, count); | |
int first_len = (char *) rawmemchr(head, ' ') - head; | |
char *tail = second; | |
int i; | |
for (i = 0; i < count; i++) { | |
char *eol = rawmemchr(tail, '\n'); | |
// fprintf(stderr, "%.*s%.*s", first_len, head, eol-tail+1, tail); | |
memcpy(out_ptr, head, first_len); | |
out_ptr += first_len; | |
memcpy(out_ptr, tail, eol-tail+1); | |
out_ptr += eol-tail+1; | |
if (out_ptr - *out_buf >= CHUNK) { | |
write(1, *out_buf, sizeof(*out_buf)); | |
memcpy(*out_buf, out_buf[1], out_ptr - *out_buf - CHUNK); | |
out_ptr -= CHUNK; | |
} | |
tail = eol+1; | |
} | |
//fprintf(stderr, "done\n"); | |
head = rawmemchr(head, ' ') + 1; | |
} | |
write(1, *out_buf, out_ptr - *out_buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment