Commits

Anonymous committed aa6dc5b

field declarations: ?dump_fmt for custom dumping formats

  • Participants
  • Parent commits dbce0db

Comments (0)

Files changed (4)

 
     let () = cstruct "mystruct"
       [ uint8_t "ui8"
-      ; int64_t "si64"
+      ; int64_t "si64" ~dump_fmt:"0x%LX"
       ; myenum  "en"
       ]
 
   Here "mystruct" is defined, module Mystruct is generated.
 Fields can have either numeric or enum type.
+  Optional argument ~dump_fmt is a Printf-style format string that specifies
+how the field value will be dumped with Mystruct.dump.  Omitting ~dump_fmt
+means that field value will be dumped in decimal notation.
 
   At last, run code generation:
 

lib/cstruct_codegen.ml

 *)
 
 
-type cfielddef = { fname : string ; ftype : ftype }
+type cfielddef = { fname : string ; ftype : ftype ; fdump_fmt : option string }
 ;
 
 type cstructdef = (string * list cfielddef)
         ]
     }
   in
-    fun n ->
+    fun ?dump_fmt n ->
       { fname = n
       ; ftype = fty
+      ; fdump_fmt = dump_fmt
       }
 ;
 
       in
       let ftype = Fenum fe
       in
-        fun fieldname -> { fname = fieldname ; ftype = ftype }
+        fun fieldname ->
+          { fname = fieldname ; ftype = ftype ; fdump_fmt = None }
     ;
 
   end
   ; sig_ "(* getters and setters: *)"
   ; str_ ""
 
-  ; let for_field { fname = fname_orig ; ftype = ftype_gen } =
+  ; let for_field { fname = fname_orig ; ftype = ftype_gen ; fdump_fmt = _ } =
       let fname = fname_orig in
       let gen_get_set ~ftype ~fname =
         let getter = "get_" ^ fname
      (fun i f ->
         ( str_ "    Buffer.add_string b %S;"
             ((if i = 0 then "{ " else " ; ") ^ f.fname ^ "=")
-        ; str_ "    Buffer.add_string b (%s);"
-            (match f.ftype with
-             [ Fint _ ->
-                 Printf.sprintf "%s (get_%s t)" (printer f.ftype) f.fname
-             | Fenum fe ->
-                 Printf.sprintf
-                   "try %s.to_string (get_%s_exn t) with Enum (_, v) -> v"
-                   (enum_module fe) f.fname
-             ]
-            )
+        ; match f.fdump_fmt with
+          [ None ->
+              str_ "    Buffer.add_string b (%s);"
+                (match f.ftype with
+                 [ Fint _ ->
+                     Printf.sprintf "%s (get_%s t)" (printer f.ftype) f.fname
+                 | Fenum fe ->
+                     Printf.sprintf
+                       "try %s.to_string (get_%s_exn t) with Enum (_, v) -> v"
+                       (enum_module fe) f.fname
+                 ]
+                )
+         | Some fmt ->
+             str_ "    Printf.bprintf b %S (get_%s t);"
+               fmt f.fname
+         ]
         )
      )
      csfields

lib/cstruct_codegen.mli

 (* how to define structure's fields: *)
 
 (* these are represented as [int]: *)
-value int8_t   : string -> cfielddef;
-value uint8_t  : string -> cfielddef;
-value int16_t  : string -> cfielddef;
-value uint16_t : string -> cfielddef;
-value int31_t  : string -> cfielddef;
+value int8_t   : ?dump_fmt:string -> string -> cfielddef;
+value uint8_t  : ?dump_fmt:string -> string -> cfielddef;
+value int16_t  : ?dump_fmt:string -> string -> cfielddef;
+value uint16_t : ?dump_fmt:string -> string -> cfielddef;
+value int31_t  : ?dump_fmt:string -> string -> cfielddef;
 
 (* .. as [int32]: *)
-value int32_t  : string -> cfielddef;
+value int32_t  : ?dump_fmt:string -> string -> cfielddef;
 
 (* .. as [int64]: *)
-value int64_t  : string -> cfielddef;
+value int64_t  : ?dump_fmt:string -> string -> cfielddef;
 
 
 (* how to define enumeration: *)
 
 let () = cstruct "mystruct"
   [ uint8_t  "ui8"
-  ; int64_t "si64"
+  ; int64_t "si64" ~dump_fmt:"0x%LX"
   ; myenum  "en"
   ]