2 /* Copyright (C) 2000-2014 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 3 of the License, or
19 (at your option) any later version.
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
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "stringclass.h"
51 #if defined(DEBUGGING)
52 # define FPUTC(X,Y) do { fputc((X),(Y)); fputc((X), stderr); fflush(stderr); } while (0)
53 # define FPUTS(X,Y) do { fputs((X),(Y)); fputs((X), stderr); fflush(stderr); } while (0)
54 # define PUTC(X,Y) do { putc((X),(Y)); putc((X), stderr); fflush(stderr); } while (0)
56 # define FPUTC(X,Y) do { fputc((X),(Y)); } while (0)
57 # define FPUTS(X,Y) do { fputs((X),(Y)); } while (0)
58 # define PUTC(X,Y) do { putc((X),(Y)); } while (0)
63 * word - initialise a word and set next to NULL
66 word::word (const char *w, int n)
75 * destroy word and the string copy.
84 * word_list - create an empty word list.
87 word_list::word_list ()
88 : length(0), head(0), tail(0)
93 * flush - flush a word list to a FILE, f, and return the
94 * length of the buffered string.
97 int word_list::flush (FILE *f)
111 #if defined(DEBUGGING)
112 fflush(f); // just for testing
118 * add_word - adds a word to the outstanding word list.
121 void word_list::add_word (const char *s, int n)
124 head = new word(s, n);
127 tail->next = new word(s, n);
134 * get_length - returns the number of characters buffered
137 int word_list::get_length (void)
143 * the classes and methods for simple_output manipulation
146 simple_output::simple_output(FILE *f, int n)
147 : fp(f), max_line_length(n), col(0), fixed_point(0), newlines(0)
151 simple_output &simple_output::set_file(FILE *f)
159 simple_output &simple_output::copy_file(FILE *infp)
162 while ((c = getc(infp)) != EOF)
167 simple_output &simple_output::end_line()
177 simple_output &simple_output::special(const char *)
182 simple_output &simple_output::simple_comment(const char *s)
194 simple_output &simple_output::begin_comment(const char *s)
202 last_word.add_word(s, strlen(s));
206 simple_output &simple_output::end_comment()
210 put_string("-->").nl();
215 * check_newline - checks to see whether we are able to issue
216 * a newline and that one is needed.
219 simple_output &simple_output::check_newline(int n)
221 if ((col + n + last_word.get_length() + 1 > max_line_length) && (newlines)) {
223 col = last_word.flush(fp);
229 * space_or_newline - will emit a newline or a space later on
230 * depending upon the current column.
233 simple_output &simple_output::space_or_newline (void)
235 if ((col + last_word.get_length() + 1 > max_line_length) && (newlines)) {
237 if (last_word.get_length() > 0) {
238 col = last_word.flush(fp);
243 if (last_word.get_length() != 0) {
248 col += last_word.flush(fp);
255 * force_nl - forces a newline.
258 simple_output &simple_output::force_nl (void)
261 col += last_word.flush(fp);
268 * nl - writes a newline providing that we
269 * are not in the first column.
272 simple_output &simple_output::nl (void)
275 col += last_word.flush(fp);
281 simple_output &simple_output::set_fixed_point(int n)
283 assert(n >= 0 && n <= 10);
288 simple_output &simple_output::put_raw_char(char c)
290 col += last_word.flush(fp);
296 simple_output &simple_output::put_string(const char *s, int n)
298 last_word.add_word(s, n);
302 simple_output &simple_output::put_string(const char *s)
304 last_word.add_word(s, strlen(s));
308 simple_output &simple_output::put_string(const string &s)
310 last_word.add_word(s.contents(), s.length());
314 simple_output &simple_output::put_number(int n)
316 char buf[1 + INT_DIGITS + 1];
317 sprintf(buf, "%d", n);
322 simple_output &simple_output::put_float(double d)
326 sprintf(buf, "%.4f", d);
331 simple_output &simple_output::enable_newlines (int auto_newlines)
334 newlines = auto_newlines;
340 * flush_last_word - flushes the last word and adjusts the
341 * col position. It will insert a newline
342 * before the last word if allowed and if
346 void simple_output::flush_last_word (void)
348 int len=last_word.get_length();
352 if (col + len + 1 > max_line_length) {
359 len += last_word.flush(fp);
363 col += last_word.flush(fp);