Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stdin stdout support #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions convertvec.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
//#include <malloc.h>
#include <stdlib.h>

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);
Expand Down Expand Up @@ -54,9 +67,22 @@ 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);
Expand Down Expand Up @@ -96,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)
Expand Down