home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / unix_mw2.sha / md.c < prev    next >
C/C++ Source or Header  |  1985-04-26  |  5KB  |  349 lines

  1. #include    <stdio.h>
  2. #include    <ctype.h>
  3.  
  4. #include    "gen.h"
  5. #include    "option.h"
  6. #include    "md.h"
  7.  
  8. LOCAL    FILEPTR    p_offset    = -1;
  9. LOCAL    FILEPTR    h_offset    = -1;
  10. LOCAL    FILEPTR    f_offset    = -1;
  11.  
  12. LOCAL    bool    display_header    = TRUE;
  13. LOCAL    bool    display_footer    = TRUE;
  14.  
  15. LOCAL    int    document_count;
  16. LOCAL    int    header_count;
  17. LOCAL    int    footer_count;
  18.  
  19. int    page_cnt        = 0;
  20. int    page_no            = 0;
  21. bool    title_page        = FALSE;
  22.  
  23. read_globals()
  24.     {
  25.  
  26.     int    version;
  27.     REG    int    i;
  28.     int    length;
  29.  
  30.     if( (version = get_int(infd)) != MW_ID )
  31.         warning( "Unknown Macwrite version %d", version );
  32.  
  33.     p_offset = get_int(infd);
  34.     document_count = get_int(infd);
  35.     header_count = get_int(infd);
  36.     footer_count = get_int(infd);
  37.  
  38.     title_page = get_byte(infd);
  39.     (void)get_byte(infd);            /* display scrap */
  40.     display_footer =  get_byte(infd);
  41.     display_header =  get_byte(infd);
  42.     (void)get_byte(infd);            /* rulers showing */
  43.     (void)get_byte(infd);            /* spare byte (?) */
  44.     (void)get_byte(infd);            /* active document */
  45.     page_no = get_int(infd) - 1;
  46.  
  47.     /*  get the seek locations of the header and footer info  */
  48.     set( infd, p_offset );
  49.     for( i=0; i<document_count; i++ )
  50.         {
  51.         (void)get_int(infd);    /* paragraph type */
  52.         length = get_int(infd);    /* paragraph length */
  53.  
  54.         skip( infd, length );
  55.         }
  56.     
  57.     h_offset = pos(infd);
  58.  
  59.  
  60.     for( i=0; i<header_count; i++ )
  61.         {
  62.         (void)get_int(infd);    /* paragraph type */
  63.         length = get_int(infd);    /* paragraph length */
  64.  
  65.         skip( infd, length );
  66.         }
  67.     
  68.     /*    set the bottom margin, based on the footer length */
  69.     f_offset = pos(infd);
  70.     init_footer();
  71.  
  72.     if( verbose )
  73.         {
  74.         fprintf( stderr, "Macwrite(%d) %d header, %d body, %d footer paragraghs\n", 
  75.         version, header_count, document_count,  footer_count );
  76.         fprintf( stderr, "footers %s headers %s\n", 
  77.             display_footer ? "displayed" : "hidden",
  78.             display_footer ? "displayed" : "hidden" );
  79.         }
  80.     }
  81.  
  82.  
  83. read_paragraphs( off, count )
  84. FILEPTR    off;
  85. int    count;
  86.     {
  87.     int    i;
  88.  
  89.     set(infd, off);
  90.  
  91.     for( i=0; i<count; i++ )
  92.         paragraph();
  93.     
  94.     }
  95.  
  96. body()
  97.     {
  98.     read_paragraphs( p_offset, document_count );
  99.     }
  100.  
  101. header()
  102.     {
  103.     FILEPTR    save_pos;
  104.  
  105.     if( !display_header )
  106.         return;
  107.  
  108.     save_pos = pos(infd);
  109.     save_ruler();
  110.  
  111.     read_paragraphs( h_offset, header_count );
  112.  
  113.     restore_ruler();
  114.     set( infd, save_pos );
  115.     }
  116.  
  117.  
  118. footer()
  119.     {
  120.     FILEPTR    save_pos;
  121.  
  122.     if( !display_footer )
  123.         return;
  124.  
  125.     save_pos = pos(infd);
  126.     save_ruler();
  127.  
  128.     read_paragraphs( f_offset, footer_count );
  129.  
  130.     restore_ruler();
  131.     set( infd, save_pos );
  132.     }
  133.  
  134.  
  135. paragraph()
  136.     {
  137.     int    type;
  138.     int    length;
  139.     int    line_length;
  140.     int    flength;
  141.     FILEPTR    paragraph_end;
  142.     REG    int    i;
  143.  
  144.     byte    cur_style;
  145.     byte    cur_size;
  146.     short    cur_font;
  147.  
  148.     Char    buf[200];
  149.     bool    freeline = FALSE;
  150.     Char    * line = buf;
  151.  
  152.     int    chpos;
  153.     int    end_pos;
  154.  
  155.  
  156.     if( (type = get_int(infd)) != RULER 
  157.     &&  type != TEXT
  158.     &&  type != PICTURE )
  159.         fatal( "botch - invalid paragraph type %d in macwrite file",
  160.             type );
  161.     
  162.     length = get_int(infd);
  163.     paragraph_end = pos(infd) + length;
  164.  
  165.     if( type == TEXT )
  166.         {
  167.  
  168.         line_length = get_int(infd);
  169.         if( line_length > 200 )
  170.             {
  171.             line = (Char *)malloc( sizeof(Char) * line_length );
  172.             if( line == NULL )
  173.                 fatal( "fatal - out of space\n" );
  174.             freeline = TRUE;
  175.             }
  176.  
  177.  
  178.         for( i=0; i<line_length; i++ )
  179.             line[i].c_char = get_byte(infd);
  180.  
  181.         if( line_length % 2 )
  182.             skip(infd,1);
  183.  
  184.         /*    get font info    */
  185.         if( (flength = get_int(infd)) % 6 != 0 )
  186.             {
  187.             fatal( "invalid font length (%d) in macwrite file\n",
  188.             flength );
  189.             }
  190.  
  191.         for( chpos = get_int(infd); flength; chpos = end_pos )
  192.             {
  193.             cur_size = get_byte(infd);
  194.             cur_style = get_byte(infd);
  195.             cur_font = get_int(infd);
  196.  
  197.             flength -= 6;
  198.             if( flength )
  199.                 end_pos = get_int(infd);
  200.             else
  201.                 end_pos = line_length;
  202.  
  203.             
  204.             for( i=chpos; i<end_pos; i++ )
  205.                 {
  206.                 line[i].c_size = cur_size;
  207.                 line[i].c_style = cur_style;
  208.                 line[i].c_font = cur_font;
  209.                 }
  210.             }
  211.         
  212.         /*
  213.          *    mystery bytes 
  214.          */
  215.         set( infd, paragraph_end );
  216.  
  217.         out_line( line_length, line );
  218.  
  219.         if( freeline )
  220.             free( line );
  221.         }
  222.     else
  223.     if( type == PICTURE )
  224.         {
  225.  
  226.         /*    page breaks are pictures    */
  227.         if( (length = get_int(infd)) == 0 )
  228.             page();
  229.         else
  230.             warning( "pictures not yet supported" );
  231.         set( infd, paragraph_end );
  232.  
  233.         }
  234.     else
  235.     if( type == RULER )
  236.         {
  237.         PIX    lmargin;
  238.         PIX    rmargin;
  239.         PIX    indent;
  240.         PIX    tab;
  241.         int    tablen;
  242.         int    spacing;
  243.         int    justify;
  244.  
  245.         lmargin = get_int(infd);
  246.         rmargin = get_int(infd);
  247.  
  248.         justify = get_byte(infd);
  249.         
  250.         tablen = get_byte(infd);
  251.         get_byte(infd);        /* ??? mystery byte */
  252.  
  253.         spacing = get_byte(infd);
  254.         indent = get_int(infd);
  255.  
  256.         set_ruler( lmargin, rmargin, indent, spacing, justify );
  257.  
  258.         /*
  259.          *    pick up the tab stops
  260.          */
  261.         clr_tabs();
  262.  
  263.         for( i=0; i<tablen; i++ )
  264.             {
  265.             set_tab( get_int(infd) );
  266.             }
  267.         
  268.         for( i=0; i < 10-flength; i++ )
  269.             get_int(infd);
  270.         
  271.         /*
  272.          *    more mystery bytes
  273.          */
  274.         get_int(infd);
  275.         get_int(infd);
  276.  
  277.         set( infd, paragraph_end );
  278.  
  279.         }
  280.  
  281.     }
  282.  
  283.  
  284. get_int(fd)
  285. FILE    * fd;
  286.     {
  287.     int    ch;
  288.  
  289.     ch = get_byte(fd);
  290.     ch = ch << 8 | get_byte(fd);
  291.  
  292.     return( ch );
  293.     }
  294.  
  295. long
  296. get_long(fd)
  297. FILE    * fd;
  298.     {
  299.     long    val;
  300.     int    i;
  301.  
  302.     for( val=0, i=0; i<4; i++ )
  303.         {
  304.         val <<= 8;
  305.         val |= get_byte(fd);
  306.         }
  307.     
  308.     return( val );
  309.     }
  310.  
  311. int
  312. get_byte(fd)
  313. FILE    * fd;
  314.     {
  315.     int    ch;
  316.  
  317.     if( (ch = getc(fd)) == EOF )
  318.         fatal( "unexpected EOF" );
  319.  
  320.     return( ch & 0xff );
  321.     }
  322.  
  323.  
  324. char    * 
  325. Style( style_no )
  326. byte    style_no;
  327.     {
  328.  
  329.     style_no &= (ST_PLAIN|ST_BOLD|ST_ITALIC);
  330.  
  331.     switch( style_no )
  332.         {
  333.         case ST_PLAIN:
  334.             return( "r" );
  335.  
  336.         case ST_BOLD:
  337.             return( "b" );
  338.  
  339.         case ST_ITALIC:
  340.             return( "ti" );
  341.  
  342.         default:
  343.             fprintf( stderr, "unknown style %d\n", style_no );
  344.             break;
  345.         }
  346.     return( "??" );
  347.  
  348.     }
  349.