From: "viktor tron" <viktor.tron.ml@gmail.com>
To: caml-list@inria.fr
Subject: copy the rest of a file after scanning
Date: Mon, 17 Mar 2008 14:00:35 -0400 [thread overview]
Message-ID: <8cc3d8520803171100q55ea2229uec70b0eb92c8173f@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 2547 bytes --]
Dear list,
I have the funniest problem.
I use Scan to scan a file, and output an edited variant, when edits are done
I just need to copy the remainder of the file.
This ludicrious task is proving more elusive to handle than the whole
project.
The problem is that when I finish editing, the scanning buffer is active and
not empty and
my reading position in the input channel is not where I am currently
at in scanning.
I don't want to use scanner to consume the remaining 3 terrabytes digesting
it line by line. I can't seem to be able to scan bigger chunks than lines
either unless I can name a character
that certainly does not appear in the text I am reading.
If there is a character like this, say @, then I am ok with
1)
scan "%s@@%!" (fun s -> Printf.fprintf corpus_out "%s" s)
this one reads till the next @ character which is ignored or the end of the
input, which is checked with putting %! explicitly.
This passes my tests, but horribly ugly, since there is no character that I
can guarantee this way.
Plus I might not have memory for passing this whole chunk as one
string if the file is large.
So as an alternative I did this:
2)
(* we set the input channel reading position to where we are in scanning *)
let _ = scan "%n" (fun x -> seek_in corpus_in (x - 1)) in
(* and then dump the rest trivially in chunks of buf_size chars *)
let buf = String.create buf_size in
let rec dump () =
let len = input corpus_in buf 0 buf_size in
if len > 0 then (output corpus_out buf 0 len; dump () )
in
or in one go
3)
let end_pos = in_channel_length corpus_in
let len = end_pos - pos_in corpus_in in
let s = String.create len in
let _ = really_input corpus_in s 0 len in
Printf.fprintf corpus_out "%s" s;
On my mac and linux, all works smoothly, till I used it on windows.
These do not work on windows, since in_channel_length and seek do not take
into account the newline translations that take
place at reading.
Or in other words, the scan module reports character positions incorrectly
since CRLF=\013\010 is counted as one character and matched by \n.
I suspect 2 does not work either, since seek is probably the same as
in_channel_length when it comes to counting chars.
So there is no way to combine scan positions and in_channel/seek type
positions.
If there was a way to dump and empty the scanning buffer, then I could then
just use (2), since
then scanning pos and pos_in would align, but I found no way of doing that.
I have no idea how to solve this. Well, I guess I am missing something
trivial.
Thanks for help
Viktor
[-- Attachment #2: Type: text/html, Size: 3013 bytes --]
next reply other threads:[~2008-03-17 18:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-17 18:00 viktor tron [this message]
2008-03-17 23:29 viktor tron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8cc3d8520803171100q55ea2229uec70b0eb92c8173f@mail.gmail.com \
--to=viktor.tron.ml@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox