[wg-camlp4] getting bitstring to work with Core

Anil Madhavapeddy anil at recoil.org
Mon May 27 11:47:27 BST 2013


Core currently hides two functions needed by the Bitstring syntax extension: Char.code and String.unsafe_get.  I'm not sure what the best way to work around this is. Possibilities are:

- Core.Std.Caml has the original namespace from OCaml, so add a camlp4 flag to pa_bitstring to locally open Caml if the flag is specified.

- Add Char.code and String.unsafe_get to Core -- but there will be more similar incompatibilities lurking.

- Do nothing, and wait for the more explicit extension_points to show up.

- The below hack to get it to compile. Ugh.

--
open Core.Std

module Char = struct
  include Char
  let code = to_int
end

module String = struct
  include String
  let unsafe_get = Caml.String.unsafe_get
end

let show_gif_info filename () =
  let bits = Bitstring.bitstring_of_file filename in

  bitmatch bits with
  | { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
      width : 16 : littleendian;
      height : 16 : littleendian;
      colormap : 1;                     (* Has colormap? *)
      colorbits : 3;                    (* Color res = colorbits+1 *)
      sortflag : 1;
      bps : 3;                          (* Bits/pixel = bps+1 *)
      bg : 8;                           (* Background colour. *)
      aspectratio : 8 } ->
      printf "%s: GIF image:\n" filename;
      printf "  size %d %d\n" width height;
      printf "  has global colormap? %b\n" colormap;
      printf "  colorbits %d\n" (colorbits+1);
      printf "  global colormap is sorted? %b\n" sortflag;
      printf "  bits/pixel %d\n" (bps+1);
      printf "  background color index %d\n" bg;
      printf "  aspect ratio %d\n" aspectratio

  | { _ } ->
      eprintf "%s: Not a GIF image\n" filename

let () =
  Command.basic ~summary:"Display information about GIF file"
    Command.Spec.(
      empty +>
      anon ("filename" %: file)
    )
    show_gif_info
  |> Command.run



More information about the wg-camlp4 mailing list