util.mli (5219B)
1 (* Unison file synchronizer: src/ubase/util.mli *) 2 (* Copyright 1999-2020, Benjamin C. Pierce (see COPYING for details) *) 3 4 (* Miscellaneous utility functions and datatypes *) 5 6 (* ---------------------------------------------------------------------- *) 7 (* Exceptions *) 8 9 exception Fatal of string 10 exception Transient of string 11 12 val encodeException : string -> [`Transient | `Fatal] -> exn -> 'a 13 val convertUnixErrorsToTransient : string -> (unit -> 'a) -> 'a 14 val convertUnixErrorsToFatal : string -> (unit -> 'a) -> 'a 15 val ignoreTransientErrors : (unit -> unit) -> unit 16 17 (* [unwindProtect e1 e2] executes e1, catching the above two exceptions and 18 executing e2 (passing it the exception packet, so that it can log a 19 message or whatever) before re-raising them *) 20 val unwindProtect : (unit -> 'a) -> (exn -> unit) -> 'a 21 22 (* [finalize e1 e2] executes e1 and then e2. If e1 raises either of the 23 above two exceptions e2 is still executed and the exception is reraised *) 24 val finalize : (unit -> 'a) -> (unit -> unit) -> 'a 25 26 (* For data structures that need to record when operations have succeeded or 27 failed *) 28 type confirmation = 29 Succeeded 30 | Failed of string 31 32 val printException : exn -> string 33 34 val process_status_to_string : Unix.process_status -> string 35 36 (* [blockSignals sigs f] blocks signals [sigs] (if supported by OS), 37 executes [f ()] and restores the original signal mask before returning 38 the result of executing [f ()] (value or exception). *) 39 val blockSignals : int list -> (unit -> 'a) -> 'a 40 41 (* ---------------------------------------------------------------------- *) 42 (* Strings *) 43 44 (* Case insensitive comparison *) 45 val nocase_cmp : string -> string -> int 46 val nocase_eq : string -> string -> bool 47 val lowercase_latin1 : char -> char 48 49 (* Ready-build set and map implementations *) 50 module StringSet : Set.S with type elt = string 51 module StringMap : Map.S with type key = string 52 val stringSetFromList : string list -> StringSet.t 53 54 (* String manipulation *) 55 val truncateString : string -> int (* number of Unicode code points *) -> string 56 val startswith : string -> string -> bool (* STR,PREFIX *) 57 val endswith : string -> string -> bool 58 val findsubstring : ?reverse:bool -> string -> string -> int option 59 val replacesubstring : string -> string -> string -> string (* IN,FROM,TO *) 60 val replacesubstrings : string -> (string * string) list -> string 61 val concatmap : string -> ('a -> string) -> 'a list -> string 62 val removeTrailingCR : string -> string 63 val trimWhitespace : string -> string 64 val splitAtChar : ?reverse:bool -> string -> char -> (string * string option) 65 val splitIntoWords : ?esc:char -> string -> char -> string list 66 (* Empty words are not returned; escaped whitespace is non splitting *) 67 val splitAtString : ?reverse:bool -> string -> string -> (string * string option) 68 val splitIntoWordsByString : string -> string -> string list (* IN,SEP *) 69 (* Invariant: [s = concat sep (splitIntoWords s sep)] *) 70 val padto : int -> string -> string 71 72 (* ---------------------------------------------------------------------- *) 73 (* Miscellaneous *) 74 75 (* Options *) 76 val extractValueFromOption : 'a option -> 'a 77 val option2string: ('a -> string) -> ('a option -> string) 78 79 (* Miscellaneous *) 80 val time2string : float -> string 81 val percentageOfTotal : 82 int -> (* current value *) 83 int -> (* total value *) 84 int (* percentage of total *) 85 val monthname : int -> string 86 val percent2string : float -> string 87 val bytes2string : int64 -> string 88 89 (* Just like the versions in the Unix module, but raising Transient 90 instead of Unix_error *) 91 val localtime : float -> Unix.tm 92 val time : unit -> float 93 94 (* Global debugging printer (it's exposed as a ref so that modules loaded 95 before Trace can use it; the ref will always be set to Some(Trace.debug)) *) 96 val debugPrinter : ((string -> (unit->unit) -> unit) option) ref 97 (* A synonym for Trace.debug *) 98 val debug : string -> (unit->unit) -> unit 99 100 (* The UI must supply a function to warn the user; a default calling Util.msg 101 is set up initially. *) 102 val warnPrinter : (string -> unit) option ref 103 val warn : string -> unit 104 105 (* Gives the path of the archive directory on the machine, depending on *) 106 (* which OS we use *) 107 val unisonDir : string 108 109 (* build a path representing an archive child path whose name is given *) 110 val fileInUnisonDir : string -> string 111 val fileMaybeRelToUnisonDir : string -> string 112 113 (* Printing and formatting functions *) 114 115 val format : ('a, Format.formatter, unit) format -> 'a 116 (** Format some text on the current formatting channel. 117 This is the only formatting function that should be called anywhere in the program! *) 118 119 val flush : unit -> unit 120 121 val format_to_string : (unit -> unit) -> string 122 (** [format_to_string f] runs [f] in a context where the Format functions are redirected to 123 a string, which it returns. *) 124 125 (* Format and print messages on the standard error stream, being careful to 126 flush the stream after each one *) 127 val msg : ('a, out_channel, unit) format -> 'a 128 129 (* Set the info line. 130 [~clr] is an alternative clear sequence to clear this info only. *) 131 val set_infos : ?clr:string -> string -> unit