Fix compilation errors with gcc5 (closes #12)
[mldonkey.git] / tools / get_range.ml
blob6d6ee0d9ea38cac3509d59ee0aaf156211f1527b
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 open Sys
21 open Int64ops
22 open Gettext
23 open Md4
24 open LittleEndian
25 open Unix
26 open Printf2
28 let _s x = _s "Get_range" x
29 let _b x = _b "Get_range" x
31 (*************************************************************************)
32 (* *)
33 (* tiger_of_array *)
34 (* *)
35 (*************************************************************************)
39 (*************************************************************************)
40 (* *)
41 (* MAIN *)
42 (* *)
43 (*************************************************************************)
45 let usage () =
46 lprintf "Bad number of arguments: get_range [(range|rangex) <begin_pos> <end_pos> <file_num> | size] <filename>\n";
47 exit 2
49 let _ =
50 try
51 if Array.length Sys.argv < 2 then usage ();
52 match Sys.argv.(1) with
53 "size" ->
54 if Array.length Sys.argv <> 3 then usage ();
55 let filename = argv.(2) in
56 Printf.printf "[SIZE %Ld]\n" (Unix32.getsize filename)
58 | "range" ->
59 if Array.length Sys.argv <> 6 then usage ();
60 let begin_pos = Int64.of_string argv.(2) in
61 let end_pos = Int64.of_string argv.(3) in
62 let file_num = argv.(4) in
63 let filename = argv.(5) in
64 let t = Unix32.create_ro filename in
66 let segment = kilobytes 10 in
67 let bufferlen = Int64.to_int segment in
68 let buffer = String.create bufferlen in
69 let rec iter begin_pos end_pos =
70 let len = end_pos -- begin_pos in
71 if len > zero then
72 let len64 = min segment len in
73 let len = Int64.to_int len64 in
74 Unix32.read t begin_pos buffer 0 len;
75 let encoded = Base64.encode_substring buffer 0 len in
76 Printf.printf "[SEGMENT %s %Ld %d %d]\n" file_num begin_pos len (
77 String.length encoded);
78 output_string Pervasives.stdout encoded;
79 Printf.printf "\n[/SEGMENT]\n";
80 iter (begin_pos ++ len64) end_pos
82 iter begin_pos end_pos
84 | "rangex" ->
85 if Array.length Sys.argv <> 6 then usage ();
86 let begin_pos = Int64.of_string argv.(2) in
87 let end_pos = Int64.of_string argv.(3) in
88 let file_num = argv.(4) in
89 let filename = argv.(5) in
90 let t = Unix32.create_ro filename in
92 let segment = kilobytes 10 in
93 let bufferlen = Int64.to_int segment in
94 let buffer = String.create bufferlen in
95 let rec iter begin_pos end_pos =
96 let len = end_pos -- begin_pos in
97 if len > zero then
98 let len64 = min segment len in
99 let len = Int64.to_int len64 in
100 Unix32.read t begin_pos buffer 0 len;
101 let encoded = String.sub buffer 0 len in
102 Printf.printf "[SEGMENT 8bits %s %Ld %d %d]\n" file_num begin_pos len (
103 String.length encoded);
104 output_string Pervasives.stdout encoded;
105 Printf.printf "[/SEGMENT]\n";
106 iter (begin_pos ++ len64) end_pos
108 iter begin_pos end_pos
110 | _ -> usage ()
111 with e ->
112 lprintf "Exception %s\n" (Printexc2.to_string e);
113 exit 2