User | Revision | Line number | New contents of line |
ganlikun |
0:06036f8bee2d
|
1
|
/* mbed Microcontroller Library
|
ganlikun |
0:06036f8bee2d
|
2
|
* Copyright (c) 2006-2013 ARM Limited
|
ganlikun |
0:06036f8bee2d
|
3
|
*
|
ganlikun |
0:06036f8bee2d
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
ganlikun |
0:06036f8bee2d
|
5
|
* you may not use this file except in compliance with the License.
|
ganlikun |
0:06036f8bee2d
|
6
|
* You may obtain a copy of the License at
|
ganlikun |
0:06036f8bee2d
|
7
|
*
|
ganlikun |
0:06036f8bee2d
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
ganlikun |
0:06036f8bee2d
|
9
|
*
|
ganlikun |
0:06036f8bee2d
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
ganlikun |
0:06036f8bee2d
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
ganlikun |
0:06036f8bee2d
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
ganlikun |
0:06036f8bee2d
|
13
|
* See the License for the specific language governing permissions and
|
ganlikun |
0:06036f8bee2d
|
14
|
* limitations under the License.
|
ganlikun |
0:06036f8bee2d
|
15
|
*/
|
ganlikun |
0:06036f8bee2d
|
16
|
#include "platform/Stream.h"
|
ganlikun |
0:06036f8bee2d
|
17
|
#include "platform/mbed_error.h"
|
ganlikun |
0:06036f8bee2d
|
18
|
#include <errno.h>
|
ganlikun |
0:06036f8bee2d
|
19
|
|
ganlikun |
0:06036f8bee2d
|
20
|
namespace mbed {
|
ganlikun |
0:06036f8bee2d
|
21
|
|
ganlikun |
0:06036f8bee2d
|
22
|
Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
|
ganlikun |
0:06036f8bee2d
|
23
|
// No lock needed in constructor
|
ganlikun |
0:06036f8bee2d
|
24
|
/* open ourselves */
|
ganlikun |
0:06036f8bee2d
|
25
|
_file = fdopen(this, "w+");
|
ganlikun |
0:06036f8bee2d
|
26
|
// fdopen() will make us buffered because Stream::isatty()
|
ganlikun |
0:06036f8bee2d
|
27
|
// wrongly returns zero which is not being changed for
|
ganlikun |
0:06036f8bee2d
|
28
|
// backward compatibility
|
ganlikun |
0:06036f8bee2d
|
29
|
if (_file) {
|
ganlikun |
0:06036f8bee2d
|
30
|
mbed_set_unbuffered_stream(_file);
|
ganlikun |
0:06036f8bee2d
|
31
|
} else {
|
ganlikun |
0:06036f8bee2d
|
32
|
error("Stream obj failure, errno=%d\r\n", errno);
|
ganlikun |
0:06036f8bee2d
|
33
|
}
|
ganlikun |
0:06036f8bee2d
|
34
|
}
|
ganlikun |
0:06036f8bee2d
|
35
|
|
ganlikun |
0:06036f8bee2d
|
36
|
Stream::~Stream() {
|
ganlikun |
0:06036f8bee2d
|
37
|
// No lock can be used in destructor
|
ganlikun |
0:06036f8bee2d
|
38
|
fclose(_file);
|
ganlikun |
0:06036f8bee2d
|
39
|
}
|
ganlikun |
0:06036f8bee2d
|
40
|
|
ganlikun |
0:06036f8bee2d
|
41
|
int Stream::putc(int c) {
|
ganlikun |
0:06036f8bee2d
|
42
|
lock();
|
ganlikun |
0:06036f8bee2d
|
43
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
44
|
int ret = std::fputc(c, _file);
|
ganlikun |
0:06036f8bee2d
|
45
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
46
|
return ret;
|
ganlikun |
0:06036f8bee2d
|
47
|
}
|
ganlikun |
0:06036f8bee2d
|
48
|
int Stream::puts(const char *s) {
|
ganlikun |
0:06036f8bee2d
|
49
|
lock();
|
ganlikun |
0:06036f8bee2d
|
50
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
51
|
int ret = std::fputs(s, _file);
|
ganlikun |
0:06036f8bee2d
|
52
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
53
|
return ret;
|
ganlikun |
0:06036f8bee2d
|
54
|
}
|
ganlikun |
0:06036f8bee2d
|
55
|
int Stream::getc() {
|
ganlikun |
0:06036f8bee2d
|
56
|
lock();
|
ganlikun |
0:06036f8bee2d
|
57
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
58
|
int ret = mbed_getc(_file);
|
ganlikun |
0:06036f8bee2d
|
59
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
60
|
return ret;
|
ganlikun |
0:06036f8bee2d
|
61
|
}
|
ganlikun |
0:06036f8bee2d
|
62
|
char* Stream::gets(char *s, int size) {
|
ganlikun |
0:06036f8bee2d
|
63
|
lock();
|
ganlikun |
0:06036f8bee2d
|
64
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
65
|
char *ret = mbed_gets(s,size,_file);
|
ganlikun |
0:06036f8bee2d
|
66
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
67
|
return ret;
|
ganlikun |
0:06036f8bee2d
|
68
|
}
|
ganlikun |
0:06036f8bee2d
|
69
|
|
ganlikun |
0:06036f8bee2d
|
70
|
int Stream::close() {
|
ganlikun |
0:06036f8bee2d
|
71
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
72
|
}
|
ganlikun |
0:06036f8bee2d
|
73
|
|
ganlikun |
0:06036f8bee2d
|
74
|
ssize_t Stream::write(const void* buffer, size_t length) {
|
ganlikun |
0:06036f8bee2d
|
75
|
const char* ptr = (const char*)buffer;
|
ganlikun |
0:06036f8bee2d
|
76
|
const char* end = ptr + length;
|
ganlikun |
0:06036f8bee2d
|
77
|
|
ganlikun |
0:06036f8bee2d
|
78
|
lock();
|
ganlikun |
0:06036f8bee2d
|
79
|
while (ptr != end) {
|
ganlikun |
0:06036f8bee2d
|
80
|
if (_putc(*ptr++) == EOF) {
|
ganlikun |
0:06036f8bee2d
|
81
|
break;
|
ganlikun |
0:06036f8bee2d
|
82
|
}
|
ganlikun |
0:06036f8bee2d
|
83
|
}
|
ganlikun |
0:06036f8bee2d
|
84
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
85
|
|
ganlikun |
0:06036f8bee2d
|
86
|
return ptr - (const char*)buffer;
|
ganlikun |
0:06036f8bee2d
|
87
|
}
|
ganlikun |
0:06036f8bee2d
|
88
|
|
ganlikun |
0:06036f8bee2d
|
89
|
ssize_t Stream::read(void* buffer, size_t length) {
|
ganlikun |
0:06036f8bee2d
|
90
|
char* ptr = (char*)buffer;
|
ganlikun |
0:06036f8bee2d
|
91
|
char* end = ptr + length;
|
ganlikun |
0:06036f8bee2d
|
92
|
|
ganlikun |
0:06036f8bee2d
|
93
|
lock();
|
ganlikun |
0:06036f8bee2d
|
94
|
while (ptr != end) {
|
ganlikun |
0:06036f8bee2d
|
95
|
int c = _getc();
|
ganlikun |
0:06036f8bee2d
|
96
|
if (c==EOF) break;
|
ganlikun |
0:06036f8bee2d
|
97
|
*ptr++ = c;
|
ganlikun |
0:06036f8bee2d
|
98
|
}
|
ganlikun |
0:06036f8bee2d
|
99
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
100
|
|
ganlikun |
0:06036f8bee2d
|
101
|
return ptr - (const char*)buffer;
|
ganlikun |
0:06036f8bee2d
|
102
|
}
|
ganlikun |
0:06036f8bee2d
|
103
|
|
ganlikun |
0:06036f8bee2d
|
104
|
off_t Stream::seek(off_t offset, int whence) {
|
ganlikun |
0:06036f8bee2d
|
105
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
106
|
}
|
ganlikun |
0:06036f8bee2d
|
107
|
|
ganlikun |
0:06036f8bee2d
|
108
|
off_t Stream::tell() {
|
ganlikun |
0:06036f8bee2d
|
109
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
110
|
}
|
ganlikun |
0:06036f8bee2d
|
111
|
|
ganlikun |
0:06036f8bee2d
|
112
|
void Stream::rewind() {
|
ganlikun |
0:06036f8bee2d
|
113
|
}
|
ganlikun |
0:06036f8bee2d
|
114
|
|
ganlikun |
0:06036f8bee2d
|
115
|
int Stream::isatty() {
|
ganlikun |
0:06036f8bee2d
|
116
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
117
|
}
|
ganlikun |
0:06036f8bee2d
|
118
|
|
ganlikun |
0:06036f8bee2d
|
119
|
int Stream::sync() {
|
ganlikun |
0:06036f8bee2d
|
120
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
121
|
}
|
ganlikun |
0:06036f8bee2d
|
122
|
|
ganlikun |
0:06036f8bee2d
|
123
|
off_t Stream::size() {
|
ganlikun |
0:06036f8bee2d
|
124
|
return 0;
|
ganlikun |
0:06036f8bee2d
|
125
|
}
|
ganlikun |
0:06036f8bee2d
|
126
|
|
ganlikun |
0:06036f8bee2d
|
127
|
int Stream::printf(const char* format, ...) {
|
ganlikun |
0:06036f8bee2d
|
128
|
lock();
|
ganlikun |
0:06036f8bee2d
|
129
|
std::va_list arg;
|
ganlikun |
0:06036f8bee2d
|
130
|
va_start(arg, format);
|
ganlikun |
0:06036f8bee2d
|
131
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
132
|
int r = vfprintf(_file, format, arg);
|
ganlikun |
0:06036f8bee2d
|
133
|
va_end(arg);
|
ganlikun |
0:06036f8bee2d
|
134
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
135
|
return r;
|
ganlikun |
0:06036f8bee2d
|
136
|
}
|
ganlikun |
0:06036f8bee2d
|
137
|
|
ganlikun |
0:06036f8bee2d
|
138
|
int Stream::scanf(const char* format, ...) {
|
ganlikun |
0:06036f8bee2d
|
139
|
lock();
|
ganlikun |
0:06036f8bee2d
|
140
|
std::va_list arg;
|
ganlikun |
0:06036f8bee2d
|
141
|
va_start(arg, format);
|
ganlikun |
0:06036f8bee2d
|
142
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
143
|
int r = vfscanf(_file, format, arg);
|
ganlikun |
0:06036f8bee2d
|
144
|
va_end(arg);
|
ganlikun |
0:06036f8bee2d
|
145
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
146
|
return r;
|
ganlikun |
0:06036f8bee2d
|
147
|
}
|
ganlikun |
0:06036f8bee2d
|
148
|
|
ganlikun |
0:06036f8bee2d
|
149
|
int Stream::vprintf(const char* format, std::va_list args) {
|
ganlikun |
0:06036f8bee2d
|
150
|
lock();
|
ganlikun |
0:06036f8bee2d
|
151
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
152
|
int r = vfprintf(_file, format, args);
|
ganlikun |
0:06036f8bee2d
|
153
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
154
|
return r;
|
ganlikun |
0:06036f8bee2d
|
155
|
}
|
ganlikun |
0:06036f8bee2d
|
156
|
|
ganlikun |
0:06036f8bee2d
|
157
|
int Stream::vscanf(const char* format, std::va_list args) {
|
ganlikun |
0:06036f8bee2d
|
158
|
lock();
|
ganlikun |
0:06036f8bee2d
|
159
|
fflush(_file);
|
ganlikun |
0:06036f8bee2d
|
160
|
int r = vfscanf(_file, format, args);
|
ganlikun |
0:06036f8bee2d
|
161
|
unlock();
|
ganlikun |
0:06036f8bee2d
|
162
|
return r;
|
ganlikun |
0:06036f8bee2d
|
163
|
}
|
ganlikun |
0:06036f8bee2d
|
164
|
|
ganlikun |
0:06036f8bee2d
|
165
|
} // namespace mbed
|
ganlikun |
0:06036f8bee2d
|
166
|
|