COSA
An Object-Oriented Platform for Arduino Programming
IOStream_Device.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/IOStream.hh"
22 
23 int
25 {
26  return (0);
27 }
28 
29 int
31 {
32  return (0);
33 }
34 
35 int
37 {
38  int res = write(&c, sizeof(c));
39  return (res == sizeof(c) ? c & 0xff : res);
40 }
41 
42 int
43 IOStream::Device::puts(const char* s)
44 {
45  const char* bp = (const char*) s;
46  int n = 0;
47  for (char c; (c = *bp++) != 0; n++)
48  if (UNLIKELY(putchar(c) < 0))
49  break;
50  return (n);
51 }
52 
53 int
55 {
56  const char* bp = (const char*) s;
57  int n = 0;
58  for (char c; (c = pgm_read_byte(bp++)) != 0; n++)
59  if (UNLIKELY(putchar(c) < 0))
60  break;
61  return (n);
62 }
63 
64 int
65 IOStream::Device::write(const void* buf, size_t size)
66 {
67  char* bp = (char*) buf;
68  size_t n = 0;
69  for(; n < size; n++)
70  if (UNLIKELY(putchar(*bp++) < 0))
71  break;
72  return (n);
73 }
74 
75 int
76 IOStream::Device::write_P(const void* buf, size_t size)
77 {
78  char* bp = (char*) buf;
79  size_t n = 0;
80  for(; n < size; n++)
81  if (UNLIKELY(putchar(pgm_read_byte(bp++)) < 0))
82  break;
83  return (n);
84 }
85 
86 int
88 {
89  size_t size = 0;
90  for (const iovec_t* vp = vec; vp->buf != NULL; vp++) {
91  size_t res = (size_t) write(vp->buf, vp->size);
92  if (UNLIKELY(res == 0)) break;
93  size += res;
94  }
95  return (size);
96 }
97 
98 int
100 {
101  return (EOF);
102 }
103 
104 int
106 {
107  UNUSED(c);
108  return (EOF);
109 }
110 
111 int
113 {
114  return (EOF);
115 }
116 
117 char*
118 IOStream::Device::gets(char *s, size_t count)
119 {
120  char* res = s;
121  while (--count) {
122  int c = getchar();
123  if (UNLIKELY(c == EOF)) {
124  if (m_blocking) {
125  while (c == EOF) {
126  yield();
127  c = getchar();
128  }
129  }
130  else break;
131  }
132  if (c == '\r') {
133  if (m_eol == CRLF_MODE) continue;
134  c = '\n';
135  }
136  *s++ = c;
137  if (c == '\n') break;
138  }
139  *s = 0;
140  return (s == res ? NULL : res);
141 }
142 
143 int
144 IOStream::Device::read(void* buf, size_t size)
145 {
146  char* bp = (char*) buf;
147  size_t n = 0;
148  for (; n < size; n++) {
149  int c = getchar();
150  if (UNLIKELY(c < 0)) break;
151  *bp++ = c;
152  }
153  return (n);
154 }
155 
156 int
158 {
159  size_t size = 0;
160  for (const iovec_t* vp = vec; vp->buf != NULL; vp++) {
161  size_t res = (size_t) read(vp->buf, vp->size);
162  if (UNLIKELY(res == 0)) break;
163  size += res;
164  }
165  return (size);
166 }
167 
168 int
170 {
171  return (EOF);
172 }
173 
174 void
176 {
177 }
virtual int write_P(const void *buf, size_t size)
virtual int putchar(char c)
virtual int puts(const char *s)
virtual int write(const void *buf, size_t size)
virtual int read(void *buf, size_t size)
#define NULL
Definition: Types.h:101
virtual int room()
virtual void empty()
Definition: Types.h:391
const class prog_str * str_P
Definition: Types.h:187
void(* yield)()
void * buf
Buffer pointer.
Definition: Types.h:392
#define UNUSED(x)
Definition: ATmega328P.hh:31
static const int EOF
Definition: IOStream.hh:33
virtual int flush()
virtual int available()
virtual char * gets(char *s, size_t count)
virtual int getchar()
virtual int peekchar()
#define UNLIKELY(x)
Definition: Types.h:153