Re: How to read carriage return codes?

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 07/30/2013 12:59 PM, Carlo Capelli wrote:
> Hi Wouter
>
> I have this old code I used to transfer files with arbitrary encodings...
>
> copy_file(Source, Target) :- open(Source, read, S, [type(binary)]),
> open(Target, write, T, [type(binary)]), copy_stream_data(S, T), close(S),
> close(T).

Not sure this is what Wouter is after.  Yes, one way to get the cr is
to open the file as binary.  That also stops interpreting character
locale handling (e.g., UTF-8 --> Unicode).  To accommodate handling
crlf files on POSIX platforms, there is a property called `newline'
attached to a stream.  You can do

	open(File, read, In),
	set_stream(In, newline(posix)),

which will stop newline translation.  Very hacky.  I can't really
imagine a scenario where you would like to do that.  Somewhat more
realistically might be

	open(File, write, Out),
	set_stream(Out, newline(dos)),

which will write \r\n at the end of each line regardless of the
operating system.

The names posix and dos are a bit misleading.  Probably frustration
trying to make programs behave consistently in mixed Windows/Unix
environments :-(

	Cheers --- Jan

> bye Carlo
>
>
>
> 2013/7/30 Wouter Beek <[email protected]>
>
>> Hi all,
>>
>> I want to read a file with CRLF line endings to a list of codes.
>> When I use open/3 and read_stream_to_codes/2 to do this the code list only
>> has linefeeds (no carriage return).
>>
>> I'm probably overlooking something simple here... Is there a setting/flag
>> to get the CR codes as well? I'm using 64bit Windows.
>>
>> The file as viewed in a Hex editor:
>> ~~~{.hex}
>> 0D 0A 0D 0A 0D 0A 0D 0A 0D 0A
>> ~~~
>>
>> The Prolog code:
>> ~~~
>> :- module(crlf, []).
>>
>> :- use_module(library(apply)).
>> :- use_module(library(readutil)).
>>
>> :- initialization(crlf).
>>
>> crlf:-
>>    source_file(crlf, ThisFile),
>>    file_directory_name(ThisFile, ThisDirectory),
>>    absolute_file_name(
>>      crlf,
>>      File,
>>      [access(read), extensions([txt]), relative_to(ThisDirectory)]
>>    ),
>>    open(File, read, Stream),
>>    read_stream_to_codes(Stream, Codes),
>>    maplist(writeln, Codes).
>> ~~~
>>
>> The output of the Prolog code:
>> ~~~
>> 10
>> 10
>> 10
>> 10
>> 10
>> ~~~
>>
>> ---
>> Cheers!,
>> Wouter.
>>
>> E-mail: [email protected]
>> WWW: www.wouterbeek.com
>> Tel.: 0647674624
>> -------------- next part --------------
>> HTML attachment scrubbed and removed
>> -------------- next part --------------
>>
>>
>>
>>
>>
>> -------------- next part --------------
>> A non-text attachment was scrubbed...
>> Name: crlf.pl
>> Type: application/octet-stream
>> Size: 406 bytes
>> Desc: not available
>> URL: <
>> https://lists.iai.uni-bonn.de/pipermail/swi-prolog/attachments/20130730/5508b300/attachment.obj
>>>
>> _______________________________________________
>> SWI-Prolog mailing list
>> [email protected]
>> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.