crystal-lang / crystal_lib

Automatic binding generator for native libraries in Crystal
138 stars 30 forks source link

Bitfield support #76

Open kojix2 opened 3 years ago

kojix2 commented 3 years ago

Hello. Thanks for the great tool. However, the tool seems to fail to parse structs with bit fields. Please provide support for bit fields.

kojix2 commented 3 years ago
struct BGZF {
    // Reserved bits should be written as 0; read as "don't care"
    unsigned errcode:16, reserved:1, is_write:1, no_eof_block:1, is_be:1;
    signed compress_level:9;
    unsigned last_block_eof:1, is_compressed:1, is_gzip:1;
    int cache_size;
    int block_length, block_clength, block_offset;
    int64_t block_address, uncompressed_address;
    void *uncompressed_block, *compressed_block;
    bgzf_cache_t *cache;
    struct hFILE *fp; // actual file handle
    struct bgzf_mtaux_t *mt; // only used for multi-threading
    bgzidx_t *idx;      // BGZF index
    int idx_build_otf;  // build index on the fly, set by bgzf_index_build_init()
    struct z_stream_s *gz_stream; // for gzip-compressed files
    int64_t seeked;     // virtual offset of last seek
};

Actual code generated by crystal_lib:

  struct Bgzf
    errcode : LibC::UInt
    reserved : LibC::UInt
    is_write : LibC::UInt
    no_eof_block : LibC::UInt
    is_be : LibC::UInt
    compress_level : LibC::Int
    last_block_eof : LibC::UInt
    is_compressed : LibC::UInt
    is_gzip : LibC::UInt
    cache_size : LibC::Int
    block_length : LibC::Int
    block_clength : LibC::Int
    block_offset : LibC::Int
    block_address : Int64T
    uncompressed_address : Int64T
    uncompressed_block : Void*
    compressed_block : Void*
    cache : BgzfCacheT
    fp : HFile*
    mt : BgzfMtauxT*
    idx : BgzidxT
    idx_build_otf : LibC::Int
    gz_stream : ZStreamS*
    seeked : Int64T
  end

Bad but somewhat better code:

    struct Bgzf
      bitfields : Uint32T # Fixme
      # errcode : LibC::UInt
      # reserved : LibC::UInt
      # is_write : LibC::UInt
      # no_eof_block : LibC::UInt
      # is_be : LibC::UInt
      # compress_level : LibC::Int
      # last_block_eof : LibC::UInt
      # is_compressed : LibC::UInt
      # is_gzip : LibC::UInt
      cache_size : LibC::Int
      block_length : LibC::Int
      block_clength : LibC::Int
      block_offset : LibC::Int
      block_address : Int64T
      uncompressed_address : Int64T
      uncompressed_block : Void*
      compressed_block : Void*
      cache : BgzfCacheT
      fp : HFile*
      mt : BgzfMtauxT*
      idx : BgzidxT
      idx_build_otf : LibC::Int
      gz_stream : ZStreamS*
      seeked : Int64T
    end
kojix2 commented 3 years ago

It would be better if we could link bindings created with crystal_lib with this. https://github.com/elorest/bitfields