-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filestream.hpp
75 lines (61 loc) · 1.05 KB
/
Filestream.hpp
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
#pragma once
#include <cstdio>
#include <vector>
#include <string>
#include <Debug.hpp>
#include <Logger.hpp>
struct filestream {
FILE *fp = stdin;
std::vector<char> buf;
filestream(FILE *fp=stdin):
fp(fp)
{}
void set_stream(FILE *new_fp) {
fp = new_fp;
buf.clear();
}
char getc() {
if(buf.empty()) {
return fgetc(fp);
}
char c = buf[buf.size() - 1];
buf.pop_back();
return c;
}
void ungetc(char c) {
buf.push_back(c);
}
char probe() {
char c = getc();
ungetc(c);
return c;
}
int get_int() {
int d;
fscanf(fp, " %d", &d);
return d;
}
std::string probe(int len) {
std::string s;
s.reserve(len);
for(int i = 0; i < len; ++i) {
s += getc();
}
for(int i = 0; i < len; ++i) {
ungetc(s[s.length() - i - 1]);
}
return s;
}
void skip_space() {
while(isspace(probe()))getc();
}
void skip_line() {
char c;
while((c = getc()) != '\n') {
if(c == EOF) {
ungetc(c);
break;
}
}
}
};