Method: CHD#read_hunk

Defined in:
ext/chd.c

#read_hunk(idx) ⇒ String

Read a CHD hunk.

Parameters:

  • idx (Integer)

    hunk index (start at 0)

Returns:

  • (String)

Raises:

  • (RangeError)

    if the requested hunk doesn’t exists



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'ext/chd.c', line 729

static VALUE
chd_m_read_hunk(VALUE self, VALUE idx) {
    // Retrieve typed data
    struct chd_rb_data *chd;
    chd_rb_get_typeddata(chd, self);
    chd_rb_ensure_initialized(chd);
    chd_rb_ensure_opened(chd);

    uint32_t hunkidx = VALUE_TO_UINT32(idx);
    if ((hunkidx < 0) || (hunkidx >= chd->header->totalhunks)) {
  rb_raise(rb_eRangeError, "hunk index (%d) is out of range (%d..%d)",
     hunkidx, 0, chd->header->totalhunks - 1);
    }

    VALUE strdata = rb_str_buf_new(chd->header->hunkbytes);
    char *buffer  = RSTRING_PTR(strdata);
    
    chd_error err = chd_read(chd->file, hunkidx, buffer);
    chd_rb_raise_if_error(err);

    rb_str_set_len(strdata, chd->header->hunkbytes);
    return strdata;
}