home *** CD-ROM | disk | FTP | other *** search
- /*
- PAGE.CPP
- Copyright (c) Les Hancock 1990
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stream.hpp>
- #include "list.hpp"
- #include "parms.hpp"
- #include "line.hpp"
- #include "header.hpp"
- #include "page.hpp"
-
- page::page(parms& parms, const char *file_name) :
- next_line((parms.max_cols - parms.gutter) / 2,
- (unsigned int) parms.tab_size)
- {
- rows = parms.max_rows;
- cols = parms.max_cols;
- tab_size = parms.tab_size;
- gutter = parms.gutter;
- if (parms.title == 1) // if user chose the title option, create header
- {
- hdr_ptr = new header(file_name, cols);
- rows -= 2; // header gets two rows to itself
- }
- else
- hdr_ptr = (header *) 0; // no header
- bufptr = new char [rows * cols];
- end_of_input = 0; // not at end of input stream
- new_page(); // blank page
- }
-
- /*
- If new line available via input stream, insert
- it in the page, else this page is full and we're done.
- */
- istream& operator>>(istream& in, page& pg)
- {
- while (pg.full() == no) // until the page is full
- {
- in >> pg.next_line; // get next line from input stream
- if (pg.next_line.is_eof() == yes) // if no more input available ...
- pg.full(pg.eof(yes)); // set private flags to show it's done
- else // else we got a new line; insert it, and set page-full flag ...
- pg.full(pg.insert(pg.next_line.head)); // ... to show result
- }
- return in;
- }
-
- /*
- Print the page to ostream.
- */
- ostream& operator<<(ostream& out, page& pg)
- {
- if (pg.current_row != 0)
- {
- if (pg.hdr_ptr != 0) // if user chose title option, print header
- out << *pg.hdr_ptr;
- for (int i = 0; i < pg.rows; ++i)
- {
- out << ' ';
- for (int j = 0; j < pg.cols; ++j)
- out << *(pg.bufptr + i * pg.cols + j);
- out << '\n';
- }
- }
- pg.new_page();
- return out;
- }
-
- /*
- Given a line of text, insert it into the page.
- It's guaranteed to fit -- your job is to put it in
- at the right spot. If insertion completes the page
- return yes, else return no.
- */
- yesno
- page::insert(char *your_buf)
- {
- memmove(bufptr // target: head of buffer plus appropriate offsets, viz:
- + ((cols - gutter) / 2 + gutter) // col in which right-hand side begins
- * (current_row / rows) // 0 for left-hand side, 1 for right-hand side
- + (current_row % rows) * cols, // number of rows down
- your_buf, // source is of course this buffer
- (cols - gutter) / 2); // and this is the size of a line
- if (++current_row == 2 * rows)
- return yes; // yes, page is full
- return no; // no, page is not full
- }
-
- /*
- Re-use buffer for new page.
- */
- void
- page::new_page()
- {
- current_row = 0;
- page_full = 0; // not full, not at end
- memset(bufptr, ' ', rows * cols);
- }
-