How to use C++ htslib to read VCF contig name and size? A typical VCF file has:

##contig=&lt;ID=chr1,length=248956422&gt;
##contig=&lt;ID=chr2,length=242193529&gt;


I would like to use htslib in C++ to read it. My attempt:

htsFile *fp = bcf_open("my.vcf", "r");
bcf_hdr_t *hdr = bcf_hdr_read(fp); 


In https://github.com/samtools/htslib/blob/develop/htslib/vcf.h, I'm not able to find a function that can do that for me.

How to read chr1 and 248956422 in C++?
#include "htslib/vcf.h"

int main(int argc, char *argv[])
{
    htsFile *fp;
    bcf_hdr_t *hdr;
    bcf_idpair_t *ctg;
    int i;
    if (argc == 1) {
        fprintf(stderr, "Usage: print-ctg &lt;in.vcf&gt;\n");
        return 1;
    }
    fp = vcf_open(argv[1], "r");
    hdr = vcf_hdr_read(fp);
    ctg = hdr-&gt;id[BCF_DT_CTG];
    for (i = 0; i &lt; hdr-&gt;n[BCF_DT_CTG]; ++i)
        printf("%s\t%d\n", ctg[i].key, ctg[i].val-&gt;info[0]);
    bcf_hdr_destroy(hdr);
    vcf_close(fp);
    return 0;
}


On stability: this use has been in htslib forever. In general, functions/structs/variables in the public headers are meant to be stable. However, there is no guarantee that future versions will always keep the same APIs.