2 /* Copyright (C) 2000, 2001, 2003, 2004 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 ()
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)
167 while ((putPB(getPB()) != '\n') && (! eofFound
)) {
173 * skipUntilToken - skips until a token is seen
176 void pushBackBuffer::skipUntilToken (void)
180 while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound
)) {
189 * isString - returns TRUE if the string, s, matches the pushed back string.
190 * if TRUE is returned then this string is consumed, otherwise it is
194 int pushBackBuffer::isString (const char *s
)
196 int length
=strlen(s
);
199 while ((i
<length
) && (putPB(getPB())==s
[i
])) {
200 if (getPB() != s
[i
]) {
201 ERROR("assert failed");
210 if (putPB(s
[i
]) != s
[i
]) {
211 ERROR("assert failed");
220 * isDigit - returns TRUE if the character, ch, is a digit.
223 static int isDigit (char ch
)
225 return( ((ch
>='0') && (ch
<='9')) );
229 * isHexDigit - returns TRUE if the character, ch, is a hex digit.
233 static int isHexDigit (char ch
)
235 return( (isDigit(ch
)) || ((ch
>='a') && (ch
<='f')) );
240 * readInt - returns an integer from the input stream.
243 int pushBackBuffer::readInt (void)
250 while (isWhite(ch
)) {
259 while (isDigit(ch
)) {
261 if ((ch
>='0') && (ch
<='9')) {
267 if (ch
!= putPB(ch
)) {
268 ERROR("assert failed");
274 * convertToFloat - converts integers, a and b into a.b
277 static double convertToFloat (int a
, int b
)
285 f
= ((double)a
) + (((double)b
)/((double)c
));
290 * readNumber - returns a float representing the word just read.
293 double pushBackBuffer::readNumber (void)
299 if ((ch
= getPB()) == '.') {
300 return convertToFloat(i
, readInt());
307 * readString - reads a string terminated by white space
308 * and returns a malloced area of memory containing
309 * a copy of the characters.
312 char *pushBackBuffer::readString (void)
314 char buffer
[MAXPUSHBACKSTACK
];
319 while (isWhite(ch
)) {
322 while ((i
< MAXPUSHBACKSTACK
) && (! isWhite(ch
)) && (! eofFound
)) {
327 if (i
< MAXPUSHBACKSTACK
) {
329 str
= (char *)malloc(strlen(buffer
)+1);