123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136(*
* Copyright 2011 The Savonet Team
*
* This file is part of ocaml-mm.
*
* ocaml-mm 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.
*
* ocaml-mm 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 Lesser General Public License
* along with ocaml-mm; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* As a special exception to the GNU Library General Public License, you may
* link, statically or dynamically, a "work that uses the Library" with a publicly
* distributed version of the Library to produce an executable file containing
* portions of the Library, and distribute that executable file under terms of
* your choice, without any of the additional requirements listed in clause 6
* of the GNU Library General Public License.
* By "a publicly distributed version of the Library", we mean either the unmodified
* Library as distributed by The Savonet Team, or a modified version of the Library that is
* distributed under the conditions defined in clause 3 of the GNU Library General
* Public License. This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU Library General Public License.
*
*)(** Helper functions for reading and writing. *)exceptionInvalid_datamoduleUnix=struct(** To be inherited to read and write from files. *)classvirtualrw?(read=false)?(write=false)fname=objectvalfd=letflag,perms=match(read,write)with|false,false->assertfalse|true,false->([Unix.O_RDONLY],0o644)|false,true->([Unix.O_WRONLY;Unix.O_CREAT;Unix.O_TRUNC],0o644)|true,true->([Unix.O_RDWR;Unix.O_CREAT;Unix.O_TRUNC],0o644)inUnix.openfilefnameflagpermsmethodprivatestream_readbufofslen=Unix.readfdbufofslenmethodprivatestream_writebufofslen=Unix.writefd(Bytes.of_stringbuf)ofslenmethodprivatestream_close=Unix.closefdmethodprivatestream_seekn=ignore(Unix.lseekfdnUnix.SEEK_SET)methodprivatestream_cur_pos=Unix.lseekfd0Unix.SEEK_CURendendclassvirtualhelper=object(self)methodvirtualprivatestream_read:Bytes.t->int->int->intmethodprivateinput_oncen=letbuf=Bytes.createninletn=self#stream_readbuf0ninifn=Bytes.lengthbufthenbufelseBytes.subbuf0nmethodprivateinputn=letbuf=self#input_onceninletbuf=Bytes.to_stringbufinletbuflen=String.lengthbufinifbuflen=n||buflen=0thenbufelsebuf^self#input(n-buflen)methodprivatereally_inputn=letbuf=self#inputninifString.lengthbuf<>nthenraiseInvalid_data;bufmethodprivateinput_byte=letbuf=self#really_input1inint_of_charbuf.[0](* TODO: use really_input instead of input_byte *)methodprivateinput_int_num_bytesn=letrecaux=function|0->0|n->letb=self#input_byteinb+(256*aux(n-1))inauxnmethodprivateinput_int=self#input_int_num_bytes4methodprivateinput_short=self#input_int_num_bytes2methodprivateinput_int_num_bytes_ben=letans=ref0inletbuf=self#really_inputninfori=0ton-1doans:=(256*!ans)+int_of_charbuf.[i]done;!ansmethodprivateinput_int_be=self#input_int_num_bytes_be4methodprivateinput_short_be=self#input_int_num_bytes_be2methodvirtualprivatestream_write:string->int->int->intmethodprivateoutputs=letlen=String.lengthsinassert(self#stream_writes0len=len)methodprivateoutput_numbn=lets=Bytes.createbinfori=0tob-1doBytes.setsi(char_of_int((nlsr(8*i))land0xff))done;self#output(Bytes.to_strings)methodprivateoutput_byten=self#output_num1nmethodprivateoutput_shortn=self#output_num2nmethodprivateoutput_intn=self#output_num4nmethodprivateoutput_num_bebn=lets=Bytes.createbinfori=0tob-1doBytes.setsi(char_of_int((nlsr(8*(b-i-1)))land0xff))done;self#output(Bytes.to_strings)methodprivateoutput_short_ben=self#output_num_be2nmethodprivateoutput_int_ben=self#output_num_be4nend