Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file misc.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2015 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program 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 General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)(*c==v=[String.strip_string]=1.0====*)letstrip_strings=letlen=String.lengthsinletreciter_firstn=ifn>=lenthenNoneelsematchs.[n]with' '|'\t'|'\n'|'\r'->iter_first(n+1)|_->Someninmatchiter_first0withNone->""|Somefirst->letreciter_lastn=ifn<=firstthenNoneelsematchs.[n]with' '|'\t'|'\n'|'\r'->iter_last(n-1)|_->Someninmatchiter_last(len-1)withNone->String.subsfirst1|Somelast->String.subsfirst((last-first)+1)(*/c==v=[String.strip_string]=1.0====*)letstrip_blank_linesstr=letblank_line=function'\n'|'\r'->true|_->falseinletprefix,suffix=ref0,ref0inletlen=String.lengthstrinwhile!prefix<len&&blank_linestr.[!prefix]doincrprefixdone;while!suffix>0&&blank_linestr.[!prefix]dodecrsuffixdone;String.substr!prefix(len-!suffix-!prefix);;(*c==v=[File.string_of_file]=1.1====*)letstring_of_filename=letchanin=open_in_binnameinletlen=1024inlets=Bytes.createleninletbuf=Buffer.createleninletreciter()=tryletn=inputchanins0leninifn=0then()else(Buffer.add_subbytesbufs0n;iter())withEnd_of_file->()initer();close_inchanin;Buffer.contentsbuf(*/c==v=[File.string_of_file]=1.1====*)(*c==v=[File.file_of_string]=1.1====*)letfile_of_string~files=letoc=open_outfileinoutput_stringocs;close_outoc(*/c==v=[File.file_of_string]=1.1====*)(*c==v=[String.split_string]=1.2====*)letsplit_string?(keep_empty=false)schars=letlen=String.lengthsinletreciteraccpos=ifpos>=lenthenmatchaccwith""->ifkeep_emptythen[""]else[]|_->[acc]elseifList.mems.[pos]charsthenmatchaccwith""->ifkeep_emptythen""::iter""(pos+1)elseiter""(pos+1)|_->acc::(iter""(pos+1))elseiter(Printf.sprintf"%s%c"accs.[pos])(pos+1)initer""0(*/c==v=[String.split_string]=1.2====*)(*c==v=[String.lowercase]=1.0====*)letlowercases=letlen=String.lengthsinletb=Buffer.createleninfori=0tolen-1doletc=matchs.[i]with|'à'|'â'|'ä'->'a'|'é'|'è'|'ê'|'ë'->'e'|'î'|'ï'->'i'|'ô'|'ö'->'o'|'ù'|'û'|'ü'->'u'|'ç'->'c'|c->Char.lowercase_asciicinBuffer.add_charbcdone;Buffer.contentsb(*/c==v=[String.lowercase]=1.0====*)(*c==v=[List.list_chop]=1.0====*)letreclist_chopn=function[]->[]|h::q->ifn>0thenh::(list_chop(n-1)q)else[](*/c==v=[List.list_chop]=1.0====*)letmkdirdir=tryUnix.mkdirdir0o755with|Unix.Unix_error(Unix.EEXIST,_,_)->()|Unix.Unix_error(e,s1,s2)->failwith(Printf.sprintf"%s: %s %s"(Unix.error_messagee)s1s2);;(*c==v=[String.is_prefix]=1.0====*)letis_prefixs1s2=letlen1=String.lengths1inletlen2=String.lengths2in(len1<=len2)&&(String.subs20len1)=s1(*/c==v=[String.is_prefix]=1.0====*)(*c==v=[List.list_remove_doubles]=1.0====*)letlist_remove_doubles?(pred=(=))l=List.fold_left(funacce->ifList.exists(prede)accthenaccelsee::acc)[](List.revl)(*/c==v=[List.list_remove_doubles]=1.0====*)letmd5s=Digest.to_hex(Digest.strings);;letcount_charsc=letr=ref0infori=0toString.lengths-1doifs.[i]=cthenincrrdone;!r;;letencode_charc=Printf.sprintf"&#%03d;"(Char.codec);;letencode_strings=letlen=String.lengthsinletb=Buffer.create(6*len)infori=0tolen-1doBuffer.add_stringb(encode_chars.[i])done;Buffer.contentsb;;letmap_optf=functionNone->None|Somex->Some(fx);;letlist_concat?sepl=matchsepwithNone->l|Somesep->letreciteracc=function[]->List.revacc|h::q->matchaccwith[]->iter[h]q|acc->iter(h::sep::acc)qiniter[]l;;letdot_to_svgdot=lettemp_file=Filename.temp_file"stogdot2svg""svg"inletcom=Printf.sprintf"echo %s | dot -Tsvg | tail -n +7 > %s"(Filename.quotedot)(Filename.quotetemp_file)inmatchSys.commandcomwith0->letsvg=string_of_filetemp_fileinSys.removetemp_file;svg|n->letmsg=Printf.sprintf"Execution failed (%d): %s"ncominfailwithmsg;;letreclist_comparecompl1l2=matchl1,l2with[],[]->0|[],_->-1|_,[]->1|h1::q1,h2::q2->matchcomph1h2with0->list_comparecompq1q2|n->n;;letfilename_extensions=letlen=String.lengthsintryletp=String.rindexs'.'inifp<len-1thenString.subs(p+1)(len-p-1)else""withNot_found->"";;letsafe_mkdirdir=letcom=Printf.sprintf"mkdir -p %s"(Filename.quotedir)inmatchSys.commandcomwith0->()|n->letmsg=Printf.sprintf"Execution failed (%d): %s"ncominfailwithmsg;;(*c==v=[String.string_of_opt]=1.0====*)letstring_of_opt=functionNone->""|Somes->s(*/c==v=[String.string_of_opt]=1.0====*)(*c==v=[String.opt_of_string]=1.0====*)letopt_of_string=function""->None|s->Somes(*/c==v=[String.opt_of_string]=1.0====*)letfile_mtimefile=trySome(Unix.statfile).Unix.st_mtimewith_->None;;letpath_under~parentfile=ifis_prefixparentfilethenbeginletlen=String.lengthparent+1inString.subfilelen(String.lengthfile-len)endelsefailwith(Printf.sprintf"%s is not under %s"fileparent);;letstring_of_timet=letd=Unix.gmtimetinPrintf.sprintf"%04d/%02d/%02d-%02d:%02d:%02d"(d.Unix.tm_year+1900)(d.Unix.tm_mon+1)d.Unix.tm_mdayd.Unix.tm_hourd.Unix.tm_mind.Unix.tm_sec;;