Re: How to read carriage return codes?
Wouter Beek <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CAE1un7Mq=RnGnpU8KcpVrh6AN2fFkdBPNWog-vVi84gAZrBL0g@mail.gmail.com> |
Hi Carlo, Jan, Reading the stream as binary is indeed a good option, since it gives the exact same results as the Hex editor (not surprisingly). This is fine for my current use, so thanks a lot! Jan is right, loosing locale handling is sometimes a big drawback. Are newline handling and locate handling intertwined? I mean, would it be hard to have an option `newline(as_is)` and locate handling at the same time? --- Cheers!, Wouter. E-mail: [email protected] WWW: www.wouterbeek.com Tel.: 0647674624 On Tue, Jul 30, 2013 at 1:54 PM, Jan Wielemaker <[email protected]> wrote: > 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<https://lists.iai.uni-bonn.de/pipermail/swi-prolog/attachments/20130730/5508b300/attachment.obj> >>> >>>> >>>> ______________________________**_________________ >>> SWI-Prolog mailing list >>> [email protected].**de <[email protected]> >>> https://lists.iai.uni-bonn.de/**mailman/listinfo.cgi/swi-**prolog<https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog> >>> >>> -------------- next part -------------- >> HTML attachment scrubbed and removed >> ______________________________**_________________ >> SWI-Prolog mailing list >> [email protected].**de <[email protected]> >> https://lists.iai.uni-bonn.de/**mailman/listinfo.cgi/swi-**prolog<https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog> >> >> > > -------------- next part -------------- HTML attachment scrubbed and removed