home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / macintsh / mwrescue.c < prev    next >
C/C++ Source or Header  |  1989-03-21  |  3KB  |  100 lines

  1. /*
  2.  * mwrescue.c -- rescue a damaged MacWrite file (version 3.0 and later)
  3.  *
  4.  * Versions of MacWrite after 3.0 and before 4.0 have been unreliable.
  5.  * This program decodes damaged MacWrite files and recovers the text in
  6.  * them (once you've uploaded them to Unix).  Someone should get it
  7.  * running on the native Mac.
  8.  *
  9.  * It is currently very simple since it treats the entire file as compacted 
  10.  * text.  However, MacWrite files can also contain some uncompacted text 
  11.  * and some formatting information.  The uncompacted text can be retrieved
  12.  * with the Unix strings command.  The formatting information is output by
  13.  * this program as nonsense text.  
  14.  *
  15.  * The text retrieved by this program emerges in the order it is stored in 
  16.  * the file by MacWrite, which is not necessarily the order in which it is 
  17.  * supposed to appear in the document.
  18.  *
  19.  * As primitive as this program is, I'm releasing it because:
  20.  *    1) I hope someone will improve it.
  21.  *    2) I've found that panicked students are more than glad to get
  22.  *        their papers back even if the paragraphs are in random
  23.  *        order with garbage text in between.
  24.  *
  25.  * History:
  26.  *
  27.  *     5/11/85        Winkler        Created
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33.  
  34. #define TRUE 1
  35. #define FALSE 0
  36.  
  37. /* Compressed characters for English.  MacWrite stores this string
  38.  * in the resource fork of the document.  A future improvement would
  39.  * be to find the string there (presumably in file.rsrc on Unix) rather 
  40.  * than assume this one.
  41.  */
  42. char compchars[] = {' ', 'e', 't', 'n', 'r', 'o', 'a', 'i', 's',
  43.             'd', 'l', 'h', 'c', 'f', 'p', '\n' } ;
  44.  
  45. main()
  46. /* read encoded file from stdin and write text to stdout */
  47. {
  48.     int c ;
  49.  
  50.     do
  51.     {
  52.         c = getnibble() ;
  53.         if ( c != 0x0000000f ) macputchar( compchars[c] ) ;
  54.         else 
  55.         {
  56.             int c2 ;
  57.  
  58.             c = getnibble() ; c2 = getnibble() ;
  59.             /* need variables because C doesn't guarantee
  60.              * the order of evaluation of arguments.  So if
  61.              * c and c2 below were replaced by getnibble()'s
  62.              * there would be no way of knowing which of the
  63.              * next two nibbles on stdin would be shifted
  64.              */
  65.             macputchar( (c << 4) + c2 ) ;
  66.         }
  67.     }
  68.     while ( TRUE ) ; /* infinite loop. getnibble() exits on EOF */
  69. }
  70.  
  71. macputchar( c )
  72. /* translate returns to newlines and suppress nonprinting characters */
  73.     int c;
  74. {
  75.     if ( isprint(c) )
  76.     {
  77.         if ( c != '\r' ) putchar(c) ;
  78.         else putchar('\n');
  79.     }
  80. }
  81.  
  82. getnibble()
  83. /* return the next nibble from stdin.  exits on EOF. */
  84. {
  85.     static int got = FALSE ;
  86.     static int c ;
  87.  
  88.     if ( ! got )
  89.     {
  90.         if ( (c = getchar()) == EOF ) exit(1) ;
  91.         got = TRUE ;
  92.         return ( ( c >> 4 ) & 0x0000000f) ;
  93.     }
  94.     else
  95.     {
  96.         got = FALSE ;
  97.         return (c & 0x0000000f) ;
  98.     }
  99. }
  100.