4 /* Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
6 * Gaius Mulley (gaius@glam.ac.uk) wrote output.cpp
7 * but it owes a huge amount of ideas and raw code from
8 * James Clark (jjc@jclark.com) grops/ps.cpp.
12 * provide the simple low level output routines needed by html.cpp
16 This file is part of groff.
18 groff is free software; you can redistribute it and/or modify it under
19 the terms of the GNU General Public License as published by the Free
20 Software Foundation; either version 2, or (at your option) any later
23 groff is distributed in the hope that it will be useful, but WITHOUT ANY
24 WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 You should have received a copy of the GNU General Public License along
29 with groff; see the file COPYING. If not, write to the Free Software
30 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
33 #include "stringclass.h"
54 #if defined(DEBUGGING)
55 # define FPUTC(X,Y) do { fputc((X),(Y)); fputc((X), stderr); fflush(stderr); } while (0)
56 # define FPUTS(X,Y) do { fputs((X),(Y)); fputs((X), stderr); fflush(stderr); } while (0)
57 # define PUTC(X,Y) do { putc((X),(Y)); putc((X), stderr); fflush(stderr); } while (0)
59 # define FPUTC(X,Y) do { fputc((X),(Y)); } while (0)
60 # define FPUTS(X,Y) do { fputs((X),(Y)); } while (0)
61 # define PUTC(X,Y) do { putc((X),(Y)); } while (0)
66 * word - initialise a word and set next to NULL
69 word::word (const char *w
, int n
)
78 * destroy word and the string copy.
87 * word_list - create an empty word list.
90 word_list::word_list ()
91 : length(0), head(0), tail(0)
96 * flush - flush a word list to a FILE, f, and return the
97 * length of the buffered string.
100 int word_list::flush (FILE *f
)
114 #if defined(DEBUGGING)
115 fflush(f
); // just for testing
121 * add_word - adds a word to the outstanding word list.
124 void word_list::add_word (const char *s
, int n
)
127 head
= new word(s
, n
);
130 tail
->next
= new word(s
, n
);
137 * get_length - returns the number of characters buffered
140 int word_list::get_length (void)
146 * the classes and methods for simple_output manipulation
149 simple_output::simple_output(FILE *f
, int n
)
150 : fp(f
), max_line_length(n
), col(0), fixed_point(0), newlines(0)
154 simple_output
&simple_output::set_file(FILE *f
)
162 simple_output
&simple_output::copy_file(FILE *infp
)
165 while ((c
= getc(infp
)) != EOF
)
170 simple_output
&simple_output::end_line()
180 simple_output
&simple_output::special(const char *)
185 simple_output
&simple_output::simple_comment(const char *s
)
197 simple_output
&simple_output::begin_comment(const char *s
)
205 last_word
.add_word(s
, strlen(s
));
209 simple_output
&simple_output::end_comment()
213 put_string("-->").nl();
218 * check_newline - checks to see whether we are able to issue
219 * a newline and that one is needed.
222 simple_output
&simple_output::check_newline(int n
)
224 if ((col
+ n
+ last_word
.get_length() + 1 > max_line_length
) && (newlines
)) {
226 col
= last_word
.flush(fp
);
232 * space_or_newline - will emit a newline or a space later on
233 * depending upon the current column.
236 simple_output
&simple_output::space_or_newline (void)
238 if ((col
+ last_word
.get_length() + 1 > max_line_length
) && (newlines
)) {
240 if (last_word
.get_length() > 0) {
241 col
= last_word
.flush(fp
);
246 if (last_word
.get_length() != 0) {
251 col
+= last_word
.flush(fp
);
258 * force_nl - forces a newline.
261 simple_output
&simple_output::force_nl (void)
264 col
+= last_word
.flush(fp
);
271 * nl - writes a newline providing that we
272 * are not in the first column.
275 simple_output
&simple_output::nl (void)
278 col
+= last_word
.flush(fp
);
284 simple_output
&simple_output::set_fixed_point(int n
)
286 assert(n
>= 0 && n
<= 10);
291 simple_output
&simple_output::put_raw_char(char c
)
293 col
+= last_word
.flush(fp
);
299 simple_output
&simple_output::put_string(const char *s
, int n
)
301 last_word
.add_word(s
, n
);
305 simple_output
&simple_output::put_string(const char *s
)
307 last_word
.add_word(s
, strlen(s
));
311 simple_output
&simple_output::put_string(const string
&s
)
313 last_word
.add_word(s
.contents(), s
.length());
317 simple_output
&simple_output::put_number(int n
)
319 char buf
[1 + INT_DIGITS
+ 1];
320 sprintf(buf
, "%d", n
);
325 simple_output
&simple_output::put_float(double d
)
329 sprintf(buf
, "%.4f", d
);
334 simple_output
&simple_output::enable_newlines (int auto_newlines
)
337 newlines
= auto_newlines
;
343 * flush_last_word - flushes the last word and adjusts the
344 * col position. It will insert a newline
345 * before the last word if allowed and if
349 void simple_output::flush_last_word (void)
351 int len
=last_word
.get_length();
355 if (col
+ len
+ 1 > max_line_length
) {
362 len
+= last_word
.flush(fp
);
366 col
+= last_word
.flush(fp
);