/** * Check to see if a stream still has data. * * @return True if data can be read from the stream, false otherwise. * * @remarks TTY devices require special handling. But, if stdin is opened * through a pipe, it won't be marked as a TTY, so we need to check for * that also. */ bool SysFile::hasData() { // not available for reads? Can't have data if (!readable) { return false; } if (isTTY || (isStdIn() && !hasBufferedInput())) { int bytesWaiting; ioctl(fileHandle, FIONREAD, &bytesWaiting); if (bytesWaiting) { return true; } else { return false; } } // we might have something buffered, but also check the // actual stream. return !atEof(); }
/* PycBuffer */ int PycBuffer::getByte() { if (atEof()) return EOF; int ch = (int)(*(m_buffer + m_pos)); ++m_pos; return ch & 0xFF; // Make sure it's just a byte! }
bool SysFile::seekForwardLines(int64_t startPosition, int64_t &lineCount, int64_t &endPosition) { // make sure we flush any output data flush(); // get a buffer for searching char *mybuffer = (char *)malloc(LINE_POSITIONING_BUFFER); if (mybuffer == NULL) { errInfo = ENOMEM; return false; } for (;;) { int readLength = LINE_POSITIONING_BUFFER; // This is likely due to hitting the end-of-file. We'll just // return our current count and indicate this worked. if (!setPosition(startPosition, startPosition)) { free(mybuffer); // set the return position and get outta here endPosition = startPosition; return true; } size_t bytesRead; if (!read(mybuffer, readLength, bytesRead)) { free(mybuffer); // if we've hit an eof condition, this is the end if (atEof()) { // set the return position and get outta here endPosition = startPosition; return true; } // read error, return false; } // have we hit the eof? if (bytesRead == 0) { free(mybuffer); // set the return position and get outta here endPosition = startPosition; return true; } size_t offset = 0; while (offset < bytesRead) { // we're only interested in \n character, since this will // mark the transition point between lines. if (mybuffer[offset] == '\n') { // reduce the line count by one. lineCount--; // reached the requested point? if (lineCount == 0) { // set the return position and get outta here endPosition = startPosition + offset + 1; free(mybuffer); return true; } } // step to the next character; offset++; } // move the start position...if at the end, we might not // get a full buffer startPosition += bytesRead; } }