Created
August 31, 2011 18:45
-
-
Save grassa/1184339 to your computer and use it in GitHub Desktop.
parse tab delimited standard input
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* The block below is included so that the program will compile on the macintosh, which lacks gnu's getline. Compilation produces a non-fatal warning*/ | |
/* This code is public domain -- Will Hartung 4/9/09 -from http://stackoverflow.com/questions/735126/are-there-alternate-implementations-of-gnu-getline-interface*/ | |
size_t getline(char **lineptr, size_t *n, FILE *stream) { | |
char *bufptr = NULL; | |
char *p = bufptr; | |
size_t size; | |
int c; | |
if (lineptr == NULL) { | |
return -1; | |
} | |
if (stream == NULL) { | |
return -1; | |
} | |
if (n == NULL) { | |
return -1; | |
} | |
bufptr = *lineptr; | |
size = *n; | |
c = fgetc(stream); | |
if (c == EOF) { | |
return -1; | |
} | |
if (bufptr == NULL) { | |
bufptr = malloc(128); | |
if (bufptr == NULL) { | |
return -1; | |
} | |
size = 128; | |
} | |
p = bufptr; | |
while(c != EOF) { | |
if ((p - bufptr) > (size - 1)) { | |
size = size + 128; | |
bufptr = realloc(bufptr, size); | |
if (bufptr == NULL) { | |
return -1; | |
} | |
} | |
*p++ = c; | |
if (c == '\n') { | |
break; | |
} | |
c = fgetc(stream); | |
} | |
*p++ = '\0'; | |
*lineptr = bufptr; | |
*n = size; | |
return p - bufptr - 1; | |
} | |
/* This code is public domain -- Will Hartung 4/9/09 -from: http://stackoverflow.com/questions/735126/are-there-alternate-implementations-of-gnu-getline-interface*/ | |
/* The block above is included so that the program will compile on the macintosh, which lacks gnu's getline*/ | |
int main() | |
{ | |
while(1){ | |
if(feof( stdin ) !=0){ | |
break; | |
} | |
int bytes_read; | |
int nbytes = 100; | |
char *my_string; | |
my_string = (char *) malloc (nbytes + 1); | |
bytes_read = getline (&my_string, &nbytes, stdin); | |
if (bytes_read == -1){ | |
break; | |
} | |
else{ | |
int field = 0; | |
char delim[] = "\t"; | |
/*yes, i know should probably place these in an array*/ | |
char *contig; | |
char *site; | |
char *ref; | |
char *nreads; | |
char *cigar; | |
char *quals; | |
char *result = NULL; | |
result = strtok( my_string, delim ); | |
while( result != NULL ){ | |
if (field > 5){ | |
break; | |
} | |
if( field == 0){ | |
contig = result; | |
} | |
if( field == 1){ | |
site = result; | |
} | |
if( field == 2){ | |
ref = result; | |
} | |
if( field == 3){ | |
nreads = result; | |
} | |
if( field == 4){ | |
cigar = result; | |
} | |
if( field == 5){ | |
quals = result; | |
printf("contig: %s\n", contig); | |
printf("site: %s\n", site); | |
printf("ref: %s\n", ref); | |
printf("nreads: %s\n", nreads); | |
printf("cigar: %s\n", cigar); | |
printf("quals: %s\n", quals); | |
} | |
result = strtok( NULL, delim ); | |
field++; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment