Version française
Home     About     Download     Resources     Contact us    
Browse thread
Str library for channels?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alex Baretta <alex@b...>
Subject: Str library for channels?
I have been confronted with the following little problem: finding the 
first occurrence of a string matching a given regular expression in an 
indefinitely long file. My solution was based on ocamllex. Here it is.

let expr = ... as expr

rule find = parse
     | ['\000'-'\255'] { find lexbuf }
     | expr { print_string expr; copy lexbuf }

and copy = shortest
| ([^'\n']* '\n') as line { print_string line; copy lexbuf }
| ([^'\n']* as line) eof { print_string line }

{ find (Lexing.from_channel stdin) }

I think that using ocamllex for such a minimal task is rather 
cumbersome. What I would like to do is the following:

let re = Str.regexp ...
let () = Pervasives.seek_in (Str.seek_forward re stdin)
let () = copy_channel stdin stdout

Where the following is defined somewhere.

let copy_channel ?(s=String.create 1024) in_ch out_ch =
   let length = String.length s in
   let rec loop bytes_read =
     output out_ch s 0 bytes_read;
     loop (input in_ch s 0 length)
   in loop (input in_ch s 0 length)

Str does not support scanning files. Is this a limitation in the API or 
in the regexp engine? Could Str be extended to handle files as well as 
strings?

Alex

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>