This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

redirecting cin


hi all,

quick question (that may be dumb - or inappropriate, but i didn't know where 
else to post to. so if i'm wrong, sorry in advance)

i have a bunch of different parsers in a collection. the parsers are 
flex/accent (like bison but parses context free grammars). the parser code is 
entirely written in C so i'm encapsulating the actual frontend parser in a 
shared library to be dynamically loaded when the executable needs it. this 
does a pretty good job avoiding collisions between the parser code (e.g. 
yytext). since the entire application is written in C++, i'm writing 
interfaces in C++ so i can just call:

MyParser parser;
parser.parser( some_file_stream );

such that the parser module is loaded during the persistence of the parser 
object. it works (worked) nicely until i upgraded to gcc 3.0.2 and lost the 
ability to grab the file descriptor from the streambuf. alas... okay enough 
background. on to the question.

i'm having some trouble redirecting a file passed on the command line to cin 
such that flex/accent can parse the file from stdin. the trouble is that it 
just doesn't work. i've read as much as i can from the internet, but can't 
find a really solid example of how to do this. does this make any sense?

anyway, here's the old code (give or take):

int MyParser::parse( ifstream &file )
{
	int fd = file.rdbuf()->fd();
	dup2( 0, fd );
	return yyparse();
}

worked great. now, since i don't get access to the file descriptor, i'm 
trying to redirect to cin, but like i said: doesn't work so great.

int MyParser::parse( std::ifstream &file )
{
	std::streambuf *in = std::cin.rdbuf( file.rdbuf() );
	yyparse();
	std::cin.rdbuf( in );
}

can somebody please help me with this? it's been frustrating me for about 2 
days now. a pointer to a good example would be great too :)

thanks,

andrew sutton
ansutton@sep.com

p.s. can responses come directly to me? i'm not a member of the list, but i 
really need some help on this.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]