1. Re: Mac text files and gets()

CChris wrote:
> 
> 
> I happened to have a text file with \r line terminators on my HD, and wanted
> to process it using gets(). It didn't work the expected way.
> 
> I ran the following test:
> }}}
<eucode>
> integer fh
> fh=open("test.txt","wb")
> puts(fh,{65,10,66,26})
> close(fh)
> fh=open("test.txt","r")
> ?gets(fh) -- {65,10}
> ?gets(fh) -- {66}
> ?gets(fh) -- -1
> ?machine_func(26,0)
> </eucode>
{{{

> 
> This shows that a Unix style file can be read properly.
> Now replace 10 by 13 in the puts() sequence (this is how a Mac file would look
> like), and the output is
> {65,66}
> -1
> -1
> Not the expected thing.
> 
> Since it is about as likely, under Windows, to have Unix files or Mac files
> on one's HD, isn't fixing/extending gets() to handle both formats a good idea?
> No need to modify the behaviour of EGets() for stdin, so changing lines
> 2668-72
> in be_runtime.c from
> 		if (c <= '\n') {
> 		    if (c == '\n') {
> 			break;
> 		    }
> 
> to
> 		if (c <= '\r') {
> 		    if (c == '\n') {
> 			break;
> 		    }
> 		    if (c == '\r') {
> 			c = '\n';
>                         break;
> 		    }
> should be enough. The extra test is taken only if a character in the 0.13
> range
> (a control charatcter) is read, so the impact should be nil on text files.
> 
> Did I miss some ripple effect?
> 
> CChris

Since there were exactly zero comments regarding this earler post, I'll update
the backend and the doc for gets() this weekend, unless there is a late outcry.

Addiionally, I'll consider changing the second test to "<=", so that VT and FF
are also treated as EOL by gets(). This can be useful when importing files output
by mainframe computers, and is already implemented in the D language.

CChris

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu