forked from sourcesimian/uICAL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.cpp
177 lines (149 loc) · 3.48 KB
/
stream.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
/*############################################################################
# Copyright (c) 2020 Source Simian : https://github.com/sourcesimian/uICAL #
############################################################################*/
#include "uICAL/cppstl.h"
#include "uICAL/stream.h"
#include "uICAL/string.h"
namespace uICAL
{
ostream &ostream::operator<<(const ostream &stm)
{
for (string st : stm.strings)
{
this->strings.push_back(st);
}
return *this;
}
ostream &ostream::operator<<(const char *st)
{
this->strings.push_back(st);
return *this;
}
ostream &ostream::operator<<(const string &st)
{
this->strings.push_back(st);
return *this;
}
ostream &ostream::operator<<(char ch)
{
this->strings.push_back(string::fmt("%c", ch));
return *this;
}
ostream &ostream::operator<<(int i)
{
this->strings.push_back(string::fmt("%d", i));
return *this;
}
ostream &ostream::operator<<(unsigned int i)
{
this->strings.push_back(string::fmt("%u", i));
return *this;
}
ostream &ostream::operator<<(long long int i)
{
this->strings.push_back(string::fmt("%lld", i));
return *this;
}
ostream::operator string() const
{
return this->str();
}
bool ostream::empty() const
{
return this->strings.size() == 0;
}
void ostream::clear()
{
this->strings.clear();
}
string ostream::str() const
{
string ret = "";
for (string st : this->strings)
{
ret += st;
}
return ret;
}
#ifdef ARDUINO
istream_Stream::istream_Stream(Stream &stm)
: stm(stm)
{
}
char istream_Stream::peek() const
{
int ch = this->stm.peek();
return ch;
}
char istream_Stream::get()
{
int ch = this->stm.read();
return ch;
}
bool istream_Stream::readuntil(string &st, char delim)
{
size_t len = 81;
char buf[len];
size_t read = this->stm.readBytesUntil(delim, buf, len - 1);
if (read > 0)
{
buf[read] = 0;
st = buf;
return true;
}
return false;
}
istream_String::istream_String(const String &st)
: st(st), pos(0)
{
}
char istream_String::peek() const
{
return this->st.charAt(this->pos);
}
char istream_String::get()
{
return this->st.charAt(this->pos++);
}
bool istream_String::readuntil(string &st, char delim)
{
if (this->pos >= this->st.length())
{
return false;
}
size_t index = this->st.indexOf(delim, this->pos);
if (index == (size_t)-1)
{
st = this->st.substring(this->pos);
this->pos = this->st.length();
}
else
{
st = this->st.substring(this->pos, index);
this->pos = index + 1;
}
return true;
}
#else
istream_stl::istream_stl(std::istream &istm)
: istm(istm)
{
}
char istream_stl::peek() const
{
return this->istm.peek();
}
char istream_stl::get()
{
return this->istm.get();
}
bool istream_stl::readuntil(string &st, char delim)
{
if (std::getline(this->istm, st, delim))
{
return true;
}
return false;
}
#endif
}