File: testagrep.ml

package info (click to toggle)
ocamlagrep 1.0-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 164 kB
  • sloc: ml: 478; ansic: 252; makefile: 73
file content (50 lines) | stat: -rw-r--r-- 1,600 bytes parent folder | download | duplicates (6)
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
(* Test harness *)

open Printf

let numerrs = ref 0
and wholeword = ref false
and verbatim = ref false
and fromfile = ref false

let do_search p s =
  if !fromfile then begin
    let ic = open_in s in
    begin try
      while true do
        let l = input_line ic in
        let n = Agrep.errors_substring_match p
                  ~numerrs:!numerrs ~wholeword:!wholeword
                  l ~pos:0 ~len:(String.length l) in
        if n < max_int then printf "%d %s\n" n l
      done
    with End_of_file -> ()
    end;
    close_in ic
  end else begin
    let n = Agrep.errors_substring_match p
              ~numerrs:!numerrs ~wholeword:!wholeword
              s ~pos:0 ~len:(String.length s) in
    if n = max_int
    then printf "No match"
    else printf "Match, with %d error(s)" n;
    print_newline()
  end

let _ =
  let pattern = ref None in
  Arg.parse
    ["-1", Arg.Unit(fun () -> numerrs := 1), "  one error";
     "-2", Arg.Unit(fun () -> numerrs := 2), "  two error";
     "-3", Arg.Unit(fun () -> numerrs := 3), "  three error";
     "-e", Arg.Int(fun n -> numerrs := n), "<n>  n errors";
     "-f", Arg.Set fromfile, "  search in given file rather than in string";
     "-w", Arg.Set wholeword, "  match entire words";
     "-v", Arg.Set verbatim,  "  match string verbatim (no special chars)"]
    (fun s ->
      match !pattern with
        None ->
          pattern := Some(if !verbatim then Agrep.pattern_string s
                                       else Agrep.pattern s)
      | Some p -> do_search p s)
    "Usage: testagrep [options] <pattern> <string>\nOptions are:"