(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_files.ml,v 1.2 2004/09/09 12:21:22 rich Exp $ * * This program 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. * * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Printf (*----- File functions. -----*) let (//) = Filename.concat let rec input_all_lines chan = try let line = input_line chan in line :: input_all_lines chan with End_of_file -> [] let input_all chan = let buf = Buffer.create 16384 in let tmpsize = 16384 in let tmp = String.create tmpsize in let n = ref 0 in while n := input chan tmp 0 tmpsize; !n > 0 do Buffer.add_substring buf tmp 0 !n; done; Buffer.contents buf let input_file filename = let chan = open_in_bin filename in let data = input_all chan in close_in chan; data let output_file filename data = let chan = open_out_bin filename in output_string chan data; close_out chan let output_tempfile data = let filename, chan = Filename.open_temp_file "tmp" ".tmp" in output_string chan data; close_out chan; filename (*----- Command functions. -----*) let cmd cmd = let code = Sys.command cmd in if code <> 0 then failwith (cmd ^ ": error code " ^ string_of_int code) let copy infile outfile = cmd (sprintf "cp %s %s" infile outfile) let pget cmd = let chan = Unix.open_process_in cmd in let lines = input_all_lines chan in let stat = Unix.close_process_in chan in (match stat with Unix.WEXITED 0 -> () | Unix.WEXITED i -> failwith ("command failed with code " ^ string_of_int i) | Unix.WSIGNALED i -> failwith ("command killed by signal " ^ string_of_int i) | Unix.WSTOPPED i -> failwith ("command stopped by signal " ^ string_of_int i)); lines let unlink file = try Unix.unlink file with Unix.Unix_error _ -> ()