MCPcopy Create free account
hub / github.com/THUNLP-MT/MEAN / fill_buffer

Method fill_buffer

evaluation/TMscore.cpp:2086–2136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2084 */
2085 template <typename C, typename T>
2086 bool
2087 basic_pstreambuf<C,T>::fill_buffer(bool non_blocking)
2088 {
2089 const std::streamsize pb1 = this->gptr() - this->eback();
2090 const std::streamsize pb2 = pbsz;
2091 const std::streamsize npb = std::min(pb1, pb2);
2092
2093 char_type* const rbuf = rbuffer();
2094
2095 if (npb)
2096 traits_type::move(rbuf + pbsz - npb, this->gptr() - npb, npb);
2097
2098 std::streamsize rc = -1;
2099
2100 if (non_blocking)
2101 {
2102 const int flags = ::fcntl(rpipe(), F_GETFL);
2103 if (flags != -1)
2104 {
2105 const bool blocking = !(flags & O_NONBLOCK);
2106 if (blocking)
2107 ::fcntl(rpipe(), F_SETFL, flags | O_NONBLOCK); // set non-blocking
2108
2109 error_ = 0;
2110 rc = read(rbuf + pbsz, bufsz - pbsz);
2111
2112 if (rc == -1 && error_ == EAGAIN) // nothing available
2113 rc = 0;
2114 else if (rc == 0) // EOF
2115 rc = -1;
2116
2117 if (blocking)
2118 ::fcntl(rpipe(), F_SETFL, flags); // restore
2119 }
2120 }
2121 else
2122 rc = read(rbuf + pbsz, bufsz - pbsz);
2123
2124 if (rc > 0 || (rc == 0 && non_blocking))
2125 {
2126 this->setg( rbuf + pbsz - npb,
2127 rbuf + pbsz,
2128 rbuf + pbsz + rc );
2129 return true;
2130 }
2131 else
2132 {
2133 this->setg(NULL, NULL, NULL);
2134 return false;
2135 }
2136 }
2137
2138 /**
2139 * Writes up to @a n characters to the pipe from the buffer @a s.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected