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