Go to the documentation of this file.00001
00010 #include "StandardIo.h"
00011 #include <string>
00012 #include <sys/poll.h>
00013 #include <cstdio>
00014 #include <cstring>
00015
00016 using namespace qrk;
00017 using namespace std;
00018
00019
00020 struct StandardIo::pImpl
00021 {
00022 string error_message_;
00023 struct pollfd nfds_;
00024
00025
00026 pImpl(void)
00027 {
00028 nfds_.fd = 0;
00029 nfds_.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
00030 nfds_.revents = 0;
00031 }
00032 };
00033
00034
00035 StandardIo::StandardIo(void) : pimpl(new pImpl)
00036 {
00037 }
00038
00039
00040 StandardIo::~StandardIo(void)
00041 {
00042 }
00043
00044
00045 const char* StandardIo::what(void) const
00046 {
00047 return pimpl->error_message_.c_str();
00048 }
00049
00050
00051 bool StandardIo::connect(const char* device, long baudrate)
00052 {
00053 static_cast<void>(device);
00054 static_cast<void>(baudrate);
00055
00056
00057
00058 return true;
00059 }
00060
00061
00062 void StandardIo::disconnect(void)
00063 {
00064
00065 }
00066
00067
00068 bool StandardIo::setBaudrate(long baudrate)
00069 {
00070 static_cast<void>(baudrate);
00071
00072
00073 return true;
00074 }
00075
00076
00077 long StandardIo::baudrate(void) const
00078 {
00079
00080 return 0;
00081 }
00082
00083
00084 bool StandardIo::isConnected(void) const
00085 {
00086 return true;
00087 }
00088
00089
00090 int StandardIo::send(const char* data, size_t count)
00091 {
00092 return fwrite(data, 1, count, stdout);
00093 }
00094
00095
00096 int StandardIo::receive(char* data, size_t count, int timeout)
00097 {
00098 int filled = 0;
00099
00100 while (filled < static_cast<int>(count)) {
00101 if (poll(&pimpl->nfds_, 1, timeout) == 0) {
00102 break;
00103 }
00104
00105 fgets(&data[filled], count - filled, stdin);
00106 int n = strlen(&data[filled]);
00107 if (n < 0) {
00108 return filled;
00109 }
00110 filled += n;
00111 }
00112 return filled;
00113 }
00114
00115
00116 size_t StandardIo::size(void) const
00117 {
00118
00119 return 0;
00120 }
00121
00122
00123 void StandardIo::flush(void)
00124 {
00125 fflush(stdout);
00126 }
00127
00128
00129 void StandardIo::clear(void)
00130 {
00131
00132 }
00133
00134
00135 void StandardIo::ungetc(const char ch)
00136 {
00137 std::ungetc(ch, stdin);
00138 }