[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Martin Jambon <martin_jambon@e...> |
| Subject: | Re: [Caml-list] extract info from file |
On Wed, 26 Jan 2005, Yan Jun Daisy Chen wrote:
> I am trying to extract comments in a text file i.e. text between (* and
> *). I declared a globle sting variable, buff to store them, and want to
> store all the comments extracted in a list of string. Is the sting
> variable size fixed once I initialise it?
>
> The code I came up with is:
>
> open Unix;;
>
> let fileReader = openfile "student.cd" [O_RDONLY] 0o640;;
> let buff = ref "file: ";;
> let fileSize = (fstat fileReader).st_size;;
> (*let fileSize = 50;;*)
> let noOfChar = ref 0;;
>
> let extract_comment () =
> let openIndex = 0 in
> noOfChar := read fileReader !buff openIndex fileSize;
> (*print_string !buff; print_newline();
> print_int !noOfChar; print_newline();;*)
>
>
> let main () =
> (*let fileContent = read fileReader !buff 0 5 in
> print_int fileContent;*)
> extract_comment();;
>
> main ();;
>
> Is there a simpler way to do this?
Sure:
1) Install my favorite library, Micmatch :-)
2) Use the following code (which does what you are asking for):
(* micmatch foo.ml < some_file *)
open Micmatch
let _ =
let parse_contents = MAP "(*" (_* Lazy as s) "*)" -> `Comment s in
let comments =
Text.map
(function
`Text _ -> raise Text.Skip
| `Comment s -> s)
(parse_contents (Text.channel_contents stdin)) in
List.iter print_endline comments
(***)
Martin
--
Martin Jambon, PhD
Researcher in Structural Bioinformatics since the 20th Century
The Burnham Institute http://www.burnham.org
San Diego, California