From: Martin Jambon <martin_jambon@emailuser.net>
To: Yan Jun Daisy Chen <yjc01@doc.ic.ac.uk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] extract info from file
Date: Wed, 26 Jan 2005 11:14:45 -0800 (PST) [thread overview]
Message-ID: <Pine.LNX.4.44.0501261100550.1256-100000@localhost> (raw)
In-Reply-To: <Pine.LNX.4.58.0501261533170.14221@quantum06.doc.ic.ac.uk>
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
prev parent reply other threads:[~2005-01-26 19:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-26 15:37 Yan Jun Daisy Chen
2005-01-26 19:14 ` Martin Jambon [this message]
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=Pine.LNX.4.44.0501261100550.1256-100000@localhost \
--to=martin_jambon@emailuser.net \
--cc=caml-list@inria.fr \
--cc=yjc01@doc.ic.ac.uk \
/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