Logo Search packages:      
Sourcecode: qfits version File versions  Download package

char* qfits_query_card ( const char *  filename,
const char *  keyword 
)

Query a card in a FITS (main) header by a given key.

Parameters:
filename Name of the FITS file to check.
keyword Where to read a card in the header.
Returns:
Allocated string containing the card or NULL

Definition at line 491 of file qfits_tools.c.

References qfits_expand_keyword(), and qfits_get_hdrinfo().

{
    char    *   exp_key ;
    int         fd ;
    char    *   buf ;
    char    *   buf2 ;
    char    *   where ;
    int         hs ;
    char    *   card ;

    /* Bulletproof entries */
    if (filename==NULL || keyword==NULL) return NULL ;

    /* Expand keyword */
    exp_key = qfits_expand_keyword(keyword) ;

    /* Memory-map the FITS header of the input file  */
    qfits_get_hdrinfo(filename, 0, NULL, &hs) ;
    if (hs < 1) {
        qfits_error("error getting FITS header size for %s", filename);
        return NULL ;
    }
    fd = open(filename, O_RDWR) ;
    if (fd == -1) return NULL ;
    buf = (char*)mmap(0,
                      hs,
                      PROT_READ | PROT_WRITE,
                      MAP_SHARED,
                      fd,
                      0) ;
    if (buf == (char*)-1) {
        perror("mmap") ;
        close(fd) ;
        return NULL ;
    }

    /* Apply search for the input keyword */
    buf2 = qfits_malloc(hs+1) ;
    memcpy(buf2, buf, hs) ;
    buf2[hs] = (char)0 ;
    where = buf2 ;
    do {
        where = strstr(where, exp_key);
        if (where == NULL) {
            close(fd);
            munmap(buf,hs);
            qfits_free(buf2) ;
            return NULL ;
        }
        if ((where-buf2)%80) where++ ;
    } while ((where-buf2)%80) ;
       
    where = buf + (int)(where - buf2) ;
  
    /* Create the card */
    card = qfits_malloc(81*sizeof(char)) ;
    strncpy(card, where, 80) ;
    card[80] = (char)0 ;

    /* Free and return */
    close(fd) ;
    munmap(buf, hs) ;
    qfits_free(buf2) ;
    return card ;
}


Generated by  Doxygen 1.6.0   Back to index