4 /* Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Written by Gaius Mulley (gaius@glam.ac.uk).
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING. If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
32 #include "stringclass.h"
37 #include <sys/types.h>
53 # define ERROR(X) (void)(fprintf(stderr, "%s:%d error %s\n", __FILE__, __LINE__, X) && \
54 (fflush(stderr)) && localexit(1))
57 #define MAXPUSHBACKSTACK 4096 /* maximum number of character that can be pushed back */
61 * constructor for pushBackBuffer
64 pushBackBuffer::pushBackBuffer (char *filename
)
66 charStack
= (char *)malloc(MAXPUSHBACKSTACK
);
70 stackPtr
= 0; /* index to push back stack */
75 if (strcmp(filename
, "") != 0) {
78 if (open(filename
, O_RDONLY
) != 0) {
79 sys_fatal("when trying to open file");
86 pushBackBuffer::~pushBackBuffer ()
92 /* restore stdin in file descriptor 0 */
98 * localexit - wraps exit with a return code to aid the ERROR macro.
101 int localexit (int i
)
108 * getPB - returns a character, possibly a pushed back character.
111 char pushBackBuffer::getPB (void)
115 return( charStack
[stackPtr
] );
119 if (read(0, &ch
, 1) == 1) {
135 * putPB - pushes a character onto the push back stack.
136 * The same character is returned.
139 char pushBackBuffer::putPB (char ch
)
141 if (stackPtr
<MAXPUSHBACKSTACK
) {
142 charStack
[stackPtr
] = ch
;
145 ERROR("max push back stack exceeded, increase MAXPUSHBACKSTACK constant");
151 * isWhite - returns TRUE if a white character is found. This character is NOT consumed.
154 static int isWhite (char ch
)
156 return( (ch
==' ') || (ch
== '\t') || (ch
== '\n') );
160 * skipToNewline - skips characters until a newline is seen.
163 void pushBackBuffer::skipToNewline (void)
165 while ((putPB(getPB()) != '\n') && (! eofFound
)) {
171 * skipUntilToken - skips until a token is seen
174 void pushBackBuffer::skipUntilToken (void)
178 while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound
)) {
187 * isString - returns TRUE if the string, s, matches the pushed back string.
188 * if TRUE is returned then this string is consumed, otherwise it is
192 int pushBackBuffer::isString (const char *s
)
194 int length
=strlen(s
);
197 while ((i
<length
) && (putPB(getPB())==s
[i
])) {
198 if (getPB() != s
[i
]) {
199 ERROR("assert failed");
208 if (putPB(s
[i
]) != s
[i
]) {
209 ERROR("assert failed");
218 * isDigit - returns TRUE if the character, ch, is a digit.
221 static int isDigit (char ch
)
223 return( ((ch
>='0') && (ch
<='9')) );
227 * isHexDigit - returns TRUE if the character, ch, is a hex digit.
231 static int isHexDigit (char ch
)
233 return( (isDigit(ch
)) || ((ch
>='a') && (ch
<='f')) );
238 * readInt - returns an integer from the input stream.
241 int pushBackBuffer::readInt (void)
248 while (isWhite(ch
)) {
257 while (isDigit(ch
)) {
259 if ((ch
>='0') && (ch
<='9')) {
265 if (ch
!= putPB(ch
)) {
266 ERROR("assert failed");
272 * convertToFloat - converts integers, a and b into a.b
275 static double convertToFloat (int a
, int b
)
283 f
= ((double)a
) + (((double)b
)/((double)c
));
288 * readNumber - returns a float representing the word just read.
291 double pushBackBuffer::readNumber (void)
297 if ((ch
= getPB()) == '.') {
298 return convertToFloat(i
, readInt());
305 * readString - reads a string terminated by white space
306 * and returns a malloced area of memory containing
307 * a copy of the characters.
310 char *pushBackBuffer::readString (void)
312 char buffer
[MAXPUSHBACKSTACK
];
317 while (isWhite(ch
)) {
320 while ((i
< MAXPUSHBACKSTACK
) && (! isWhite(ch
)) && (! eofFound
)) {
325 if (i
< MAXPUSHBACKSTACK
) {
327 str
= (char *)malloc(strlen(buffer
)+1);