www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - readLine and line terminators

reply llee <llee goucher.edu> writes:
I'm parsing a cgi request and was using the din.readLine function to get each
line. readLine will read bytes from a stream until it encounters either '\n' or
'\r'. I need it return lines terminated with "\n\r". Does anyone know how to do
this. Or if there are any other options. 
Jun 18 2008
parent reply "Nick Sabalausky" <a a.a> writes:
"llee" <llee goucher.edu> wrote in message 
news:g3br0o$21np$1 digitalmars.com...
 I'm parsing a cgi request and was using the din.readLine function to get 
 each line. readLine will read bytes from a stream until it encounters 
 either '\n' or '\r'. I need it return lines terminated with "\n\r". Does 
 anyone know how to do this. Or if there are any other options.
I'm not familiar with the details of the text IO APIs, but you could always just wrap it in another function like this: //Psuedocode string ReadLineGuaranteeLineEnding() // Could probably think of a better name { // Read from din.readLine // Check for and remove any trailing '\n' and '\r' // Append "\n\r" // Return result }
Jun 18 2008
parent reply llee <llee goucher.edu> writes:
Nick Sabalausky Wrote:

 "llee" <llee goucher.edu> wrote in message 
 news:g3br0o$21np$1 digitalmars.com...
 I'm parsing a cgi request and was using the din.readLine function to get 
 each line. readLine will read bytes from a stream until it encounters 
 either '\n' or '\r'. I need it return lines terminated with "\n\r". Does 
 anyone know how to do this. Or if there are any other options.
I'm not familiar with the details of the text IO APIs, but you could always just wrap it in another function like this: //Psuedocode string ReadLineGuaranteeLineEnding() // Could probably think of a better name { // Read from din.readLine // Check for and remove any trailing '\n' and '\r' // Append "\n\r" // Return result }
I need din.readLine to read lines that are terminated with '\n\r' with these characters removed. Right now, readLine will read a line until it encounters '\n' and return. It will then read the '\r' and return. I wanted to know if there is a way to read a line that is terminated with '\n\r' and return it with these characters removed.
Jun 18 2008
parent "Nick Sabalausky" <a a.a> writes:
"llee" <llee goucher.edu> wrote in message 
news:g3bt8f$276o$1 digitalmars.com...
 I need din.readLine to read lines that are terminated with '\n\r' with 
 these characters removed. Right now, readLine will read a line until it 
 encounters '\n' and return. It will then read the '\r' and return. I 
 wanted to know if there is a way to read a line that is terminated with 
 '\n\r' and return it with these characters removed.
Ahh, I see. If you do this: auto str = din.readLine(); char c; din.read(c); Does that put the '\n' or '\r' into the 'c' variable? (Or is that character just skipped? If it's just skipped, is it possible to seek back one character and read it?) If so, then maybe you could use that to reconstruct what you need. Or maybe you could just use either "void read(out char x)" or "size_t readBlock(void* buffer, size_t size)" to create your own version of readLine(). Although, unless I'm wrong (and I might be), creatting your own readLine with "read(out char)" might be slow, and creating it with readBlock might mke you lose out on any fancy UTF-interpreting that might be going in the other versions. Or if you're using D1, Tango might have a way of doing what you want. Sorry I can't be of more help than that.
Jun 18 2008