Partial improvement for compress

Jose Da Silva <[email protected]>
Newsgroups gmane.comp.gnu.aspell.devel
Message-ID <[email protected]>
Only a partial improvement, I think there is room for more improvements, so 
there is a question to ask too before I send a further update.

Appears diff caught some more trailing spaces.... but main reason for sending 
this was....

1)modification to get_word():
get_word() function and inconsistent EOF output.
I was a bit puzzled about why there is an ungetc(c,in) because compress.c by 
itself does not seem to need an ungetc() to work correctly... the only 
reason I can seem to think it's there is in case some calling program wants 
to see an EOF
Reason for change:
the first if (c == EOF) return 0; does not reverse but the second if (c == 
EOF return 0; does reverse input using an ungetc(c, in).

My preference would be to remove the ungetc() completely from the routine, 
but if you have a reason for it staying, I think this modification to the 
get_word() function should return more consistent results if a parent 
program needs to see an EOF on the input file.

question: can the ungetc() be removed, or does it have to stay?

2)a little less code by removing a query:
-      while (prev[i] != '\0' && cur[i] != '\0' && prev[i] == cur[i])
+      while (prev[i] != '\0' && prev[i] == cur[i])
I removed the && (cur[i] != 0) since you can't have (cur[i] ==0) if you are 
going to have (prev[i] == cur[i]).

3)added fflush(stdout)
the '\n' flushes out whatever is in the buffers, but since this is a binary 
file missing a final '\n', you will need to add a fflush() to send 
everything to stdout.

_______________________________________________
Aspell-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/aspell-devel
compress_diff.txt (text/plain, 2.3 KB)
--- aspell-0.60/prog/compress.c	2004-06-23 02:14:26.000000000 -0700
+++ compress.c	2004-08-29 14:52:31.000000000 -0700
@@ -4,8 +4,8 @@
  *
  * Permission to use, copy, modify, distribute and sell this software
  * and its documentation for any purpose is hereby granted without
- * fee, provided that the above copyright notice appear in all copies  
- * and that both that copyright notice and this permission notice 
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
  * appear in supporting documentation.  Kevin Atkinson makes no
  * representations about the suitability of this software for any
  * purpose.  It is provided "as is" without express or implied
@@ -30,7 +30,7 @@
 
 #define BUFSIZE 256
 
-void usage () 
+void usage ()
 {
   fputs("Compresses or uncompresses sorted word lists.\n"     , stderr);
   fputs("For best result the locale should be set to C\n"    , stderr);
@@ -41,19 +41,20 @@
 }
 
 // PRECOND: bufsize >= 2
-static int get_word(FILE * in, char * w, size_t bufsize) 
+static int get_word(FILE * in, char * w, size_t bufsize)
 {
   int c;
   while (c = getc(in), c != EOF && c <= 32);
-  if (c == EOF) return 0;
-  do {
-    *w++ = (char)(c);
-    --bufsize;
-  } while (c = getc(in), c != EOF && c > 32 && bufsize > 1);
+  if (c != EOF) {
+    do {
+      *w++ = (char)(c);
+      --bufsize;
+    } while (c = getc(in), c != EOF && c > 32 && bufsize > 1);
+  }
   *w = '\0';
   ungetc(c, in);
   if (c == EOF) return 0;
-  else return 1;
+  return 1;
 }
 
 int main (int argc, const char *argv[]) {
@@ -62,7 +63,7 @@
 
     usage();
     return 1;
-    
+
   } else if (argv[1][0] == 'c') {
 
     char s1[BUFSIZE];
@@ -76,7 +77,7 @@
     while (get_word(stdin, cur, BUFSIZE)) {
       int i = 0;
       /* get the length of the prefix */
-      while (prev[i] != '\0' && cur[i] != '\0' && prev[i] == cur[i])
+      while (prev[i] != '\0' && prev[i] == cur[i])
         ++i;
       if (i > 31) {
         putc('\0', stdout);
@@ -89,11 +90,12 @@
         prev = s2; cur = s1;
       }
     }
+    fflush(stdout);
     return 0;
 
   } else if (argv[1][0] == 'd') {
-    
-    char cur[256];
+
+    char cur[BUFSIZE];
     int i;
     int c;
 
@@ -123,6 +125,6 @@
 
     usage();
     return 1;
-    
+
   }
 }
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.