(* O'Caml Freenet Client Protocol client module *) (* by Travis Bemann *) (* *) (* This program is free software; you can redistribute it and/or *) (* modify it under the terms of the GNU Lesser General Public *) (* License as published by the Free Software Foundation; either *) (* version 2 of the License, or (at your option) any later version. *) (* *) (* This program 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 Lesser General Public License for more details. *) let strip_left s = let rec strip_left_step s i = let l = String.length s in if l <> i then match (String.get s i) with ' ' | '\t' | '\n' | '\r' -> strip_left_step s (i + 1) | _ -> String.sub s i (l - i) else "" in strip_left_step s 0;; let strip_right s = let rec strip_right_step s i = if i >= 0 then match (String.get s i) with ' ' | '\t' | '\n' | '\r' -> strip_right_step s (i - 1) | _ -> String.sub s 0 (i + 1) else "" in let l = String.length s in if l > 0 then strip_right_step s (l - 1) else "";; let strip s = strip_right (strip_left s);; let split_line s = try let i = String.index s '\n' in if i <> 0 then begin if String.get s (i - 1) = '\r' then String.sub s 0 (i - 1) else String.sub s 0 i end, String.sub s (i + 1) ((String.length s) - (i + 1)) else "", String.sub s 1 ((String.length s) - 1) with Not_found -> s, "";; let split_line_full s = let rec split_step s i l = try let ic = String.index_from s i '\n' in if ic <> i then let p = if String.get s (ic - 1) = '\r' then String.sub s i ((ic - 1) - i) else String.sub s i (ic - i) in split_step s (ic + 1) (p :: l) else split_step s (i + 1) ("" :: l) with Not_found -> if i <> (String.length s) then List.rev ((String.sub s i ((String.length s) - i)) :: l) else List.rev l in split_step s 0 [];; let int_of_hex x = let rec int_of_hex_step x i n = if (String.length x) > i then let c = String.get x i in int_of_hex_step x (i + 1) ((n * 0x10) + (match c with '0' .. '9' -> (int_of_char c) - (int_of_char '0') | 'A' .. 'F' -> ((int_of_char c) - (int_of_char 'A')) + 0xA | 'a' .. 'f' -> ((int_of_char c) - (int_of_char 'a')) + 0xA | _ -> raise (Failure "Not a hexadecimal value"))) else n in int_of_hex_step x 0 0;; let split2 string char = try let off = String.index string char in let pre = String.sub string 0 off and post = String.sub string (off + 1) ((String.length string) - (off + 1)) in (pre, post) with Invalid_argument x -> assert false let splitstrip2 s c = let a, b = split2 s c in (strip_left a, strip_right b)