forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.cpp
201 lines (177 loc) · 4.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "platform/Stream.h"
#include "platform/mbed_error.h"
#include <errno.h>
namespace mbed {
Stream::Stream(const char *name) :
FileLike(name)
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
, _file(NULL)
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
{
// No lock needed in constructor
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
/* open ourselves */
_file = fdopen(this, "w+");
// fdopen() will make us buffered because Stream::isatty()
// wrongly returns zero which is not being changed for
// backward compatibility
if (_file) {
mbed_set_unbuffered_stream(_file);
} else {
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
}
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
}
Stream::~Stream()
{
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
// No lock can be used in destructor
fclose(_file);
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
}
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
int Stream::putc(int c)
{
lock();
std::fseek(_file, 0, SEEK_CUR);
int ret = std::fputc(c, _file);
unlock();
return ret;
}
int Stream::puts(const char *s)
{
lock();
std::fseek(_file, 0, SEEK_CUR);
int ret = std::fputs(s, _file);
unlock();
return ret;
}
int Stream::getc()
{
lock();
fflush(_file);
int ret = std::fgetc(_file);
unlock();
return ret;
}
char *Stream::gets(char *s, int size)
{
lock();
fflush(_file);
char *ret = std::fgets(s, size, _file);
unlock();
return ret;
}
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
int Stream::close()
{
return 0;
}
ssize_t Stream::write(const void *buffer, size_t length)
{
const char *ptr = (const char *)buffer;
const char *end = ptr + length;
lock();
while (ptr != end) {
if (_putc(*ptr++) == EOF) {
break;
}
}
unlock();
return ptr - (const char *)buffer;
}
ssize_t Stream::read(void *buffer, size_t length)
{
char *ptr = (char *)buffer;
char *end = ptr + length;
lock();
while (ptr != end) {
int c = _getc();
if (c == EOF) {
break;
}
*ptr++ = c;
}
unlock();
return ptr - (const char *)buffer;
}
off_t Stream::seek(off_t offset, int whence)
{
return 0;
}
off_t Stream::tell()
{
return 0;
}
void Stream::rewind()
{
}
int Stream::isatty()
{
return 0;
}
int Stream::sync()
{
return 0;
}
off_t Stream::size()
{
return 0;
}
#if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
int Stream::printf(const char *format, ...)
{
lock();
std::va_list arg;
va_start(arg, format);
std::fseek(_file, 0, SEEK_CUR);
int r = vfprintf(_file, format, arg);
va_end(arg);
unlock();
return r;
}
int Stream::scanf(const char *format, ...)
{
lock();
std::va_list arg;
va_start(arg, format);
fflush(_file);
int r = vfscanf(_file, format, arg);
va_end(arg);
unlock();
return r;
}
int Stream::vprintf(const char *format, std::va_list args)
{
lock();
std::fseek(_file, 0, SEEK_CUR);
int r = vfprintf(_file, format, args);
unlock();
return r;
}
int Stream::vscanf(const char *format, std::va_list args)
{
lock();
fflush(_file);
int r = vfscanf(_file, format, args);
unlock();
return r;
}
#endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
} // namespace mbed