Download code

Jump to: navigation, search

Back to Word_count_(OCaml)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

wc.ml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Word_count_(OCaml)?oldid=18682
14 *)
15 
16 #load "str.cma"
17 open Arg
18   
19 type opts = { showLines : bool;
20               showWords : bool;
21               showChars : bool }
22 
23 let words =
24   Str.split (Str.regexp "[ \t\n]+")
25 
26 let rec getCount hdl (f,ls,ws,cs) =
27   try
28     let line = input_line hdl in
29     let ls = ls + 1
30     and ws = ws + List.length (words line)
31     and cs = cs + String.length line + 1 in
32       getCount hdl (f,ls,ws,cs)
33   with End_of_file ->
34     (f,ls,ws,cs)
35 
36 let countFile file =
37   let hdl = open_in file in
38   let count = getCount hdl (file,0,0,0) in
39     close_in hdl;
40     count
41 
42 let printCountLine opts (f,ls,ws,cs) =
43   if opts.showLines then Printf.printf "\t%d" ls;
44   if opts.showWords then Printf.printf "\t%d" ws;
45   if opts.showChars then Printf.printf "\t%d" cs;
46   Printf.printf "\t%s\n" f
47 
48 let countAndPrintFile opts (tf,tls,tws,tcs) file =
49   let (f,ls,ws,cs) as count = countFile file in
50     printCountLine opts count;
51     (tf, tls+ls, tws+ws, tcs+cs)
52 
53 let wc opts = function
54     []
55   | ["-"] -> printCountLine opts (getCount stdin ("",0,0,0))
56   | [file] -> printCountLine opts (countFile file)
57   | files ->
58       let totalcount = List.fold_left (countAndPrintFile opts) ("total",0,0,0) files in
59         printCountLine opts totalcount
60 
61 
62 let header = "Usage: wc [OPTION...] [files...]"
63 
64 let parseOpts () =
65   let lines = ref false
66   and words = ref false
67   and chars = ref false
68   and files = ref [] in
69   let process_arg file =
70     files := file :: !files in
71   let options =
72     [ "-l", Set lines, "show line count";
73       "-w", Set words, "show word count";
74       "-c", Set chars, "show character count";
75       "-", Unit (fun () -> process_arg "-"), "input via stdin" ] in
76     parse options process_arg header;
77     if not !lines && not !words && not !chars then
78       lines := true; words := true; chars := true;
79     { showLines = !lines;
80       showWords = !words;
81       showChars = !chars }, List.rev !files            
82 
83 let () =
84   let opts, files = parseOpts () in
85     wc opts files


hijacker
hijacker
hijacker
hijacker