-
Notifications
You must be signed in to change notification settings - Fork 0
/
iostream.h
189 lines (179 loc) · 3.73 KB
/
iostream.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
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
/* A minimal version */
#ifndef PITTSFIELD_IOSTREAM_H_INCLUDED
#define PITTSFIELD_IOSTREAM_H_INCLUDED
#ifndef IS_THE_LIBRARY
#include <stdio.h>
#endif
class ios {
public:
enum open_mode {
in = 1,
out = 2,
ate = 4,
app = 8,
trunc = 16,
nocreate = 32,
noreplace = 64,
bin = 128,
binary = 128 };
enum { skipws=01 };
};
class istream;
class ostream;
typedef ostream& (*omanip)(ostream&);
typedef ssize_t streamsize;
typedef unsigned long fmtflags;
class istream {
private:
void skip_ws() {
for (;;) {
int c = getc(fp);
if (c == EOF) {
return;
} else if (!isspace(c)) {
ungetc(c, fp);
return;
}
}
}
public:
istream() : fp(0), failed(false) { }
FILE *fp;
bool failed;
istream& operator>>(char *cp) {
if (fscanf(fp, " %s", cp) != 1) {
failed = 1;
}
return *this;
}
istream& operator>>(char& cr) {
char c;
if (fscanf(fp, " %c", &c) != 1) {
failed = 1;
} else {
cr = c;
}
return *this;
}
istream& operator>>(unsigned char& cr) {
unsigned char c;
if (fscanf(fp, " %c", (char *)&c) != 1) {
failed = 1;
} else {
cr = c;
}
return *this;
}
istream& operator>>(int& v) {
int x;
if (fscanf(fp, " %d", &x) != 1) {
failed = 1;
} else {
v = x;
}
return *this;
}
istream& operator>>(float& v) {
float f;
if (fscanf(fp, " %f", &f) != 1) {
failed = 1;
} else {
v = f;
}
return *this;
}
istream& operator>>(double& v) {
double f;
if (fscanf(fp, " %lf", &f) != 1) {
failed = 1;
} else {
v = f;
}
return *this;
}
int eof() const {
return feof(fp);
}
istream& get(char& c) {
int ch = fgetc(fp);
if (ch == EOF) {
/* XXX do nothing? */
} else {
c = ch;
}
return *this;
}
istream& get(unsigned char& c) { return get((char&)c); }
istream& putback(char ch) {
ungetc(ch, fp);
return *this;
}
fmtflags unsetf(fmtflags mask) { return 0; /* XXX */ }
int good() const { return !failed; }
int fail() const { return failed; }
operator void*() const { return failed ? (void*)0 : (void*)(-1); }
int operator!() const { return failed; }
};
class ostream {
public:
ostream() : fp(0), failed(false) { }
FILE *fp;
bool failed;
int good() const { return !failed; }
int fail() const { return failed; }
ostream& put(char c) {
putc(c, fp);
return *this;
}
fmtflags unsetf(fmtflags mask);
ostream& write(const char *s, streamsize n) {
fwrite(s, n, 1, fp);
return *this;
}
ostream& operator<<(const char *s) {
fputs(s, fp);
return *this;
}
ostream& operator<<(int n) {
fprintf(fp, "%d", n);
return *this;
}
ostream& operator<<(unsigned int n) {
fprintf(fp, "%u", n);
return *this;
}
ostream& operator<<(long n) {
fprintf(fp, "%ld", n);
return *this;
}
ostream& operator<<(unsigned long n) {
fprintf(fp, "%lu", n);
return *this;
}
ostream& operator<<(short n) {return operator<<((int)n);}
ostream& operator<<(unsigned short n) {return operator<<((unsigned int)n);}
ostream& operator<<(bool b) { return operator<<((int)b); }
ostream& operator<<(double n) {
fprintf(fp, "%g", n);
return *this;
}
ostream& operator<<(float f) {
fprintf(fp, "%g", f);
return *this;
}
ostream& operator<<(long double n) {
fprintf(fp, "%Lg", n);
return *this;
}
ostream& operator<<(const void *p) {
fprintf(fp, "%p", p);
return *this;
}
ostream& operator<<(omanip func) { return (*func)(*this); }
operator void*() const { return failed ? (void*)0 : (void*)(-1); }
int operator!() const { return failed; }
};
ostream& endl(ostream& outs);
extern istream cin;
extern ostream cout, cerr, clog;
#endif