Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stream.cpp Source File

Stream.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "drivers/Stream.h"
00017 
00018 namespace mbed {
00019 
00020 Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
00021     // No lock needed in constructor
00022     /* open ourselves */
00023     char buf[12]; /* :0x12345678 + null byte */
00024     std::sprintf(buf, ":%p", this);
00025     _file = std::fopen(buf, "w+");
00026     mbed_set_unbuffered_stream(_file);
00027 }
00028 
00029 Stream::~Stream() {
00030     // No lock can be used in destructor
00031     fclose(_file);
00032 }
00033 
00034 int Stream::putc(int c) {
00035     lock();
00036     fflush(_file);
00037     int ret = std::fputc(c, _file);
00038     unlock();
00039     return ret;
00040 }
00041 int Stream::puts(const char *s) {
00042     lock();
00043     fflush(_file);
00044     int ret = std::fputs(s, _file);
00045     unlock();
00046     return ret;
00047 }
00048 int Stream::getc() {
00049     lock();
00050     fflush(_file);
00051     int ret = mbed_getc(_file);
00052     unlock();
00053     return ret;
00054 }
00055 char* Stream::gets(char *s, int size) {
00056     lock();
00057     fflush(_file);
00058     char *ret = mbed_gets(s,size,_file);
00059     unlock();
00060     return ret;
00061 }
00062 
00063 int Stream::close() {
00064     return 0;
00065 }
00066 
00067 ssize_t Stream::write(const void* buffer, size_t length) {
00068     const char* ptr = (const char*)buffer;
00069     const char* end = ptr + length;
00070 
00071     lock();
00072     while (ptr != end) {
00073         if (_putc(*ptr++) == EOF) {
00074             break;
00075         }
00076     }
00077     unlock();
00078 
00079     return ptr - (const char*)buffer;
00080 }
00081 
00082 ssize_t Stream::read(void* buffer, size_t length) {
00083     char* ptr = (char*)buffer;
00084     char* end = ptr + length;
00085 
00086     lock();
00087     while (ptr != end) {
00088         int c = _getc();
00089         if (c==EOF) break;
00090         *ptr++ = c;
00091     }
00092     unlock();
00093 
00094     return ptr - (const char*)buffer;
00095 }
00096 
00097 off_t Stream::seek(off_t offset, int whence) {
00098     return 0;
00099 }
00100 
00101 off_t Stream::tell() {
00102     return 0;
00103 }
00104 
00105 void Stream::rewind() {
00106 }
00107 
00108 int Stream::isatty() {
00109     return 0;
00110 }
00111 
00112 int Stream::sync() {
00113     return 0;
00114 }
00115 
00116 size_t Stream::size() {
00117     return 0;
00118 }
00119 
00120 int Stream::printf(const char* format, ...) {
00121     lock();
00122     std::va_list arg;
00123     va_start(arg, format);
00124     fflush(_file);
00125     int r = vfprintf(_file, format, arg);
00126     va_end(arg);
00127     unlock();
00128     return r;
00129 }
00130 
00131 int Stream::scanf(const char* format, ...) {
00132     lock();
00133     std::va_list arg;
00134     va_start(arg, format);
00135     fflush(_file);
00136     int r = vfscanf(_file, format, arg);
00137     va_end(arg);
00138     unlock();
00139     return r;
00140 }
00141 
00142 int Stream::vprintf(const char* format, std::va_list args) {
00143     lock();
00144     fflush(_file);
00145     int r = vfprintf(_file, format, args);
00146     unlock();
00147     return r;
00148 }
00149 
00150 int Stream::vscanf(const char* format, std::va_list args) {
00151     lock();
00152     fflush(_file);
00153     int r = vfscanf(_file, format, args);
00154     unlock();
00155     return r;
00156 }
00157 
00158 } // namespace mbed