123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110(******************************************************************************)(* Metadb *)(* Copyright (C) 2022 Nathan Guermond *)(* *)(* 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, either version 3 of the License, or (at your option) *)(* any later version. *)(* *)(* 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, see <https://www.gnu.org/licenses/>. *)(* *)(******************************************************************************)exceptionInvalidRootTypeofstringexceptionInvalidRelTypeofstringexceptionInvalidNameTypeofstringtyperoot=stringtyperel=stringtypename=string(* TODO: Look at the Fpath library & Fileutils *)letdebug=reftrueletset_debug(d:bool):unit=debug:=d(* TODO: Maybe we should distinguish between files and directories? *)letroot_type=(Str.regexp"\\(/\\|\\(/[^/]+\\)+\\)$")letrel_type=(Str.regexp"[^/]+\\(/[^/]+\\)*$")letname_type=(Str.regexp"[^/]+$")letleaf_type=(Str.regexp"/[^/]+$")letmk_root(root:string):root=if((not!debug)||Str.string_matchroot_typeroot0)thenrootelseraise(InvalidRootTyperoot)letmk_rel(rel:string):rel=if((not!debug)||Str.string_matchrel_typerel0)thenrelelseraise(InvalidRelTyperel)letmk_name(name:string):name=if((not!debug)||Str.string_matchname_typename0)thennameelseraise(InvalidNameTypename)letmk_path(path:stringlist):namelist=if!debugthen(List.mapmk_namepath)elsepathletunroot(root:root):root*rel=(mk_root"/",(Str.replace_first(Str.regexp"/")""root))letstring_of_root(root:root):string=rootletstring_of_rel(rel:rel):string=relletstring_of_name(name:name):string=nameletmerge_lst(root:root)(path:namelist):root=String.concat"/"(root::path)letmerge(root:root)(path:rel):root=merge_lstroot[path]letsplit(path:rel):namelist=letnames=String.split_on_char'/'pathinif!debugthen(List.mapmk_namenames)elsenamesletadd_file_ext(ext:string)(root:root):root=(root^"."^ext)letremove_file_ext_rel(ext:string)(path:rel):rel=(Str.replace_first(Str.regexp((Str.quote("."^ext))^"$"))""path)letremove_file_ext(ext:string)(root:root):root=remove_file_ext_relextrootletstrip_root(root:root)(path:root):rel=ifroot="/"thensnd(unrootpath)else(Str.replace_first(Str.regexp((Str.quoteroot)^"/"))""path)letdrop_leaf(root:root):root=(Str.replace_firstleaf_type""root)letget_leaf_rel(path:rel):name=letnames=(splitpath)inList.nthnames((List.lengthnames)-1)letget_leaf(root:root):name=letpath=snd(unrootroot)inget_leaf_relpathlethidden(path:root):bool=letleaf=get_leaf(strip_root"/"path)inleaf.[0]='.'letpp_rootppf(path:root)=Format.fprintfppf"%s"(string_of_rootpath)letpp_relppf(path:rel)=Format.fprintfppf"%s"(string_of_relpath)