1
0
Fork 0
sattools/qfits/src/fits_p.c

371 lines
8.7 KiB
C
Raw Normal View History

2022-08-06 20:55:07 -06:00
/*----------------------------------------------------------------------------*/
/**
@file fits_p.c
@author N. Devillard
@date Mar 2000
@version $Revision: 1.4 $
@brief FITS parser for a single card
This module contains various routines to help parsing a single FITS
card into its components: key, value, comment.
*/
/*----------------------------------------------------------------------------*/
/*
$Id: fits_p.c,v 1.4 2003/01/23 09:18:54 yjung Exp $
$Author: yjung $
$Date: 2003/01/23 09:18:54 $
$Revision: 1.4 $
*/
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "simple.h"
/*-----------------------------------------------------------------------------
Defines
-----------------------------------------------------------------------------*/
/* Define the following to get zillions of debug messages */
/* #define DEBUG_FITSHEADER */
/*-----------------------------------------------------------------------------
Function codes
-----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/**
@brief Find the keyword in a key card (80 chars)
@param line allocated 80-char line from a FITS header
@return statically allocated char *
Find out the part of a FITS line corresponding to the keyword.
Returns NULL in case of error. The returned pointer is statically
allocated in this function, so do not modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
2022-08-06 22:20:48 -06:00
char *
qfits_getkey (char *line)
2022-08-06 20:55:07 -06:00
{
2022-08-06 22:20:48 -06:00
static char key[81];
int i;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
if (line == NULL)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getkey: NULL input line\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* Special case: blank keyword */
if (!strncmp (line, " ", 8))
{
strcpy (key, " ");
return key;
}
/* Sort out special cases: HISTORY, COMMENT, END do not have = in line */
if (!strncmp (line, "HISTORY ", 8))
{
strcpy (key, "HISTORY");
return key;
}
if (!strncmp (line, "COMMENT ", 8))
{
strcpy (key, "COMMENT");
return key;
}
if (!strncmp (line, "END ", 4))
{
strcpy (key, "END");
return key;
}
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
memset (key, 0, 81);
/* General case: look for the first equal sign */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i >= 80)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getkey: cannot find equal sign\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
i--;
/* Equal sign found, now backtrack on blanks */
while (line[i] == ' ' && i >= 0)
i--;
if (i <= 0)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getkey: error backtracking on blanks\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
i++;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* Copy relevant characters into output buffer */
strncpy (key, line, i);
/* Null-terminate the string */
key[i + 1] = (char) 0;
return key;
2022-08-06 20:55:07 -06:00
}
/*----------------------------------------------------------------------------*/
/**
@brief Find the value in a key card (80 chars)
@param line allocated 80-char line from a FITS header
@return statically allocated char *
Find out the part of a FITS line corresponding to the value.
Returns NULL in case of error, or if no value can be found. The
returned pointer is statically allocated in this function, so do not
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
2022-08-06 22:20:48 -06:00
char *
qfits_getvalue (char *line)
2022-08-06 20:55:07 -06:00
{
2022-08-06 22:20:48 -06:00
static char value[81];
int i;
int from, to;
int inq;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
if (line == NULL)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: NULL input line\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* Special cases */
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* END has no associated value */
if (!strncmp (line, "END ", 4))
{
return NULL;
}
/*
* HISTORY has for value everything else on the line, stripping
* blanks before and after. Blank HISTORY is also accepted.
*/
memset (value, 0, 81);
if (!strncmp (line, "HISTORY ", 8) || !strncmp (line, " ", 8))
{
i = 7;
/* Strip blanks from the left side */
while (line[i] == ' ' && i < 80)
i++;
if (i >= 80)
return NULL; /* Blank HISTORY */
from = i;
/* Strip blanks from the right side */
to = 79;
while (line[to] == ' ')
to--;
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
}
else if (!strncmp (line, "COMMENT ", 8))
{
/* COMMENT is like HISTORY */
/* Strip blanks from the left side */
i = 7;
while (line[i] == ' ' && i < 80)
i++;
if (i >= 80)
return NULL;
from = i;
/* Strip blanks from the right side */
to = 79;
while (line[to] == ' ')
to--;
if (to < from)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: inconsistent value search in COMMENT\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
2022-08-06 20:55:07 -06:00
}
2022-08-06 22:20:48 -06:00
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
}
/* General case - Get past the keyword */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i > 80)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: no equal sign found on line\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
i++;
while (line[i] == ' ' && i < 80)
i++;
if (i > 80)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: no value past the equal sign\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
2022-08-06 20:55:07 -06:00
}
2022-08-06 22:20:48 -06:00
from = i;
/* Now value section: Look for the first slash '/' outside a string */
inq = 0;
while (i < 80)
{
if (line[i] == '\'')
inq = !inq;
if (line[i] == '/')
if (!inq)
break;
i++;
}
i--;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* Backtrack on blanks */
while (line[i] == ' ' && i >= 0)
i--;
if (i < 0)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: error backtracking on blanks\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
to = i;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
if (to < from)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getvalue: from>to?\n");
printf ("line=[%s]\n", line);
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
/* Copy relevant characters into output buffer */
strncpy (value, line + from, to - from + 1);
/* Null-terminate the string */
value[to - from + 1] = (char) 0;
return value;
2022-08-06 20:55:07 -06:00
}
/*----------------------------------------------------------------------------*/
/**
@brief Find the comment in a key card (80 chars)
@param line allocated 80-char line from a FITS header
@return statically allocated char *
Find out the part of a FITS line corresponding to the comment.
Returns NULL in case of error, or if no comment can be found. The
returned pointer is statically allocated in this function, so do not
modify or try to free it.
*/
/*----------------------------------------------------------------------------*/
2022-08-06 22:20:48 -06:00
char *
qfits_getcomment (char *line)
2022-08-06 20:55:07 -06:00
{
2022-08-06 22:20:48 -06:00
static char comment[81];
int i;
int from, to;
int inq;
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
if (line == NULL)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getcomment: null line in input\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
2022-08-06 20:55:07 -06:00
2022-08-06 22:20:48 -06:00
/* Special cases: END, HISTORY, COMMENT and blank have no comment */
if (!strncmp (line, "END ", 4))
return NULL;
if (!strncmp (line, "HISTORY ", 8))
return NULL;
if (!strncmp (line, "COMMENT ", 8))
return NULL;
if (!strncmp (line, " ", 8))
return NULL;
memset (comment, 0, 81);
/* Get past the keyword */
i = 0;
while (line[i] != '=' && i < 80)
i++;
if (i >= 80)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getcomment: no equal sign on line\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
i++;
/* Get past the value until the slash */
inq = 0;
while (i < 80)
{
if (line[i] == '\'')
inq = !inq;
if (line[i] == '/')
if (!inq)
break;
i++;
}
if (i >= 80)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getcomment: no slash found on line\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
i++;
/* Get past the first blanks */
while (line[i] == ' ')
i++;
from = i;
/* Now backtrack from the end of the line to the first non-blank char */
to = 79;
while (line[to] == ' ')
to--;
if (to < from)
{
2022-08-06 20:55:07 -06:00
#ifdef DEBUG_FITSHEADER
2022-08-06 22:20:48 -06:00
printf ("qfits_getcomment: from>to?\n");
2022-08-06 20:55:07 -06:00
#endif
2022-08-06 22:20:48 -06:00
return NULL;
}
/* Copy relevant characters into output buffer */
strncpy (comment, line + from, to - from + 1);
/* Null-terminate the string */
comment[to - from + 1] = (char) 0;
return comment;
2022-08-06 20:55:07 -06:00
}
/* vim: set ts=4 et sw=4 tw=75 */