[Seqinr-commits] r1990 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Sep 1 15:02:24 CEST 2016


Author: simonpenel
Date: 2016-09-01 15:02:24 +0200 (Thu, 01 Sep 2016)
New Revision: 1990

Modified:
   pkg/src/alignment.c
Log:
improved read.alignment function thanks  to Matthew Krause

Modified: pkg/src/alignment.c
===================================================================
--- pkg/src/alignment.c	2016-08-31 02:45:12 UTC (rev 1989)
+++ pkg/src/alignment.c	2016-09-01 13:02:24 UTC (rev 1990)
@@ -500,7 +500,9 @@
    }
    tot_spec = -1;
    do	{
-     fgets(line,sizeof(line),in);
+     if(!fgets(line,sizeof(line),in))
+       break; 
+
      if( (p = strstr(line, "Name:") ) == NULL) continue;
      tot_spec++;
      q = strstr(p, " Len: "); 
@@ -590,8 +592,9 @@
   if(in==NULL) {
     error("file not found");
   }
-  fgets(line,sizeof(line),in);
-  if( sscanf(line, "%d%d", &totseqs, &lenseqs) != 2) {
+  
+  if(!fgets(line,sizeof(line),in) ||
+     sscanf(line, "%d%d", &totseqs, &lenseqs) != 2) {
     error("Not a PHYLIP file");
     totseqs = 0;
     goto fini;
@@ -614,7 +617,7 @@
     comments[i] = NULL;
   }
   for(i=0; i<totseqs; i++) {
-    fgets(line,sizeof(line),in);
+    if(!fgets(line,sizeof(line),in)) goto eof;
     memcpy(seqname[i],line,PHYNAME); seqname[i][PHYNAME] = 0;
     p = line+PHYNAME; q = seq[i];
     while(*p != '\n') {
@@ -624,9 +627,9 @@
   }
   l = q - seq[totseqs - 1];
   while( l < lenseqs) {
-    fgets(line,sizeof(line),in);
+    if(!fgets(line,sizeof(line),in)) goto eof;
     for(i=0; i<totseqs; i++) {
-      fgets(line,sizeof(line),in);
+      if(!fgets(line,sizeof(line),in)) goto eof;
       p = line; q = seq[i] + l;
       while(*p != '\n') {
 	if(*p != ' ') *(q++) = *p;
@@ -657,6 +660,10 @@
  error("Not enough memory!");
  totseqs = 0;
  goto fini;
+ eof:
+ error("Unexpected EOF/Corrupt File");
+ totseqs = 0;
+ goto fini;
 }
 
 
@@ -792,8 +799,8 @@
     return 0;
   }
 
-  fgets(line,sizeof(line),in);
-  if(strncmp(line,"CLUSTAL",7) != 0) { /* skip 1st line with CLUSTAL in it */
+  
+  if(!fgets(line,sizeof(line),in) || strncmp(line,"CLUSTAL",7) != 0) { /* skip 1st line with CLUSTAL in it */
     error("File not in CLUSTAL format!");
     tot_spec = -1; goto fini;
   }
@@ -802,7 +809,11 @@
   do	{
     carac = getc(in);
     if(carac == ' ') {
-      fgets(line,sizeof(line),in);
+      if(!fgets(line,sizeof(line),in)) {
+	    error("Unexpected EOF");
+	    tot_spec = 0;
+	    goto fini;
+       }
       carac = getc(in);
     }
   }



More information about the Seqinr-commits mailing list