All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/connection/ConnectionUtils.cpp
Go to the documentation of this file.
00001 
00010 #include "ConnectionUtils.h"
00011 #include "Connection.h"
00012 
00013 using namespace qrk;
00014 
00015 
00016 bool qrk::isLF(const char ch)
00017 {
00018     return ((ch == '\r') || (ch == '\n')) ? true : false;
00019 }
00020 
00021 
00022 void qrk::skip(Connection* con, int total_timeout, int each_timeout)
00023 {
00024     if (each_timeout <= 0) {
00025         each_timeout = total_timeout;
00026     }
00027 
00028     // !!! total_timeout を使う実装に修正する
00029     char recv_ch;
00030     while (1) {
00031         int n = con->receive(&recv_ch, 1, each_timeout);
00032         if (n <= 0) {
00033             break;
00034         }
00035     }
00036 }
00037 
00038 
00039 int qrk::readline(Connection* con, char* buf, const size_t count, int timeout)
00040 {
00041     // 1文字ずつ読み出して評価する
00042     bool is_timeout = false;
00043     size_t filled = 0;
00044 
00045     while (filled < count) {
00046         char recv_ch;
00047         int n = con->receive(&recv_ch, 1, timeout);
00048         if (n <= 0) {
00049             is_timeout = true;
00050             break;
00051         } else if (isLF(recv_ch)) {
00052             break;
00053         }
00054         buf[filled++] = recv_ch;
00055     }
00056     if (filled == count) {
00057         --filled;
00058         con->ungetc(buf[filled]);
00059     }
00060     buf[filled] = '\0';
00061 
00062     if ((filled == 0) && is_timeout) {
00063         return -1;
00064     } else {
00065         return static_cast<int>(filled);
00066     }
00067 }