2 /* Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Written by Gaius Mulley (gaius@glam.ac.uk).
5 This file is part of groff.
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. */
30 #include "stringclass.h"
35 #include <sys/types.h>
51 # define ERROR(X) (void)(fprintf(stderr, "%s:%d error %s\n", __FILE__, __LINE__, X) && \
52 (fflush(stderr)) && localexit(1))
55 #define MAXPUSHBACKSTACK 4096 /* maximum number of character that can be pushed back */
59 * constructor for pushBackBuffer
62 pushBackBuffer::pushBackBuffer (char *filename
)
64 charStack
= (char *)malloc(MAXPUSHBACKSTACK
);
68 stackPtr
= 0; /* index to push back stack */
73 if (strcmp(filename
, "") != 0) {
76 if (open(filename
, O_RDONLY
) != 0) {
77 sys_fatal("when trying to open file");
84 pushBackBuffer::~pushBackBuffer ()
90 /* restore stdin in file descriptor 0 */
96 * localexit - wraps exit with a return code to aid the ERROR macro.
106 * getPB - returns a character, possibly a pushed back character.
109 char pushBackBuffer::getPB (void)
113 return( charStack
[stackPtr
] );
117 if (read(0, &ch
, 1) == 1) {
133 * putPB - pushes a character onto the push back stack.
134 * The same character is returned.
137 char pushBackBuffer::putPB (char ch
)
139 if (stackPtr
<MAXPUSHBACKSTACK
) {
140 charStack
[stackPtr
] = ch
;
143 ERROR("max push back stack exceeded, increase MAXPUSHBACKSTACK constant");
149 * isWhite - returns TRUE if a white character is found. This character is NOT consumed.
152 static int isWhite (char ch
)
154 return( (ch
==' ') || (ch
== '\t') || (ch
== '\n') );
158 * skipToNewline - skips characters until a newline is seen.
161 void pushBackBuffer::skipToNewline (void)
163 while ((putPB(getPB()) != '\n') && (! eofFound
)) {
169 * skipUntilToken - skips until a token is seen
172 void pushBackBuffer::skipUntilToken (void)
176 while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound
)) {
185 * isString - returns TRUE if the string, s, matches the pushed back string.
186 * if TRUE is returned then this string is consumed, otherwise it is
190 int pushBackBuffer::isString (const char *s
)
192 int length
=strlen(s
);
195 while ((i
<length
) && (putPB(getPB())==s
[i
])) {
196 if (getPB() != s
[i
]) {
197 ERROR("assert failed");
206 if (putPB(s
[i
]) != s
[i
]) {
207 ERROR("assert failed");
216 * isDigit - returns TRUE if the character, ch, is a digit.
219 static int isDigit (char ch
)
221 return( ((ch
>='0') && (ch
<='9')) );
225 * isHexDigit - returns TRUE if the character, ch, is a hex digit.
229 static int isHexDigit (char ch
)
231 return( (isDigit(ch
)) || ((ch
>='a') && (ch
<='f')) );
236 * readInt - returns an integer from the input stream.
239 int pushBackBuffer::readInt (void)
246 while (isWhite(ch
)) {
255 while (isDigit(ch
)) {
257 if ((ch
>='0') && (ch
<='9')) {
263 if (ch
!= putPB(ch
)) {
264 ERROR("assert failed");
270 * convertToFloat - converts integers, a and b into a.b
273 static double convertToFloat (int a
, int b
)
281 f
= ((double)a
) + (((double)b
)/((double)c
));
286 * readNumber - returns a float representing the word just read.
289 double pushBackBuffer::readNumber (void)
295 if ((ch
= getPB()) == '.') {
296 return convertToFloat(i
, readInt());
303 * readString - reads a string terminated by white space
304 * and returns a malloced area of memory containing
305 * a copy of the characters.
308 char *pushBackBuffer::readString (void)
310 char buffer
[MAXPUSHBACKSTACK
];
315 while (isWhite(ch
)) {
318 while ((i
< MAXPUSHBACKSTACK
) && (! isWhite(ch
)) && (! eofFound
)) {
323 if (i
< MAXPUSHBACKSTACK
) {
325 str
= (char *)malloc(strlen(buffer
)+1);