123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154# 1 "Camomile/public/oOChannel.ml"(** Object Oriented Channel *)(* Copyright (C) 2002, 2003, 2010 Yamagata Yoriyuki. *)(* This library 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. *)(* As a special exception to the GNU Library General Public License, you *)(* may link, statically or dynamically, a "work that uses this library" *)(* with a publicly distributed version of this library to produce an *)(* executable file containing portions of this 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 this library", *)(* we mean either the unmodified Library as distributed by the authors, *)(* or a modified version of this 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 . *)(* This library 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 *)(* Lesser General Public License for more details. *)(* You should have received a copy of the GNU Lesser General Public *)(* License along with this library; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)(* USA *)(* You can contact the authour by sending email to *)(* yoriyuki.y@gmail.com *)classtype['a]obj_input_channel=objectmethodget:unit->'amethodclose_in:unit->unitendclasstype['a]obj_output_channel=objectmethodput:'a->unitmethodflush:unit->unitmethodclose_out:unit->unitendclass['a]channel_of_streams=objectvals=smethodget():'a=tryStream.nextswithStream.Failure->raiseEnd_of_filemethodclose_in()=()endletstream_of_channelinchan=Stream.from(fun_->trySome(inchan#get())withEnd_of_file->None)classtypechar_input_channel=objectmethodinput:Bytes.t->int->int->intmethodclose_in:unit->unitendclasstypechar_output_channel=objectmethodoutput:Bytes.t->int->int->intmethodflush:unit->unitmethodclose_out:unit->unitendclasschar_input_channel_of(oc:char#obj_input_channel)=objectmethodclose_in()=oc#close_in()methodinputbposlen=letp=refposin(trywhile!p<pos+lendoBytes.setb!p(oc#get());incrp;done;()withEnd_of_file->());letlen=!p-posiniflen<=0thenraiseEnd_of_fileelselenendclasschar_obj_input_channel_of(ic:char_input_channel)=letb=Bytes.make1024'\000'inletpos=ref0inletlen=ref0inobject(self)methodget()=if!pos>=!lenthenbeginlen:=ic#inputb01024;pos:=0;self#get()endelseletc=Bytes.getb!posinincrpos;cmethodclose_in()=ic#close_in()endclasschar_output_channel_of(oc:char#obj_output_channel)=objectmethodflush=oc#flushmethodclose_out=oc#close_outmethodoutputbplen=fori=ptop+len-1dooc#put(Bytes.getbi)done;lenendclasschar_obj_output_channel_of(out:char_output_channel)=letb=Bytes.make1024'\000'inletpos=ref0inobjectmethodputc=Bytes.setb!posc;incrpos;if!pos>=1024thenletn=out#outputb01024inBytes.blitbnb0(1024-n);pos:=1024-nmethodflush()=letn=out#outputb0!posinifn<!posthenbeginBytes.blitbnb0(!pos-n);pos:=!pos-n;failwith"OOChannel.char_output_channel_of#flush: \
Cannot flush the entire buffer";endelsepos:=0methodclose_out()=out#flush();out#close_out()endclassof_in_channelp_in=objectmethodclose_in()=close_inp_inmethodinputbplen=letlen=inputp_inbpleniniflen=0thenraiseEnd_of_fileelselenendclassof_out_channelp_out=objectmethodclose_out()=close_outp_outmethodoutputbplen=outputp_outbplen;lenmethodflush()=flushp_outend