瀏覽代碼

Implementing Child::readDelimited().


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@85 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz 9 年之前
父節點
當前提交
d23e25b988
共有 1 個文件被更改,包括 79 次插入0 次删除
  1. 79
    0
      child.hpp

+ 79
- 0
child.hpp 查看文件

@@ -473,6 +473,25 @@ namespace CodeDweller {
*/
void put(char const *ptr, size_t nBytes);

/** Check whether specified data is present in the buffer.

This method check whether the specified data is present in
the buffer, and provides the number of characters before the
specified data.

@param[in] delimiter is the specified data.

@param[out] nBytesBefore is the number of bytes before the
specified data.

@returns true if the delimiter is present in the buffer,
false otherwise.

*/
template<type U>
bool checkForDelimiter(U &delimiter, size_t &nbytesBefore) {
}

/** Get bytes from the buffer.

This method gets the specified number of bytes from the
@@ -912,6 +931,66 @@ namespace CodeDweller {

}

/** Non-blocking request to get data up to a delimiter read from
the child.

This method check whether the specified delimiter is in the
data received from the child.

This method attempts to get data up to and not including a
specified delimiter from the input buffer containing data
received from the child. The data that is provided and the
delimiter are erased from the input buffer.

The type T must have the following methods:

<ol>

<li>Methods required by CircularBuffer::getAndErase().</li>

<li>size().</li>

</ol>

Both std::vector<char> and std::string can be used for T and U.

@param[out] data contains the copied data.

@param[in] delimiter contains the delimiter.

@returns the number of bytes copied.

@see CircularBuffer::getAndErase().

*/
template<typename T, typename U>
bool readDelimited(T &data, U &delimiter) {

if (!isRunning()) {
throw std::logic_error("No child process is running.");
}

data.clear();

// Can be called in the user thread without locking the mutex.
if (readBuffer.empty()) {
return false;
}

std::lock_guard<std::mutex> lock(readBufferMutex);

size_t nBytesToRead = nBytes;

if (nBytesToRead > readBuffer.nUsed()) {
nBytesToRead = readBuffer.nUsed();
}
readBuffer.getAndErase(data, nBytesToRead);

return data.size();

}

/** Check whether the child process is running.

\returns True if the child process is running, false

Loading…
取消
儲存