123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204# 1 "Camomile/public/uLine.ml"(** Line IO *)(* Copyright (C) 2003 Yamagata Yoriyuki. distributed with LGPL *)(* 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 *)openOOChanneltypeseparator=[`CR|`LF|`CRLF|`NEL|`LS|`PS]classinput(op:separator)(inchan:UChar.t#obj_input_channel)=letsp=matchopwith`CR->[UChar.chr_of_uint0x000d]|`LF->[UChar.chr_of_uint0x000a]|`CRLF->[UChar.chr_of_uint0x000d;UChar.chr_of_uint0x000a]|`NEL->[UChar.chr_of_uint0x0085]|`LS->[UChar.chr_of_uint0x2028]|`PS->[UChar.chr_of_uint0x2029]inletsp_hd=List.hdspinletsp_tl=List.tlspinobject(self)valmutablewait=false(*whether the last char is CR*)valmutableout_buf=[]methodget()=matchout_bufwithu::rest->out_buf<-rest;u|[]->letu=inchan#get()inifwaitthenbeginwait<-false;matchUChar.uint_codeuwith0x000a->out_buf<-sp_tl;sp_hd|0x000d->wait<-true;out_buf<-sp_tl;sp_hd|0x0085->out_buf<-sp_tl@sp;sp_hd|_->out_buf<-sp_tl@[u];sp_hdendelsematchUChar.uint_codeuwith0x000d->wait<-true;self#get()|0x000a|0x0085->out_buf<-sp_tl;sp_hd|_->umethodclose_in()=out_buf<-[];inchan#close_in()endclassoutput(op:separator)(outchan:UChar.t#obj_output_channel)=letsp=matchopwith`CR->[UChar.chr_of_uint0x000d]|`LF->[UChar.chr_of_uint0x000a]|`CRLF->[UChar.chr_of_uint0x000d;UChar.chr_of_uint0x000a]|`NEL->[UChar.chr_of_uint0x0085]|`LS->[UChar.chr_of_uint0x2028]|`PS->[UChar.chr_of_uint0x2029]inobject(self)valmutablewait=falsemethodprivateoutput_newline=List.iteroutchan#putspmethodputu=ifwaitthenbeginwait<-false;matchUChar.uint_codeuwith0x000a->()|_->self#putuendelsematchUChar.uint_codeuwith0x000d->self#output_newline;wait<-true|0x000a|0x0085|0x2028|0x2029->self#output_newline|_->outchan#putumethodclose_out()=wait<-false;outchan#close_out()methodflush:unit->unit=outchan#flushendmoduletypeType=sigtypetextclassinput_line:UChar.t#obj_input_channel->[text]obj_input_channelclassoutput_line:?sp:[`CR|`CRLF|`LF|`LS|`NEL|`PS]->UChar.t#obj_output_channel->[text]obj_output_channelendmoduleMake(Text:UnicodeString.Type)=structtypetext=Text.tclassinput_lineinchan=objectvalb=Text.Buf.create0valmutablewait=falsemethodget()=Text.Buf.clearb;letrecloop()=letx=waitinwait<-false;matchUChar.uint_code(inchan#get())with0x0a->ifxthenloop()else()|0x0d->wait<-true|0x85|0x0c|0x2028|0x2029->()|n->Text.Buf.add_charb(UChar.chr_of_uintn);loop()intryloop();Text.Buf.contentsbwithEnd_of_file->ifText.length(Text.Buf.contentsb)>0thenText.Buf.contentsbelseraiseEnd_of_filemethodclose_in():unit=Text.Buf.resetb;inchan#close_in()endclassoutput_line?(sp:separator=`LF)outchan=letsp=matchspwith`CR->[UChar.chr_of_uint0x000d]|`LF->[UChar.chr_of_uint0x000a]|`CRLF->[UChar.chr_of_uint0x000d;UChar.chr_of_uint0x000a]|`NEL->[UChar.chr_of_uint0x0085]|`LS->[UChar.chr_of_uint0x2028]|`PS->[UChar.chr_of_uint0x2029]inobject(self)methodprivateoutput_newline=List.iteroutchan#putspmethodputt=Text.iteroutchan#putt;self#output_newlinemethodflush:unit->unit=outchan#flushmethodclose_out():unit=outchan#close_out()endend