Re: Misleading error message with unreadable ~/.idata

Matt Kraai <[email protected]>
Newsgroups gmane.mail.ifile.general
Message-ID <20020829173831.GE3088@catalonia>
Howdy,

The appended patch should fix this problem.

Matt

--- ifile-1.0.9/database.c	2002-08-29 10:02:06.000000000 -0700
+++ ifile/database.c	2002-08-29 10:35:43.000000000 -0700
@@ -112,8 +112,7 @@
   long int i;
   long int num;
 
-  readline(line, sizeof(line), DATA);
-  if (line[0] == '\0' || feof(DATA)) return -1;
+  if (readline(line, sizeof(line), DATA) == NULL) return -1;
 
   /* read folder names */
   token = strtok(line, " \t");
@@ -124,8 +123,7 @@
     }
   idata->num_folders = i;
 
-  readline(line, sizeof(line), DATA);
-  if (line[0] == '\0' || feof(DATA)) return -1;
+  if (readline(line, sizeof(line), DATA) == NULL) return -1;
 
   /* read word frequencies */
   token = strtok(line, " \t");
@@ -140,8 +138,7 @@
   if (i != idata->num_folders)
     ifile_verbosify(ifile_quiet, "Bad data file format - line #2\n");
 
-  readline(line, sizeof(line), DATA);
-  if (line[0] == '\0' || feof(DATA)) return -1;
+  if (readline(line, sizeof(line), DATA) == NULL) return -1;
 
   /* read document frequencies */
   token = strtok(line, " \t");
@@ -173,9 +170,7 @@
   char line[MAX_STR_LEN*4];
   long int i = 1;
 
-  readline(line, sizeof(line), DATA);
-
-  while (!feof(DATA))
+  while (readline(line, sizeof(line), DATA) != NULL)
     {
       if (line[0] == '\0')
 	ifile_verbosify(ifile_quiet, "Line # %d not in proper word entry format\n", (i+3));
@@ -183,7 +178,6 @@
 	idata->num_words += ifile_read_word_entry(line, idata);
       
       i++;
-      readline(line, sizeof(line), DATA);
     }
 
   return idata->num_words;
@@ -281,6 +275,9 @@
   
   words = ifile_read_word_frequencies (DATA, idata);
 
+  if (ferror(DATA))
+    ifile_error("Error reading %s.\n", data_file);
+
   fclose(DATA);
 
   DMZ_end = clock();
--- ifile-1.0.9/util.c	2002-08-29 09:54:03.000000000 -0700
+++ ifile/util.c	2002-08-29 10:33:47.000000000 -0700
@@ -162,7 +162,8 @@
 /* Reads up to and including the next feedline (\n) and stores the
  * string in the STRING buffer.  This function relies on STRING
  * being allocated with space for at least MAX_LEN-1 characters.
- * Wrapper for fgets().  Returns a pointer to STRING. */
+ * Wrapper for fgets().  Returns a pointer to STRING on success,
+ * and NULL on EOF or error.  */
 /* Written by Jason Rennie <[email protected]> for ifile */
 char *
 readline (char * string, long int max_len, FILE * stream)
@@ -174,11 +175,11 @@
       last = strlen(string)-1;
       if (string[last] == '\n')
        string[last] = '\0';
+
+      return string;
     }
   else
-    string[0] = '\0';
-  
-  return string;
+    return NULL;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.