send_feedback_form can be used by anyone.
[cocanwiki.git] / scripts / cocanwiki_files.ml
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.2 2004/09/09 12:21:22 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Printf
23
24 (*----- File functions. -----*)
25
26 let (//) = Filename.concat
27
28 let rec input_all_lines chan =
29   try
30     let line = input_line chan in
31     line :: input_all_lines chan
32   with
33       End_of_file -> []
34
35 let input_all chan =
36   let buf = Buffer.create 16384 in
37   let tmpsize = 16384 in
38   let tmp = String.create tmpsize in
39   let n = ref 0 in
40   while n := input chan tmp 0 tmpsize; !n > 0 do
41     Buffer.add_substring buf tmp 0 !n;
42   done;
43   Buffer.contents buf
44
45 let input_file filename =
46   let chan = open_in_bin filename in
47   let data = input_all chan in
48   close_in chan;
49   data
50
51 let output_file filename data =
52   let chan = open_out_bin filename in
53   output_string chan data;
54   close_out chan
55
56 let output_tempfile data =
57   let filename, chan = Filename.open_temp_file "tmp" ".tmp" in
58   output_string chan data;
59   close_out chan;
60   filename
61
62 (*----- Command functions. -----*)
63
64 let cmd cmd =
65   let code = Sys.command cmd in
66   if code <> 0 then failwith (cmd ^ ": error code " ^ string_of_int code)
67
68 let copy infile outfile =
69   cmd (sprintf "cp %s %s" infile outfile)
70
71 let pget cmd =
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
75   (match stat with
76        Unix.WEXITED 0 -> ()
77      | Unix.WEXITED i ->
78          failwith ("command failed with code " ^ string_of_int i)
79      | Unix.WSIGNALED i ->
80          failwith ("command killed by signal " ^ string_of_int i)
81      | Unix.WSTOPPED i ->
82          failwith ("command stopped by signal " ^ string_of_int i));
83   lines
84
85 let unlink file =
86   try Unix.unlink file with Unix.Unix_error _ -> ()