Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get result into a string variable in OCaml

Tags:

ocaml

frama-c

I have this function working which prints out the value in an offsetmap:

let pretty_offsetmap_original lv fmt offsetmap =
  begin match offsetmap with
  | None ->  Format.fprintf fmt "<BOTTOM>"
  | Some off ->
    let typ = Some (typeOfLval lv)
    in
    Format.fprintf fmt "%a%a"
      pretty_lval_or_absolute lv
      (Cvalue.V_Offsetmap.pretty_typ typ) off
end

Now I would like to get the value in to a string variable to transform it for my purpose. I replaced Format.fprintf fmt by Printf.sprintf but it does not work. The compiling error:

Error: This expression has type
      Format.formatter -> Cvalue.V_Offsetmap.t -> unit
    but an expression was expected of type unit -> 'a -> string
like image 572
user2544482 Avatar asked Mar 24 '23 07:03

user2544482


2 Answers

Unfortunately, you are correct: Format.sprintf does not have the good type. Within Frama-C, the function Pretty_utils.sfprintf will do exactly what you need. You may also want to have a look at Pretty_utils.to_string.

like image 161
byako Avatar answered Mar 31 '23 11:03

byako


Seems like you'd need to replace Format.fprintf with Format.sprintf not with Printf.sprintf.

like image 42
Jeffrey Scofield Avatar answered Mar 31 '23 13:03

Jeffrey Scofield