File: get_range.ml

package info (click to toggle)
mldonkey 3.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,124 kB
  • ctags: 27,135
  • sloc: ml: 146,511; cpp: 11,802; ansic: 7,701; sh: 4,039; asm: 3,870; xml: 1,092; perl: 102; makefile: 101
file content (114 lines) | stat: -rw-r--r-- 4,354 bytes parent folder | download | duplicates (7)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
(*
    This file is part of mldonkey.

    mldonkey is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    mldonkey is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with mldonkey; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

open Sys
open Int64ops
open Gettext
open Md4
open LittleEndian
open Unix
open Printf2

let _s x = _s "Get_range" x
let _b x = _b "Get_range" x  

(*************************************************************************)
(*                                                                       *)
(*                         tiger_of_array                                *)
(*                                                                       *)
(*************************************************************************)

  
  
(*************************************************************************)
(*                                                                       *)
(*                         MAIN                                          *)
(*                                                                       *)
(*************************************************************************)

let usage () =
  lprintf "Bad number of arguments: get_range [(range|rangex) <begin_pos> <end_pos> <file_num> | size] <filename>\n";
  exit 2
  
let _ =
  try  
    if Array.length Sys.argv < 2 then usage ();
    match Sys.argv.(1) with
      "size" -> 
        if Array.length Sys.argv <> 3 then usage ();
        let filename = argv.(2) in
        Printf.printf "[SIZE %Ld]\n" (Unix32.getsize filename)
        
    | "range" ->
        if Array.length Sys.argv <> 6 then usage ();
        let begin_pos = Int64.of_string argv.(2) in
        let end_pos = Int64.of_string argv.(3) in
        let file_num = argv.(4) in
        let filename = argv.(5) in
        let t = Unix32.create_ro filename in
        
        let segment = kilobytes 10 in
        let bufferlen = Int64.to_int segment in
        let buffer = String.create bufferlen in
        let rec iter begin_pos end_pos =
          let len = end_pos -- begin_pos in
          if len > zero then
            let len64 = min segment len in
            let len = Int64.to_int len64 in
            Unix32.read t begin_pos buffer 0 len;
            let encoded = Base64.encode_substring buffer 0 len in
            Printf.printf "[SEGMENT %s %Ld %d %d]\n" file_num begin_pos len (
              String.length encoded);
            output_string Pervasives.stdout encoded;
            Printf.printf "\n[/SEGMENT]\n";
            iter (begin_pos ++ len64) end_pos
        in
        iter begin_pos end_pos
        
    | "rangex" ->
        if Array.length Sys.argv <> 6 then usage ();
        let begin_pos = Int64.of_string argv.(2) in
        let end_pos = Int64.of_string argv.(3) in
        let file_num = argv.(4) in
        let filename = argv.(5) in
        let t = Unix32.create_ro filename in
        
        let segment = kilobytes 10 in
        let bufferlen = Int64.to_int segment in
        let buffer = String.create bufferlen in
        let rec iter begin_pos end_pos =
          let len = end_pos -- begin_pos in
          if len > zero then
            let len64 = min segment len in
            let len = Int64.to_int len64 in
            Unix32.read t begin_pos buffer 0 len;
            let encoded = String.sub buffer 0 len in
            Printf.printf "[SEGMENT 8bits %s %Ld %d %d]\n" file_num begin_pos len (
              String.length encoded);
            output_string Pervasives.stdout encoded;
            Printf.printf "[/SEGMENT]\n";
            iter (begin_pos ++ len64) end_pos
        in
        iter begin_pos end_pos
        
    | _ -> usage ()
  with e ->
      lprintf "Exception %s\n" (Printexc2.to_string e);
      exit 2