fetch rows up to and including the specified row, when available, i.e. do not block when pipe is non-blocking
| 1763 | |
| 1764 | // fetch rows up to and including the specified row, when available, i.e. do not block when pipe is non-blocking |
| 1765 | bool Query::fetch(int row) |
| 1766 | { |
| 1767 | int prev_rows = rows_; |
| 1768 | |
| 1769 | while (rows_ <= row) |
| 1770 | { |
| 1771 | bool incomplete = false; |
| 1772 | |
| 1773 | // look for the first newline character in the buffer |
| 1774 | char *nlptr = static_cast<char*>(memchr(buffer_, '\n', buflen_)); |
| 1775 | |
| 1776 | if (nlptr == NULL) |
| 1777 | { |
| 1778 | // no newline and buffer is not full and not EOF reached yet, get more data |
| 1779 | if (buflen_ < QUERY_BUFFER_SIZE && !eof_) |
| 1780 | { |
| 1781 | #ifdef OS_WIN |
| 1782 | |
| 1783 | // try to fetch more data from the non-blocking pipe when immediately available |
| 1784 | DWORD nread = 0; |
| 1785 | bool avail = !pending_; |
| 1786 | |
| 1787 | if (pending_) |
| 1788 | { |
| 1789 | pending_ = false; |
| 1790 | |
| 1791 | if (!GetOverlappedResult(hPipe_, &overlapped_, &nread, FALSE)) |
| 1792 | { |
| 1793 | switch (GetLastError()) |
| 1794 | { |
| 1795 | case ERROR_IO_INCOMPLETE: |
| 1796 | pending_ = true; |
| 1797 | break; |
| 1798 | |
| 1799 | case ERROR_MORE_DATA: |
| 1800 | break; |
| 1801 | |
| 1802 | case ERROR_HANDLE_EOF: |
| 1803 | default: |
| 1804 | close(search_pipe_[0]); |
| 1805 | eof_ = true; |
| 1806 | Static::cancel_ugrep(); |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | if (avail) |
| 1812 | { |
| 1813 | pending_ = false; |
| 1814 | |
| 1815 | if (!ReadFile(hPipe_, buffer_ + buflen_, static_cast<DWORD>(QUERY_BUFFER_SIZE - buflen_), &nread, blocking_ ? NULL : &overlapped_)) |
| 1816 | { |
| 1817 | switch (GetLastError()) |
| 1818 | { |
| 1819 | case ERROR_IO_PENDING: |
| 1820 | pending_ = true; |
| 1821 | break; |
| 1822 |