-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharstream.c
78 lines (67 loc) · 1.52 KB
/
charstream.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
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "charstream.h"
#ifdef NOCODE
typedef
struct {
unsigned int type; // 0 is (char *), 1 is (FILE *)
unsigned int done;
FILE *ptr;
char **str;
char buff;
} charstream;
#endif
char next(charstream *cs) {
ASSERT(cs, -1);
if (cs->done) {
cs->buff = 0;
} else {
switch(cs->type) {
case 0:
if (**cs->str) {
(void)(*(cs->str))++;
} else {
cs->done = 1;
}
cs->buff = **cs->str;
break;
case 1:
cs->buff = fgetc(cs->ptr);
if (cs->buff == EOF) {
cs->done = 1;
cs->buff = 0;
}
}
}
return cs->buff;
}
char seq(charstream *cs) {
ASSERT(cs, -1);
return cs->buff;
}
void skip(charstream *cs, unsigned int nt) {
for(unsigned int i=0; i<nt && !cs->done; i++) {
(void) next(cs);
}
}
charstream *streamfile(FILE *ptr) {
charstream *res = calloc(sizeof(charstream), 1);
res->type = 1;
res->done = 0;
res->ptr = ptr;
res->str = 0;
res->buff = fgetc(ptr);
return res;
}
charstream *streamstr(char *str) {
charstream *res = calloc(sizeof(charstream), 1);
res->type = 0;
res->done = 0;
res->ptr = 0;
res->str = calloc(sizeof(char *), 1);
(res->str)[0] = strdup(str);
res->buff = *str;
return res;
}