-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathistream.cpp
224 lines (188 loc) · 6.27 KB
/
istream.cpp
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
#include <istream>
#include <stdio.h>
#include <stdarg.h>
namespace ppcStreams
{
/**************************************************************/
istream::istream()
/**************************************************************/
{
}
/**************************************************************/
istream::~istream()
/**************************************************************/
{
}
/**************************************************************/
int istream::InFunction(const char* format, ...)
/**************************************************************/
{
va_list argList;
va_start(argList, format);
return(scanf(format, va_arg(argList, double))); //Sloppy, stack manip problems
}
/**************************************************************/
bool istream::eof()
/**************************************************************/
{
if ((this->rdstate())&ios::eofbit)
return true;
else
return false;
}
/**************************************************************/
istream& istream::getline(char* s, int streamsize, char delimiter)
/**************************************************************/
{
int ctr = 0;
char current;
do
{
istream& me = *this;
me>>current; //get single character off stream
s[ctr]=current;
ctr++;
}
while (current!=delimiter && (!(eof())) && ctr<streamsize);
return (*this);
}
/**************************************************************/
istream& istream::getline(string& s, char delimiter)
/**************************************************************/
{
int ctr = 0;
char current; //embedded visual c++ 4.0 bug, can't disambiguate between char and char*
//TODO: This implementation gives bugs when using istringstring or stringstream.getline()
//because istringstream is stupid
do
{
operator>>(current); //get single character off stream
s = s + current;
}
while (current!=delimiter && (!(eof())));
return (*this);
}
/**************************************************************/
template <typename T> istream& istream::operator>>(T &var)
/**************************************************************/
{
//I don't like this C++ style template
//because I'm going to get two copies
//of a function that do the same thing
return(istream::operator >>(*this, var));
}
/**************************************************************/
template <typename T> istream& istream::operator>>(istream& stream, T &var)
/**************************************************************/
{
//This is a static method, and my weird way of enabling third party classes
//to override stream operators...it's not quite the way standard implementation
//works...but follows the interface.
return(operator>>(stream, var)); //The class utilizing stream overloading must
//have a external overload for this method(standard)
}
/**************************************************************/
istream& istream::operator >>(char& c)
/**************************************************************/
{
/*NOTE: If you want to skip spaces, uncomment follow loop*/
// do
// {
// InFunction("%c", &c); //Strange bug, scanf,fscanf, sscanf don't ignore
// //spacing when passing in single char arguments
// }
// while (c==' ');
InFunction("%c", &c); //...all comment out this line if you uncomment the loop
return (*this);
}
/**************************************************************/
istream& istream::operator >>(char* s)
/**************************************************************/
{
InFunction("%s", s);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(const char* s)
/**************************************************************/
{
InFunction("%s", s);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(double& d)
/**************************************************************/
{
InFunction("%Lf", &d);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(float& f)
/**************************************************************/
{
InFunction("%f", &f);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(int& i)
/**************************************************************/
{
InFunction("%i", &i);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(long& l)
/**************************************************************/
{
InFunction("%li", &l);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(short& s)
/**************************************************************/
{
InFunction("%hi", &s);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(unsigned int& ui)
/**************************************************************/
{
InFunction("%u", &ui);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(unsigned long& ul)
/**************************************************************/
{
InFunction("%lu", &ul);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(unsigned short& us)
/**************************************************************/
{
InFunction("%hu", &us);
return (*this);
}
/**************************************************************/
istream& istream::operator >>(string& s)
/**************************************************************/
{
char buffer[MAX_SIZE_STD_STRING_IN_STREAM]; //The buffer size can't be known, max 2 K
InFunction("%s", buffer);
string temp(buffer);
s = buffer;
return (*this);
}
/**************************************************************/
istream::operator void*()
/**************************************************************/
{
if (!(this->ios::operator void *()))
return false;
if (eof())
return false;
return (void*)true;
}
}; /*end namespace ppcStreams*/