-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.h
101 lines (74 loc) · 2.65 KB
/
stream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef _HAD_STREAM_H
#define _HAD_STREAM_H
/*
$NiH: stream.h,v 1.6 2002/04/22 22:12:31 dillo Exp $
stream.h -- stream interface
Copyright (C) 2002 Dieter Baron and Thomas Klausner
This file is part of cg, a program to assemble and decode binary Usenet
postings. The authors can be contacted at <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdio.h>
enum token_type { TOK_EOF = -1, TOK_LINE, TOK_DATA,
TOK_EOS, TOK_EOH, TOK_EOA, TOK_EOP,
TOK_FNAME, TOK_DEBUG, TOK_ERR };
/* if you change this, don't forget to update initializer of tok_eof
in stream.c */
struct token {
enum token_type type;
int n; /* data len or error number */
int alloced; /* is line malloc()ed? */
char *line;
};
#define TOK_ERR_WARNING 0
#define TOK_ERR_ERROR 1
typedef struct token token;
struct token_list {
token tok;
struct token_list *next;
};
typedef struct token_list token_list;
struct stream {
token *(*get)();
int (*close)();
token tok;
token *tok_cleanup; /* token to free on next get */
token_list queue;
token_list *queue_tail;
int queue_len;
int queue_cleanup;
struct stream *source;
int eof;
};
typedef struct stream stream;
extern token *TOKEN_EOF;
/* internal */
void stream_free(stream *st);
stream *stream_new(size_t size, token *(*get)(),
int (*close)(), stream *source);
/* external */
int stream_close(stream *st);
void stream_dequeue(stream *st);
token *stream_enqueue(stream *st);
int stream_eof(stream *st);
token *stream_get(stream *st);
token *stream_queue_peek(stream *st);
#define token_clean(t) ((t)->alloced ? free((t)->line),(t)->line=NULL : NULL)
token *token_copy(token *d, token *s);
void token_free(token *t);
token *token_new(enum token_type type, char *line);
token *token_printf3(token *t, enum token_type type, int n, char *fmt, ...);
token *token_set(token *t, enum token_type type, char *line);
token *token_set3(token *t, enum token_type type, int n, char *line);
#endif /* stream.h */