1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: cocanwiki_files.ml,v 1.1 2004/10/21 11:42:05 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 (*----- File functions. -----*)
26 let (//) = Filename.concat
28 let rec input_all_lines chan =
30 let line = input_line chan in
31 line :: input_all_lines chan
36 let buf = Buffer.create 16384 in
37 let tmpsize = 16384 in
38 let tmp = String.create tmpsize in
40 while n := input chan tmp 0 tmpsize; !n > 0 do
41 Buffer.add_substring buf tmp 0 !n;
45 let input_file filename =
46 let chan = open_in_bin filename in
47 let data = input_all chan in
51 let output_file filename data =
52 let chan = open_out_bin filename in
53 output_string chan data;
56 let output_tempfile data =
57 let filename, chan = Filename.open_temp_file "tmp" ".tmp" in
58 output_string chan data;
62 (*----- Command functions. -----*)
65 let code = Sys.command cmd in
66 if code <> 0 then failwith (cmd ^ ": error code " ^ string_of_int code)
68 let copy infile outfile =
69 cmd (sprintf "cp %s %s" infile outfile)
72 let chan = Unix.open_process_in cmd in
73 let lines = input_all_lines chan in
74 let stat = Unix.close_process_in chan in
78 failwith ("command failed with code " ^ string_of_int i)
80 failwith ("command killed by signal " ^ string_of_int i)
82 failwith ("command stopped by signal " ^ string_of_int i));
86 try Unix.unlink file with Unix.Unix_error _ -> ()