123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147(* Formatters for resource leaves in the tree structure *)(* This file is part of ocp-ocamlres - subformats
* (C) 2013 OCamlPro - Benjamin CANOU
*
* ocp-ocamlres 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.0 of the License, or (at your option) any later
* version, with linking exception.
*
* ocp-ocamlres 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 LICENSE file for more details *)moduletypeSubFormat=sigtypetvalfrom_raw:OCamlRes.Path.t->string->tvalto_raw:OCamlRes.Path.t->t->stringvalpprint:OCamlRes.Path.t->t->PPrint.documentvalpprint_header:OCamlRes.Path.t->t->PPrint.documentoptionvalpprint_footer:OCamlRes.Path.t->t->PPrint.documentoptionvalname:OCamlRes.Path.t->t->stringvaltype_name:OCamlRes.Path.t->t->stringvalmod_name:OCamlRes.Path.t->t->stringendmoduleInt=structtypet=intletfrom_raw_str=Scanf.sscanfstr"%i"(funi->i)letto_raw_i=Printf.sprintf"%i"iletpprint_i=PPrint.OCaml.intiletpprint_header__=Noneletpprint_footer__=Noneletname__="int"lettype_name__="int"letmod_name__="OCamlResSubFormats.Int"endmoduleRaw=structtypet=stringletfrom_raw_raw_text=raw_textletto_raw_raw_text=raw_textletpprintpathdata=letopenPPrintinletlen=String.lengthdatainletlooks_like_text=letrecloopiacc=ifi=lenthenacc<=len/10(* allow 10% of escaped chars *)elseletc=Char.codedata.[i]inifc<32&&c<>10&&c<>13&&c<>9thenfalseelseifChar.codedata.[i]>=128thenloop(i+1)(acc+1)elseloop(i+1)accinloop00inlethexd=[|'0';'1';'2';'3';'4';'5';'6';'7';'8';'9';'A';'B';'C';'D';'E';'F'|]inifnotlooks_like_textthen(* (* less ugly, too costly *)
let rec blobs acc ofs w =
if ofs >= len then List.rev acc
else
let len = (min w (len - ofs)) in
let blob = String.create (len * 4) in
for i = 0 to len - 1 do
let c = Char.code data.[ofs + i] in
blob.[i * 4] <- '\\' ;
blob.[i * 4 + 1] <- 'x' ;
blob.[i * 4 + 2] <- (hexd.(c lsr 4)) ;
blob.[i * 4 + 3] <- (hexd.(c land 15)) ;
done ;
blobs (!^blob :: acc) (ofs + w) w
in
let blobs = blobs [] 0 20 in
group (!^"\"" ^^ align (separate (ifflat empty (!^"\\" ^^ hardline)) blobs) ^^ !^"\"")
*)group(!^"\""^^!^(String.escapeddata)^^!^"\"")elseletchunklasti=!^(String.subdatalast(i-last))inletrecloopacclasti=ifi=lenthenaccelsematchdata.[i],data.[min(i+1)(len-1)]with|'\r','\n'->loop(acc^^chunklasti^^!^"\\r")(i+1)(i+1)|'\r',' '->loop(acc^^chunklasti^^!^"\\r\\"^^hardline^^!^"\\")(i+1)(i+1)|'\r',_->loop(acc^^chunklasti^^!^"\\r\\"^^hardline^^!^" ")(i+1)(i+1)|'\n',' '->loop(acc^^chunklasti^^!^"\\n\\"^^hardline^^!^"\\")(i+1)(i+1)|'\n',_->loop(acc^^chunklasti^^!^"\\n\\"^^hardline^^!^" ")(i+1)(i+1)|'\t',_->loop(acc^^chunklasti^^!^"\\t")(i+1)(i+1)|'"',_->loop(acc^^chunklasti^^!^"\\\"")(i+1)(i+1)|'\\',_->loop(acc^^chunklasti^^!^"\\\\")(i+1)(i+1)|c,_whenChar.codec>=128||Char.codec<32->letc=Char.codecinlets=Bytes.create4inBytes.sets0'\\';Bytes.sets1'x';Bytes.sets2(hexd.(clsr4));Bytes.sets3(hexd.(cland15));lets=Bytes.unsafe_to_stringsinloop(acc^^chunklasti^^!^s)(i+1)(i+1)|c,_wheni=len-1->acc^^chunklast(i+1)|c,_->loopacclast(i+1)ingroup(align(!^"\""^^loopempty00^^!^"\""))letpprint_header__=Noneletpprint_footer__=Noneletname__="raw"lettype_name__="string"letmod_name__="OCamlResSubFormats.Raw"endmoduleLines=structtypet=stringlistletfrom_raw_str=Str.split(Str.regexp"[\r\n]")strletto_raw_lines=String.concat"\n"linesletpprintpathlns=letopenPPrintinletcontents=separate_map(!^" ;"^^break1)(Raw.pprintpath)lnsingroup(!^"[ "^^nest2contents^^!^" ]")letpprint_header__=Noneletpprint_footer__=Noneletname__="lines"lettype_name__="string list"letmod_name__="OCamlResSubFormats.Lines"end