~ubuntu-branches/ubuntu/trusty/ben/trusty

« back to all changes in this revision

Viewing changes to lib/benl_marshal.ml

  • Committer: Package Import Robot
  • Author(s): Stéphane Glondu, Stéphane Glondu, Mehdi Dogguy
  • Date: 2012-07-01 22:10:36 UTC
  • Revision ID: package-import@ubuntu.com-20120701221036-qwibgq0sf2x6mkyr
Tags: 0.6.1
[ Stéphane Glondu ]
* Initial packaging

[ Mehdi Dogguy ]
* Initial Release (Closes: #679547)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(**************************************************************************)
 
2
(*  Copyright © 2009-2010 Stéphane Glondu <steph@glondu.net>              *)
 
3
(*            © 2010 Mehdi Dogguy <mehdi@dogguy.org>                      *)
 
4
(*                                                                        *)
 
5
(*  This program is free software: you can redistribute it and/or modify  *)
 
6
(*  it under the terms of the GNU Affero General Public License as        *)
 
7
(*  published by the Free Software Foundation, either version 3 of the    *)
 
8
(*  License, or (at your option) any later version, with the additional   *)
 
9
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
 
10
(*                                                                        *)
 
11
(*  This program is distributed in the hope that it will be useful, but   *)
 
12
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
 
13
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
 
14
(*  Affero General Public License for more details.                       *)
 
15
(*                                                                        *)
 
16
(*  You should have received a copy of the GNU Affero General Public      *)
 
17
(*  License along with this program.  If not, see                         *)
 
18
(*  <http://www.gnu.org/licenses/>.                                       *)
 
19
(**************************************************************************)
 
20
 
 
21
open Benl_core
 
22
open Benl_base
 
23
open Benl_error
 
24
 
 
25
module type MARSHALLABLE = sig
 
26
  type t
 
27
  val magic_number : string
 
28
end
 
29
 
 
30
module Make (I : MARSHALLABLE) = struct
 
31
 
 
32
  let load filename =
 
33
    with_in_file filename begin
 
34
      fun ic ->
 
35
        let n = String.length I.magic_number in
 
36
        let buf = String.create n in
 
37
        really_input ic buf 0 n;
 
38
        if buf = I.magic_number then begin
 
39
          (input_value ic : I.t)
 
40
        end else begin
 
41
          raise (Bad_marshalled_data filename)
 
42
        end
 
43
    end
 
44
 
 
45
  let dump filename (data : I.t) =
 
46
    with_out_file filename begin
 
47
      fun ic ->
 
48
        output_string ic I.magic_number;
 
49
        output_value ic data
 
50
    end
 
51
 
 
52
end