From d8db348315debd57a51c96139e5123fd8ca0ad60 Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Mon, 5 Oct 2015 14:11:26 -0400 Subject: [PATCH 1/2] Hack in stdin/stdout support for bin2txt --- convertvec.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/convertvec.c b/convertvec.c index 9496684..e989915 100644 --- a/convertvec.c +++ b/convertvec.c @@ -4,7 +4,7 @@ #include #include #include -#include +//#include #include const long long max_w = 2000; @@ -54,9 +54,21 @@ void txt2bin(char * input_path, char * output_path){ // Convert from binary to text format void bin2txt(char * input_path, char * output_path){ - FILE * fi = fopen(input_path, "rb"); - FILE * fo = fopen(output_path, "wb"); - + FILE * fi; + FILE * fo; + + if(input_path == NULL || strlen(input_path) == 0 + || strcmp(input_path, "-") == 0) { + fi = freopen(NULL, "rb", stdin); + } else { + fi = fopen(input_path, "rb"); + } + if(output_path == NULL || strlen(output_path) == 0 + || strcmp(output_path, "-") == 0) { + fo = freopen(NULL, "wb", stdout); + } else { + fo = fopen(output_path, "wb"); + } long long words, size; fscanf(fi, "%lld", &words); fscanf(fi, "%lld", &size); From 2037b250918b4711e6c2621b1c7d6af7bfbfdf3c Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Mon, 5 Oct 2015 14:18:52 -0400 Subject: [PATCH 2/2] Add stdin/stdout support to txt2bin too --- convertvec.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/convertvec.c b/convertvec.c index e989915..91d953d 100644 --- a/convertvec.c +++ b/convertvec.c @@ -11,8 +11,21 @@ const long long max_w = 2000; // Convert from text format to binary void txt2bin(char * input_path, char * output_path){ - FILE * fi = fopen(input_path, "rb"); - FILE * fo = fopen(output_path, "wb"); + FILE * fi; + FILE * fo; + + if (input_path == NULL || strlen(input_path) == 0 + || strcmp(input_path, "-") == 0) { + fi = freopen(NULL, "rb", stdin); + } else { + fi = fopen(input_path, "rb"); + } + if (output_path == NULL || strlen(output_path) == 0 + || strcmp(output_path, "-") == 0) { + fo = freopen(NULL, "wb", stdout); + } else { + fo = fopen(output_path, "wb"); + } long long words, size; fscanf(fi, "%lld", &words); @@ -57,18 +70,19 @@ void bin2txt(char * input_path, char * output_path){ FILE * fi; FILE * fo; - if(input_path == NULL || strlen(input_path) == 0 - || strcmp(input_path, "-") == 0) { + if (input_path == NULL || strlen(input_path) == 0 + || strcmp(input_path, "-") == 0) { fi = freopen(NULL, "rb", stdin); } else { fi = fopen(input_path, "rb"); } - if(output_path == NULL || strlen(output_path) == 0 - || strcmp(output_path, "-") == 0) { + if (output_path == NULL || strlen(output_path) == 0 + || strcmp(output_path, "-") == 0) { fo = freopen(NULL, "wb", stdout); } else { fo = fopen(output_path, "wb"); } + long long words, size; fscanf(fi, "%lld", &words); fscanf(fi, "%lld", &size); @@ -108,6 +122,7 @@ int main(int argc, char **argv) { return 0; } + // TODO: it should be possible to sniff the input file to determine this if(strcmp(argv[1], "bin2txt") == 0) bin2txt(argv[2], argv[3]); else if(strcmp(argv[1], "txt2bin") == 0)