lex.c revision 65912f00
1/* $XFree86: xc/programs/xditview/lex.c,v 1.3 2000/12/04 21:01:01 dawes Exp $ */
2
3#include <X11/Xos.h>
4#include <X11/IntrinsicP.h>
5#include <X11/StringDefs.h>
6#include <stdio.h>
7#include <ctype.h>
8#include "DviP.h"
9
10int
11DviGetAndPut(DviWidget dw, int *cp)
12{
13    if (dw->dvi.ungot)
14    {
15	dw->dvi.ungot = 0;
16	*cp = getc (dw->dvi.file);
17    }
18    else
19    {
20	*cp = getc (dw->dvi.file);
21	putc (*cp, dw->dvi.tmpFile);
22    }
23    return *cp;
24}
25
26char *
27GetLine(DviWidget dw, char *Buffer, int Length)
28{
29	int 	i = 0, c;
30	char	*p = Buffer;
31
32	Length--;			    /* Save room for final NULL */
33
34	while ((!p || i < Length) && DviGetC (dw, &c) != EOF && c != '\n')
35		if (p)
36			*p++ = c;
37#if 0
38	if (c == '\n' && p)		    /* Retain the newline like fgets */
39		*p++ = c;
40#endif
41	if (c == '\n')
42		DviUngetC(dw, c);
43	if (p)
44		*p = '\0';
45	return (Buffer);
46}
47
48char *
49GetWord(DviWidget dw, char *Buffer, int Length)
50{
51	int 	i = 0, c;
52	char	*p = Buffer;
53
54	Length--;			    /* Save room for final NULL */
55	while (DviGetC(dw, &c) != EOF && isspace(c))
56		;
57	if (c != EOF)
58		DviUngetC(dw, c);
59	while (i < Length && DviGetC(dw, &c) != EOF && !isspace(c))
60		if (p)
61			*p++ = c;
62	if (c != EOF)
63		DviUngetC(dw, c);
64	if (p)
65		*p = '\0';
66	return (Buffer);
67}
68
69int
70GetNumber(DviWidget dw)
71{
72	int	i = 0,  c;
73
74	while (DviGetC(dw, &c) != EOF && isspace(c))
75		;
76	if (c != EOF)
77		DviUngetC(dw, c);
78	while (DviGetC(dw, &c) != EOF && isdigit(c))
79		i = i*10 + c - '0';
80	if (c != EOF)
81		DviUngetC(dw, c);
82	return (i);
83}
84