Soldered SD Card Arduino Library 1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Loading...
Searching...
No Matches
bufstream.h
Go to the documentation of this file.
1
25#ifndef bufstream_h
26#define bufstream_h
31#include "iostream.h"
32#include <string.h>
33//==============================================================================
38class ibufstream : public istream
39{
40 public:
43 {
44 }
49 explicit ibufstream(const char *str)
50 {
51 init(str);
52 }
57 void init(const char *str)
58 {
59 m_buf = str;
60 m_len = strlen(m_buf);
61 m_pos = 0;
62 clear();
63 }
64
65 protected:
67 int16_t getch()
68 {
69 if (m_pos < m_len)
70 {
71 return m_buf[m_pos++];
72 }
74 return -1;
75 }
76 void getpos(pos_t *pos)
77 {
78 pos->position = m_pos;
79 }
80 bool seekoff(off_type off, seekdir way)
81 {
82 (void)off;
83 (void)way;
84 return false;
85 }
86 bool seekpos(pos_type pos)
87 {
88 if (pos < m_len)
89 {
90 m_pos = pos;
91 return true;
92 }
93 return false;
94 }
95 void setpos(pos_t *pos)
96 {
97 m_pos = pos->position;
98 }
99 pos_type tellpos()
100 {
101 return m_pos;
102 }
104 private:
105 const char *m_buf = nullptr;
106 size_t m_len = 0;
107 size_t m_pos;
108};
109//==============================================================================
114class obufstream : public ostream
115{
116 public:
119 {
120 }
125 obufstream(char *buf, size_t size)
126 {
127 init(buf, size);
128 }
133 void init(char *buf, size_t size)
134 {
135 m_buf = buf;
136 buf[0] = '\0';
137 m_size = size;
138 m_in = 0;
139 }
141 char *buf()
142 {
143 return m_buf;
144 }
146 size_t length()
147 {
148 return m_in;
149 }
150
151 protected:
153 void putch(char c)
154 {
155 if ((m_in + 1) >= m_size)
156 {
158 return;
159 }
160 m_buf[m_in++] = c;
161 m_buf[m_in] = '\0';
162 }
163 void putstr(const char *str)
164 {
165 while (*str)
166 {
167 putch(*str++);
168 }
169 }
170 bool seekoff(off_type off, seekdir way)
171 {
172 (void)off;
173 (void)way;
174 return false;
175 }
176 bool seekpos(pos_type pos)
177 {
178 if (pos > m_in)
179 {
180 return false;
181 }
182 m_in = pos;
183 m_buf[m_in] = '\0';
184 return true;
185 }
186 bool sync()
187 {
188 return true;
189 }
190 pos_type tellpos()
191 {
192 return m_in;
193 }
195 private:
196 char *m_buf = nullptr;
197 size_t m_size = 0;
198 size_t m_in = 0;
199};
200#endif // bufstream_h
parse a char string
Definition bufstream.h:39
size_t m_len
Definition bufstream.h:106
void init(const char *str)
Definition bufstream.h:57
ibufstream(const char *str)
Definition bufstream.h:49
const char * m_buf
Definition bufstream.h:105
size_t m_pos
Definition bufstream.h:107
ibufstream()
Definition bufstream.h:42
int32_t off_type
Definition ios.h:73
seekdir
Definition ios.h:87
uint32_t pos_type
Definition ios.h:71
static const iostate badbit
Definition ios.h:59
static const iostate eofbit
Definition ios.h:61
void clear(iostate state=goodbit)
Definition ios.h:480
void setstate(iostate state)
Definition ios.h:488
Input Stream.
Definition istream.h:38
format a char string
Definition bufstream.h:115
char * m_buf
Definition bufstream.h:196
size_t m_in
Definition bufstream.h:198
obufstream()
Definition bufstream.h:118
obufstream(char *buf, size_t size)
Definition bufstream.h:125
void init(char *buf, size_t size)
Definition bufstream.h:133
char * buf()
Definition bufstream.h:141
size_t length()
Definition bufstream.h:146
size_t m_size
Definition bufstream.h:197
Output Stream.
Definition ostream.h:38
iostream class
Definition FsStructs.h:133
uint64_t position
Definition FsStructs.h:134