123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(**************************************************************************************)(* Copyright (C) 2009 Pietro Abate <pietro.abate@pps.jussieu.fr> *)(* Copyright (C) 2009 Mancoosi Project *)(* *)(* 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 3 of the *)(* License, or (at your option) any later version. A special linking *)(* exception to the GNU Lesser General Public License applies to this *)(* library, see the COPYING file for more information. *)(**************************************************************************************)openExtLibincludeUtil.Logging(structletlabel="dose_common.input"end)letgzip_open_filefile=letch=Gzip.open_infileinletinput_charch=tryGzip.input_charchwithEnd_of_file->raiseIO.No_more_inputinletreadch=tryGzip.inputchwithEnd_of_file->raiseIO.No_more_inputinIO.create_in~read:(fun()->input_charch)~input:(readch)~close:(fun()->Gzip.close_inch)letxz_open_filefile=letch=Unix.open_process_in("xzcat "^file)inletreadch=tryinputchwithEnd_of_file->raiseIO.No_more_inputinIO.create_in~read:(fun()->input_charch)~input:(readch)~close:(fun()->close_inch)letbzip_open_filefile=(* workaround to avoid segfault :
* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=602170 *)let_=Bz2.versioninlets=Bytes.create1inletch=Bz2.open_in(open_infile)inletinput_charch=tryignore(Bz2.readchs01);Bytes.gets0withEnd_of_file->raiseIO.No_more_inputinletreadchsposlen=tryBz2.readchsposlenwithEnd_of_file->raiseIO.No_more_inputinIO.create_in~read:(fun()->input_charch)~input:(funx->readchx)~close:(fun()->Bz2.close_inch)letstd_open_filefile=IO.input_channel(open_infile)letopen_chch=IO.input_channelchletclose_chch=IO.close_inchexceptionFile_emptyletopen_filefile=ifnot(Sys.file_existsfile)thenfatal"Input file %s does not exist."fileelseif(Unix.statfile).Unix.st_size=0then(warning"Input file %s is empty"file;raiseFile_empty)elseletopenfun=tryletch=open_infileinletopenfun=matchinput_bytechwith(* gzip magic is 0x1f 0x8b *)|0x1f->(matchinput_bytechwith|0x8b->gzip_open_file|_->std_open_file)(* bz2 magic is "BZh" *)|0x42->(matchinput_bytechwith|0x5a->(matchinput_bytechwith|0x68->bzip_open_file|_->std_open_file)|_->std_open_file)(* xz magic is 0xfd "7zXZ" *)|0xfd->(matchinput_bytechwith|0x37->(matchinput_bytechwith|0x7a->(matchinput_bytechwith|0x58->(matchinput_bytechwith|0x5a->xz_open_file|_->std_open_file)|_->std_open_file)|_->std_open_file)|_->std_open_file)|_->std_open_fileinclose_inch;openfunwithEnd_of_file->std_open_fileinopenfunfileletparse_uris=leturl=Url.of_stringsinletpath=url.Url.pathin(url.Url.scheme,(None,None,None,None,path),None)letguess_formaturilist=matchList.flattenurilistwith|uri::_->let(p,_,_)=parse_uriuriinp|_->fatal"Impossible to guess input format"