Skip to content

Commit

Permalink
binary array reading
Browse files Browse the repository at this point in the history
  • Loading branch information
DINH Viet Huy committed Mar 25, 2015
1 parent 08c0db0 commit 4cec7ff
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/massCorrelation.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ entry point for processing tab separated data to correlation values
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

#include "correlation.h"
#include "arrayIO.h"

static void die(const char*);
static float* readInputVectorBin(FILE*,size_t*,size_t*);
/**
* print error message and exit program
*
Expand All @@ -29,13 +31,34 @@ static void die(const char* message) {
abort();
}

static float* readInputVectorBin(FILE* input,size_t* cols,size_t* rows) {
size_t out;
out=fread(cols,sizeof(size_t),1,input);
assert(out==1);
if(out!=1) {die("cannot read cols count from input");}
out=fread(rows,sizeof(size_t),1,input);
assert(out==1);
if(out!=1) {die("cannot read rows count from input");}
size_t all= *cols * *rows;

float* result=calloc(all,sizeof(float));
out=fread(result,sizeof(float),all,input);
assert(out==all);
if(out!=all) {die("cannot read all the input matrix, input shorter than expected");}

return result;
}

static void showUsage(char* exeName) {
fprintf(stderr,
"Usage :\n"
"%s < inputFile.txt > output.bin\n"
"%s -i inputfile.txt -o outputFile.txt\n"
"arguments could be mixed with pipe style calling"
, exeName,exeName);
"%s -b < inputFile.bin > output.bin\n"
"%s -b -i inputFile.bin -o output.bin\n"
"arguments could be mixed with pipe style calling\n"
"-b flag is to put binary array as input\n"
, exeName,exeName,exeName,exeName);
exit(0);
}

Expand All @@ -45,9 +68,10 @@ int main(int argc,char** argv) {
char* inputPath=NULL;
char* outputPath=NULL;

bool binaryFlag=false;
int c;
opterr=0;
while((c = getopt(argc,argv,"i:o:h"))!= -1) {
while((c = getopt(argc,argv,"i:o:hb"))!= -1) {
switch(c) {
case 'i':
inputPath=strdup(optarg);
Expand All @@ -68,6 +92,9 @@ int main(int argc,char** argv) {
case 'h':
showUsage(argv[0]);
break;
case 'b':
binaryFlag=true;
break;
default:
showUsage(argv[0]);
break;
Expand All @@ -82,7 +109,12 @@ int main(int argc,char** argv) {
fprintf(stderr,"reading input\n");
size_t cols=0;
size_t rows=0;
float* inputData=readInputVectors(input,&cols,&rows);
float* inputData;
if(binaryFlag) {
inputData=readInputVectorBin(input,&cols,&rows);
}else{
inputData=readInputVectors(input,&cols,&rows);
}

if(inputPath!=NULL) {
free(inputPath);
Expand Down

0 comments on commit 4cec7ff

Please sign in to comment.