|
|
1.1 root 1: /*
2: *
3: * Copyright (C) 1983, 86 by Lee Moore. All rights reserved.
4: *
5: * Token package
6: */
7:
8: #include <stdio.h>
9: #include <ctype.h>
10: #include "token.h"
11:
12: #define TRUE 1
13: #define FALSE 0
14:
15:
16: extern char *malloc();
17:
18:
19: /*
20: * Get one character from the input. Set flags.
21: */
22:
23: static int GetCharacter1(ts)
24: struct TokenState *ts; {
25: int c;
26:
27: c = getc(ts->Input);
28: ts->NotEndOfFile = (c != EOF);
29: return c; }
30:
31:
32:
33: /*
34: * Get one character from the input. Set Flags. (see above)
35: * Ignore comments.
36: */
37:
38: static int GetCharacter(ts)
39: struct TokenState *ts; {
40: int c;
41:
42: c = GetCharacter1(ts);
43:
44: if( c == '\\' ) /* quote the next character? */
45: c = GetCharacter1(ts);
46:
47: else if( c == '#' ) /* if this a comment? */
48: while( c != '\n' && c != EOF )
49: c = GetCharacter1(ts);
50:
51: return c; }
52:
53:
54: /*
55: * Skip over blank space. Set LastTokenInLine if we see at a new-line.
56: */
57:
58: static SkipBlankSpace(ts)
59: struct TokenState *ts; {
60: ts->LastTokenInLine = FALSE;
61:
62: while( isspace(ts->CurChar) ) {
63: if( ts->CurChar == '\n' )
64: ts->LastTokenInLine = TRUE;
65:
66: ts->CurChar = GetCharacter(ts); } }
67:
68:
69: /*
70: * Initialize token package
71: */
72:
73: struct TokenState *InitTokenStream(from)
74: FILE *from; {
75: struct TokenState *ts;
76:
77: ts = (struct TokenState *) malloc((unsigned) sizeof(struct TokenState));
78: ts->LastTokenInLine = FALSE;
79: ts->NotEndOfFile = FALSE,
80: ts->Input = from;
81: ts->CurChar = GetCharacter(ts);
82: SkipBlankSpace(ts);
83: return ts; }
84:
85:
86:
87: /*
88: * Close and release resources
89: */
90:
91: CloseTokenStream(ts)
92: struct TokenState *ts; {
93: (void) fclose(ts->Input);
94: free((char *)ts); }
95:
96:
97:
98: /*
99: * Get the next token in the input. Tokens are strings of characters that are
100: * delimited by <space>, <tab> and <new-line>
101: */
102:
103: GetToken(ts, arg, maxSize)
104: struct TokenState *ts;
105: char *arg;
106: int maxSize; {
107: while( !isspace(ts->CurChar) && ts->CurChar != EOF && maxSize > 0 ) {
108: *arg++ = ts->CurChar;
109: ts->CurChar = GetCharacter(ts);
110: maxSize--; }
111:
112: if( maxSize > 0 )
113: *arg = '\0';
114:
115: SkipBlankSpace(ts);
116: return; }
117:
118:
119: /*
120: * Test if we have read all the tokens in the current line
121: */
122:
123: EndOfLine(ts)
124: struct TokenState *ts; {
125: return ts->LastTokenInLine; }
126:
127: /*
128: * return true if we have read all the tokens in the file
129: */
130:
131: EndOfFile(ts)
132: struct TokenState *ts; {
133: return !ts->NotEndOfFile; }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.