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

void qfits_card_build ( char *  line,
const char *  key,
const char *  val,
const char *  com 
)

Write out a card to a string on 80 chars.

Parameters:
line Allocated output character buffer.
key Key to write.
val Value to write.
com Comment to write.
Returns:
void
Write out a key, value and comment into an allocated character buffer. The buffer must be at least 80 chars to receive the information. Formatting is done according to FITS standard.

Definition at line 84 of file qfits_card.c.

References qfits_is_boolean(), qfits_is_complex(), qfits_is_float(), qfits_is_int(), and qfits_pretty_string().

{
    int     len ;
    int     hierarch = 0 ;
    char    cval[81];
    char    cval2[81];
    char    cval_q[81];
    char    ccom[81];
    char    safe_line[512];
    int     i, j ;

    if (line==NULL || key==NULL) return ;

    /* Set the line with zeroes */
    memset(line, ' ', 80);
    if (key==NULL) return ;

    /* END keyword*/
    if (!strcmp(key, "END")) {
        /* Write key and return */
        sprintf(line, "END") ;
        return ;
    }
    /* HISTORY, COMMENT and blank keywords */
    if (!strcmp(key, "HISTORY") ||
        !strcmp(key, "COMMENT") ||
        !strncmp(key, "        ", 8)) {
        /* Write key */
        sprintf(line, "%s ", key);
        if (val==NULL) return ;

        /* There is a value to write, copy it correctly */
        len = strlen(val);
        /* 72 is 80 (FITS line size) - 8 (sizeof COMMENT or HISTORY) */
        if (len>72) len=72 ;
        strncpy(line+8, val, len);
        return ;
    }

    /* Check for NULL values */
    if (val==NULL) cval[0]=(char)0;
    else if (strlen(val)<1) cval[0]=(char)0;
    else strcpy(cval, val);

    /* Check for NULL comments */
    if (com==NULL) strcpy(ccom, "no comment");
    else strcpy(ccom, com);

    /* Set hierarch flag */
    if (!strncmp(key, "HIERARCH", 8)) hierarch ++ ;

    /* Boolean, int, float or complex */
    if (qfits_is_int(cval) ||
            qfits_is_float(cval) ||
            qfits_is_boolean(cval) ||
            qfits_is_complex(cval)) {
        if (hierarch) sprintf(safe_line, "%-29s= %s / %s", key, cval, ccom);
        else sprintf(safe_line, "%-8.8s= %20s / %-48s", key, cval, ccom);
        strncpy(line, safe_line, 80);
        line[80]=(char)0;
        return ;
    }

    /* Blank or NULL values */
    if (cval[0]==0) {
        if (hierarch) {
            sprintf(safe_line, "%-29s=                    / %s", key, ccom);
        } else {
        sprintf(safe_line, "%-8.8s=                      / %-48s", key, ccom);
        }
        strncpy(line, safe_line, 80);
        line[80]=(char)0;
        return ;
    }

    /* Can only be a string - Make simple quotes ['] as double [''] */
    memset(cval_q, 0, 81);
    strcpy(cval2, qfits_pretty_string(cval));
    j=0 ;
    i=0 ;
    while (cval2[i] != (char)0) {
        if (cval2[i]=='\'') {
            cval_q[j]='\'';
            j++ ;
            cval_q[j]='\'';
        } else {
            cval_q[j] = cval2[i];
        }
        i++ ;
        j++ ;
    }

    if (hierarch) {
        sprintf(safe_line, "%-29s= '%s' / %s", key, cval_q, ccom);
        if (strlen(key) + strlen(cval_q) + 3 >= 80)
            safe_line[79] = '\'';
    } else {
        sprintf(safe_line, "%-8.8s= '%-8s' / %s", key, cval_q, ccom);
    }
    strncpy(line, safe_line, 80);

    /* Null-terminate in any case */
    line[80]=(char)0;
    return ;
}


Generated by  Doxygen 1.6.0   Back to index