summaryrefslogtreecommitdiffstats
path: root/testsuite/tests/basic-io/wc.ml
blob: d6655a9460c5f4fb7f1d72a4849f17d0f0b54d97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(***********************************************************************)
(*                                                                     *)
(*                                OCaml                                *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 1995 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* Counts characters, lines and words in one or several files. *)

let chars = ref 0
and words = ref 0
and lines = ref 0

type state = Inside_word | Outside_word

let count_channel in_channel =
  let rec count status =
    let c = input_char in_channel in
    incr chars;
    match c with
      '\n' ->
        incr lines; count Outside_word
    | ' ' | '\t' ->
        count Outside_word
    | _ ->
        if status = Outside_word then begin incr words; () end;
        count Inside_word
  in
    try
      count Outside_word
    with End_of_file ->
      ()

let count_file name =
  let ic = open_in name in
  count_channel ic;
  close_in ic

let print_result () =
  print_int !chars; print_string " characters, ";
  print_int !words; print_string " words, ";
  print_int !lines; print_string " lines";
  print_newline()

let count name =
  count_file name;
  print_result ()

let _ =
try
  if Array.length Sys.argv <= 1 then
    count_channel stdin                (* No command-line arguments *)
  else
    for i = 1 to Array.length Sys.argv - 1 do
      count_file  Sys.argv.(i)
    done;
  print_result ()
with Sys_error s ->
  print_string "I/O error: ";
  print_string s;
  print_newline()