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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
########### Makefile for replace ##########
OCAMLC = ocamlc
CFLAGS =
OBJS = replace.cmo main_replace.cmo
.SUFFIXES: .ml .mli .cmo .cmi
.ml.cmo:
$(OCAMLC) -c $<
replace : ${OBJS}
${OCAMLC} ${OBJS} -o $@
clean :
rm -f *.cm* replace *~
main_replace.ml
open Sys;;
open Array;;
open Replace;;
let main () =
if not( file_exists (argv.(3))) then
(
print_endline("replace : " ^ argv.(3) ^ " : no such file");
print_endline("usage : replace key1 key2 file")
)
else
(
if length (argv) <> 4 then
print_endline("usage : replace key1 key2 file")
else
(
let source = open_in (argv.(3)) and target = open_out ("tmp") in
try
while true do
let line = input_line source in
let s = replace_line (argv.(1), argv.(2), line) in
output_string target s
done
with
| End_of_file ->
close_in source;
close_out target;
remove argv.(3);
rename "tmp" argv.(3)
)
)
;;
main ();;
replace.ml
open String;;
let replace_line keyword2replace keyword str =
let i = ref 0
and res = ref ""
and lgt = length keyword
and lgt2 = length (str)
in
while (!i <= (lgt2 - lgt)) do
if ((sub str (!i + 1) lgt) = keyword2replace) then
(
res := !res^keyword;
i := !i + lgt
)
else
(
res := !res ^ (make 1 str.[!i]); (*l'auto indentation est foireuse et je vois pas pourquoi*)
i := !i + 1
)
done;
res := !res^(sub str (!i+1) (lgt2 - !i));
!res
;; |
Partager