[Pstreams-users] out_avail
Brought to you by:
redi
From: Eric O. <e_p...@sb...> - 2003-11-18 12:53:18
|
I had an issue with pstream.h that showed up in Linux. I opened a pstream (in,out,stderr). While reading stdout, the read would eventually block because the stderr filled some buffer. I needed a way to check if a character is available on stdout (or stderr) - or a nice way to make the readline non-blocking. (if I have lots of stderr from the cmd) I added a function to the pstreambuff class. I don't know it this was the best way, but it worked /** * @return Report if characters available to be read */ template <typename C, typename T> inline bool basic_pstreambuf<C,T>::out_avail() { // setup select structures fd_set rfds; FD_ZERO(&rfds); // clear FD_SET(rpipe_[rsrc_out],&rfds); struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 1; int maxfd = rpipe_[rsrc_out]; ::select(maxfd+1,&rfds,NULL,NULL,&tv); if (FD_ISSET(rpipe_[rsrc_out],&rfds)) { return(true); } else { return (false); } } ////////////////////////// Then I can just: redi::pstream parse(cmd,std::ios_base::in|std::ios_base::app); bool oav; while (getline(parse.out(),line)) { oav = parse.rdbuf()->out_avail(); process(line); while (!oav && (getline(parse.err(),line))) { process_error(line); oav = parse.rdbuf()->out_avail(); } } while ((getline(parse.err(),line))) { process_error(line); } parse.close(); /////////////////// very nice package BTW. Exactly what I needed - Eric Olson, eo...@ci... |