package frama-c

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file format_pprint.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  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, version 2.1.                                              *)
(*                                                                        *)
(*  It 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 Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

open Format_types

let string_of_flag = function
  | FMinus -> "-"
  | FPlus -> "+"
  | FSpace -> "' '"
  | FSharp -> "#"
  | FZero -> "0"

let string_of_flags fl =
  let rec aux accu fl = match fl with
    | f::fl -> aux (accu ^ string_of_flag f) fl
    | [] -> accu in
  aux "" fl

let pp_flag ff f = Format.fprintf ff "%s" (string_of_flag f)

let pp_flags ff fl = Pretty_utils.pp_list ~sep:", " pp_flag ff fl

let string_of_fw = function
  | `FWStar -> "*"
  | `FWInt i -> string_of_int i

let pp_fw ff fw = Format.fprintf ff "%s" (string_of_fw fw)

let string_of_precision = function
  | PStar -> "*"
  | PInt i -> string_of_int i

let pp_precision ff p = Format.fprintf ff ".%s" (string_of_precision p)

let string_of_lm = function
  | `hh -> "hh"
  | `h -> "h"
  | `l -> "l"
  | `ll -> "ll"
  | `j -> "j"
  | `z -> "z"
  | `t -> "t"
  | `L -> "L"

let pp_lm ff lm = Format.fprintf ff "%s" (string_of_lm lm)

let string_of_cs = function
  | `d -> "d"
  | `i -> "i"
  | `o -> "o"
  | `u -> "u"
  | `x -> "x"
  | `f -> "f"
  | `e -> "e"
  | `g -> "g"
  | `a -> "a"
  | `c -> "c"
  | `s -> "s"
  | `p -> "p"
  | `n -> "n"
  | `Brackets b -> "[" ^ b ^ "]"

let pp_cs ff (cs,capitalize) =
  let s = string_of_cs cs in
  let s = if capitalize then String.capitalize_ascii s else s in
  Format.fprintf ff "%s" s

let string_of_option ?pre:(pre="") ?suf:(suf="") f = function
  | Some o ->  pre ^ (f o) ^ suf
  | None -> ""

let pp_f_specification ff spec =
  let suf = "; " in
  Format.fprintf ff "<";

  if List.length spec.f_flags <> 0 then
    (Format.fprintf ff "Flags: "; pp_flags ff spec.f_flags;
     Format.fprintf ff "%s" suf);

  Format.fprintf ff "%s%s%s"
    (string_of_option ~pre:"Field width: " ~suf:suf
       string_of_fw spec.f_field_width)
    (string_of_option ~pre:"Precision: " ~suf:suf
       string_of_precision spec.f_precision)
    (string_of_option ~pre:"Length modifier: " ~suf:suf
       string_of_lm spec.f_length_modifier);

  Format.fprintf ff "Conversion specifier: %s>"
    (string_of_cs spec.f_conversion_specifier)

let pp_s_specification ff (spec: s_conversion_specification) =
  let suf = "; " in
  Format.fprintf ff "<%s; %s%s"
    ("Assignment: " ^ (string_of_bool (not spec.s_assignment_suppression)))
    (string_of_option ~pre:"Field width: " ~suf:suf
       string_of_fw spec.s_field_width)
    (string_of_option ~pre:"Length modifier: " ~suf:suf
       string_of_lm spec.s_length_modifier);
  Format.fprintf ff "Conversion specifier: %s>"
    (string_of_cs spec.s_conversion_specifier)

let pp_f_format ff fl =
  let fl = List.filter_map
      (function Specification s -> Some s | _ -> None) fl in
  Pretty_utils.pp_list ~sep:"@." (fun ff s -> pp_f_specification ff s) ff fl

let pp_s_format ff (fl: s_format) =
  let fl = List.filter_map
      (function | Specification s -> Some s | _ -> None) fl in
  Pretty_utils.pp_list ~sep:"@." (fun ff s -> pp_s_specification ff s) ff fl

let pp_format ff = function
  | FFormat s -> pp_f_format ff s
  | SFormat s -> pp_s_format ff s

let rec f_format_to_cstring fl =
  let aux spec =
    "%"
    ^ (string_of_flags spec.f_flags)
    ^ (string_of_option string_of_fw spec.f_field_width)
    ^ (string_of_option ~pre: "." string_of_precision spec.f_precision)
    ^ (string_of_option string_of_lm spec.f_length_modifier)
    ^ (string_of_cs spec.f_conversion_specifier) in
  match fl with
  | [] -> ""
  | Char '%' :: fl -> "%%" ^ f_format_to_cstring fl
  | Char c :: fl -> (String.make 1 c) ^ f_format_to_cstring fl
  | Specification s :: fl -> (aux s) ^ f_format_to_cstring fl

let rec s_format_to_cstring fl =
  let aux spec =
    "%"
    ^ (if spec.s_assignment_suppression then "*" else "")
    ^ (string_of_option string_of_fw spec.s_field_width)
    ^ (string_of_option string_of_lm spec.s_length_modifier)
    ^ (string_of_cs spec.s_conversion_specifier) in
  match fl with
  | [] -> ""
  | Char '%' :: fl -> "%%" ^ s_format_to_cstring fl
  | Char c :: fl -> (String.make 1 c) ^ s_format_to_cstring fl
  | Specification s :: fl -> (aux s) ^ s_format_to_cstring fl

let format_to_cstring = function
  | FFormat s -> f_format_to_cstring s
  | SFormat s -> s_format_to_cstring s
OCaml

Innovation. Community. Security.