-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_fcat.c
116 lines (85 loc) · 2.56 KB
/
stream_fcat.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
$NiH: stream_fcat.c,v 1.5 2002/04/22 22:12:31 dillo Exp $
stream_fcat.c -- concatenate files
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 <errno.h>
#include <stdio.h>
#include <string.h>
#include "stream.h"
#include "stream_types.h"
#include "util.h"
struct stream_fcat {
stream st;
int n, i;
char **names;
FILE *cur;
};
static void close_current(struct stream_fcat *this);
static int fcat_close(struct stream_fcat *st);
static token *fcat_get(struct stream_fcat *st);
stream *
stream_fcat_open(int n, char **names)
{
struct stream_fcat *this;
this = (struct stream_fcat *)stream_new(sizeof(struct stream_fcat),
fcat_get, fcat_close, NULL);
this->n = n;
this->names = names;
this->i = 0;
return (stream *)this;
}
static int
fcat_close(struct stream_fcat *this)
{
close_current(this);
stream_free((stream *)this);
return 0;
}
static token *
fcat_get(struct stream_fcat *this)
{
token *t;
if (this->st.source == NULL) {
if (this->i >= this->n) {
close_current(this);
return TOKEN_EOF;
}
if ((this->cur=fopen(this->names[this->i++], "r")) == NULL) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "cannot open file `%s': %s",
this->names[this->i-1], strerror(errno));
return token_set(&this->st.tok, TOK_EOA, NULL);
}
this->st.source = stream_file_open(this->cur, 0);
}
t = stream_get(this->st.source);
if (t->type == EOF) {
close_current(this);
return token_set(&this->st.tok, TOK_EOA, NULL);
}
return t;
}
static void
close_current(struct stream_fcat *this)
{
if (this->st.source) {
stream_close(this->st.source);
fclose(this->cur);
this->st.source = NULL;
this->cur = NULL;
}
}