* extract info from file
@ 2005-01-26 15:37 Yan Jun Daisy Chen
2005-01-26 19:14 ` [Caml-list] " Martin Jambon
0 siblings, 1 reply; 2+ messages in thread
From: Yan Jun Daisy Chen @ 2005-01-26 15:37 UTC (permalink / raw)
To: caml-list
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?
Thanks
Daisy
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] extract info from file
2005-01-26 15:37 extract info from file Yan Jun Daisy Chen
@ 2005-01-26 19:14 ` Martin Jambon
0 siblings, 0 replies; 2+ messages in thread
From: Martin Jambon @ 2005-01-26 19:14 UTC (permalink / raw)
To: Yan Jun Daisy Chen; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-01-26 19:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-26 15:37 extract info from file Yan Jun Daisy Chen
2005-01-26 19:14 ` [Caml-list] " Martin Jambon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox