2 /* Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4 * Gaius Mulley (gaius@glam.ac.uk) wrote output.cpp
5 * but it owes a huge amount of ideas and raw code from
6 * James Clark (jjc@jclark.com) grops/ps.cpp.
10 * provide the simple low level output routines needed by html.cpp
14 This file is part of groff.
16 groff is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free
18 Software Foundation; either version 2, or (at your option) any later
21 groff is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 You should have received a copy of the GNU General Public License along
27 with groff; see the file COPYING. If not, write to the Free Software
28 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
31 #include "stringclass.h"
52 #if defined(DEBUGGING)
53 # define FPUTC(X,Y) do { fputc((X),(Y)); fputc((X), stderr); fflush(stderr); } while (0)
54 # define FPUTS(X,Y) do { fputs((X),(Y)); fputs((X), stderr); fflush(stderr); } while (0)
55 # define PUTC(X,Y) do { putc((X),(Y)); putc((X), stderr); fflush(stderr); } while (0)
57 # define FPUTC(X,Y) do { fputc((X),(Y)); } while (0)
58 # define FPUTS(X,Y) do { fputs((X),(Y)); } while (0)
59 # define PUTC(X,Y) do { putc((X),(Y)); } while (0)
64 * word - initialise a word and set next to NULL
67 word::word (const char *w, int n)
76 * destroy word and the string copy.
85 * word_list - create an empty word list.
88 word_list::word_list ()
89 : length(0), head(0), tail(0)
94 * flush - flush a word list to a FILE, f, and return the
95 * length of the buffered string.
98 int word_list::flush (FILE *f)
112 #if defined(DEBUGGING)
113 fflush(f); // just for testing
119 * add_word - adds a word to the outstanding word list.
122 void word_list::add_word (const char *s, int n)
125 head = new word(s, n);
128 tail->next = new word(s, n);
135 * get_length - returns the number of characters buffered
138 int word_list::get_length (void)
144 * the classes and methods for simple_output manipulation
147 simple_output::simple_output(FILE *f, int n)
148 : fp(f), max_line_length(n), col(0), fixed_point(0), newlines(0)
152 simple_output &simple_output::set_file(FILE *f)
160 simple_output &simple_output::copy_file(FILE *infp)
163 while ((c = getc(infp)) != EOF)
168 simple_output &simple_output::end_line()
178 simple_output &simple_output::special(const char *)
183 simple_output &simple_output::simple_comment(const char *s)
195 simple_output &simple_output::begin_comment(const char *s)
203 last_word.add_word(s, strlen(s));
207 simple_output &simple_output::end_comment()
211 put_string("-->").nl();
216 * check_newline - checks to see whether we are able to issue
217 * a newline and that one is needed.
220 simple_output &simple_output::check_newline(int n)
222 if ((col + n + last_word.get_length() + 1 > max_line_length) && (newlines)) {
224 col = last_word.flush(fp);
230 * space_or_newline - will emit a newline or a space later on
231 * depending upon the current column.
234 simple_output &simple_output::space_or_newline (void)
236 if ((col + last_word.get_length() + 1 > max_line_length) && (newlines)) {
238 if (last_word.get_length() > 0) {
239 col = last_word.flush(fp);
244 if (last_word.get_length() != 0) {
249 col += last_word.flush(fp);
256 * force_nl - forces a newline.
259 simple_output &simple_output::force_nl (void)
262 col += last_word.flush(fp);
269 * nl - writes a newline providing that we
270 * are not in the first column.
273 simple_output &simple_output::nl (void)
276 col += last_word.flush(fp);
282 simple_output &simple_output::set_fixed_point(int n)
284 assert(n >= 0 && n <= 10);
289 simple_output &simple_output::put_raw_char(char c)
291 col += last_word.flush(fp);
297 simple_output &simple_output::put_string(const char *s, int n)
299 last_word.add_word(s, n);
303 simple_output &simple_output::put_string(const char *s)
305 last_word.add_word(s, strlen(s));
309 simple_output &simple_output::put_string(const string &s)
311 last_word.add_word(s.contents(), s.length());
315 simple_output &simple_output::put_number(int n)
317 char buf[1 + INT_DIGITS + 1];
318 sprintf(buf, "%d", n);
323 simple_output &simple_output::put_float(double d)
327 sprintf(buf, "%.4f", d);
332 simple_output &simple_output::enable_newlines (int auto_newlines)
335 newlines = auto_newlines;
341 * flush_last_word - flushes the last word and adjusts the
342 * col position. It will insert a newline
343 * before the last word if allowed and if
347 void simple_output::flush_last_word (void)
349 int len=last_word.get_length();
353 if (col + len + 1 > max_line_length) {
360 len += last_word.flush(fp);
364 col += last_word.flush(fp);