~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to examples/basics/wc_unix.ml

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(***********************************************************************)
2
 
(*                                                                     *)
3
 
(*                           Objective Caml                            *)
4
 
(*                                                                     *)
5
 
(*               Pierre Weis, projet Cristal, INRIA Rocquencourt       *)
6
 
(*                                                                     *)
7
 
(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
8
 
(*  en Automatique.  All rights reserved.  This file is distributed    *)
9
 
(*  only by permission.                                                *)
10
 
(*                                                                     *)
11
 
(***********************************************************************)
12
 
(*
13
 
- Records must be defined in Caml.
14
 
  Field contents cannot be modified unless the field has been declared mutable. 
15
 
 
16
 
- If r is a record with field f then r.f denotes the contents of field f.
17
 
 
18
 
- If r is a record with mutable field f then r.f <- v writes v in field f of r.
19
 
*)
20
 
type counts = {
21
 
 mutable chars : int;
22
 
 mutable lines : int;
23
 
 mutable words : int;
24
 
};;
25
 
 
26
 
let wc_count = {chars = 0; lines = 0; words = 0}
27
 
and wc_count_total = {chars = 0; lines = 0; words = 0};;
28
 
 
29
 
let reset_wc_count () =
30
 
 wc_count.chars <- 0; wc_count.lines <- 0; wc_count.words <- 0;;
31
 
 
32
 
let cumulate_wc_count () =
33
 
 wc_count_total.chars <- wc_count_total.chars + wc_count.chars;
34
 
 wc_count_total.lines <- wc_count_total.lines + wc_count.lines;
35
 
 wc_count_total.words <- wc_count_total.words + wc_count.words;;
36
 
 
37
 
let rec counter ic iw =
38
 
 let c = input_char ic in
39
 
 wc_count.chars <- wc_count.chars + 1;
40
 
 match c with
41
 
 | '\n' ->
42
 
    wc_count.lines <- wc_count.lines + 1;
43
 
    counter ic false
44
 
 | ' ' | '\t' ->
45
 
    counter ic false
46
 
 | _ ->
47
 
    if not iw then wc_count.words <- wc_count.words + 1 else ();
48
 
    counter ic true;;
49
 
 
50
 
let count_channel ic =
51
 
 reset_wc_count ();
52
 
 try counter ic false with
53
 
 | End_of_file -> cumulate_wc_count (); close_in ic;;
54
 
 
55
 
(*
56
 
- The Printf.printf function is analoguous to the C printf function.
57
 
*)
58
 
let print_wc l w c f =
59
 
 Printf.printf "%10d%10d%10d %s\n" l w c f;;
60
 
 
61
 
let print_wc_file f =
62
 
 print_wc wc_count.lines wc_count.words wc_count.chars f
63
 
;;
64
 
 
65
 
let print_wc_total () =
66
 
 print_wc
67
 
  wc_count_total.lines wc_count_total.words wc_count_total.chars "total"
68
 
;;
69
 
 
70
 
let count_file file_name =
71
 
 try
72
 
  count_channel (open_in file_name);
73
 
  print_wc_file file_name
74
 
 with Sys_error s -> print_string s; print_newline (); exit 2
75
 
;;
76
 
 
77
 
let main () =
78
 
 let args = Sys.argv in
79
 
 let nb_files = Array.length args - 1 in
80
 
 for i = 1 to nb_files do
81
 
  count_file args.(i)
82
 
 done;
83
 
 if nb_files > 1 then print_wc_total ();
84
 
 exit 0;;
85
 
 
86
 
if !Sys.interactive then () else main ();;