[Mndiff-dev] [mndiff]: r37 - trunk/mnzip/mnzip.c

michael subversion at mplayerhq.hu
Wed Jun 20 14:22:08 CEST 2007


Author: michael
Date: Wed Jun 20 14:22:08 2007
New Revision: 37

Log:
support file output and stdin input patch by Corey Hickey


Modified:
   trunk/mnzip/mnzip.c

Modified: trunk/mnzip/mnzip.c
==============================================================================
--- trunk/mnzip/mnzip.c	(original)
+++ trunk/mnzip/mnzip.c	Wed Jun 20 14:22:08 2007
@@ -458,7 +458,7 @@ fprintf(stderr, "idx init done\n");
     6,5 ( 3) -> 689
 */
 
-static int ibwt(uint8_t *in, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){
+static int ibwt(uint8_t *in, FILE *fo, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){
     int shift= low_mem*3;
     int shift2= shift + 8;
     int mask= (1<<shift)-1;
@@ -539,7 +539,7 @@ static int ibwt(uint8_t *in, unsigned in
         if(pretype)
             inv_pretransform(buffer, pret_buf, len2, pretype);
 
-        fwrite(buffer, len2, 1, stdout);
+        fwrite(buffer, len2, 1, fo);
     }
 
     free(idx);
@@ -766,7 +766,7 @@ fprintf(stderr," done (%d %d %d)\n", len
     return out_size+16;
 }
 
-static int decompress(FILE *fi, int low_mem){
+static int decompress(FILE *fi, FILE *fo, int low_mem){
     uint8_t *in;
     unsigned int in_len;
     unsigned int out_len,start,i, j, pretype;
@@ -834,25 +834,34 @@ fprintf(stderr," block (%d %d %d)\n", ou
         fprintf(stderr, "error (%d)\n", c.bytestream - c.bytestream_end);
         return -1;
     }
-    ibwt(tmp, out_len, start, histogram, low_mem, pretype);
+    ibwt(tmp, fo, out_len, start, histogram, low_mem, pretype);
 
     return out_len;
 }
 
 static void help(void){
     fprintf(stderr, 
-        "Usage: mnzip [OPTIONS] [INPUT-FILE]\n"
+        "Usage: mnzip [OPTIONS] [INPUT_FILE|-] [OUTPUT_FILE|-]\n"
         "options:\n"
         "   c   compress\n"
         "   d   decompress\n"
         "   l   slow/low memory mode\n"
         "   0-9 block size in 2^x megabyte (4mb is default)\n"
+        "\n"
+        "examples:\n"
+        "   Compress one file to another file:\n"
+        "       mnzip c input_file output_file\n"
+        "   Compress from stdin to stdout:\n"
+        "       mnzip c\n"
+        "       mnzip c - -\n"
+        "   Decompress from stdin to a file:\n"
+        "       mnzip d - output_file\n"
     );
     exit(1);
 }
 
 int main(int argc, char **argv){
-    FILE *fi;
+    FILE *fi, *fo;
     uint8_t *out, *in;
     int comp= !strcmp(argv[0], "mnzip");
     int low_mem= 0;
@@ -860,9 +869,7 @@ int main(int argc, char **argv){
     int outsize, i;
     static const uint8_t header[7]={'M','N','Z','I','P',0,0};
 
-    if (argc != 3)
-        help();
-
+    if (argc > 1) {
     for(i=0; argv[1][i]; i++){
         switch(argv[1][i]){
         case 'c'        : comp=1                            ; break;
@@ -873,26 +880,36 @@ int main(int argc, char **argv){
         default         : help()                            ;
         }
     }
+    }
 
-    if (! (fi = fopen(argv[2], "rb"))) {
+    if (argc < 3 || !strcmp(argv[2], "-"))
+        fi = stdin;
+    else if (! (fi = fopen(argv[2], "rb"))) {
         perror("Error opening input file");
         exit(1);
     }
 
+    if (argc < 4 || !strcmp(argv[3], "-"))
+        fo = stdout;
+    else if (! (fo = fopen(argv[3], "w+b"))) {
+        perror("Error opening output file");
+        exit(1);
+    }
+
     init_prob(prob);
 
     if(comp){
         in = malloc(block_size+RADIX_PASSES);
         out= malloc(2*block_size + 5000);//FIXME int and buffer overfl
 
-        fwrite(header, 7, 1, stdout);
+        fwrite(header, 7, 1, fo);
 
         while(!feof(fi)){
             block_size= fread(in, 1, block_size, fi);
 fprintf(stderr, "bwt in %p %p %d\n", out, in, block_size); 
             outsize= compress(out, in, block_size);
 fprintf(stderr, "bwt out %d\n", outsize); 
-            fwrite(out, outsize, 1, stdout);
+            fwrite(out, outsize, 1, fo);
         }
     }else{
         uint8_t tmp[256];
@@ -903,7 +920,7 @@ fprintf(stderr, "bwt out %d\n", outsize)
             exit(1);
         }
         while(!feof(fi)){
-            if(decompress(fi, low_mem)<0)
+            if(decompress(fi, fo, low_mem)<0)
                 exit(1);
         }
     }



More information about the Mndiff-dev mailing list