home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / LASER / DJPRNT.ZIP / PAGE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-10  |  3.0 KB  |  103 lines

  1. /*
  2.     PAGE.CPP
  3.     Copyright (c) Les Hancock 1990
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stream.hpp>
  9. #include "list.hpp"
  10. #include "parms.hpp"
  11. #include "line.hpp"
  12. #include "header.hpp"
  13. #include "page.hpp"
  14.  
  15. page::page(parms& parms, const char *file_name) : 
  16.     next_line((parms.max_cols - parms.gutter) / 2,
  17.         (unsigned int) parms.tab_size)
  18. {
  19.     rows = parms.max_rows;
  20.     cols = parms.max_cols;
  21.     tab_size = parms.tab_size;
  22.     gutter = parms.gutter;
  23.     if (parms.title == 1)       // if user chose the title option, create header
  24.     {
  25.         hdr_ptr = new header(file_name, cols);
  26.         rows -= 2;                                // header gets two rows to itself
  27.     }
  28.     else
  29.         hdr_ptr = (header *) 0;                                        // no header
  30.     bufptr = new char [rows * cols];    
  31.     end_of_input = 0;                                    // not at end of input stream
  32.     new_page();                                                      // blank page
  33. }
  34.  
  35. /*
  36.     If new line available via input stream, insert
  37.     it in the page, else this page is full and we're done.
  38. */
  39. istream& operator>>(istream& in, page& pg)
  40. {
  41.     while (pg.full() == no)                              // until the page is full
  42.     {
  43.         in >> pg.next_line;                   // get next line from input stream
  44.         if (pg.next_line.is_eof() == yes)     // if no more input available ...
  45.             pg.full(pg.eof(yes));          // set private flags to show it's done
  46.         else   // else we got a new line; insert it, and set page-full flag ...
  47.             pg.full(pg.insert(pg.next_line.head));          // ... to show result
  48.     }
  49.     return in;
  50. }
  51.  
  52. /*
  53.     Print the page to ostream.
  54. */
  55. ostream& operator<<(ostream& out, page& pg)
  56. {
  57.     if (pg.current_row != 0)
  58.     {
  59.         if (pg.hdr_ptr != 0)         // if user chose title option, print header
  60.              out << *pg.hdr_ptr;
  61.         for (int i = 0; i < pg.rows; ++i)
  62.         {
  63.             out << ' ';
  64.             for (int j = 0; j < pg.cols; ++j)
  65.                 out << *(pg.bufptr + i * pg.cols + j);
  66.             out << '\n';
  67.         }
  68.     }
  69.     pg.new_page();
  70.     return out;
  71. }
  72.  
  73. /*
  74.     Given a line of text, insert it into the page.
  75.     It's guaranteed to fit -- your job is to put it in
  76.     at the right spot.  If insertion completes the page
  77.     return yes, else return no.
  78. */
  79. yesno
  80. page::insert(char *your_buf)
  81. {
  82.     memmove(bufptr     // target: head of buffer plus appropriate offsets, viz:
  83.     + ((cols - gutter) / 2 + gutter)     // col in which right-hand side begins
  84.        * (current_row / rows)      // 0 for left-hand side, 1 for right-hand side
  85.     + (current_row % rows) * cols,                       // number of rows down
  86.     your_buf,                                // source is of course this buffer
  87.     (cols - gutter) / 2);                      // and this is the size of a line
  88.     if (++current_row == 2 * rows)
  89.         return yes;                                            // yes, page is full
  90.     return no;                                          // no, page is not full
  91. }
  92.  
  93. /*
  94.     Re-use buffer for new page.
  95. */
  96. void
  97. page::new_page()
  98. {
  99.     current_row = 0;
  100.     page_full = 0;                                    // not full, not at end
  101.     memset(bufptr, ' ', rows * cols);
  102. }
  103.