diff --git a/src/ecogrep.c b/src/ecogrep.c index 04c0b77..357f560 100644 --- a/src/ecogrep.c +++ b/src/ecogrep.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #define VERSION "0.1" @@ -23,7 +25,6 @@ int ispatternmatching(char **line, char **pattern, int32_t numpattern, int32_t e sscanf(line[4],"%d",&seq->taxid); seq->AC = strdup(line[0]); - //seq->DE = strdup(line[]); seq->SQ = strdup(line[18]); seq->SQ_length = strlen(line[18]); @@ -66,9 +67,9 @@ static void PrintHelp() PP " -r : [R]estrict taxomic id\n\n"); PP " -v : in[V]ert the sense of matching, to select non-matching lines.\n"); PP "------------------------------------------\n"); - PP " Pattern : oligonucleotide pattern\n");; + PP " Pattern : oligonucleotide pattern\n"); PP "------------------------------------------\n\n"); - PP " https://www.grenoble.prabi.fr/trac/ecoPCR/wiki"); + PP " https://www.grenoble.prabi.fr/trac/ecoPCR/wiki\n"); PP "------------------------------------------\n\n"); } @@ -109,26 +110,31 @@ int main(int argc, char **argv){ ecotaxonomy_t *taxonomy; - char *filename = NULL; // stores the datafile name char *database = NULL; // stores the database path (for taxonomy) int32_t *restricted_taxid = NULL; // stores the restricted taxid int32_t *ignored_taxid = NULL; // stores the ignored taxid char **pattern = NULL; // stores the regex pattern char *line[19] = {0}; // stores the line - FILE *file = NULL; char *stream = ECOMALLOC(sizeof(char *)*10000,"error stream buffer allocation"); int current_taxid; char *buffer; + + struct stat buf; + + // check stdin + if (fstat(fileno(stdin),&buf) == 0) + if ( (buf.st_dev) == 0) + file = stdin; + while ((carg = getopt(argc, argv, "f:d:i:r:e:vh")) != -1) { switch (carg) { case 'f': - filename = ECOMALLOC(strlen(optarg)+1, - "Error on filename allocation"); - strcpy(filename,optarg); + if ( (file = fopen(optarg, "r")) == NULL) + errflag++; break; case 'd': @@ -201,22 +207,14 @@ int main(int argc, char **argv){ /** * check everything required is provided */ - if (!filename || - !database || - (!p && restricted_taxid == NULL && ignored_taxid == NULL)) + if ( !file || + !database || + (!p && restricted_taxid == NULL && ignored_taxid == NULL) + ) errflag++; - + if (errflag) ExitUsage(errflag); - - /** - * Open the ecoPCR file - */ - if (filename) - { - if( (file = fopen(filename, "r")) == NULL) - ECOERROR(ECO_IO_ERROR,"Cannot open the file !"); - } /** * Get the taxonomy back @@ -282,6 +280,9 @@ int main(int argc, char **argv){ printf("# %d matching result\n",matchingresult); + if ( file != stdin ) + fclose(file); + freememory(line,i); freememory(pattern,p); ECOFREE(pattern,"Error in free pattern");