2 * Copyright (c) 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 2000, 2001, 2003 - 2005 Free Software Foundation, Inc.
5 * Written by Gaius Mulley (gaius@glam.ac.uk).
7 * groff is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any later
12 * groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with groff; see the file COPYING. If not, write to the Free Software
19 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "html-config.h"
25 #include <sys/types.h>
38 #include "stringclass.h"
44 (void)(fprintf(stderr, "%s:%d error %s\n", __FILE__, __LINE__, X) &&\
45 (fflush(stderr)) && localexit(1))
47 pushBackBuffer::pushBackBuffer (char *filename
)
49 charStack
= (char *)malloc(MAXPUSHBACKSTACK
);
53 stackPtr
= 0; /* index to push back stack */
58 if (strcmp(filename
, "") != 0) {
61 if (open(filename
, O_RDONLY
) != 0) {
62 sys_fatal("when trying to open file");
69 pushBackBuffer::~pushBackBuffer ()
75 /* restore stdin in file descriptor 0 */
81 * localexit - wraps exit with a return code to aid the ERROR macro.
91 * getPB - returns a character, possibly a pushed back character.
94 char pushBackBuffer::getPB (void)
98 return( charStack
[stackPtr
] );
102 if (read(0, &ch
, 1) == 1) {
118 * putPB - pushes a character onto the push back stack.
119 * The same character is returned.
122 char pushBackBuffer::putPB (char ch
)
124 if (stackPtr
<MAXPUSHBACKSTACK
) {
125 charStack
[stackPtr
] = ch
;
128 ERROR("max push back stack exceeded, increase MAXPUSHBACKSTACK constant");
134 * isWhite - returns true if a white character is found. This character is NOT consumed.
137 static int isWhite (char ch
)
139 return( (ch
==' ') || (ch
== '\t') || (ch
== '\n') );
143 * skipToNewline - skips characters until a newline is seen.
146 void pushBackBuffer::skipToNewline (void)
148 while ((putPB(getPB()) != '\n') && (! eofFound
)) {
154 * skipUntilToken - skips until a token is seen
157 void pushBackBuffer::skipUntilToken (void)
161 while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound
)) {
170 * isString - returns true if the string, s, matches the pushed back string.
171 * if true is returned then this string is consumed, otherwise it is
175 int pushBackBuffer::isString (const char *s
)
177 int length
=strlen(s
);
180 while ((i
<length
) && (putPB(getPB())==s
[i
])) {
181 if (getPB() != s
[i
]) {
182 ERROR("assert failed");
191 if (putPB(s
[i
]) != s
[i
]) {
192 ERROR("assert failed");
201 * isDigit - returns true if the character, ch, is a digit.
204 static int isDigit (char ch
)
206 return( ((ch
>='0') && (ch
<='9')) );
210 * isHexDigit - returns true if the character, ch, is a hex digit.
214 static int isHexDigit (char ch
)
216 return( (isDigit(ch
)) || ((ch
>='a') && (ch
<='f')) );
221 * readInt - returns an integer from the input stream.
224 int pushBackBuffer::readInt (void)
231 while (isWhite(ch
)) {
240 while (isDigit(ch
)) {
242 if ((ch
>='0') && (ch
<='9')) {
248 if (ch
!= putPB(ch
)) {
249 ERROR("assert failed");
255 * convertToFloat - converts integers, a and b into a.b
258 static double convertToFloat (int a
, int b
)
266 f
= ((double)a
) + (((double)b
)/((double)c
));
271 * readNumber - returns a float representing the word just read.
274 double pushBackBuffer::readNumber (void)
280 if ((ch
= getPB()) == '.') {
281 return convertToFloat(i
, readInt());
288 * readString - reads a string terminated by white space
289 * and returns a malloced area of memory containing
290 * a copy of the characters.
293 char *pushBackBuffer::readString (void)
295 char buffer
[MAXPUSHBACKSTACK
];
300 while (isWhite(ch
)) {
303 while ((i
< MAXPUSHBACKSTACK
) && (! isWhite(ch
)) && (! eofFound
)) {
308 if (i
< MAXPUSHBACKSTACK
) {
310 str
= (char *)malloc(strlen(buffer
)+1);