/
Stream.c
228 lines (164 loc) · 4.72 KB
/
Stream.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include "Stream.h"
#include "String.h"
#include "Scanner.h"
#include "Out.h"
struct _streamprivate {
Scanner * s;
Out * o;
StreamType type;
FILE * fp;
};
Stream * actual_stream;
OBJECT_TYPE (Stream, stream, STREAM);
PUBLIC int
StrmOpen (char * filename, char * mode) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
int i;
priv->fp = fopen (filename, mode);
if (priv->fp == NULL) {
perror ("opening the file");
return 0;
}
scanner (priv->s)->setFileDescriptor (priv->fp);
i = 0;
for (i = 0; i < strlen (mode); i++) {
if (mode[i] == 'b') {
printf ("\nUP\n");
priv->type = T_BINARY;
i = 0;
break;
}
}
if (i != 0) {
priv->type = T_CHAR;
}
return 1;
}
PUBLIC void
StrmClose (void) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
if (priv->fp != NULL) {
fflush (priv->fp);
fclose (priv->fp);
}
}
PUBLIC void
StrmWrite (char * str) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
if (priv->type == T_BINARY) {
out (priv->o)->Stdout ("Use WriteBinary stream method for binary stream mode [!!]\n");
exit (1);
}
out (priv->o)->File (priv->fp, str);
}
PUBLIC void
StrmWriteFmt (char * fmt, ...) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
if (priv->type == T_BINARY) {
out (priv->o)->Stdout ("Use WriteBinary stream method for binary stream mode [!!]\n");
exit (1);
}
va_list ap;
va_start (ap, fmt);
vfprintf (priv->fp, fmt, ap);
va_end (ap);
}
PUBLIC int
StrmWriteBinary (void * buf, size_t len) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
int written;
if (priv->type != T_BINARY) {
out (priv->o)->Stdout ("Use Write stream method for non-binary stream mode [!!]\n");
exit (1);
}
written = fwrite (buf, len, 1, priv->fp);
return written;
}
PUBLIC char *
StrmRead (unsigned int len) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
if (priv->type == T_BINARY) {
out (priv->o)->Stdout ("Use ReadBinary stream method for binary stream mode [!!]\n");
exit (1);
}
return scanner (priv->s)->getString (len);
}
PUBLIC int
StrmReadBinary (void * buf, size_t size) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
size_t read;
if (priv->type != T_BINARY) {
out (priv->o)->Stdout ("Use Read stream method for non-binary stream mode [!!]\n");
exit (1);
}
read = fread (buf, size, 1, priv->fp);
//((char *)buf)[size] = 0x00;
if (read != size) {
if (feof (priv->fp)) {
out (priv->o)->Stdout ("Error reading the file: End of file found\n");
return -1;
}
}
return 1;
}
PUBLIC void
StrmReWind (void) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
rewind (priv->fp);
}
PUBLIC FILE *
StrmGetDescriptor (void) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
return priv->fp;
}
PUBLIC void
StrmSetDescriptor (FILE * fp) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
priv->fp = fp;
if (fp != stdout) {
scanner (priv->s)->setFileDescriptor (fp);
}
}
PUBLIC void
StrmDtor (void) {
StreamPrivate * priv = OBJECT_GET_PRIVATE (Stream, actual_stream);
scanner (priv->s)->dtor ();
out (priv->o)->dtor ();
if (priv->fp != NULL && priv->fp != stdout && priv->fp != stdin && priv->fp != stderr) {
fflush (priv->fp);
fclose (priv->fp);
}
DEALLOC (priv);
DEALLOC (actual_stream);
actual_stream = NULL;
priv = NULL;
}
PUBLIC Stream *
new_stream (char * filename, char * mode) {
Stream * strm = NEW (Stream);
StreamPrivate * priv = NEW (StreamPrivate);
priv->s = new_scanner (NULL);
priv->o = new_out ();
strm->priv = priv;
strm->Open = StrmOpen;
strm->Close = StrmClose;
strm->Write = StrmWrite;
strm->WriteFmt = StrmWriteFmt;
strm->WriteBinary = StrmWriteBinary;
strm->Read = StrmRead;
strm->ReadBinary = StrmReadBinary;
strm->ReWind = StrmReWind;
strm->GetDescriptor = StrmGetDescriptor;
strm->SetDescriptor = StrmSetDescriptor;
strm->dtor = StrmDtor;
if (filename != NULL && mode != NULL) {
if ((stream (strm)->Open (filename, mode)) == 0) {
stream (strm)->dtor ();
strm = NULL;
}
}
return strm;
}