diff --git a/qfits/include/qfits.h b/qfits/include/qfits.h index c1c0224..7106a83 100644 --- a/qfits/include/qfits.h +++ b/qfits/include/qfits.h @@ -7,7 +7,7 @@ #include #include -extern char * qfits_version(void); +extern char *qfits_version (void); /*----------------------------------------------------------------------------*/ @@ -21,7 +21,7 @@ extern char * qfits_version(void); fairly small, you should not need to care too much about this. */ /*----------------------------------------------------------------------------*/ -void qfits_cache_purge(void); +void qfits_cache_purge (void); /*----------------------------------------------------------------------------*/ /** @brief Expand a keyword from shortFITS to HIERARCH notation. @@ -42,7 +42,7 @@ void qfits_cache_purge(void); not dots '.') the result is identical to the input. */ /*----------------------------------------------------------------------------*/ -char * qfits_expand_keyword(const char * keyword); +char *qfits_expand_keyword (const char *keyword); /*----------------------------------------------------------------------------*/ /** @brief FITS header object @@ -53,11 +53,12 @@ char * qfits_expand_keyword(const char * keyword); accessor functions. */ /*----------------------------------------------------------------------------*/ -typedef struct qfits_header { - void * first ; /* Pointer to list start */ - void * last ; /* Pointer to list end */ - int n ; /* Number of cards in list */ -} qfits_header ; +typedef struct qfits_header +{ + void *first; /* Pointer to list start */ + void *last; /* Pointer to list end */ + int n; /* Number of cards in list */ +} qfits_header; /*----------------------------------------------------------------------------- Function ANSI prototypes @@ -72,7 +73,7 @@ typedef struct qfits_header { an allocated linked-list handler with an empty card list. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_new(void) ; +qfits_header *qfits_header_new (void); /*----------------------------------------------------------------------------*/ /** @@ -84,7 +85,7 @@ qfits_header * qfits_header_new(void) ; (SIMPLE=T) and the last one (END). */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_default(void) ; +qfits_header *qfits_header_default (void); /*----------------------------------------------------------------------------*/ /** @@ -102,12 +103,8 @@ qfits_header * qfits_header_default(void) ; are allowed to get NULL values. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add( - qfits_header * hdr, - char * key, - char * val, - char * com, - char * lin) ; +void qfits_header_add (qfits_header * hdr, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -125,13 +122,9 @@ void qfits_header_add( can be NULL, except after and key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add_after( - qfits_header * hdr, - char * after, - char * key, - char * val, - char * com, - char * lin) ; +void qfits_header_add_after (qfits_header * hdr, + char *after, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -147,12 +140,9 @@ void qfits_header_add_after( NULL except key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_append( - qfits_header * hdr, - const char * key, - const char * val, - const char * com, - const char * lin) ; +void qfits_header_append (qfits_header * hdr, + const char *key, + const char *val, const char *com, const char *lin); /*----------------------------------------------------------------------------*/ /** @@ -165,7 +155,7 @@ void qfits_header_append( the key is removed. */ /*----------------------------------------------------------------------------*/ -void qfits_header_del(qfits_header * hdr, char * key) ; +void qfits_header_del (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -181,7 +171,7 @@ void qfits_header_del(qfits_header * hdr, char * key) ; line is set to NULL in the card. */ /*----------------------------------------------------------------------------*/ -void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ; +void qfits_header_mod (qfits_header * hdr, char *key, char *val, char *com); /*----------------------------------------------------------------------------*/ /** @@ -190,7 +180,7 @@ void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ; @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_header_sort(qfits_header ** hdr) ; +int qfits_header_sort (qfits_header ** hdr); /*----------------------------------------------------------------------------*/ /** @@ -202,7 +192,7 @@ int qfits_header_sort(qfits_header ** hdr) ; header. The returned header must be freed using qfits_header_destroy. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_copy(qfits_header * src) ; +qfits_header *qfits_header_copy (qfits_header * src); /*----------------------------------------------------------------------------*/ /** @@ -214,7 +204,7 @@ qfits_header * qfits_header_copy(qfits_header * src) ; freed and set to NULL. Useful when a header needs to be reformatted. */ /*----------------------------------------------------------------------------*/ -void qfits_header_touchall(qfits_header * hdr) ; +void qfits_header_touchall (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -225,7 +215,7 @@ void qfits_header_touchall(qfits_header * hdr) ; Dump a FITS header to stdout. Mostly for debugging purposes. */ /*----------------------------------------------------------------------------*/ -void qfits_header_consoledump(qfits_header * hdr) ; +void qfits_header_consoledump (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -236,7 +226,7 @@ void qfits_header_consoledump(qfits_header * hdr) ; Frees all memory associated to a given qfits_header object. */ /*----------------------------------------------------------------------------*/ -void qfits_header_destroy(qfits_header * hdr) ; +void qfits_header_destroy (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -252,7 +242,7 @@ void qfits_header_destroy(qfits_header * hdr) ; Returns NULL if no matching key is found or no value is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getstr(qfits_header * hdr, const char * key) ; +char *qfits_header_getstr (qfits_header * hdr, const char *key); /*----------------------------------------------------------------------------*/ /** @@ -276,7 +266,7 @@ char * qfits_header_getstr(qfits_header * hdr, const char * key) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_header_findmatch(qfits_header * hdr, char * key) ; +char *qfits_header_findmatch (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -315,13 +305,9 @@ char * qfits_header_findmatch(qfits_header * hdr, char * key) ; available in this module. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getitem( - qfits_header * hdr, - int idx, - char * key, - char * val, - char * com, - char * lin) ; +int qfits_header_getitem (qfits_header * hdr, + int idx, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -337,7 +323,7 @@ int qfits_header_getitem( Returns NULL if no matching key is found or no line is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getline(qfits_header * hdr, char * key) ; +char *qfits_header_getline (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -354,7 +340,7 @@ char * qfits_header_getline(qfits_header * hdr, char * key) ; Returns NULL if no matching key is found or no comment is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getcom(qfits_header * hdr, char * key) ; +char *qfits_header_getcom (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -369,7 +355,7 @@ char * qfits_header_getcom(qfits_header * hdr, char * key) ; attached. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ; +int qfits_header_getint (qfits_header * hdr, const char *key, int errval); /*----------------------------------------------------------------------------*/ /** @@ -384,7 +370,8 @@ int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ; attached. */ /*----------------------------------------------------------------------------*/ -double qfits_header_getdouble(qfits_header * hdr, const char * key, double errval) ; +double qfits_header_getdouble (qfits_header * hdr, const char *key, + double errval); /*----------------------------------------------------------------------------*/ /** @@ -408,7 +395,7 @@ double qfits_header_getdouble(qfits_header * hdr, const char * key, double errva beginning with a 'n' (no), a 'f' (false) or the digit '0'. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ; +int qfits_header_getboolean (qfits_header * hdr, const char *key, int errval); /*----------------------------------------------------------------------------*/ /** @@ -424,7 +411,7 @@ int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ; Formatting is done according to FITS standard. */ /*----------------------------------------------------------------------------*/ -void keytuple2str(char * line, char * key, char * val, char * com) ; +void keytuple2str (char *line, char *key, char *val, char *com); /*----------------------------------------------------------------------------*/ /** @@ -436,7 +423,7 @@ void keytuple2str(char * line, char * key, char * val, char * com) ; Dumps a FITS header to an opened file pointer. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump(qfits_header * hdr, FILE * out) ; +int qfits_header_dump (qfits_header * hdr, FILE * out); /*----------------------------------------------------------------------------*/ /** @@ -449,7 +436,7 @@ int qfits_header_dump(qfits_header * hdr, FILE * out) ; This function is meant to create hdr files. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ; +int qfits_header_dump_hdr (qfits_header * hdr, FILE * out); /*----------------------------------------------------------------------------*/ /** @@ -467,7 +454,7 @@ int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ; 'hsize'. The returned block must be deallocated using free(). */ /*----------------------------------------------------------------------------*/ -char * qfits_header_to_memblock(qfits_header * fh, int * hsize) ; +char *qfits_header_to_memblock (qfits_header * fh, int *hsize); /*----------------------------------------------------------------------------*/ /** @brief Compute the MD5 hash of data zones in a FITS file. @@ -484,7 +471,7 @@ char * qfits_header_to_memblock(qfits_header * fh, int * hsize) ; of error. */ /*----------------------------------------------------------------------------*/ -char * qfits_datamd5(char * filename); +char *qfits_datamd5 (char *filename); /*----------------------------------------------------------------------------- Function ANSI C prototypes -----------------------------------------------------------------------------*/ @@ -500,7 +487,7 @@ char * qfits_datamd5(char * filename); allocated in this function, so do not modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getkey(char * line) ; +char *qfits_getkey (char *line); /*----------------------------------------------------------------------------*/ /** @@ -514,7 +501,7 @@ char * qfits_getkey(char * line) ; modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getvalue(char * line) ; +char *qfits_getvalue (char *line); /*----------------------------------------------------------------------------*/ /** @@ -528,7 +515,7 @@ char * qfits_getvalue(char * line) ; modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getcomment(char * line) ; +char *qfits_getcomment (char *line); /*----------------------------------------------------------------------------*/ /** @brief Read a FITS header from a file to an internal structure. @@ -550,7 +537,7 @@ char * qfits_getcomment(char * line) ; Value, comment, and original line might be NULL pointers. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read(char * filename) ; +qfits_header *qfits_header_read (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -564,7 +551,7 @@ qfits_header * qfits_header_read(char * filename) ; a hdr file. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr(char * filename) ; +qfits_header *qfits_header_read_hdr (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -577,9 +564,8 @@ qfits_header * qfits_header_read_hdr(char * filename) ; object. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr_string( - unsigned char * hdr_str, - int nb_char) ; +qfits_header *qfits_header_read_hdr_string (unsigned char *hdr_str, + int nb_char); /*----------------------------------------------------------------------------*/ /** @@ -595,7 +581,7 @@ qfits_header * qfits_header_read_hdr_string( Returns NULL in case of error. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_readext(char * filename, int xtnum) ; +qfits_header *qfits_header_readext (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -607,7 +593,7 @@ qfits_header * qfits_header_readext(char * filename, int xtnum) ; for the file size to reach a multiple of 2880, as required by FITS. */ /*----------------------------------------------------------------------------*/ -void qfits_zeropad(char * filename) ; +void qfits_zeropad (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -619,7 +605,7 @@ void qfits_zeropad(char * filename) ; 0 else. If the file does not exist, returns -1. */ /*----------------------------------------------------------------------------*/ -int is_fits_file(char *filename) ; +int is_fits_file (char *filename); /* FITS header constants */ /** FITS block size */ @@ -656,7 +642,7 @@ int is_fits_file(char *filename) ; ((x) == BPP_16_SIGNED) ? 2 : \ ((x) == BPP_32_SIGNED) ? 4 : \ ((x) == BPP_IEEE_FLOAT) ? 4 : \ - ((x) == BPP_IEEE_DOUBLE) ? 8 : 0 ) + ((x) == BPP_IEEE_DOUBLE) ? 8 : 0 ) /*----------------------------------------------------------------------------- Function ANSI C prototypes -----------------------------------------------------------------------------*/ @@ -683,7 +669,7 @@ int is_fits_file(char *filename) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_dir_name(char * filename) ; +char *qfits_get_dir_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -705,7 +691,7 @@ char * qfits_get_dir_name(char * filename) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_base_name(const char *filename) ; +char *qfits_get_base_name (const char *filename); /*----------------------------------------------------------------------------*/ /** @@ -737,7 +723,7 @@ char * qfits_get_base_name(const char *filename) ; to free it or modify its contents. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_root_name(char * filename) ; +char *qfits_get_root_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -760,7 +746,7 @@ char * qfits_get_root_name(char * filename) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_ext_name(char * filename) ; +char *qfits_get_ext_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -776,7 +762,7 @@ char * qfits_get_ext_name(char * filename) ; point to a string which first character is a null character. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_login_name(void) ; +char *qfits_get_login_name (void); /*----------------------------------------------------------------------------*/ /** @@ -826,22 +812,22 @@ char * qfits_get_login_name(void) ; * Test a float variable for NaN value. * Do not call directly, call qfits_isnan(). */ -int _qfits_isnanf(float f); +int _qfits_isnanf (float f); /** * Test a float variable for Inf value. * Do not call directly, call qfits_isinf(). */ -int _qfits_isinff(float f); +int _qfits_isinff (float f); /** * Test a double variable for NaN value. * Do not call directly, call qfits_isnan(). */ -int _qfits_isnand(double d); +int _qfits_isnand (double d); /** * Test a double variable for Inf value. * Do not call directly, call qfits_isinf(). */ -int _qfits_isinfd(double d); +int _qfits_isinfd (double d); /*----------------------------------------------------------------------------- Function prototypes @@ -873,12 +859,10 @@ int _qfits_isinfd(double d); the file. */ /*----------------------------------------------------------------------------*/ -FILE * qfits_paf_print_header( - char * filename, - char * paf_id, - char * paf_desc, - char * login_name, - char * datetime) ; +FILE *qfits_paf_print_header (char *filename, + char *paf_id, + char *paf_desc, + char *login_name, char *datetime); /*----------------------------------------------------------------------------*/ /** @@ -894,9 +878,7 @@ FILE * qfits_paf_print_header( If the key is not found, this function returns NULL. */ /*----------------------------------------------------------------------------*/ -char * qfits_paf_query( - char * filename, - char * key) ; +char *qfits_paf_query (char *filename, char *key); /*----------------------------------------------------------------------------*/ /** @@ -908,7 +890,7 @@ char * qfits_paf_query( is checked with the presence of PAF.HDR.START at the beginning */ /*----------------------------------------------------------------------------*/ -int qfits_is_paf_file(char * filename) ; +int qfits_is_paf_file (char *filename); /*----------------------------------------------------------------------------- Defines @@ -933,7 +915,7 @@ int qfits_is_paf_file(char * filename) ; for readability. */ /*----------------------------------------------------------------------------*/ -typedef unsigned char byte ; +typedef unsigned char byte; /*----------------------------------------------------------------------------*/ /** @@ -1006,49 +988,50 @@ int main(int argc, char * argv[]) @endcode */ /*----------------------------------------------------------------------------*/ -typedef struct qfitsloader { +typedef struct qfitsloader +{ /** Private field to see if structure has been initialized */ - int _init ; - + int _init; + /** input: Name of the file you want to read pixels from */ - char * filename ; + char *filename; /** input: xtension number you want to read */ - int xtnum ; + int xtnum; /** input: Index of the plane you want, from 0 to np-1 */ - int pnum ; + int pnum; /** input: Pixel type you want (PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE) */ - int ptype ; + int ptype; /** input: Guarantee file copy or allow file mapping */ - int map ; + int map; /** output: Total number of extensions found in file */ - int exts ; + int exts; /** output: Size in X of the requested plane */ - int lx ; + int lx; /** output: Size in Y of the requested plane */ - int ly ; + int ly; /** output: Number of planes present in this extension */ - int np ; + int np; /** output: BITPIX for this extension */ - int bitpix ; + int bitpix; /** output: Start of the data segment (in bytes) for your request */ - int seg_start ; + int seg_start; /** output: Size of the data segment (in bytes) for your request */ - int seg_size ; + int seg_size; /** output: BSCALE found for this extension */ - double bscale ; + double bscale; /** output: BZERO found for this extension */ - double bzero ; + double bzero; /** output: Pointer to pixel buffer loaded as integer values */ - int * ibuf ; + int *ibuf; /** output: Pointer to pixel buffer loaded as float values */ - float * fbuf ; + float *fbuf; /** output: Pointer to pixel buffer loaded as double values */ - double * dbuf ; + double *dbuf; -} qfitsloader ; +} qfitsloader; /*----------------------------------------------------------------------------*/ @@ -1095,25 +1078,26 @@ typedef struct qfitsloader { could have been re-directed). */ /*----------------------------------------------------------------------------*/ -typedef struct qfitsdumper { +typedef struct qfitsdumper +{ /** Name of the file to dump to, "STDOUT" to dump to stdout */ - char * filename ; + char *filename; /** Number of pixels in the buffer to dump */ - int npix ; + int npix; /** Buffer type: PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE */ - int ptype ; + int ptype; /** Pointer to input integer pixel buffer */ - int * ibuf ; + int *ibuf; /** Pointer to input float pixel buffer */ - float * fbuf ; + float *fbuf; /** Pointer to input double pixel buffer */ - double * dbuf ; + double *dbuf; /** Requested BITPIX in output FITS file */ - int out_ptype ; -} qfitsdumper ; + int out_ptype; +} qfitsdumper; /*----------------------------------------------------------------------------- @@ -1145,7 +1129,7 @@ typedef struct qfitsdumper { is performed by qfits_loadpix() afterwards. */ /*----------------------------------------------------------------------------*/ -int qfitsloader_init(qfitsloader * ql); +int qfitsloader_init (qfitsloader * ql); /*----------------------------------------------------------------------------*/ /** @@ -1167,7 +1151,7 @@ int qfitsloader_init(qfitsloader * ql); code before you make calls to the pixel loader here. */ /*----------------------------------------------------------------------------*/ -int qfits_loadpix(qfitsloader * ql); +int qfits_loadpix (qfitsloader * ql); /*----------------------------------------------------------------------------*/ /** @@ -1190,7 +1174,7 @@ int qfits_loadpix(qfitsloader * ql); free on the returned pointer. */ /*----------------------------------------------------------------------------*/ -float * qfits_pixin_float (byte *, int, int, double, double); +float *qfits_pixin_float (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -1213,7 +1197,7 @@ float * qfits_pixin_float (byte *, int, int, double, double); free on the returned pointer. */ /*----------------------------------------------------------------------------*/ -int * qfits_pixin_int (byte *, int, int, double, double); +int *qfits_pixin_int (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -1234,9 +1218,9 @@ int * qfits_pixin_int (byte *, int, int, double, double); The returned buffer must be deallocated using the free() offered by xmemory. It is enough to #include "xmemory.h" before calling free on the returned pointer. - */ + */ /*----------------------------------------------------------------------------*/ -double * qfits_pixin_double(byte *, int, int, double, double); +double *qfits_pixin_double (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -1273,7 +1257,7 @@ double * qfits_pixin_double(byte *, int, int, double, double); will be performed to stdout. */ /*----------------------------------------------------------------------------*/ -int qfits_pixdump(qfitsdumper * qd) ; +int qfits_pixdump (qfitsdumper * qd); /*----------------------------------------------------------------------------*/ /** @@ -1287,9 +1271,9 @@ int qfits_pixdump(qfitsdumper * qd) ; suitable for dumping to a FITS file (i.e. big-endian, in the requested pixel type). The returned pointer must be deallocated using the free() function offered by xmemory. - */ + */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_float(float * buf, int npix, int ptype); +byte *qfits_pixdump_float (float *buf, int npix, int ptype); /*----------------------------------------------------------------------------*/ /** @@ -1305,7 +1289,7 @@ byte * qfits_pixdump_float(float * buf, int npix, int ptype); using the free() function offered by xmemory. */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_int(int * buf, int npix, int ptype); +byte *qfits_pixdump_int (int *buf, int npix, int ptype); /*----------------------------------------------------------------------------*/ /** @@ -1321,7 +1305,7 @@ byte * qfits_pixdump_int(int * buf, int npix, int ptype); using the free() function offered by xmemory. */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_double(double * buf, int npix, int ptype); +byte *qfits_pixdump_double (double *buf, int npix, int ptype); /*----------------------------------------------------------------------------*/ /** @@ -1333,7 +1317,7 @@ byte * qfits_pixdump_double(double * buf, int npix, int ptype); messages using the registered functions, otherwise they do nothing. */ /*----------------------------------------------------------------------------*/ -int qfits_err_statget(void); +int qfits_err_statget (void); /*----------------------------------------------------------------------------*/ /** @@ -1353,7 +1337,7 @@ int qfits_err_statget(void); \end{verbatim} */ /*----------------------------------------------------------------------------*/ -int qfits_err_statset(int sta); +int qfits_err_statset (int sta); /*----------------------------------------------------------------------------*/ /** @@ -1379,7 +1363,7 @@ int qfits_err_statset(int sta); returning -1. */ /*----------------------------------------------------------------------------*/ -int qfits_err_register( void (*dispfn)(char*) ) ; +int qfits_err_register (void (*dispfn) (char *)); /*----------------------------------------------------------------------------- Defines -----------------------------------------------------------------------------*/ @@ -1428,7 +1412,7 @@ int qfits_err_register( void (*dispfn)(char*) ) ; Returns NULL in case the requested keyword cannot be found. */ /*----------------------------------------------------------------------------*/ -char * qfits_query_hdr(char * filename, const char * keyword) ; +char *qfits_query_hdr (char *filename, const char *keyword); /*----------------------------------------------------------------------------*/ /** @@ -1443,7 +1427,7 @@ char * qfits_query_hdr(char * filename, const char * keyword) ; strictly identical to qfits_query_hdr(). */ /*----------------------------------------------------------------------------*/ -char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ; +char *qfits_query_ext (char *filename, const char *keyword, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -1455,7 +1439,7 @@ char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ; extension is found, and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_n_ext(char * filename) ; +int qfits_query_n_ext (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -1467,7 +1451,7 @@ int qfits_query_n_ext(char * filename) ; and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_nplanes(char * filename, int extnum) ; +int qfits_query_nplanes (char *filename, int extnum); /*----------------------------------------------------------------------------*/ /** @@ -1487,7 +1471,7 @@ int qfits_query_nplanes(char * filename, int extnum) ; */ /*----------------------------------------------------------------------------*/ -char * qfits_pretty_string(char * s) ; +char *qfits_pretty_string (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1498,7 +1482,7 @@ char * qfits_pretty_string(char * s) ; Identifies if a FITS value is boolean. */ /*----------------------------------------------------------------------------*/ -int qfits_is_boolean(char * s) ; +int qfits_is_boolean (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1509,7 +1493,7 @@ int qfits_is_boolean(char * s) ; Identifies if a FITS value is an integer. */ /*----------------------------------------------------------------------------*/ -int qfits_is_int(char * s) ; +int qfits_is_int (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1520,7 +1504,7 @@ int qfits_is_int(char * s) ; Identifies if a FITS value is float. */ /*----------------------------------------------------------------------------*/ -int qfits_is_float(char * s) ; +int qfits_is_float (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1531,7 +1515,7 @@ int qfits_is_float(char * s) ; Identifies if a FITS value is complex. */ /*----------------------------------------------------------------------------*/ -int qfits_is_complex(char * s) ; +int qfits_is_complex (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1542,7 +1526,7 @@ int qfits_is_complex(char * s) ; Identifies if a FITS value is a string. */ /*----------------------------------------------------------------------------*/ -int qfits_is_string(char * s) ; +int qfits_is_string (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1560,7 +1544,7 @@ int qfits_is_string(char * s) ; - QFITS_STRING (5) for a FITS string. */ /*----------------------------------------------------------------------------*/ -int qfits_get_type(char * s) ; +int qfits_get_type (char *s); /*----------------------------------------------------------------------------*/ /** @@ -1581,11 +1565,8 @@ int qfits_get_type(char * s) ; main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_hdrinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) ; +int qfits_get_hdrinfo (char *filename, + int xtnum, int *seg_start, int *seg_size); /*----------------------------------------------------------------------------*/ /** @@ -1606,11 +1587,8 @@ int qfits_get_hdrinfo( main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_datinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) ; +int qfits_get_datinfo (char *filename, + int xtnum, int *seg_start, int *seg_size); /*----------------------------------------------------------------------------*/ /** @@ -1620,9 +1598,7 @@ int qfits_get_datinfo( @return Allocated string containing the card or NULL */ /*----------------------------------------------------------------------------*/ -char * qfits_query_card( - char * filename, - char * keyword) ; +char *qfits_query_card (char *filename, char *keyword); /*----------------------------------------------------------------------------*/ /** @@ -1643,10 +1619,8 @@ char * qfits_query_card( Returns 0 if everything worked Ok, -1 otherwise. */ /*----------------------------------------------------------------------------*/ -int qfits_replace_card( - char * filename, - const char * keyword, - char * substitute) ; +int qfits_replace_card (char *filename, + const char *keyword, char *substitute); /*----------------------------------------------------------------------------- Defines @@ -1688,7 +1662,7 @@ long qfits_date_now (void); Example: 15:36:12.84 is returned as 15361284 */ /*----------------------------------------------------------------------------*/ -long qfits_time_now(void); +long qfits_time_now (void); /*----------------------------------------------------------------------------*/ /** @@ -1700,7 +1674,7 @@ long qfits_time_now(void); in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_date_iso8601(void); +char *qfits_get_date_iso8601 (void); /*----------------------------------------------------------------------------*/ /** @@ -1712,7 +1686,7 @@ char * qfits_get_date_iso8601(void); statically allocated string in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_datetime_iso8601(void); +char *qfits_get_datetime_iso8601 (void); /*----------------------------------------------------------------------------- Defines @@ -1732,27 +1706,28 @@ char * qfits_get_datetime_iso8601(void); /*----------------------------------------------------------------------------*/ /** @brief Column data type - */ + */ /*----------------------------------------------------------------------------*/ -typedef enum _TFITS_DATA_TYPE_ { - TFITS_ASCII_TYPE_A, - TFITS_ASCII_TYPE_D, - TFITS_ASCII_TYPE_E, - TFITS_ASCII_TYPE_F, - TFITS_ASCII_TYPE_I, - TFITS_BIN_TYPE_A, - TFITS_BIN_TYPE_B, - TFITS_BIN_TYPE_C, - TFITS_BIN_TYPE_D, - TFITS_BIN_TYPE_E, - TFITS_BIN_TYPE_I, - TFITS_BIN_TYPE_J, - TFITS_BIN_TYPE_L, - TFITS_BIN_TYPE_M, - TFITS_BIN_TYPE_P, - TFITS_BIN_TYPE_X, - TFITS_BIN_TYPE_UNKNOWN -} tfits_type ; +typedef enum _TFITS_DATA_TYPE_ +{ + TFITS_ASCII_TYPE_A, + TFITS_ASCII_TYPE_D, + TFITS_ASCII_TYPE_E, + TFITS_ASCII_TYPE_F, + TFITS_ASCII_TYPE_I, + TFITS_BIN_TYPE_A, + TFITS_BIN_TYPE_B, + TFITS_BIN_TYPE_C, + TFITS_BIN_TYPE_D, + TFITS_BIN_TYPE_E, + TFITS_BIN_TYPE_I, + TFITS_BIN_TYPE_J, + TFITS_BIN_TYPE_L, + TFITS_BIN_TYPE_M, + TFITS_BIN_TYPE_P, + TFITS_BIN_TYPE_X, + TFITS_BIN_TYPE_UNKNOWN +} tfits_type; /*----------------------------------------------------------------------------*/ /** @@ -1776,13 +1751,13 @@ typedef struct qfits_col it is the number of characters. A field with two complex object will have atom_nb = 4. */ - int atom_nb ; + int atom_nb; /** Number of decimals in a ASCII field. This value is always 0 for BIN tables */ - int atom_dec_nb ; + int atom_dec_nb; /** Size of one element in bytes. In ASCII tables, atom_size is the size @@ -1803,44 +1778,44 @@ typedef struct qfits_col conversion of the field is atom_size. In BIN tables, the size in bytes of a field is always atom_nb*atom_size. */ - int atom_size ; - + int atom_size; + /** Type of data in the column as specified in TFORM keyword In ASCII tables : TFITS_ASCII_TYPE_* with *=A, I, F, E or D In BIN tables : TFITS_BIN_TYPE_* with *=L, X, B, I, J, A, E, D, C, M or P */ - tfits_type atom_type ; + tfits_type atom_type; /** Label of the column */ - char tlabel[FITSVALSZ] ; + char tlabel[FITSVALSZ]; /** Unit of the data */ - char tunit[FITSVALSZ] ; - + char tunit[FITSVALSZ]; + /** Null value */ - char nullval[FITSVALSZ] ; + char nullval[FITSVALSZ]; /** Display format */ - char tdisp[FITSVALSZ] ; - + char tdisp[FITSVALSZ]; + /** zero and scale are used when the quantity in the field does not represent a true physical quantity. Basically, thez should be used when they are present: physical_value = zero + scale * field_value They are read from TZERO and TSCAL in the header */ - int zero_present ; - float zero ; - int scale_present ; - float scale ; + int zero_present; + float zero; + int scale_present; + float scale; /** Offset between the beg. of the table and the beg. of the column. */ - int off_beg ; - + int off_beg; + /** Flag to know if the column is readable. An empty col is not readable */ - int readable ; -} qfits_col ; + int readable; +} qfits_col; /*----------------------------------------------------------------------------*/ @@ -1879,21 +1854,21 @@ typedef struct qfits_table /** Name of the file the table comes from or it is intended to end to */ - char filename[FILENAMESZ] ; + char filename[FILENAMESZ]; /** Table type. Possible values: QFITS_INVALIDTABLE, QFITS_BINTABLE, QFITS_ASCIITABLE */ - int tab_t ; + int tab_t; /** Width in bytes of the table */ - int tab_w ; + int tab_w; /** Number of columns */ - int nc ; + int nc; /** Number of raws */ - int nr ; + int nr; /** Array of qfits_col objects */ - qfits_col * col ; -} qfits_table ; + qfits_col *col; +} qfits_table; /*----------------------------------------------------------------------------- Function prototypes @@ -1910,7 +1885,7 @@ typedef struct qfits_table FITS table. */ /*----------------------------------------------------------------------------*/ -int qfits_is_table(char * filename, int xtnum) ; +int qfits_is_table (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -1918,7 +1893,7 @@ int qfits_is_table(char * filename, int xtnum) ; @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_prim_header_default(void) ; +qfits_header *qfits_table_prim_header_default (void); /*----------------------------------------------------------------------------*/ /** @@ -1926,7 +1901,7 @@ qfits_header * qfits_table_prim_header_default(void) ; @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_ext_header_default(qfits_table *) ; +qfits_header *qfits_table_ext_header_default (qfits_table *); /*----------------------------------------------------------------------------*/ /** @@ -1941,12 +1916,9 @@ qfits_header * qfits_table_ext_header_default(qfits_table *) ; qfits_table_close() */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_new( - char * filename, - int table_type, - int table_width, - int nb_cols, - int nb_raws) ; +qfits_table *qfits_table_new (char *filename, + int table_type, + int table_width, int nb_cols, int nb_raws); /*----------------------------------------------------------------------------*/ /** @@ -1972,21 +1944,18 @@ qfits_table * qfits_table_new( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_col_fill( - qfits_col * qc, - int atom_nb, - int atom_dec_nb, - int atom_size, - tfits_type atom_type, - char * label, - char * unit, - char * nullval, - char * disp, - int zero_present, - float zero, - int scale_present, - float scale, - int offset_beg) ; +int qfits_col_fill (qfits_col * qc, + int atom_nb, + int atom_dec_nb, + int atom_size, + tfits_type atom_type, + char *label, + char *unit, + char *nullval, + char *disp, + int zero_present, + float zero, + int scale_present, float scale, int offset_beg); /*----------------------------------------------------------------------------*/ /** @@ -1999,9 +1968,7 @@ int qfits_col_fill( newly allocated qfits_table structure. */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_open( - char * filename, - int xtnum) ; +qfits_table *qfits_table_open (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -2011,7 +1978,7 @@ qfits_table * qfits_table_open( Frees all memory associated to a qfits_table structure. */ /*----------------------------------------------------------------------------*/ -void qfits_table_close(qfits_table * t) ; +void qfits_table_close (qfits_table * t); /*----------------------------------------------------------------------------*/ /** @@ -2042,10 +2009,8 @@ void qfits_table_close(qfits_table * t) ; the pixel loader here. */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column( - qfits_table * th, - int colnum, - int * selection) ; +unsigned char *qfits_query_column (qfits_table * th, + int colnum, int *selection); /*----------------------------------------------------------------------------*/ /** @@ -2059,11 +2024,9 @@ unsigned char * qfits_query_column( Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column_seq( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows) ; +unsigned char *qfits_query_column_seq (qfits_table * th, + int colnum, + int start_ind, int nb_rows); /*----------------------------------------------------------------------------*/ /** @@ -2072,7 +2035,7 @@ unsigned char * qfits_query_column_seq( @return the width (-1 in error case) */ /*----------------------------------------------------------------------------*/ -int qfits_compute_table_width(qfits_table * th) ; +int qfits_compute_table_width (qfits_table * th); /*----------------------------------------------------------------------------*/ /** @@ -2099,11 +2062,8 @@ int qfits_compute_table_width(qfits_table * th) ; the pixel loader here. */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_data( - qfits_table * th, - int colnum, - int * selection, - void * null_value) ; +void *qfits_query_column_data (qfits_table * th, + int colnum, int *selection, void *null_value); /*----------------------------------------------------------------------------*/ /** @@ -2118,12 +2078,10 @@ void * qfits_query_column_data( of rows. Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_seq_data( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows, - void * null_value) ; +void *qfits_query_column_seq_data (qfits_table * th, + int colnum, + int start_ind, + int nb_rows, void *null_value); /*----------------------------------------------------------------------------*/ /** @@ -2136,12 +2094,9 @@ void * qfits_query_column_seq_data( @return array with 1 for NULLs and 0 for non-NULLs */ /*----------------------------------------------------------------------------*/ -int * qfits_query_column_nulls( - qfits_table * th, - int colnum, - int * selection, - int * nb_vals, - int * nb_nulls) ; +int *qfits_query_column_nulls (qfits_table * th, + int colnum, + int *selection, int *nb_vals, int *nb_nulls); /*----------------------------------------------------------------------------*/ /** @@ -2152,10 +2107,8 @@ int * qfits_query_column_nulls( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_save_table_hdrdump( - void ** array, - qfits_table * table, - qfits_header * fh) ; +int qfits_save_table_hdrdump (void **array, + qfits_table * table, qfits_header * fh); /*----------------------------------------------------------------------------*/ /** @@ -2173,10 +2126,8 @@ int qfits_save_table_hdrdump( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension( - FILE * outfile, - qfits_table * t, - void ** data) ; +int qfits_table_append_xtension (FILE * outfile, + qfits_table * t, void **data); /*----------------------------------------------------------------------------*/ /** @@ -2194,11 +2145,9 @@ int qfits_table_append_xtension( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension_hdr( - FILE * outfile, - qfits_table * t, - void ** data, - qfits_header * hdr) ; +int qfits_table_append_xtension_hdr (FILE * outfile, + qfits_table * t, + void **data, qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -2210,13 +2159,10 @@ int qfits_table_append_xtension_hdr( @return the string to write */ /*----------------------------------------------------------------------------*/ -char * qfits_table_field_to_string( - qfits_table * table, - int col_id, - int row_id, - int use_zero_scale) ; +char *qfits_table_field_to_string (qfits_table * table, + int col_id, + int row_id, int use_zero_scale); #endif - diff --git a/qfits/include/xmemory.h b/qfits/include/xmemory.h index 49bb5fb..b5b5790 100644 --- a/qfits/include/xmemory.h +++ b/qfits/include/xmemory.h @@ -66,14 +66,14 @@ Function prototypes -----------------------------------------------------------------------------*/ -void * xmemory_malloc(size_t, const char *, int) ; -void * xmemory_calloc(size_t, size_t, const char *, int) ; -void * xmemory_realloc(void *, size_t, const char *, int) ; -void xmemory_free(void *, const char *, int) ; -char * xmemory_strdup(const char *, const char *, int) ; -char * xmemory_falloc(char *, size_t, size_t *, const char *, int) ; -void xmemory_fdealloc(void *, size_t, size_t, const char *, int) ; +void *xmemory_malloc (size_t, const char *, int); +void *xmemory_calloc (size_t, size_t, const char *, int); +void *xmemory_realloc (void *, size_t, const char *, int); +void xmemory_free (void *, const char *, int); +char *xmemory_strdup (const char *, const char *, int); +char *xmemory_falloc (char *, size_t, size_t *, const char *, int); +void xmemory_fdealloc (void *, size_t, size_t, const char *, int); -void xmemory_status_(const char * filename, int lineno) ; +void xmemory_status_ (const char *filename, int lineno); #endif diff --git a/qfits/main/dfits.c b/qfits/main/dfits.c index 86740a3..63233a1 100644 --- a/qfits/main/dfits.c +++ b/qfits/main/dfits.c @@ -45,275 +45,307 @@ Function prototypes -----------------------------------------------------------------------------*/ -void usage(char * pname) ; -void parse_cmd_line(int, char **, int *, int *, int *) ; -int dump_fits_filter(FILE *, int) ; -int dump_fits(char *, int) ; -char * rstrip(char *) ; +void usage (char *pname); +void parse_cmd_line (int, char **, int *, int *, int *); +int dump_fits_filter (FILE *, int); +int dump_fits (char *, int); +char *rstrip (char *); /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - int xtnum ; - int c_arg ; - int filter ; - int err ; + int xtnum; + int c_arg; + int filter; + int err; - /* No arguments prints out a usage message */ - if (argc<2) usage(argv[0]); + /* No arguments prints out a usage message */ + if (argc < 2) + usage (argv[0]); - /* Parse command-line options */ - parse_cmd_line(argc, argv, &xtnum, &filter, &c_arg); + /* Parse command-line options */ + parse_cmd_line (argc, argv, &xtnum, &filter, &c_arg); - /* Filter mode: process data received from stdin */ - if (filter) { + /* Filter mode: process data received from stdin */ + if (filter) + { #if HAVE_ZLIB - printf("filter mode does not support gzipped files\n"); - printf("use: gunzip -c file.fits | dfits -\n"); - return 1 ; + printf ("filter mode does not support gzipped files\n"); + printf ("use: gunzip -c file.fits | dfits -\n"); + return 1; #else - return dump_fits_filter(stdin, xtnum); + return dump_fits_filter (stdin, xtnum); #endif } - /* Normal mode: loop on all file names given on command-line */ - err = 0 ; - while (c_arg < argc) { - err += dump_fits(argv[c_arg], xtnum); - c_arg++; - } - return err ; /* Returns number of errors during process */ + /* Normal mode: loop on all file names given on command-line */ + err = 0; + while (c_arg < argc) + { + err += dump_fits (argv[c_arg], xtnum); + c_arg++; + } + return err; /* Returns number of errors during process */ } -void usage(char * pname) +void +usage (char *pname) { - printf( -"\n\n" -"usage: %s [-x xtnum] \n" -"usage: %s [-x xtnum] -\n" -"\n" -"The former version expects file names.\n" -"The latter expects data coming in from stdin.\n" -"\n" -"-x xtnum specifies the extension header to print\n" -"-x 0 specifies main header + all extensions\n" -"\n\n", - pname, pname); + printf ("\n\n" + "usage: %s [-x xtnum] \n" + "usage: %s [-x xtnum] -\n" + "\n" + "The former version expects file names.\n" + "The latter expects data coming in from stdin.\n" + "\n" + "-x xtnum specifies the extension header to print\n" + "-x 0 specifies main header + all extensions\n" + "\n\n", pname, pname); #if HAVE_ZLIB - printf( -"This program was compiled against zlib %s\n" -"This means you can use it with gzipped FITS files\n" -"as with uncompressed FITS files.\n" -"NB: this does not apply to the '-' option (input from stdin)\n" -"\n\n", ZLIB_VERSION); + printf ("This program was compiled against zlib %s\n" + "This means you can use it with gzipped FITS files\n" + "as with uncompressed FITS files.\n" + "NB: this does not apply to the '-' option (input from stdin)\n" + "\n\n", ZLIB_VERSION); #endif - exit(1) ; + exit (1); } -void parse_cmd_line( - int argc, - char ** argv, - int * xtnum, - int * filter, - int * c_arg) +void +parse_cmd_line (int argc, char **argv, int *xtnum, int *filter, int *c_arg) { - *filter = 0 ; - *xtnum = -1 ; - *c_arg = argc-1 ; + *filter = 0; + *xtnum = -1; + *c_arg = argc - 1; - /* If '-' is on the command-line, it must be the last argument */ - if (!strcmp(argv[argc-1], "-")) *filter = 1 ; - /* If -x xtnum is on the command-line, it must be the first two arguments */ - if (!strcmp(argv[1], "-x")) { - *xtnum = atoi(argv[2]); - *c_arg = 3 ; - } else { - *c_arg = 1 ; - } - return ; + /* If '-' is on the command-line, it must be the last argument */ + if (!strcmp (argv[argc - 1], "-")) + *filter = 1; + /* If -x xtnum is on the command-line, it must be the first two arguments */ + if (!strcmp (argv[1], "-x")) + { + *xtnum = atoi (argv[2]); + *c_arg = 3; + } + else + { + *c_arg = 1; + } + return; } /* Strip off all blank characters in a string from the right-side. */ -char * rstrip(char * s) +char * +rstrip (char *s) { - int len ; - if (s==NULL) return s ; - len = strlen(s); - if (len<1) return s ; - len -- ; - while (s[len]== ' ') { - s[len]=(char)0 ; - len --; - if (len<0) break ; + int len; + if (s == NULL) + return s; + len = strlen (s); + if (len < 1) + return s; + len--; + while (s[len] == ' ') + { + s[len] = (char) 0; + len--; + if (len < 0) + break; } - return s ; + return s; } /* Dump the requested header (main or extension) from a filename. */ -int dump_fits(char * name, int xtnum) +int +dump_fits (char *name, int xtnum) { - FILE * in ; - int err ; + FILE *in; + int err; - if ((in=fopen(name, "r"))==NULL) { - fprintf(stderr, "error: cannot open file [%s]\n", name); - return 1 ; - } + if ((in = fopen (name, "r")) == NULL) + { + fprintf (stderr, "error: cannot open file [%s]\n", name); + return 1; + } - printf("====> file %s (main) <====\n", name) ; - err = dump_fits_filter(in, xtnum); - fclose(in); - return err ; + printf ("====> file %s (main) <====\n", name); + err = dump_fits_filter (in, xtnum); + fclose (in); + return err; } /* Dump the requested header (main or extension) from a FILE * */ -int dump_fits_filter(FILE * in, int xtnum) +int +dump_fits_filter (FILE * in, int xtnum) { - int n_xt ; - char buf[LGTH+1]; - int err ; - int data_bytes, naxis ; - char * read_val ; - int skip_blocks ; - int seeked ; + int n_xt; + char buf[LGTH + 1]; + int err; + int data_bytes, naxis; + char *read_val; + int skip_blocks; + int seeked; - /* Try getting the first 80 chars */ - memset(buf, 0, LGTH+1); - if (fread(buf, sizeof(char), LGTH, in)!=LGTH) { - fprintf(stderr, "error reading input\n"); - return 1; - } - /* Check that it is indeed FITS */ - if (strncmp(buf, MAGIC, strlen(MAGIC))) { - fprintf(stderr, "not a FITS file\n"); - return 1 ; - } - naxis = 0 ; - data_bytes = 1 ; - if (xtnum<1) { - /* Output main header */ - printf("%s\n", rstrip(buf)); - data_bytes = 1 ; - naxis = 0 ; - while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) { - printf("%s\n", rstrip(buf)); - /* Look for BITPIX keyword */ - if (buf[0]=='B' && - buf[1]=='I' && - buf[2]=='T' && - buf[3]=='P' && - buf[4]=='I' && - buf[5]=='X' && - buf[6]==' ') { - read_val = qfits_getvalue(buf); - data_bytes *= (int)atoi(read_val) / 8 ; - if (data_bytes<0) data_bytes *= -1 ; - } else - /* Look for NAXIS keyword */ - if (buf[0]=='N' && - buf[1]=='A' && - buf[2]=='X' && - buf[3]=='I' && - buf[4]=='S') { + /* Try getting the first 80 chars */ + memset (buf, 0, LGTH + 1); + if (fread (buf, sizeof (char), LGTH, in) != LGTH) + { + fprintf (stderr, "error reading input\n"); + return 1; + } + /* Check that it is indeed FITS */ + if (strncmp (buf, MAGIC, strlen (MAGIC))) + { + fprintf (stderr, "not a FITS file\n"); + return 1; + } + naxis = 0; + data_bytes = 1; + if (xtnum < 1) + { + /* Output main header */ + printf ("%s\n", rstrip (buf)); + data_bytes = 1; + naxis = 0; + while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH) + { + printf ("%s\n", rstrip (buf)); + /* Look for BITPIX keyword */ + if (buf[0] == 'B' && + buf[1] == 'I' && + buf[2] == 'T' && + buf[3] == 'P' && + buf[4] == 'I' && buf[5] == 'X' && buf[6] == ' ') + { + read_val = qfits_getvalue (buf); + data_bytes *= (int) atoi (read_val) / 8; + if (data_bytes < 0) + data_bytes *= -1; + } + else + /* Look for NAXIS keyword */ + if (buf[0] == 'N' && + buf[1] == 'A' && + buf[2] == 'X' && buf[3] == 'I' && buf[4] == 'S') + { - if (buf[5]==' ') { - /* NAXIS keyword */ - read_val = qfits_getvalue(buf); - naxis = (int)atoi(read_val); - } else { - /* NAXIS?? keyword (axis size) */ - read_val = qfits_getvalue(buf); - data_bytes *= (int)atoi(read_val); - } - } else - /* Look for END keyword */ - if (buf[0]=='E' && - buf[1]=='N' && - buf[2]=='D') { - break ; - } + if (buf[5] == ' ') + { + /* NAXIS keyword */ + read_val = qfits_getvalue (buf); + naxis = (int) atoi (read_val); } - if (err!=LGTH) return 1 ; - } - if (xtnum<0) return 0 ; - - n_xt=0 ; - while (1) { - /* - * Skip the previous data section if pixels were declared - */ - if (naxis>0) { - /* Skip as many blocks as there are declared pixels */ - skip_blocks = data_bytes/BLOCK_SIZE ; - if ((data_bytes % BLOCK_SIZE)!=0) skip_blocks ++ ; - seeked = fseek(in, skip_blocks*BLOCK_SIZE, SEEK_CUR); - if (seeked<0) return -1 ; - } - - /* Look for next XTENSION keyword */ - while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) { - if (buf[0]=='X' && - buf[1]=='T' && - buf[2]=='E' && - buf[3]=='N' && - buf[4]=='S' && - buf[5]=='I' && - buf[6]=='O' && - buf[7]=='N') break ; + else + { + /* NAXIS?? keyword (axis size) */ + read_val = qfits_getvalue (buf); + data_bytes *= (int) atoi (read_val); } - if (err==0) break ; - if (err!=LGTH) return 1 ; - - n_xt++ ; - - if (xtnum==0 || xtnum==n_xt) { - printf("===> xtension %d\n", n_xt) ; - printf("%s\n", rstrip(buf)); - } - - data_bytes = 1 ; - naxis = 0 ; - while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) { - if (xtnum==0 || xtnum==n_xt) printf("%s\n", rstrip(buf)); - - /* Look for BITPIX keyword */ - if (buf[0]=='B' && - buf[1]=='I' && - buf[2]=='T' && - buf[3]=='P' && - buf[4]=='I' && - buf[5]=='X' && - buf[6]==' ') { - read_val = qfits_getvalue(buf); - data_bytes *= (int)atoi(read_val) / 8 ; - if (data_bytes<0) data_bytes *= -1 ; - } else - /* Look for NAXIS keyword */ - if (buf[0]=='N' && - buf[1]=='A' && - buf[2]=='X' && - buf[3]=='I' && - buf[4]=='S') { - - if (buf[5]==' ') { - /* NAXIS keyword */ - read_val = qfits_getvalue(buf); - naxis = (int)atoi(read_val); - } else { - /* NAXIS?? keyword (axis size) */ - read_val = qfits_getvalue(buf); - data_bytes *= (int)atoi(read_val); - } - } else - /* Look for END keyword */ - if (buf[0]=='E' && - buf[1]=='N' && - buf[2]=='D') break ; - } - if (n_xt==xtnum) break ; + } + else + /* Look for END keyword */ + if (buf[0] == 'E' && buf[1] == 'N' && buf[2] == 'D') + { + break; + } } - return 0 ; + if (err != LGTH) + return 1; + } + if (xtnum < 0) + return 0; + + n_xt = 0; + while (1) + { + /* + * Skip the previous data section if pixels were declared + */ + if (naxis > 0) + { + /* Skip as many blocks as there are declared pixels */ + skip_blocks = data_bytes / BLOCK_SIZE; + if ((data_bytes % BLOCK_SIZE) != 0) + skip_blocks++; + seeked = fseek (in, skip_blocks * BLOCK_SIZE, SEEK_CUR); + if (seeked < 0) + return -1; + } + + /* Look for next XTENSION keyword */ + while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH) + { + if (buf[0] == 'X' && + buf[1] == 'T' && + buf[2] == 'E' && + buf[3] == 'N' && + buf[4] == 'S' && + buf[5] == 'I' && buf[6] == 'O' && buf[7] == 'N') + break; + } + if (err == 0) + break; + if (err != LGTH) + return 1; + + n_xt++; + + if (xtnum == 0 || xtnum == n_xt) + { + printf ("===> xtension %d\n", n_xt); + printf ("%s\n", rstrip (buf)); + } + + data_bytes = 1; + naxis = 0; + while ((err = fread (buf, sizeof (char), LGTH, in)) == LGTH) + { + if (xtnum == 0 || xtnum == n_xt) + printf ("%s\n", rstrip (buf)); + + /* Look for BITPIX keyword */ + if (buf[0] == 'B' && + buf[1] == 'I' && + buf[2] == 'T' && + buf[3] == 'P' && + buf[4] == 'I' && buf[5] == 'X' && buf[6] == ' ') + { + read_val = qfits_getvalue (buf); + data_bytes *= (int) atoi (read_val) / 8; + if (data_bytes < 0) + data_bytes *= -1; + } + else + /* Look for NAXIS keyword */ + if (buf[0] == 'N' && + buf[1] == 'A' && + buf[2] == 'X' && buf[3] == 'I' && buf[4] == 'S') + { + + if (buf[5] == ' ') + { + /* NAXIS keyword */ + read_val = qfits_getvalue (buf); + naxis = (int) atoi (read_val); + } + else + { + /* NAXIS?? keyword (axis size) */ + read_val = qfits_getvalue (buf); + data_bytes *= (int) atoi (read_val); + } + } + else + /* Look for END keyword */ + if (buf[0] == 'E' && buf[1] == 'N' && buf[2] == 'D') + break; + } + if (n_xt == xtnum) + break; + } + return 0; } diff --git a/qfits/main/dtfits.c b/qfits/main/dtfits.c index 77504a0..2bdcfb7 100644 --- a/qfits/main/dtfits.c +++ b/qfits/main/dtfits.c @@ -31,512 +31,568 @@ #define DISP_SIZE_INT 5 #define DISP_SIZE_DOUBLE 8 #define DISP_SIZE_FLOAT 7 -#define DISP_SIZE_CHAR 1 +#define DISP_SIZE_CHAR 1 /*----------------------------------------------------------------------------- Function prototypes -----------------------------------------------------------------------------*/ -static int dump_extension_bin(qfits_table *, FILE *, void **, char, int, - int) ; -static int dump_extension_ascii(qfits_table *, FILE *, void **, char, - int, int) ; -static int dump_extension(qfits_table *, FILE *, char, int, int) ; -static void qfits_dump(char *, char *, int, char, int); -static void usage(char * pname) ; -static char prog_desc[] = "FITS table dump" ; +static int dump_extension_bin (qfits_table *, FILE *, void **, char, int, + int); +static int dump_extension_ascii (qfits_table *, FILE *, void **, char, + int, int); +static int dump_extension (qfits_table *, FILE *, char, int, int); +static void qfits_dump (char *, char *, int, char, int); +static void usage (char *pname); +static char prog_desc[] = "FITS table dump"; /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - char name_i[FILENAMESZ] ; - char name_o[FILENAMESZ] ; - int i ; - int data_only ; - char separator ; - int ext ; + char name_i[FILENAMESZ]; + char name_o[FILENAMESZ]; + int i; + int data_only; + char separator; + int ext; - /* Initialize */ - data_only = 0 ; - separator = '|' ; - ext = -1 ; - - i=1 ; + /* Initialize */ + data_only = 0; + separator = '|'; + ext = -1; - if (argc<2) usage(argv[0]); + i = 1; - while (i=argc) { - fprintf(stderr, "option -s needs an argument\n"); - return -1 ; - } - i++ ; - separator = argv[i][0] ; + if (argc < 2) + usage (argv[0]); + + while (i < argc) + { + if (!strcmp (argv[i], "--help") || !strcmp (argv[i], "-h")) + { + usage (argv[0]); + } + else if (!strcmp (argv[i], "-d")) + { + data_only = 1; + } + else if (!strcmp (argv[i], "-s")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -s needs an argument\n"); + return -1; + } + i++; + separator = argv[i][0]; - } else if (!strcmp(argv[i], "-x")) { - if ((i+1)>=argc) { - fprintf(stderr, "option -x needs an argument\n"); - return -1 ; - } - i++ ; - ext = atoi(argv[i]) ; - } else { - break ; + } + else if (!strcmp (argv[i], "-x")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -x needs an argument\n"); + return -1; + } + i++; + ext = atoi (argv[i]); + } + else + { + break; + } + i++; + } + if ((argc - i) < 1) + { + fprintf (stderr, "missing input file name\n"); + return -1; + } + strcpy (name_i, argv[i]); + i++; + if ((argc - i) < 1) + name_o[0] = 0; + else + strcpy (name_o, argv[i]); + + qfits_dump (name_i, name_o, data_only, separator, ext); + return 0; +} + +static void +usage (char *pname) +{ + printf ("%s -- %s\n", pname, prog_desc); + printf ("use : %s [options] [out]\n" + "options are:\n" + "\t-d to dump data only (no headers)\n" + "\t-s to output data with separator \n" "\n", pname); + exit (0); +} + +static void +qfits_dump (char *name_i, + char *name_o, int data_only, char separator, int ext) +{ + qfits_table *tdesc; + FILE *out; + int xtnum_start, xtnum_stop; + int n_ext; + int i; + + /* Set where to send the output */ + if (name_o[0] == (char) 0) + { + out = stdout; + } + else + { + if ((out = fopen (name_o, "w")) == NULL) + { + fprintf (stderr, "cannot create output file [%s]\n", name_o); + return; + } + } + + if (!data_only) + { + fprintf (out, "#\n"); + fprintf (out, "# file %s\n", name_i); + } + + /* Query number of extensions in the file */ + n_ext = qfits_query_n_ext (name_i); + if (!data_only) + { + fprintf (out, "# extensions %d\n", n_ext); + } + /* If no extension, bail out */ + if (n_ext < 1) + { + if (out != stdout) + fclose (out); + return; + } + + /* 1 extension required or all */ + if (ext < 1) + { + xtnum_start = 1; + xtnum_stop = n_ext; + } + else if (ext > n_ext) + { + fprintf (out, "# requested extension does not exist %d\n", ext); + if (out != stdout) + fclose (out); + return; + } + else + { + xtnum_start = xtnum_stop = ext; + } + + /* Loop over all extensions */ + for (i = xtnum_start; i <= xtnum_stop; i++) + { + if (!data_only) + { + fprintf (out, "# --------------------------------------------\n"); + fprintf (out, "# XTENSION %d\n", i); + } + if ((tdesc = qfits_table_open (name_i, i)) == NULL) + { + printf ("cannot open table [%s]:[%d]\n", name_i, i); + if (out != stdout) + fclose (out); + return; + } + dump_extension (tdesc, out, separator, data_only, 1); + qfits_table_close (tdesc); + } + fclose (out); + return; +} + +static int +dump_extension (qfits_table * tdesc, + FILE * out, char separator, int data_only, int use_zero_scale) +{ + void **cols; + int i, j; + + if (!data_only) + { + fprintf (out, "# Number of columns %d\n", tdesc->nc); + fprintf (out, "#\n"); + } + + /* First read the columns in memory */ + cols = malloc (tdesc->nc * sizeof (void *)); + for (i = 0; i < tdesc->nc; i++) + { + cols[i] = qfits_query_column_data (tdesc, i, NULL, NULL); + if (cols[i] == NULL) + { + fprintf (out, "# Cannot load column nb %d\n", i + 1); + } + } + + switch (tdesc->tab_t) + { + case QFITS_BINTABLE: + dump_extension_bin (tdesc, out, cols, separator, data_only, + use_zero_scale); + break; + case QFITS_ASCIITABLE: + dump_extension_ascii (tdesc, out, cols, separator, data_only, + use_zero_scale); + break; + default: + fprintf (out, "Table type not recognized"); + break; + } + + for (i = 0; i < tdesc->nc; i++) + if (cols[i]) + free (cols[i]); + free (cols); + return 0; +} + +static int +dump_extension_bin (qfits_table * tdesc, + FILE * out, + void **cols, + char separator, int data_only, int use_zero_scale) +{ + int *col_sizes; + qfits_col *col; + char *ccol; + unsigned char *ucol; + double *dcol; + float *fcol; + short *scol; + int *icol; + int size; + int field_size; + char *str; + char ctmp[512]; + int i, j, k; + + /* GET THE FIELDS SIZES */ + col_sizes = calloc (tdesc->nc, sizeof (int)); + for (i = 0; i < tdesc->nc; i++) + { + col = tdesc->col + i; + col_sizes[i] = (int) strlen (col->tlabel); + switch (col->atom_type) + { + case TFITS_BIN_TYPE_A: + size = col->atom_size * col->atom_nb; + break; + case TFITS_BIN_TYPE_B: + size = col->atom_nb * (DISP_SIZE_INT + 2); + break; + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + size = col->atom_nb * (DISP_SIZE_DOUBLE + 2); + break; + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + size = col->atom_nb * (DISP_SIZE_FLOAT + 2); + break; + case TFITS_BIN_TYPE_I: + size = col->atom_nb * (DISP_SIZE_INT + 2); + break; + case TFITS_BIN_TYPE_J: + size = col->atom_nb * (DISP_SIZE_INT + 2); + break; + case TFITS_BIN_TYPE_L: + size = col->atom_nb * (DISP_SIZE_CHAR + 2); + break; + case TFITS_BIN_TYPE_X: + size = col->atom_nb * (DISP_SIZE_INT + 2); + break; + case TFITS_BIN_TYPE_P: + size = col->atom_nb * (DISP_SIZE_INT + 2); + break; + default: + fprintf (out, "Type not recognized"); + break; + } + if (size > col_sizes[i]) + col_sizes[i] = size; + } + + /* Print out the column names */ + if (!data_only) + { + for (i = 0; i < tdesc->nc; i++) + { + col = tdesc->col + i; + fprintf (out, "%*s", col_sizes[i], col->tlabel); + if (i != (tdesc->nc - 1)) + printf ("%c", separator); + } + fprintf (out, "\n"); + } + + /* Get the string to write according to the type */ + for (j = 0; j < tdesc->nr; j++) + { + for (i = 0; i < tdesc->nc; i++) + { + if (cols[i] == NULL) + continue; + col = tdesc->col + i; + field_size = col->atom_nb * ELEMENT_MAX_DISP_SIZE; + str = malloc (field_size * sizeof (char)); + str[0] = (char) 0; + switch (col->atom_type) + { + case TFITS_BIN_TYPE_A: + ccol = (char *) (cols[i]); + ccol += col->atom_size * col->atom_nb * j; + strncpy (ctmp, ccol, col->atom_size * col->atom_nb); + ctmp[col->atom_size * col->atom_nb] = (char) 0; + strcpy (str, ctmp); + break; + case TFITS_BIN_TYPE_B: + ucol = (unsigned char *) (cols[i]); + ucol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%d, ", (int) ucol[k]); + strcat (str, ctmp); } - i++ ; - } - if ((argc-i)<1) { - fprintf(stderr, "missing input file name\n"); - return -1 ; - } - strcpy(name_i, argv[i]); - i++ ; - if ((argc-i)<1) name_o[0] = 0 ; - else strcpy(name_o, argv[i]); - - qfits_dump(name_i, name_o, data_only, separator, ext); - return 0 ; -} - -static void usage(char * pname) -{ - printf("%s -- %s\n", pname, prog_desc); - printf( - "use : %s [options] [out]\n" - "options are:\n" - "\t-d to dump data only (no headers)\n" - "\t-s to output data with separator \n" - "\n", - pname); - exit(0) ; -} - -static void qfits_dump( - char * name_i, - char * name_o, - int data_only, - char separator, - int ext) -{ - qfits_table * tdesc ; - FILE * out ; - int xtnum_start, xtnum_stop ; - int n_ext ; - int i ; - - /* Set where to send the output */ - if (name_o[0]==(char)0) { - out = stdout ; - } else { - if ((out = fopen(name_o, "w"))==NULL) { - fprintf(stderr, "cannot create output file [%s]\n", name_o); - return ; + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", (int) ucol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + dcol = (double *) (cols[i]); + dcol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%g, ", dcol[k]); + strcat (str, ctmp); } - } - - if (!data_only) { - fprintf(out, "#\n"); - fprintf(out, "# file %s\n", name_i); - } - - /* Query number of extensions in the file */ - n_ext = qfits_query_n_ext(name_i); - if (!data_only) { - fprintf(out, "# extensions %d\n", n_ext); - } - /* If no extension, bail out */ - if (n_ext<1) { - if (out!=stdout) fclose(out) ; - return ; - } - - /* 1 extension required or all */ - if (ext < 1) { - xtnum_start = 1 ; - xtnum_stop = n_ext ; - } else if (ext > n_ext) { - fprintf(out, "# requested extension does not exist %d\n", ext) ; - if (out!=stdout) fclose(out) ; - return ; - } else { - xtnum_start = xtnum_stop = ext ; - } - - /* Loop over all extensions */ - for (i=xtnum_start ; i<=xtnum_stop ; i++) { - if (!data_only) { - fprintf(out, "# --------------------------------------------\n"); - fprintf(out, "# XTENSION %d\n", i); + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%g", dcol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + fcol = (float *) (cols[i]); + fcol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%f, ", fcol[k]); + strcat (str, ctmp); } - if ((tdesc = qfits_table_open(name_i, i)) == NULL) { - printf("cannot open table [%s]:[%d]\n", name_i, i); - if (out!=stdout) fclose(out); - return ; + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", fcol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_I: + scol = (short *) (cols[i]); + scol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%d, ", scol[k]); + strcat (str, ctmp); } - dump_extension(tdesc, out, separator, data_only, 1) ; - qfits_table_close(tdesc); + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", scol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_J: + icol = (int *) (cols[i]); + icol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%d, ", icol[k]); + strcat (str, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", icol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_L: + ccol = (char *) (cols[i]); + ccol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%c, ", ccol[k]); + strcat (str, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%c", ccol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_X: + ucol = (unsigned char *) (cols[i]); + ucol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%d, ", ucol[k]); + strcat (str, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", ucol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + case TFITS_BIN_TYPE_P: + icol = (int *) (cols[i]); + icol += col->atom_nb * j; + /* For each atom of the column */ + for (k = 0; k < col->atom_nb - 1; k++) + { + sprintf (ctmp, "%d, ", icol[k]); + strcat (str, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", icol[col->atom_nb - 1]); + strcat (str, ctmp); + break; + default: + fprintf (out, "Type not recognized"); + break; + } + fprintf (out, "%*s", col_sizes[i], str); + if (i != (tdesc->nc - 1)) + printf ("%c", separator); + free (str); } - fclose(out) ; - return ; + fprintf (out, "\n"); + } + return 0; } - -static int dump_extension( - qfits_table * tdesc, - FILE * out, - char separator, - int data_only, - int use_zero_scale) + +static int +dump_extension_ascii (qfits_table * tdesc, + FILE * out, + void **cols, + char separator, int data_only, int use_zero_scale) { - void ** cols ; - int i, j ; - - if (!data_only) { - fprintf(out, "# Number of columns %d\n", tdesc->nc); - fprintf(out, "#\n"); + int *col_sizes; + qfits_col *col; + char *ccol; + double *dcol; + float *fcol; + int *icol; + int size; + int field_size; + char *str; + char ctmp[512]; + int i, j, k; + + /* GET THE FIELDS SIZES */ + col_sizes = calloc (tdesc->nc, sizeof (int)); + for (i = 0; i < tdesc->nc; i++) + { + col = tdesc->col + i; + col_sizes[i] = (int) strlen (col->tlabel); + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + size = col->atom_nb; + break; + case TFITS_ASCII_TYPE_I: + size = DISP_SIZE_INT; + break; + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + size = DISP_SIZE_FLOAT; + break; + case TFITS_ASCII_TYPE_D: + size = DISP_SIZE_DOUBLE; + break; + default: + fprintf (out, "Type not recognized"); + break; } - - /* First read the columns in memory */ - cols = malloc(tdesc->nc * sizeof(void*)) ; - for (i=0 ; inc ; i++) { - cols[i] = qfits_query_column_data(tdesc, i, NULL, NULL) ; - if (cols[i] == NULL) { - fprintf(out, "# Cannot load column nb %d\n", i+1) ; - } + if (size > col_sizes[i]) + col_sizes[i] = size; } - switch (tdesc->tab_t) { - case QFITS_BINTABLE: - dump_extension_bin(tdesc, out, cols, separator, data_only, - use_zero_scale) ; - break ; - case QFITS_ASCIITABLE: - dump_extension_ascii(tdesc, out, cols, separator, data_only, - use_zero_scale) ; - break ; - default: - fprintf(out, "Table type not recognized") ; - break ; - } - - for (i=0 ; inc ; i++) if (cols[i]) free(cols[i]) ; - free(cols) ; - return 0 ; -} - -static int dump_extension_bin( - qfits_table * tdesc, - FILE * out, - void ** cols, - char separator, - int data_only, - int use_zero_scale) -{ - int * col_sizes ; - qfits_col * col ; - char * ccol ; - unsigned char * ucol ; - double * dcol ; - float * fcol ; - short * scol ; - int * icol ; - int size ; - int field_size ; - char * str ; - char ctmp[512]; - int i, j, k ; - - /* GET THE FIELDS SIZES */ - col_sizes = calloc(tdesc->nc, sizeof(int)) ; - for (i=0 ; inc ; i++) { - col = tdesc->col + i ; - col_sizes[i] = (int)strlen(col->tlabel) ; - switch(col->atom_type) { - case TFITS_BIN_TYPE_A: - size = col->atom_size * col->atom_nb ; - break ; - case TFITS_BIN_TYPE_B: - size = col->atom_nb * (DISP_SIZE_INT + 2) ; - break ; - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - size = col->atom_nb * (DISP_SIZE_DOUBLE + 2) ; - break ; - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - size = col->atom_nb * (DISP_SIZE_FLOAT + 2) ; - break ; - case TFITS_BIN_TYPE_I: - size = col->atom_nb * (DISP_SIZE_INT + 2) ; - break ; - case TFITS_BIN_TYPE_J: - size = col->atom_nb * (DISP_SIZE_INT + 2) ; - break ; - case TFITS_BIN_TYPE_L: - size = col->atom_nb * (DISP_SIZE_CHAR + 2) ; - break ; - case TFITS_BIN_TYPE_X: - size = col->atom_nb * (DISP_SIZE_INT + 2) ; - break ; - case TFITS_BIN_TYPE_P: - size = col->atom_nb * (DISP_SIZE_INT + 2) ; - break ; - default: - fprintf(out, "Type not recognized") ; - break ; - } - if (size > col_sizes[i]) col_sizes[i] = size ; + /* Print out the column names */ + if (!data_only) + { + for (i = 0; i < tdesc->nc; i++) + { + col = tdesc->col + i; + fprintf (out, "%*s", col_sizes[i], col->tlabel); + if (i != (tdesc->nc - 1)) + printf ("%c", separator); + } + fprintf (out, "\n"); } - /* Print out the column names */ - if (!data_only) { - for (i=0 ; inc ; i++) { - col = tdesc->col + i ; - fprintf(out, "%*s", col_sizes[i], col->tlabel); - if (i!=(tdesc->nc-1)) printf("%c", separator); - } - fprintf(out, "\n"); - } - - /* Get the string to write according to the type */ - for (j=0 ; jnr ; j++) { - for (i=0 ; inc ; i++) { - if (cols[i] == NULL) continue ; - col = tdesc->col + i ; - field_size = col->atom_nb * ELEMENT_MAX_DISP_SIZE ; - str = malloc(field_size * sizeof(char)) ; - str[0] = (char)0 ; - switch(col->atom_type) { - case TFITS_BIN_TYPE_A: - ccol = (char*)(cols[i]) ; - ccol += col->atom_size * col->atom_nb * j ; - strncpy(ctmp, ccol, col->atom_size * col->atom_nb) ; - ctmp[col->atom_size*col->atom_nb] = (char)0 ; - strcpy(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_B: - ucol = (unsigned char*)(cols[i]) ; - ucol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%d, ", (int)ucol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp,"%d",(int)ucol[col->atom_nb-1]); - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - dcol = (double*)(cols[i]) ; - dcol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%g, ", dcol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%g", dcol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - fcol = (float*)(cols[i]) ; - fcol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%f, ", fcol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", fcol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_I: - scol = (short*)(cols[i]) ; - scol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%d, ", scol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d", scol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_J: - icol = (int*)(cols[i]) ; - icol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%d, ", icol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d", icol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_L: - ccol = (char*)(cols[i]) ; - ccol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%c, ", ccol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%c", ccol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_X: - ucol = (unsigned char*)(cols[i]) ; - ucol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%d, ", ucol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d", ucol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - case TFITS_BIN_TYPE_P: - icol = (int*)(cols[i]) ; - icol += col->atom_nb * j ; - /* For each atom of the column */ - for (k=0 ; katom_nb-1 ; k++) { - sprintf(ctmp, "%d, ", icol[k]) ; - strcat(str, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d", icol[col->atom_nb-1]) ; - strcat(str, ctmp) ; - break ; - default: - fprintf(out, "Type not recognized") ; - break ; - } - fprintf(out, "%*s", col_sizes[i], str); - if (i!=(tdesc->nc-1)) printf("%c", separator); - free(str) ; - } - fprintf(out, "\n"); - } - return 0 ; -} - -static int dump_extension_ascii( - qfits_table * tdesc, - FILE * out, - void ** cols, - char separator, - int data_only, - int use_zero_scale) -{ - int * col_sizes ; - qfits_col * col ; - char * ccol ; - double * dcol ; - float * fcol ; - int * icol ; - int size ; - int field_size ; - char * str ; - char ctmp[512]; - int i, j, k ; - - /* GET THE FIELDS SIZES */ - col_sizes = calloc(tdesc->nc, sizeof(int)) ; - for (i=0 ; inc ; i++) { - col = tdesc->col + i ; - col_sizes[i] = (int)strlen(col->tlabel) ; - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - size = col->atom_nb ; - break ; - case TFITS_ASCII_TYPE_I: - size = DISP_SIZE_INT ; - break ; - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - size = DISP_SIZE_FLOAT ; - break ; - case TFITS_ASCII_TYPE_D: - size = DISP_SIZE_DOUBLE ; - break ; - default: - fprintf(out, "Type not recognized") ; - break ; - } - if (size > col_sizes[i]) col_sizes[i] = size ; + /* Get the string to write according to the type */ + for (j = 0; j < tdesc->nr; j++) + { + for (i = 0; i < tdesc->nc; i++) + { + if (cols[i] == NULL) + continue; + col = tdesc->col + i; + field_size = col->atom_nb * ELEMENT_MAX_DISP_SIZE; + str = malloc (field_size * sizeof (char)); + str[0] = (char) 0; + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + ccol = (char *) (cols[i]); + ccol += col->atom_nb * j; + strncpy (ctmp, ccol, col->atom_nb); + ctmp[col->atom_nb] = (char) 0; + strcpy (str, ctmp); + break; + case TFITS_ASCII_TYPE_I: + icol = (int *) (cols[i]); + icol += j; + sprintf (ctmp, "%d", icol[0]); + strcat (str, ctmp); + break; + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + fcol = (float *) (cols[i]); + fcol += j; + sprintf (ctmp, "%f", fcol[0]); + strcat (str, ctmp); + break; + case TFITS_ASCII_TYPE_D: + dcol = (double *) (cols[i]); + dcol += j; + sprintf (ctmp, "%g", dcol[0]); + strcat (str, ctmp); + break; + default: + fprintf (out, "Type not recognized"); + break; + } + fprintf (out, "%*s", col_sizes[i], str); + if (i != (tdesc->nc - 1)) + printf ("%c", separator); + free (str); + } + fprintf (out, "\n"); } - /* Print out the column names */ - if (!data_only) { - for (i=0 ; inc ; i++) { - col = tdesc->col + i ; - fprintf(out, "%*s", col_sizes[i], col->tlabel); - if (i!=(tdesc->nc-1)) printf("%c", separator); - } - fprintf(out, "\n"); - } - - /* Get the string to write according to the type */ - for (j=0 ; jnr ; j++) { - for (i=0 ; inc ; i++) { - if (cols[i] == NULL) continue ; - col = tdesc->col + i ; - field_size = col->atom_nb * ELEMENT_MAX_DISP_SIZE ; - str = malloc(field_size * sizeof(char)) ; - str[0] = (char)0 ; - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - ccol = (char*)(cols[i]) ; - ccol += col->atom_nb * j ; - strncpy(ctmp, ccol, col->atom_nb) ; - ctmp[col->atom_nb] = (char)0 ; - strcpy(str, ctmp) ; - break ; - case TFITS_ASCII_TYPE_I: - icol = (int*)(cols[i]) ; - icol += j ; - sprintf(ctmp, "%d", icol[0]) ; - strcat(str, ctmp) ; - break ; - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - fcol = (float*)(cols[i]) ; - fcol += j ; - sprintf(ctmp, "%f", fcol[0]) ; - strcat(str, ctmp) ; - break ; - case TFITS_ASCII_TYPE_D: - dcol = (double*)(cols[i]) ; - dcol += j ; - sprintf(ctmp, "%g", dcol[0]) ; - strcat(str, ctmp) ; - break ; - default: - fprintf(out, "Type not recognized") ; - break ; - } - fprintf(out, "%*s", col_sizes[i], str); - if (i!=(tdesc->nc-1)) printf("%c", separator); - free(str) ; - } - fprintf(out, "\n"); - } - - return 0 ; + return 0; } - - diff --git a/qfits/main/flipx.c b/qfits/main/flipx.c index a0c93c3..60c1741 100644 --- a/qfits/main/flipx.c +++ b/qfits/main/flipx.c @@ -37,132 +37,149 @@ * Swap pixels between position p1 and p2, regardless of the pixel * type and endian-ness of the local host. */ -static void swap_pix(char * buf, int p1, int p2, int psize) +static void +swap_pix (char *buf, int p1, int p2, int psize) { - int i ; - char c ; + int i; + char c; - for (i=0 ; i\n", argv[0]); - return 1 ; - } - err=0 ; - for (i=1 ; i0) { - fprintf(stderr, "%s: %d error(s) occurred\n", argv[0], err); - return -1 ; - } - return 0 ; + if (argc < 2) + { + printf ("use: %s \n", argv[0]); + return 1; + } + err = 0; + for (i = 1; i < argc; i++) + { + err += fits_flip (argv[0], argv[i]); + } + if (err > 0) + { + fprintf (stderr, "%s: %d error(s) occurred\n", argv[0], err); + return -1; + } + return 0; } diff --git a/qfits/main/frameq.c b/qfits/main/frameq.c index f83d1e7..d88ec35 100644 --- a/qfits/main/frameq.c +++ b/qfits/main/frameq.c @@ -33,31 +33,33 @@ -----------------------------------------------------------------------------*/ /* Frame informations needed to classify it */ -typedef struct _framei { - char * name ; - char * tplid ; - char * origfile ; - int expno ; - int nexp ; +typedef struct _framei +{ + char *name; + char *tplid; + char *origfile; + int expno; + int nexp; - struct _framei * next ; -} framei ; + struct _framei *next; +} framei; /* Frame queue: is mostly a pointer to a list of frame information objects */ -typedef struct _frameq { - framei * first ; - int n ; -} frameq ; +typedef struct _frameq +{ + framei *first; + int n; +} frameq; /*----------------------------------------------------------------------------- Global variables -----------------------------------------------------------------------------*/ /* List of strings to identify unwanted templates. */ -char * tpl_filter[] = { - "_acq_", - "_CheckAoCorrection", - NULL +char *tpl_filter[] = { + "_acq_", + "_CheckAoCorrection", + NULL }; /*----------------------------------------------------------------------------- @@ -71,43 +73,56 @@ char * tpl_filter[] = { @return A newly allocated framei obj. NULL in error case */ /*----------------------------------------------------------------------------*/ -static framei * framei_new(char * filename) +static framei * +framei_new (char *filename) { - framei * fi ; - char * sval ; - char * sval2 ; - int i ; + framei *fi; + char *sval; + char *sval2; + int i; - fi = malloc(sizeof(framei)); - fi->name = strdup(filename); - sval = qfits_query_hdr(filename, "tpl.id"); - if (sval!=NULL) { - /* Filter out unwanted template IDs */ - i=0 ; - while (tpl_filter[i]!=NULL) { - if (strstr(sval, tpl_filter[i])!=NULL) return NULL ; - i++ ; - } - fi->tplid = strdup(qfits_pretty_string(sval)) ; - } else { - fi->tplid = NULL ; + fi = malloc (sizeof (framei)); + fi->name = strdup (filename); + sval = qfits_query_hdr (filename, "tpl.id"); + if (sval != NULL) + { + /* Filter out unwanted template IDs */ + i = 0; + while (tpl_filter[i] != NULL) + { + if (strstr (sval, tpl_filter[i]) != NULL) + return NULL; + i++; + } + fi->tplid = strdup (qfits_pretty_string (sval)); } - sval = qfits_query_hdr(filename, "origfile"); - if (sval!=NULL) { - sval2 = qfits_pretty_string(sval) ; - fi->origfile = strdup(qfits_get_root_name(sval2)) ; - } else { - fi->origfile = NULL ; + else + { + fi->tplid = NULL; } - sval = qfits_query_hdr(filename, "tpl.expno"); - if (sval!=NULL) fi->expno = (int)atoi(sval); - else fi->expno = -1 ; - sval = qfits_query_hdr(filename, "tpl.nexp"); - if (sval!=NULL) fi->nexp = (int)atoi(sval); - else fi->nexp = -1 ; + sval = qfits_query_hdr (filename, "origfile"); + if (sval != NULL) + { + sval2 = qfits_pretty_string (sval); + fi->origfile = strdup (qfits_get_root_name (sval2)); + } + else + { + fi->origfile = NULL; + } + sval = qfits_query_hdr (filename, "tpl.expno"); + if (sval != NULL) + fi->expno = (int) atoi (sval); + else + fi->expno = -1; + sval = qfits_query_hdr (filename, "tpl.nexp"); + if (sval != NULL) + fi->nexp = (int) atoi (sval); + else + fi->nexp = -1; - fi->next = NULL ; - return fi ; + fi->next = NULL; + return fi; } /*----------------------------------------------------------------------------*/ @@ -116,18 +131,22 @@ static framei * framei_new(char * filename) @param fi Object to delete */ /*----------------------------------------------------------------------------*/ -static void framei_del(framei * fi) +static void +framei_del (framei * fi) { - if (fi==NULL) return ; - if (fi->name!=NULL) - free(fi->name); - if (fi->tplid!=NULL) { - free(fi->tplid); + if (fi == NULL) + return; + if (fi->name != NULL) + free (fi->name); + if (fi->tplid != NULL) + { + free (fi->tplid); } - if (fi->origfile!=NULL) { - free(fi->origfile); + if (fi->origfile != NULL) + { + free (fi->origfile); } - free(fi); + free (fi); } /*----------------------------------------------------------------------------*/ @@ -136,14 +155,15 @@ static void framei_del(framei * fi) @return A newly allocated empty frameq object */ /*----------------------------------------------------------------------------*/ -frameq * frameq_new(void) +frameq * +frameq_new (void) { - frameq * fq ; + frameq *fq; - fq = malloc(sizeof(frameq)); - fq->first = NULL ; - fq->n = 0 ; - return fq ; + fq = malloc (sizeof (frameq)); + fq->first = NULL; + fq->n = 0; + return fq; } /*----------------------------------------------------------------------------*/ @@ -152,21 +172,24 @@ frameq * frameq_new(void) @param fq The object to delete */ /*----------------------------------------------------------------------------*/ -void frameq_del(frameq * fq) +void +frameq_del (frameq * fq) { - framei * fi ; - framei * fin ; + framei *fi; + framei *fin; - if (fq==NULL) return ; + if (fq == NULL) + return; - fi = fq->first ; - while (fi!=NULL) { - fin = fi->next ; - framei_del(fi); - fi = fin ; + fi = fq->first; + while (fi != NULL) + { + fin = fi->next; + framei_del (fi); + fi = fin; } - free(fq); - return ; + free (fq); + return; } /*----------------------------------------------------------------------------*/ @@ -176,28 +199,31 @@ void frameq_del(frameq * fq) @param filename The file to append name */ /*----------------------------------------------------------------------------*/ -void frameq_append(frameq * fq, char * filename) +void +frameq_append (frameq * fq, char *filename) { - framei * fi ; - framei * fn ; + framei *fi; + framei *fn; - if (fq==NULL || filename==NULL) return ; - fi = framei_new(filename); - if (fi==NULL) - return ; - if (fq->n==0) { - fq->first = fi ; - fq->n = 1 ; - return ; + if (fq == NULL || filename == NULL) + return; + fi = framei_new (filename); + if (fi == NULL) + return; + if (fq->n == 0) + { + fq->first = fi; + fq->n = 1; + return; } - fn = fq->first ; - while (fn->next!=NULL) - fn = fn->next ; - - fn->next = fi ; - fq->n ++ ; - return ; + fn = fq->first; + while (fn->next != NULL) + fn = fn->next; + + fn->next = fi; + fq->n++; + return; } /*----------------------------------------------------------------------------*/ @@ -206,14 +232,15 @@ void frameq_append(frameq * fq, char * filename) @param fq The frame queue */ /*----------------------------------------------------------------------------*/ -void frameq_pop(frameq * fq) +void +frameq_pop (frameq * fq) { - framei * first ; + framei *first; - first = fq->first->next ; - framei_del(fq->first); - fq->first = first ; - fq->n -- ; + first = fq->first->next; + framei_del (fq->first); + fq->first = first; + fq->n--; } /*----------------------------------------------------------------------------*/ @@ -223,20 +250,18 @@ void frameq_pop(frameq * fq) @param out File where the queue is dumped */ /*----------------------------------------------------------------------------*/ -void frameq_dump(frameq * fq, FILE * out) +void +frameq_dump (frameq * fq, FILE * out) { - int i ; - framei * fi ; + int i; + framei *fi; - fi = fq->first ; - for (i=0 ; in ; i++) { - fprintf(out, - "%s %s %02d/%02d\n", - fi->name, - fi->tplid, - fi->expno, - fi->nexp); - fi = fi->next ; + fi = fq->first; + for (i = 0; i < fq->n; i++) + { + fprintf (out, + "%s %s %02d/%02d\n", fi->name, fi->tplid, fi->expno, fi->nexp); + fi = fi->next; } } @@ -247,42 +272,46 @@ void frameq_dump(frameq * fq, FILE * out) @return A newly allocated frame queue */ /*----------------------------------------------------------------------------*/ -frameq * frameq_load(char * dirname) +frameq * +frameq_load (char *dirname) { - frameq * fq ; - int i ; - glob_t pglob ; - char filename[FILENAMESZ]; + frameq *fq; + int i; + glob_t pglob; + char filename[FILENAMESZ]; - /* Look for *.fits *.FITS *.fits.gz *.FITS.gz */ - sprintf(filename, "%s/*.fits", dirname); - glob(filename, GLOB_MARK, NULL, &pglob); - sprintf(filename, "%s/*.FITS", dirname); - glob(filename, GLOB_APPEND, NULL, &pglob); - sprintf(filename, "%s/*.fits.gz", dirname); - glob(filename, GLOB_APPEND, NULL, &pglob); - sprintf(filename, "%s/*.FITS.gz", dirname); - glob(filename, GLOB_APPEND, NULL, &pglob); - if (pglob.gl_pathc<1) { - printf("found no frame\n"); - return NULL ; + /* Look for *.fits *.FITS *.fits.gz *.FITS.gz */ + sprintf (filename, "%s/*.fits", dirname); + glob (filename, GLOB_MARK, NULL, &pglob); + sprintf (filename, "%s/*.FITS", dirname); + glob (filename, GLOB_APPEND, NULL, &pglob); + sprintf (filename, "%s/*.fits.gz", dirname); + glob (filename, GLOB_APPEND, NULL, &pglob); + sprintf (filename, "%s/*.FITS.gz", dirname); + glob (filename, GLOB_APPEND, NULL, &pglob); + if (pglob.gl_pathc < 1) + { + printf ("found no frame\n"); + return NULL; } - /* Build frame queue */ - fq = frameq_new(); - for (i=0 ; in * sizeof(char*)); - fn = fq->first ; - for (i=0 ; in ; i++) { - if (fn->tplid==NULL) { - tplid_all[i] = strdup("none"); - } else { - tplid_all[i] = strdup(fn->tplid); - } - fn = fn->next ; + /* Get all possible values for tplid */ + tplid_all = malloc (fq->n * sizeof (char *)); + fn = fq->first; + for (i = 0; i < fq->n; i++) + { + if (fn->tplid == NULL) + { + tplid_all[i] = strdup ("none"); + } + else + { + tplid_all[i] = strdup (fn->tplid); + } + fn = fn->next; } - /* Sort all tplid's */ - qsort(tplid_all, fq->n, sizeof(char*), stringsort); + /* Sort all tplid's */ + qsort (tplid_all, fq->n, sizeof (char *), stringsort); - /* Compute how many different tplid's can be found */ - ntplid=1 ; - for (i=1 ; in ; i++) { - if (strcmp(tplid_all[i], tplid_all[i-1])) { - ntplid++ ; - } + /* Compute how many different tplid's can be found */ + ntplid = 1; + for (i = 1; i < fq->n; i++) + { + if (strcmp (tplid_all[i], tplid_all[i - 1])) + { + ntplid++; + } } - tplid = malloc(ntplid * sizeof(char*)); - tplid[0] = tplid_all[0] ; - tplid_all[0] = NULL ; - j=0 ; - for (i=1 ; in ; i++) { - if (strcmp(tplid_all[i], tplid[j])) { - j++ ; - tplid[j] = tplid_all[i] ; - } else { - free(tplid_all[i]); - } - tplid_all[i] = NULL ; + tplid = malloc (ntplid * sizeof (char *)); + tplid[0] = tplid_all[0]; + tplid_all[0] = NULL; + j = 0; + for (i = 1; i < fq->n; i++) + { + if (strcmp (tplid_all[i], tplid[j])) + { + j++; + tplid[j] = tplid_all[i]; + } + else + { + free (tplid_all[i]); + } + tplid_all[i] = NULL; } - free(tplid_all); + free (tplid_all); - *n = ntplid ; - return tplid ; + *n = ntplid; + return tplid; } /*----------------------------------------------------------------------------*/ @@ -349,29 +389,34 @@ char ** frameq_get_tplid(frameq * fq, int * n) @return Setting number */ /*----------------------------------------------------------------------------*/ -int frameq_getsetnum(char * dirname) +int +frameq_getsetnum (char *dirname) { - char pattern[FILENAMESZ]; - glob_t pglob ; - int i ; - int max ; - int num ; + char pattern[FILENAMESZ]; + glob_t pglob; + int i; + int max; + int num; - sprintf(pattern, "%s/set*", dirname); - glob(pattern, GLOB_MARK, NULL, &pglob); - if (pglob.gl_pathc<1) { - max=0 ; - } else { - sprintf(pattern, "%s/set%%02d", dirname); - max=0 ; - for (i=0 ; imax) - max=num ; - } + sprintf (pattern, "%s/set*", dirname); + glob (pattern, GLOB_MARK, NULL, &pglob); + if (pglob.gl_pathc < 1) + { + max = 0; } - globfree(&pglob); - return max+1 ; + else + { + sprintf (pattern, "%s/set%%02d", dirname); + max = 0; + for (i = 0; i < pglob.gl_pathc; i++) + { + sscanf (pglob.gl_pathv[i], pattern, &num); + if (num > max) + max = num; + } + } + globfree (&pglob); + return max + 1; } /*----------------------------------------------------------------------------*/ @@ -380,82 +425,93 @@ int frameq_getsetnum(char * dirname) @param fq Frame queue */ /*----------------------------------------------------------------------------*/ -void frameq_makelists(frameq * fq) +void +frameq_makelists (frameq * fq) { - FILE * list ; - char filename[FILENAMESZ]; - framei * fi ; - int setnum ; - int count ; - int batches ; + FILE *list; + char filename[FILENAMESZ]; + framei *fi; + int setnum; + int count; + int batches; - /* Count # of batches in input */ - fi = fq->first ; - batches=0 ; - while (fi!=NULL) { - if (fi->expno==1) - batches++ ; - fi = fi->next ; + /* Count # of batches in input */ + fi = fq->first; + batches = 0; + while (fi != NULL) + { + if (fi->expno == 1) + batches++; + fi = fi->next; } - fi = fq->first ; - count=0 ; - list=NULL ; - while (fi!=NULL) { - printf("\rclassifying batches: %d of %d", count, batches); - fflush(stdout); - if (fi->expno<0) { - fi=fi->next ; - continue ; - } - if (fi->expno==1) { - count++ ; - if (list!=NULL) { - fclose(list); - } - if (fi->tplid == NULL) { - printf("No TPL ID - abort\n") ; - return ; - } - if (fi->origfile == NULL) { - printf("No ORIGFILE - abort\n") ; - return ; - } - mkdir(fi->tplid, 0755); - setnum = frameq_getsetnum(fi->tplid); - sprintf(filename, "%s/%s_%02d", fi->tplid, fi->origfile, fi->nexp); - mkdir(filename, 0755); - sprintf(filename, "%s/%s_%02d/IN", fi->tplid,fi->origfile,fi->nexp); - list = fopen(filename, "w"); - fprintf(list,"# TPL.ID= %s\n", fi->tplid); - fprintf(list,"# NEXP = %02d\n", fi->nexp); - } - if (list) fprintf(list, "%s\n", fi->name); - fi = fi->next ; + fi = fq->first; + count = 0; + list = NULL; + while (fi != NULL) + { + printf ("\rclassifying batches: %d of %d", count, batches); + fflush (stdout); + if (fi->expno < 0) + { + fi = fi->next; + continue; + } + if (fi->expno == 1) + { + count++; + if (list != NULL) + { + fclose (list); + } + if (fi->tplid == NULL) + { + printf ("No TPL ID - abort\n"); + return; + } + if (fi->origfile == NULL) + { + printf ("No ORIGFILE - abort\n"); + return; + } + mkdir (fi->tplid, 0755); + setnum = frameq_getsetnum (fi->tplid); + sprintf (filename, "%s/%s_%02d", fi->tplid, fi->origfile, fi->nexp); + mkdir (filename, 0755); + sprintf (filename, "%s/%s_%02d/IN", fi->tplid, fi->origfile, + fi->nexp); + list = fopen (filename, "w"); + fprintf (list, "# TPL.ID= %s\n", fi->tplid); + fprintf (list, "# NEXP = %02d\n", fi->nexp); + } + if (list) + fprintf (list, "%s\n", fi->name); + fi = fi->next; } - printf("\n"); - return ; + printf ("\n"); + return; } /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char * argv[]) +int +main (int argc, char *argv[]) { - frameq * fq ; + frameq *fq; - if (argc<2) { - printf("use: %s \n", argv[0]); - return 1 ; - } - - printf("loading frames from %s...\n", argv[1]); - fq = frameq_load(argv[1]); - printf("processing lists...\n"); - frameq_makelists(fq); - frameq_del(fq); - printf("done\n"); + if (argc < 2) + { + printf ("use: %s \n", argv[0]); + return 1; + } - return 0 ; + printf ("loading frames from %s...\n", argv[1]); + fq = frameq_load (argv[1]); + printf ("processing lists...\n"); + frameq_makelists (fq); + frameq_del (fq); + printf ("done\n"); + + return 0; } - diff --git a/qfits/main/qextract.c b/qfits/main/qextract.c index c926021..3b3414d 100644 --- a/qfits/main/qextract.c +++ b/qfits/main/qextract.c @@ -5,7 +5,7 @@ @date Nov, 2002 @version $Revision: 1.5 $ @brief Extract a FITS extension and write it in a new FITS file -*/ +*/ /*----------------------------------------------------------------------------*/ /* @@ -19,249 +19,275 @@ Includes -----------------------------------------------------------------------------*/ -#include "qfits.h" +#include "qfits.h" /*----------------------------------------------------------------------------- Function prototypes -----------------------------------------------------------------------------*/ -static int textract_write_ext(char *, int) ; -static int iextract_write_ext(char *, int) ; -static char prog_desc[] = "Extract and write FITS extensions" ; -static void usage(char *) ; +static int textract_write_ext (char *, int); +static int iextract_write_ext (char *, int); +static char prog_desc[] = "Extract and write FITS extensions"; +static void usage (char *); /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - char * name_in ; - int ext ; - int nb_ext ; - int ext_type ; - - if (argc<3) usage(argv[0]) ; - - /* Get input file name and extension number */ - name_in = strdup(argv[1]) ; - ext = (int)atoi(argv[2]) ; - - /* Check if the file is a FITS file */ - if (!is_fits_file(name_in)) { - printf("[%s] is not a FITS file\n", name_in) ; - free(name_in) ; - return -1 ; - } - - /* Check if the extension is valid */ - nb_ext = qfits_query_n_ext(name_in) ; - if (nb_ext < ext) { - printf("Only %d extensions in this file\n", nb_ext) ; - free(name_in) ; - return -1 ; + char *name_in; + int ext; + int nb_ext; + int ext_type; + + if (argc < 3) + usage (argv[0]); + + /* Get input file name and extension number */ + name_in = strdup (argv[1]); + ext = (int) atoi (argv[2]); + + /* Check if the file is a FITS file */ + if (!is_fits_file (name_in)) + { + printf ("[%s] is not a FITS file\n", name_in); + free (name_in); + return -1; } - /* Check if it's a table or an image */ - ext_type = qfits_is_table(name_in, ext) ; - - switch (ext_type) { - case QFITS_BINTABLE: - case QFITS_ASCIITABLE: - if (textract_write_ext(name_in, ext) == -1) { - printf("cannot read-write extension no %d\n", ext) ; - free(name_in) ; - return -1 ; - } - break ; - case QFITS_INVALIDTABLE: - if (iextract_write_ext(name_in, ext) == -1) { - printf("cannot read-write extension no %d\n", ext) ; - free(name_in) ; - return -1 ; - } - break ; - default: - printf("Unrecognized FITS type\n") ; - free(name_in) ; - return -1 ; + /* Check if the extension is valid */ + nb_ext = qfits_query_n_ext (name_in); + if (nb_ext < ext) + { + printf ("Only %d extensions in this file\n", nb_ext); + free (name_in); + return -1; } - free(name_in) ; - - return 0 ; + + /* Check if it's a table or an image */ + ext_type = qfits_is_table (name_in, ext); + + switch (ext_type) + { + case QFITS_BINTABLE: + case QFITS_ASCIITABLE: + if (textract_write_ext (name_in, ext) == -1) + { + printf ("cannot read-write extension no %d\n", ext); + free (name_in); + return -1; + } + break; + case QFITS_INVALIDTABLE: + if (iextract_write_ext (name_in, ext) == -1) + { + printf ("cannot read-write extension no %d\n", ext); + free (name_in); + return -1; + } + break; + default: + printf ("Unrecognized FITS type\n"); + free (name_in); + return -1; + } + free (name_in); + + return 0; } /*----------------------------------------------------------------------------- Functions code -----------------------------------------------------------------------------*/ -static void usage(char * pname) +static void +usage (char *pname) { - printf("%s : %s\n", pname, prog_desc) ; - printf( - "use : %s \n" - "\n", pname) ; - exit(0) ; + printf ("%s : %s\n", pname, prog_desc); + printf ("use : %s \n" "\n", pname); + exit (0); } -static int textract_write_ext( - char * in, - int ext) +static int +textract_write_ext (char *in, int ext) { - qfits_table * th ; - void ** array ; - qfits_header * fh ; - int array_size ; - int i ; + qfits_table *th; + void **array; + qfits_header *fh; + int array_size; + int i; - /* Get the table infos */ - if ((th = qfits_table_open(in, ext)) == NULL) { - printf("cannot read extension: %d\n", ext) ; - return -1 ; - } - - /* Compute array_size */ - array_size = 0 ; - for (i=0 ; inc ; i++) { - switch (th->col[i].atom_type) { - case TFITS_ASCII_TYPE_A: - case TFITS_ASCII_TYPE_I: - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - case TFITS_ASCII_TYPE_D: - case TFITS_BIN_TYPE_A: - case TFITS_BIN_TYPE_L: - case TFITS_BIN_TYPE_B: - case TFITS_BIN_TYPE_X: - array_size += sizeof(char*) ; - break ; - - case TFITS_BIN_TYPE_I: - array_size += sizeof(short*) ; - break ; - - case TFITS_BIN_TYPE_J: - case TFITS_BIN_TYPE_E: - array_size += sizeof(int*) ; - break ; - - case TFITS_BIN_TYPE_C: - case TFITS_BIN_TYPE_P: - array_size += sizeof(float*) ; - break ; - - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - array_size += sizeof(double*) ; - break ; - default: - return -1 ; - } - } - - /* Allocate memory for array */ - array = malloc(array_size) ; - - /* Load columns in array */ - for (i=0 ; inc ; i++) { - array[i] = qfits_query_column_data(th, i, NULL, NULL) ; - if (array[i] == NULL) { - printf("cannot read column %d\n", i+1) ; - } + /* Get the table infos */ + if ((th = qfits_table_open (in, ext)) == NULL) + { + printf ("cannot read extension: %d\n", ext); + return -1; } - /* Update th : filename */ - sprintf(th->filename, "ext%d.tfits", ext) ; + /* Compute array_size */ + array_size = 0; + for (i = 0; i < th->nc; i++) + { + switch (th->col[i].atom_type) + { + case TFITS_ASCII_TYPE_A: + case TFITS_ASCII_TYPE_I: + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + case TFITS_ASCII_TYPE_D: + case TFITS_BIN_TYPE_A: + case TFITS_BIN_TYPE_L: + case TFITS_BIN_TYPE_B: + case TFITS_BIN_TYPE_X: + array_size += sizeof (char *); + break; - /* Get fits primary header */ - if ((fh = qfits_header_read(in)) == NULL) { - for (i=0 ; inc ; i++) if (array[i] != NULL) free(array[i]) ; - qfits_table_close(th) ; - free(array) ; - printf("cannot read fits header\n") ; - return -1 ; - } - - if (ext != 0) { - /* No data in primary HDU */ - qfits_header_mod(fh, "NAXIS", "0", NULL) ; - qfits_header_del(fh, "NAXIS1") ; - qfits_header_del(fh, "NAXIS2") ; - } + case TFITS_BIN_TYPE_I: + array_size += sizeof (short *); + break; - /* Write the tfits file */ - if (qfits_save_table_hdrdump((void **)array, th, fh) == -1) { - qfits_header_destroy(fh) ; - for (i=0 ; inc ; i++) if (array[i] != NULL) free(array[i]) ; - qfits_table_close(th) ; - free(array) ; - printf("cannot write fits table\n") ; - return -1 ; + case TFITS_BIN_TYPE_J: + case TFITS_BIN_TYPE_E: + array_size += sizeof (int *); + break; + + case TFITS_BIN_TYPE_C: + case TFITS_BIN_TYPE_P: + array_size += sizeof (float *); + break; + + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + array_size += sizeof (double *); + break; + default: + return -1; + } } - /* Free and return */ - qfits_header_destroy(fh) ; - for (i=0 ; inc ; i++) if (array[i] != NULL) free(array[i]) ; - qfits_table_close(th) ; - free(array) ; - return 0 ; + /* Allocate memory for array */ + array = malloc (array_size); + + /* Load columns in array */ + for (i = 0; i < th->nc; i++) + { + array[i] = qfits_query_column_data (th, i, NULL, NULL); + if (array[i] == NULL) + { + printf ("cannot read column %d\n", i + 1); + } + } + + /* Update th : filename */ + sprintf (th->filename, "ext%d.tfits", ext); + + /* Get fits primary header */ + if ((fh = qfits_header_read (in)) == NULL) + { + for (i = 0; i < th->nc; i++) + if (array[i] != NULL) + free (array[i]); + qfits_table_close (th); + free (array); + printf ("cannot read fits header\n"); + return -1; + } + + if (ext != 0) + { + /* No data in primary HDU */ + qfits_header_mod (fh, "NAXIS", "0", NULL); + qfits_header_del (fh, "NAXIS1"); + qfits_header_del (fh, "NAXIS2"); + } + + /* Write the tfits file */ + if (qfits_save_table_hdrdump ((void **) array, th, fh) == -1) + { + qfits_header_destroy (fh); + for (i = 0; i < th->nc; i++) + if (array[i] != NULL) + free (array[i]); + qfits_table_close (th); + free (array); + printf ("cannot write fits table\n"); + return -1; + } + + /* Free and return */ + qfits_header_destroy (fh); + for (i = 0; i < th->nc; i++) + if (array[i] != NULL) + free (array[i]); + qfits_table_close (th); + free (array); + return 0; } -static int iextract_write_ext( - char * in, - int ext) +static int +iextract_write_ext (char *in, int ext) { - qfitsloader ql ; - qfitsdumper qd ; - qfits_header * fh ; - char outname[1024] ; - FILE * out ; - - sprintf(outname, "ext%d.fits", ext) ; - - /* Initialize a FITS loader */ - ql.filename = in ; - ql.xtnum = ext ; - ql.pnum = 0 ; - ql.map = 1 ; - ql.ptype = PTYPE_DOUBLE ; - if (qfitsloader_init(&ql)!=0) return -1 ; - - /* Load the primary header */ - if ((fh = qfits_header_read(ql.filename)) == NULL) return -1 ; + qfitsloader ql; + qfitsdumper qd; + qfits_header *fh; + char outname[1024]; + FILE *out; - if (ext != 0) { - /* No data in primary HDU */ - qfits_header_mod(fh, "NAXIS", "0", NULL) ; - qfits_header_del(fh, "NAXIS1") ; - qfits_header_del(fh, "NAXIS2") ; - } - - /* Dump the primary header */ - if ((out = fopen(outname, "w")) == NULL) return -1 ; - qfits_header_dump(fh, out); - qfits_header_destroy(fh) ; + sprintf (outname, "ext%d.fits", ext); - if (ext != 0) { - /* Load the extension header */ - if ((fh = qfits_header_readext(ql.filename, ext)) == NULL) return -1 ; - /* Dump the extension header */ - qfits_header_dump(fh, out); - qfits_header_destroy(fh) ; + /* Initialize a FITS loader */ + ql.filename = in; + ql.xtnum = ext; + ql.pnum = 0; + ql.map = 1; + ql.ptype = PTYPE_DOUBLE; + if (qfitsloader_init (&ql) != 0) + return -1; + + /* Load the primary header */ + if ((fh = qfits_header_read (ql.filename)) == NULL) + return -1; + + if (ext != 0) + { + /* No data in primary HDU */ + qfits_header_mod (fh, "NAXIS", "0", NULL); + qfits_header_del (fh, "NAXIS1"); + qfits_header_del (fh, "NAXIS2"); } - fclose(out) ; - - /* Load the FITS image */ - if (qfits_loadpix(&ql)!=0) return -1 ; - - /* Write the FITS image */ - qd.filename = outname ; - qd.npix = ql.lx * ql.ly ; - qd.ptype = PTYPE_DOUBLE ; - qd.dbuf = ql.dbuf ; - qd.out_ptype = ql.bitpix ; - if (qfits_pixdump(&qd)!=0) return -1 ; - /* qfits_zeropad(outname) ; */ - free(ql.dbuf) ; - - return 0 ; + + /* Dump the primary header */ + if ((out = fopen (outname, "w")) == NULL) + return -1; + qfits_header_dump (fh, out); + qfits_header_destroy (fh); + + if (ext != 0) + { + /* Load the extension header */ + if ((fh = qfits_header_readext (ql.filename, ext)) == NULL) + return -1; + /* Dump the extension header */ + qfits_header_dump (fh, out); + qfits_header_destroy (fh); + } + fclose (out); + + /* Load the FITS image */ + if (qfits_loadpix (&ql) != 0) + return -1; + + /* Write the FITS image */ + qd.filename = outname; + qd.npix = ql.lx * ql.ly; + qd.ptype = PTYPE_DOUBLE; + qd.dbuf = ql.dbuf; + qd.out_ptype = ql.bitpix; + if (qfits_pixdump (&qd) != 0) + return -1; + /* qfits_zeropad(outname) ; */ + free (ql.dbuf); + + return 0; } diff --git a/qfits/main/replacekey.c b/qfits/main/replacekey.c index 824dd69..6e67dda 100644 --- a/qfits/main/replacekey.c +++ b/qfits/main/replacekey.c @@ -19,7 +19,7 @@ Include -----------------------------------------------------------------------------*/ -#include "qfits.h" +#include "qfits.h" /*----------------------------------------------------------------------------- Define @@ -31,182 +31,231 @@ Functions prototypes -----------------------------------------------------------------------------*/ -static char prog_desc[] = "replace keyword in a FITS header" ; -static void usage(char *) ; +static char prog_desc[] = "replace keyword in a FITS header"; +static void usage (char *); /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - char * name_in ; - char * card ; - char * place ; - char * key ; - char * key_tmp ; - char * val ; - char * val_tmp ; - char * com ; - char * com_tmp ; - int keep_com ; - int numeric ; - char * stmp ; - int i ; - - char card_tmp[NM_SIZ] ; + char *name_in; + char *card; + char *place; + char *key; + char *key_tmp; + char *val; + char *val_tmp; + char *com; + char *com_tmp; + int keep_com; + int numeric; + char *stmp; + int i; - if (argc<2) usage(argv[0]) ; - - /* Initialize */ - name_in = NULL ; - card = NULL ; - place = NULL ; - key = NULL ; - val = NULL ; - com = NULL ; - keep_com = 0 ; - numeric = 0 ; + char card_tmp[NM_SIZ]; - /* Command line handling */ - i=1 ; - while (i=argc) { - fprintf(stderr, "option -p needs an argument\n"); - return -1 ; - } - i++ ; - place = strdup(argv[i]); - } else if (!strcmp(argv[i], "-k")) { - if ((i+1)>=argc) { - fprintf(stderr, "option -k needs an argument\n"); - return -1 ; - } - i++ ; - key = strdup(argv[i]); - } else if (!strcmp(argv[i], "-v")) { - if ((i+1)>=argc) { - fprintf(stderr, "option -v needs an argument\n"); - return -1 ; - } - i++ ; - val = strdup(argv[i]); - } else if (!strcmp(argv[i], "-c")) { - if ((i+1)>=argc) { - fprintf(stderr, "option -c needs an argument\n"); - return -1 ; - } - i++ ; - com = strdup(argv[i]); - } else if (!strcmp(argv[i], "-C")) { - keep_com = 1 ; - } else if (!strcmp(argv[i], "-n")) { - numeric = 1 ; - } else { - break ; - } - i++ ; - } - - /* Check options coherence */ - if ((keep_com == 1) && (com != NULL)) { - fprintf(stderr, "options -c and -C should not be used together\n") ; - if (place) free(place) ; - if (key) free(key) ; - if (val) free(val) ; - free(com) ; - return -1 ; - } - if (place == NULL) { - fprintf(stderr, "options -p has to be used\n") ; - if (key) free(key) ; - if (val) free(val) ; - if (com) free(com) ; - return -1 ; + if (argc < 2) + usage (argv[0]); + + /* Initialize */ + name_in = NULL; + card = NULL; + place = NULL; + key = NULL; + val = NULL; + com = NULL; + keep_com = 0; + numeric = 0; + + /* Command line handling */ + i = 1; + while (i < argc) + { + if (!strcmp (argv[i], "--help") || !strcmp (argv[i], "-h")) + { + usage (argv[0]); + } + else if (!strcmp (argv[i], "-p")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -p needs an argument\n"); + return -1; + } + i++; + place = strdup (argv[i]); + } + else if (!strcmp (argv[i], "-k")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -k needs an argument\n"); + return -1; + } + i++; + key = strdup (argv[i]); + } + else if (!strcmp (argv[i], "-v")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -v needs an argument\n"); + return -1; + } + i++; + val = strdup (argv[i]); + } + else if (!strcmp (argv[i], "-c")) + { + if ((i + 1) >= argc) + { + fprintf (stderr, "option -c needs an argument\n"); + return -1; + } + i++; + com = strdup (argv[i]); + } + else if (!strcmp (argv[i], "-C")) + { + keep_com = 1; + } + else if (!strcmp (argv[i], "-n")) + { + numeric = 1; + } + else + { + break; + } + i++; } - /* Get input file name */ - if ((argc-i)<1) { - fprintf(stderr, "missing input file name\n"); - return -1 ; + /* Check options coherence */ + if ((keep_com == 1) && (com != NULL)) + { + fprintf (stderr, "options -c and -C should not be used together\n"); + if (place) + free (place); + if (key) + free (key); + if (val) + free (val); + free (com); + return -1; } - - /* Loop on the input files */ - while (argc-i >= 1) { - name_in = strdup(argv[i]) ; - - /* Set keyword to write */ - key_tmp = NULL ; - if (key==NULL) key_tmp = strdup(place) ; - else key_tmp = strdup(key) ; - - /* Set value to write */ - val_tmp = NULL ; - if (val==NULL) { - card = qfits_query_card(name_in, place) ; - if (card!= NULL) { - stmp = qfits_getvalue(card) ; - val_tmp = strdup(stmp) ; - } - } else val_tmp = strdup(val) ; - - com_tmp = NULL ; - /* Set comment to write */ - if ((com == NULL) && (keep_com == 1)) { - if (card == NULL) card = qfits_query_card(name_in, place) ; - if (card != NULL) { - stmp = qfits_getcomment(card) ; - com_tmp = strdup(stmp) ; - } - } else if (com != NULL) com_tmp = strdup(com) ; - if (card != NULL) free(card) ; - card = NULL ; - printf("DEBUG: %s\n", key_tmp) ; - - /* Create the card */ - keytuple2str(card_tmp, key_tmp, val_tmp, com_tmp) ; - if (key_tmp) free(key_tmp) ; - if (val_tmp) free(val_tmp) ; - if (com_tmp) free(com_tmp) ; - card = strdup(card_tmp) ; - - /* Display what will be written where */ - printf("File %s\n", name_in) ; - printf("\tcard : \n\t\t%s\n", card) ; - printf("\tplace : \n\t\t%s\n", place) ; - - /* Try to replace the first key */ - if (qfits_replace_card(name_in, place, card) == -1) { - fprintf(stderr, "cannot replace the key %s\n", place) ; - } - free(name_in); - free(card) ; - card = NULL ; - i++ ; + if (place == NULL) + { + fprintf (stderr, "options -p has to be used\n"); + if (key) + free (key); + if (val) + free (val); + if (com) + free (com); + return -1; } - - if (val) free(val) ; - if (com) free(com) ; - if (key) free(key) ; - free(place) ; - /* Free and return */ - return 0 ; + + /* Get input file name */ + if ((argc - i) < 1) + { + fprintf (stderr, "missing input file name\n"); + return -1; + } + + /* Loop on the input files */ + while (argc - i >= 1) + { + name_in = strdup (argv[i]); + + /* Set keyword to write */ + key_tmp = NULL; + if (key == NULL) + key_tmp = strdup (place); + else + key_tmp = strdup (key); + + /* Set value to write */ + val_tmp = NULL; + if (val == NULL) + { + card = qfits_query_card (name_in, place); + if (card != NULL) + { + stmp = qfits_getvalue (card); + val_tmp = strdup (stmp); + } + } + else + val_tmp = strdup (val); + + com_tmp = NULL; + /* Set comment to write */ + if ((com == NULL) && (keep_com == 1)) + { + if (card == NULL) + card = qfits_query_card (name_in, place); + if (card != NULL) + { + stmp = qfits_getcomment (card); + com_tmp = strdup (stmp); + } + } + else if (com != NULL) + com_tmp = strdup (com); + if (card != NULL) + free (card); + card = NULL; + printf ("DEBUG: %s\n", key_tmp); + + /* Create the card */ + keytuple2str (card_tmp, key_tmp, val_tmp, com_tmp); + if (key_tmp) + free (key_tmp); + if (val_tmp) + free (val_tmp); + if (com_tmp) + free (com_tmp); + card = strdup (card_tmp); + + /* Display what will be written where */ + printf ("File %s\n", name_in); + printf ("\tcard : \n\t\t%s\n", card); + printf ("\tplace : \n\t\t%s\n", place); + + /* Try to replace the first key */ + if (qfits_replace_card (name_in, place, card) == -1) + { + fprintf (stderr, "cannot replace the key %s\n", place); + } + free (name_in); + free (card); + card = NULL; + i++; + } + + if (val) + free (val); + if (com) + free (com); + if (key) + free (key); + free (place); + /* Free and return */ + return 0; } -static void usage(char * pname) +static void +usage (char *pname) { - printf("%s : %s\n", pname, prog_desc) ; - printf( - "use : %s [options] \n" - "options are:\n" - "\t-p place gives the keyword to write over (required).\n" - "\t-k key gives the new keyword name (optional).\n" - "\t-v val gives the value to write (optional).\n" - "\t-c com gives the comment to write (optional).\n" - "\t-C flag to keep comment\n" - "\n", pname) ; - exit(0) ; + printf ("%s : %s\n", pname, prog_desc); + printf ("use : %s [options] \n" + "options are:\n" + "\t-p place gives the keyword to write over (required).\n" + "\t-k key gives the new keyword name (optional).\n" + "\t-v val gives the value to write (optional).\n" + "\t-c com gives the comment to write (optional).\n" + "\t-C flag to keep comment\n" "\n", pname); + exit (0); } - diff --git a/qfits/saft/fitsmd5.c b/qfits/saft/fitsmd5.c index ff0e36f..c90cb58 100644 --- a/qfits/saft/fitsmd5.c +++ b/qfits/saft/fitsmd5.c @@ -45,187 +45,203 @@ #define GZIP_MAGIC1 0x1f #define GZIP_MAGIC2 0x8b -static is_gzipped(char * filename) +static +is_gzipped (char *filename) { - FILE * in ; - unsigned char b1, b2 ; - int r1, r2 ; + FILE *in; + unsigned char b1, b2; + int r1, r2; - if ((in=fopen(filename, "r"))==NULL) { - return -1 ; + if ((in = fopen (filename, "r")) == NULL) + { + return -1; } - r1 = fread(&b1, 1, 1, in); - r2 = fread(&b2, 1, 1, in); - fclose(in); - if (r1!=1 || r2!=1) - return 0 ; - if ((int)b1!=GZIP_MAGIC1 || (int)b2!=GZIP_MAGIC2) { - return 0 ; + r1 = fread (&b1, 1, 1, in); + r2 = fread (&b2, 1, 1, in); + fclose (in); + if (r1 != 1 || r2 != 1) + return 0; + if ((int) b1 != GZIP_MAGIC1 || (int) b2 != GZIP_MAGIC2) + { + return 0; } - return 1 ; + return 1; } -#endif +#endif /*----------------------------------------------------------------------------- Define -----------------------------------------------------------------------------*/ /* Definitions related to FITS */ -#define FITSLINESZ 80 /* a FITS line is 80 chars */ -#define FITSCARDS 36 /* 36 cards per block */ +#define FITSLINESZ 80 /* a FITS line is 80 chars */ +#define FITSCARDS 36 /* 36 cards per block */ #define FITSBLOCKSZ (FITSLINESZ*FITSCARDS) /* FITS block size=2880 */ /* Definitions related to MD5 */ -#define MD5HASHSZ 32 /* an MD5 key length is 32 bytes = 128 bits */ +#define MD5HASHSZ 32 /* an MD5 key length is 32 bytes = 128 bits */ /* FITS keyword used to store MD5 key */ #define FITSMD5KEY "DATAMD5 " - + /*----------------------------------------------------------------------------- New types -----------------------------------------------------------------------------*/ /* The following types defined for MD5 computation only */ -typedef unsigned int word32 ; +typedef unsigned int word32; -struct MD5Context { - word32 buf[4]; - word32 bits[2]; - unsigned char in[64]; +struct MD5Context +{ + word32 buf[4]; + word32 bits[2]; + unsigned char in[64]; }; /*----------------------------------------------------------------------------- Private function prototypes -----------------------------------------------------------------------------*/ -static void MD5Init(struct MD5Context *); -static void MD5Update(struct MD5Context *, unsigned char *, unsigned); -static void MD5Final(unsigned char *, struct MD5Context *); -static void MD5Transform(word32 *, word32 *); -static void byteReverse(unsigned char *, unsigned); +static void MD5Init (struct MD5Context *); +static void MD5Update (struct MD5Context *, unsigned char *, unsigned); +static void MD5Final (unsigned char *, struct MD5Context *); +static void MD5Transform (word32 *, word32 *); +static void byteReverse (unsigned char *, unsigned); -static int fits_md5_check(char *, int); -static char * fits_pretty_string(char *); -static char * fits_getvalue(char *); +static int fits_md5_check (char *, int); +static char *fits_pretty_string (char *); +static char *fits_getvalue (char *); -static void usage(void); +static void usage (void); /*----------------------------------------------------------------------------- Global variables -----------------------------------------------------------------------------*/ -static char * pname = NULL ; -static char prog_desc[] = "Compute/Update the DATAMD5 keyword/value" ; -static int silent_process=0 ; +static char *pname = NULL; +static char prog_desc[] = "Compute/Update the DATAMD5 keyword/value"; +static int silent_process = 0; /*----------------------------------------------------------------------------- MD5 function code -----------------------------------------------------------------------------*/ /* Reverse bytes in a 32-bit word. Harmless on little endian machines */ -static void byteReverse(unsigned char *buf, unsigned longs) +static void +byteReverse (unsigned char *buf, unsigned longs) { - word32 t; - do { - t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(word32 *) buf = t; - buf += 4; - } while (--longs); + word32 t; + do + { + t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + *(word32 *) buf = t; + buf += 4; + } + while (--longs); } /* Start MD5 accumulation. Set bit count to 0 and buffer to MD5 init. const. */ -static void MD5Init(struct MD5Context *ctx) +static void +MD5Init (struct MD5Context *ctx) { - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; - ctx->bits[0] = 0; - ctx->bits[1] = 0; + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; + ctx->bits[0] = 0; + ctx->bits[1] = 0; } /* Update to reflect the concatenation of another buffer full of bytes. */ -static void MD5Update(struct MD5Context *ctx, unsigned char *buf, unsigned len) +static void +MD5Update (struct MD5Context *ctx, unsigned char *buf, unsigned len) { - register word32 t; + register word32 t; - /* Update bitcount */ - t = ctx->bits[0]; - if ((ctx->bits[0] = t + ((word32) len << 3)) < t) - ctx->bits[1]++; /* Carry from low to high */ - ctx->bits[1] += len >> 29; + /* Update bitcount */ + t = ctx->bits[0]; + if ((ctx->bits[0] = t + ((word32) len << 3)) < t) + ctx->bits[1]++; /* Carry from low to high */ + ctx->bits[1] += len >> 29; - t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ + t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ - /* Handle any leading odd-sized chunks */ - if (t) { - unsigned char *p = (unsigned char *) ctx->in + t; - t = 64 - t; - if (len < t) { - memmove(p, buf, len); - return; - } - memmove(p, buf, t); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); - buf += t; - len -= t; + /* Handle any leading odd-sized chunks */ + if (t) + { + unsigned char *p = (unsigned char *) ctx->in + t; + t = 64 - t; + if (len < t) + { + memmove (p, buf, len); + return; + } + memmove (p, buf, t); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); + buf += t; + len -= t; } - /* Process data in 64-byte chunks */ - while (len >= 64) { - memmove(ctx->in, buf, 64); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); - buf += 64; - len -= 64; + /* Process data in 64-byte chunks */ + while (len >= 64) + { + memmove (ctx->in, buf, 64); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); + buf += 64; + len -= 64; } - /* Handle any remaining bytes of data. */ - memmove(ctx->in, buf, len); + /* Handle any remaining bytes of data. */ + memmove (ctx->in, buf, len); } /* Final wrapup - pad to 64-byte boundary with the bit pattern 1 0* */ /* (64-bit count of bits processed, MSB-first) */ -static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) +static void +MD5Final (unsigned char digest[16], struct MD5Context *ctx) { - unsigned int count ; - unsigned char * p ; + unsigned int count; + unsigned char *p; - /* Compute number of bytes mod 64 */ - count = (ctx->bits[0] >> 3) & 0x3F; + /* Compute number of bytes mod 64 */ + count = (ctx->bits[0] >> 3) & 0x3F; - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = ctx->in + count; - *p++ = 0x80; + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + p = ctx->in + count; + *p++ = 0x80; - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; + /* Bytes of padding needed to make 64 bytes */ + count = 64 - 1 - count; - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(p, 0, count); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); + /* Pad out to 56 mod 64 */ + if (count < 8) + { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset (p, 0, count); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); - /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset(p, 0, count - 8); + /* Now fill the next block with 56 bytes */ + memset (ctx->in, 0, 56); } - byteReverse(ctx->in, 14); + else + { + /* Pad block to 56 bytes */ + memset (p, 0, count - 8); + } + byteReverse (ctx->in, 14); - /* Append length in bits and transform */ - ((word32 *) ctx->in)[14] = ctx->bits[0]; - ((word32 *) ctx->in)[15] = ctx->bits[1]; + /* Append length in bits and transform */ + ((word32 *) ctx->in)[14] = ctx->bits[0]; + ((word32 *) ctx->in)[15] = ctx->bits[1]; - MD5Transform(ctx->buf, (word32 *) ctx->in); - byteReverse((unsigned char *) ctx->buf, 4); - memmove(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ + MD5Transform (ctx->buf, (word32 *) ctx->in); + byteReverse ((unsigned char *) ctx->buf, 4); + memmove (digest, ctx->buf, 16); + memset (ctx, 0, sizeof (ctx)); /* In case it's sensitive */ } /* The four core functions - F1 is optimized somewhat */ @@ -245,87 +261,88 @@ static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ -static void MD5Transform(word32 buf[4], word32 in[16]) +static void +MD5Transform (word32 buf[4], word32 in[16]) { - register word32 a, b, c, d; + register word32 a, b, c, d; - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; - MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); + MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7); + MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12); + MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17); + MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22); + MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7); + MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12); + MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17); + MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22); + MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7); + MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12); + MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17); + MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22); + MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7); + MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12); + MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17); + MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22); - MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); + MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5); + MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9); + MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14); + MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); + MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5); + MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9); + MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14); + MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); + MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5); + MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9); + MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14); + MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20); + MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5); + MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); + MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14); + MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); - MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); + MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4); + MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11); + MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16); + MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23); + MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4); + MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); + MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); + MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23); + MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4); + MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11); + MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16); + MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23); + MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4); + MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11); + MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); + MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23); - MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); + MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6); + MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10); + MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15); + MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21); + MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6); + MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); + MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15); + MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21); + MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); + MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); + MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15); + MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21); + MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6); + MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10); + MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); + MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21); - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; } @@ -334,457 +351,505 @@ static void MD5Transform(word32 buf[4], word32 in[16]) -----------------------------------------------------------------------------*/ /* Pretty-print a FITS string value */ -static char * fits_pretty_string(char * s) +static char * +fits_pretty_string (char *s) { - static char pretty[FITSLINESZ+1] ; - int i,j ; + static char pretty[FITSLINESZ + 1]; + int i, j; - if (s==NULL) return NULL ; + if (s == NULL) + return NULL; - pretty[0] = (char)0 ; - if (s[0]!='\'') return s ; + pretty[0] = (char) 0; + if (s[0] != '\'') + return s; - /* skip first quote */ - i=1 ; - j=0 ; - /* trim left-side blanks */ - while (s[i]==' ') { - if (i==(int)strlen(s)) break ; - i++ ; + /* skip first quote */ + i = 1; + j = 0; + /* trim left-side blanks */ + while (s[i] == ' ') + { + if (i == (int) strlen (s)) + break; + i++; } - if (i>=(int)(strlen(s)-1)) return pretty ; - /* copy string, changing double quotes to single ones */ - while (i<(int)strlen(s)) { - if (s[i]=='\'') i++ ; - pretty[j]=s[i]; - i++ ; - j++ ; - } - /* NULL-terminate the pretty string */ - pretty[j+1]=(char)0; - /* trim right-side blanks */ - j = (int)strlen(pretty)-1; - while (pretty[j]==' ') j-- ; - pretty[j+1]=(char)0; + if (i >= (int) (strlen (s) - 1)) return pretty; + /* copy string, changing double quotes to single ones */ + while (i < (int) strlen (s)) + { + if (s[i] == '\'') + i++; + pretty[j] = s[i]; + i++; + j++; + } + /* NULL-terminate the pretty string */ + pretty[j + 1] = (char) 0; + /* trim right-side blanks */ + j = (int) strlen (pretty) - 1; + while (pretty[j] == ' ') + j--; + pretty[j + 1] = (char) 0; + return pretty; } /* Get the FITS value in a FITS card */ -static char * fits_getvalue(char * line) +static char * +fits_getvalue (char *line) { - static char value[FITSLINESZ+1] ; - int from, to ; - int inq ; - int i ; + static char value[FITSLINESZ + 1]; + int from, to; + int inq; + int i; - if (line==NULL) return NULL ; - memset(value, 0, FITSLINESZ+1); - /* Get past the keyword */ - i=0 ; - while (line[i]!='=' && iFITSLINESZ) return NULL ; - i++ ; - while (line[i]==' ' && iFITSLINESZ) return NULL ; - from=i; - /* Now in the value section */ - /* Look for the first slash '/' outside of a string */ - inq = 0 ; - while (i FITSLINESZ) + return NULL; + i++; + while (line[i] == ' ' && i < FITSLINESZ) + i++; + if (i > FITSLINESZ) + return NULL; + from = i; + /* Now in the value section */ + /* Look for the first slash '/' outside of a string */ + inq = 0; + while (i < FITSLINESZ) + { + if (line[i] == '\'') + inq = !inq; + if (line[i] == '/') + if (!inq) + break; + i++; } - i-- ; - /* Backtrack on blanks */ - while (line[i]==' ' && i>=0) i-- ; - if (i<0) return NULL ; - to=i ; - if (to= 0) + i--; + if (i < 0) + return NULL; + to = i; + if (to < from) + 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; + /* + * Make it pretty: remove head and tail quote, change double + * quotes to simple ones. + */ + strcpy (value, fits_pretty_string (value)); + return value; } /* Replace the MD5 card in the input header */ -static int fits_replace_card(char * filename, int off_md5, char * datamd5) +static int +fits_replace_card (char *filename, int off_md5, char *datamd5) { - char * buf ; - int fd ; - struct stat sta ; - char card[FITSLINESZ]; - int i ; - int err ; + char *buf; + int fd; + struct stat sta; + char card[FITSLINESZ]; + int i; + int err; - /* Get file size */ - if (stat(filename, &sta)==-1) { - fprintf(stderr, "%s: cannot stat file [%s]: no update done\n", - pname, - filename); - return 1 ; - } - /* Open file */ - fd = open(filename, O_RDWR); - if (fd==-1) { - fprintf(stderr, - "%s: cannot open file [%s] for modification: no update done\n", - pname, - filename); - return 1 ; - } - /* Memory-map the file */ - buf = (char*)mmap(0, - sta.st_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, - 0); - if (buf==(char*)-1 || buf==NULL) { - perror("mmap"); - close(fd); - return 1 ; - } - /* sprintf should be safe, the MD5 signature size is 32 chars */ - sprintf(card, "%s= '%s' / data MD5 signature", FITSMD5KEY, datamd5); - i=FITSLINESZ-1 ; - while (card[i]!='e') { - card[i]=' '; - i-- ; - } - /* Copy card into file */ - memcpy(buf+off_md5, card, FITSLINESZ); - /* flush output, unmap buffer, close file and quit */ - err=0 ; - sync(); - if (close(fd)==-1) { - fprintf(stderr, "%s: error closing modified file [%s]", - pname, - filename); - err++ ; - } - if (munmap(buf, sta.st_size)==-1) { - perror("munmap"); - err++ ; - } - return err; + /* Get file size */ + if (stat (filename, &sta) == -1) + { + fprintf (stderr, "%s: cannot stat file [%s]: no update done\n", + pname, filename); + return 1; + } + /* Open file */ + fd = open (filename, O_RDWR); + if (fd == -1) + { + fprintf (stderr, + "%s: cannot open file [%s] for modification: no update done\n", + pname, filename); + return 1; + } + /* Memory-map the file */ + buf = (char *) mmap (0, + sta.st_size, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buf == (char *) -1 || buf == NULL) + { + perror ("mmap"); + close (fd); + return 1; + } + /* sprintf should be safe, the MD5 signature size is 32 chars */ + sprintf (card, "%s= '%s' / data MD5 signature", FITSMD5KEY, datamd5); + i = FITSLINESZ - 1; + while (card[i] != 'e') + { + card[i] = ' '; + i--; + } + /* Copy card into file */ + memcpy (buf + off_md5, card, FITSLINESZ); + /* flush output, unmap buffer, close file and quit */ + err = 0; + sync (); + if (close (fd) == -1) + { + fprintf (stderr, "%s: error closing modified file [%s]", + pname, filename); + err++; + } + if (munmap (buf, sta.st_size) == -1) + { + perror ("munmap"); + err++; + } + return err; } /* Display or modify the DATAMD5 value. Returns the number of errors. */ -static int fits_md5_check(char * filename, int update_header) +static int +fits_md5_check (char *filename, int update_header) { - FILE * in ; - char buf[FITSBLOCKSZ]; - char * buf_c ; - int i ; - int in_header ; - char * hdrmd5 ; - struct MD5Context ctx ; - unsigned char digest[16] ; - static char datamd5[MD5HASHSZ+1]; - int off_md5 ; - int cur_off ; - int md5keysz ; - int err ; - int check_fits ; - struct stat sta ; + FILE *in; + char buf[FITSBLOCKSZ]; + char *buf_c; + int i; + int in_header; + char *hdrmd5; + struct MD5Context ctx; + unsigned char digest[16]; + static char datamd5[MD5HASHSZ + 1]; + int off_md5; + int cur_off; + int md5keysz; + int err; + int check_fits; + struct stat sta; - if (filename==NULL) return 1 ; + if (filename == NULL) + return 1; - /* Try to stat file */ - if (stat(filename, &sta)!=0) { - fprintf(stderr, "%s: cannot stat file %s\n", pname, filename); - return 1 ; + /* Try to stat file */ + if (stat (filename, &sta) != 0) + { + fprintf (stderr, "%s: cannot stat file %s\n", pname, filename); + return 1; + } + /* See if this is a regular file */ + if (!S_ISREG (sta.st_mode)) + { + fprintf (stderr, "%s: not a regular file: %s\n", pname, filename); + return 1; + } + /* Open input file */ + if ((in = fopen (filename, "r")) == NULL) + { + fprintf (stderr, "%s: cannot open file [%s]\n", pname, filename); + return 1; + } + /* Initialize all variables */ + MD5Init (&ctx); + in_header = 1; + hdrmd5 = NULL; + off_md5 = 0; + cur_off = 0; + md5keysz = (int) strlen (FITSMD5KEY); + check_fits = 0; + /* Loop over input file */ + while (fread (buf, 1, FITSBLOCKSZ, in) == FITSBLOCKSZ) + { + /* First time in the loop: check the file is FITS */ + if (check_fits == 0) + { + /* Examine first characters in block */ + if (buf[0] != 'S' || + buf[1] != 'I' || + buf[2] != 'M' || + buf[3] != 'P' || + buf[4] != 'L' || + buf[5] != 'E' || + buf[6] != ' ' || buf[7] != ' ' || buf[8] != '=') + { + fprintf (stderr, "%s: file [%s] is not FITS\n", + pname, filename); + fclose (in); + return 1; + } + else + { + check_fits = 1; + } } - /* See if this is a regular file */ - if (!S_ISREG(sta.st_mode)) { - fprintf(stderr, "%s: not a regular file: %s\n", pname, filename); - return 1 ; - } - /* Open input file */ - if ((in=fopen(filename, "r"))==NULL) { - fprintf(stderr, "%s: cannot open file [%s]\n", pname, filename); - return 1 ; - } - /* Initialize all variables */ - MD5Init(&ctx); - in_header=1 ; - hdrmd5=NULL ; - off_md5=0; - cur_off=0; - md5keysz = (int)strlen(FITSMD5KEY) ; - check_fits=0 ; - /* Loop over input file */ - while (fread(buf, 1, FITSBLOCKSZ, in)==FITSBLOCKSZ) { - /* First time in the loop: check the file is FITS */ - if (check_fits==0) { - /* Examine first characters in block */ - if (buf[0]!='S' || - buf[1]!='I' || - buf[2]!='M' || - buf[3]!='P' || - buf[4]!='L' || - buf[5]!='E' || - buf[6]!=' ' || - buf[7]!=' ' || - buf[8]!='=') { - fprintf(stderr, "%s: file [%s] is not FITS\n", - pname, - filename); - fclose(in); - return 1 ; - } else { - check_fits=1 ; - } + /* If current block is a header block */ + if (in_header) + { + buf_c = buf; + for (i = 0; i < FITSCARDS; i++) + { + /* Try to locate MD5 keyword if not located already */ + if (hdrmd5 == NULL) + { + if (!strncmp (buf_c, FITSMD5KEY, md5keysz)) + { + hdrmd5 = fits_getvalue (buf_c); + off_md5 = cur_off; + } } - /* If current block is a header block */ - if (in_header) { - buf_c = buf ; - for (i=0 ; i\n" - "options are:\n" - "\t-u update MD5 keyword in the file: %s\n" - "\t-s silent mode\n" - "\n" - "\t-a compute MD5 sum of the complete file (incl.header)\n" - "\n" - "This utility computes the MD5 checksum of all data sections\n" - "in a given FITS file, and compares it against the value\n" - "declared in DATAMD5 if present. It can also update the value\n" - "of this keyword (if present) with its own computed MD5 sum.\n" - "\n", pname, FITSMD5KEY) ; - printf( - "You can also use it with the -a option to compute the MD5 sum\n" - "on the complete file (all bits). In this case, the file needs\n" - "not be FITS. This option is only provided to check this program\n" - "against other MD5 computation tools.\n" - "NB: Other options cannot be used together with -a.\n" - "\n"); + printf ("%s -- %s\n" "version: $Revision: 1.5 $\n", pname, prog_desc); + printf ("\n" + "use : %s [-u] [-s] [-a] \n" + "options are:\n" + "\t-u update MD5 keyword in the file: %s\n" + "\t-s silent mode\n" + "\n" + "\t-a compute MD5 sum of the complete file (incl.header)\n" + "\n" + "This utility computes the MD5 checksum of all data sections\n" + "in a given FITS file, and compares it against the value\n" + "declared in DATAMD5 if present. It can also update the value\n" + "of this keyword (if present) with its own computed MD5 sum.\n" + "\n", pname, FITSMD5KEY); + printf ("You can also use it with the -a option to compute the MD5 sum\n" + "on the complete file (all bits). In this case, the file needs\n" + "not be FITS. This option is only provided to check this program\n" + "against other MD5 computation tools.\n" + "NB: Other options cannot be used together with -a.\n" "\n"); #if HAVE_ZLIB - printf( - "\n" - "This program was compiled against zlib %s\n" - "which allows to process gzipped FITS files\n" - "as if they were normal FITS files.\n" - "Notice that you cannot use the -u option on\n" - "gzipped files, though.\n" - "\n" - "\n", - ZLIB_VERSION - ); + printf ("\n" + "This program was compiled against zlib %s\n" + "which allows to process gzipped FITS files\n" + "as if they were normal FITS files.\n" + "Notice that you cannot use the -u option on\n" + "gzipped files, though.\n" "\n" "\n", ZLIB_VERSION); #endif - exit(0) ; + exit (0); } /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - int i ; - int update_header ; - int total_md5 ; - int err ; + int i; + int update_header; + int total_md5; + int err; - /* Initialize */ - pname=argv[0]; - update_header = 0 ; - total_md5 = 0 ; + /* Initialize */ + pname = argv[0]; + update_header = 0; + total_md5 = 0; - if (argc<2) usage(); - - /* Parse arguments for options */ - for (i=1 ; i0) { - fprintf(stderr, "%s: %d error(s) during process\n", pname, err); + else if (!strcmp (argv[i], "-a")) + { + total_md5 = 1; } - return err ; + } + /* Loop on input file names */ + err = 0; + for (i = 1; i < argc; i++) + { + /* If not a command-line option */ + if (strcmp (argv[i], "-u") && + strcmp (argv[i], "-s") && strcmp (argv[i], "-a")) + { + /* Launch MD5 process on this file */ + if (total_md5) + { + err += compute_md5 (argv[i]); + } + else + { + err += fits_md5_check (argv[i], update_header); + } + } + } + if (err > 0) + { + fprintf (stderr, "%s: %d error(s) during process\n", pname, err); + } + return err; } diff --git a/qfits/saft/fitsort.c b/qfits/saft/fitsort.c index 1a58ce0..55e2d7a 100644 --- a/qfits/saft/fitsort.c +++ b/qfits/saft/fitsort.c @@ -52,8 +52,8 @@ Define -----------------------------------------------------------------------------*/ -#define MAX_STRING 128 -#define MAX_KEY 512 +#define MAX_STRING 128 +#define MAX_KEY 512 #define FMT_STRING "%%-%ds\t" /*----------------------------------------------------------------------------- @@ -61,147 +61,178 @@ -----------------------------------------------------------------------------*/ /* This holds a keyword value and a flag to indicate its presence */ -typedef struct _KEYWORD_ { - char value[MAX_STRING] ; - int present ; -} keyword ; +typedef struct _KEYWORD_ +{ + char value[MAX_STRING]; + int present; +} keyword; /* Each detected file in input has such an associated structure */ -typedef struct _RECORD_ { - char filename[MAX_STRING] ; - keyword listkw[MAX_KEY] ; -} record ; +typedef struct _RECORD_ +{ + char filename[MAX_STRING]; + keyword listkw[MAX_KEY]; +} record; /*----------------------------------------------------------------------------- Function prototypes -----------------------------------------------------------------------------*/ -static int isfilename(char *string) ; -static void getfilename(char *line, char *word) ; -static char * expand_hierarch_keyword(char *, char *) ; -static int isdetectedkeyword(char *line, char *keywords[], int nkeys) ; -static void getkeywordvalue(char *line, char *word) ; +static int isfilename (char *string); +static void getfilename (char *line, char *word); +static char *expand_hierarch_keyword (char *, char *); +static int isdetectedkeyword (char *line, char *keywords[], int nkeys); +static void getkeywordvalue (char *line, char *word); /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - char curline[MAX_STRING] ; - char word[MAX_STRING] ; - int i, j ; - int nfiles ; - record *allrecords ; - int kwnum ; - int len ; - int max_width[MAX_KEY] ; - int max_filnam ; - char fmt[8] ; - int flag ; - int printnames ; - int print_hdr ; + char curline[MAX_STRING]; + char word[MAX_STRING]; + int i, j; + int nfiles; + record *allrecords; + int kwnum; + int len; + int max_width[MAX_KEY]; + int max_filnam; + char fmt[8]; + int flag; + int printnames; + int print_hdr; - if (argc<2) { - printf("\n\nuse : %s [-d] KEY1 KEY2 ... KEYn\n", argv[0]) ; - printf("Input data is received from stdin\n") ; - printf("See man page for more details and examples\n\n") ; - return 0 ; + if (argc < 2) + { + printf ("\n\nuse : %s [-d] KEY1 KEY2 ... KEYn\n", argv[0]); + printf ("Input data is received from stdin\n"); + printf ("See man page for more details and examples\n\n"); + return 0; } - /* Initialize */ - printnames = 0 ; - print_hdr = 1 ; - nfiles = 0 ; - allrecords = (record*)calloc(1, sizeof(record)); - if (!strcmp(argv[1], "-d")) { - print_hdr = 0; - argv++ ; - argc-- ; - } - argv++ ; - - /* Uppercase all inputs */ - for (i=0 ; i<(argc-1) ; i++) { - j=0 ; - while (argv[i][j]!=0) { - argv[i][j] = toupper(argv[i][j]); - j++ ; - } - } - - while (fgets(curline, MAX_STRING, stdin) != (char*)NULL) { - flag=isfilename(curline) ; - if (flag == 1) { - /* New file name is detected, get the new file name */ - printnames = 1 ; - getfilename(curline, allrecords[nfiles].filename) ; - nfiles++ ; - - /* Initialize a new record structure to store data for this file. */ - allrecords = (record*)realloc(allrecords,(nfiles+1)*sizeof(record)); - for (i=0 ; i0) { - /* It has been detected as a searched keyword. */ - /* Get its value, store it, present flag up */ - getkeywordvalue(curline, word) ; - strcpy(allrecords[nfiles-1].listkw[kwnum].value, word) ; - allrecords[nfiles-1].listkw[kwnum].present ++ ; - } - } - } + /* Initialize */ + printnames = 0; + print_hdr = 1; + nfiles = 0; + allrecords = (record *) calloc (1, sizeof (record)); + if (!strcmp (argv[1], "-d")) + { + print_hdr = 0; + argv++; + argc--; } - for (i=0 ; imax_filnam) max_filnam=len ; - for (kwnum=0 ; kwnummax_width[kwnum]) max_width[kwnum] = len ; - } + /* Uppercase all inputs */ + for (i = 0; i < (argc - 1); i++) + { + j = 0; + while (argv[i][j] != 0) + { + argv[i][j] = toupper (argv[i][j]); + j++; } - - /* Print out header line */ - if (print_hdr) { - sprintf(fmt, FMT_STRING, max_filnam) ; - if (printnames) printf(fmt, "FILE"); - for (i=0 ; i 0) + { + /* It has been detected as a searched keyword. */ + /* Get its value, store it, present flag up */ + getkeywordvalue (curline, word); + strcpy (allrecords[nfiles - 1].listkw[kwnum].value, word); + allrecords[nfiles - 1].listkw[kwnum].present++; + } + } + } + } + for (i = 0; i < argc - 1; i++) + max_width[i] = (int) strlen (argv[i]); + + /* Record the maximum width for each column */ + max_filnam = 0; + for (i = 0; i < nfiles; i++) + { + len = (int) strlen (allrecords[i].filename); + if (len > max_filnam) + max_filnam = len; + for (kwnum = 0; kwnum < argc - 1; kwnum++) + { + if (allrecords[i].listkw[kwnum].present) + { + len = (int) strlen (allrecords[i].listkw[kwnum].value); + } + else + { + len = 0; + } + if (len > max_width[kwnum]) + max_width[kwnum] = len; + } + } + + /* Print out header line */ + if (print_hdr) + { + sprintf (fmt, FMT_STRING, max_filnam); + if (printnames) + printf (fmt, "FILE"); + for (i = 0; i < argc - 1; i++) + { + sprintf (fmt, FMT_STRING, max_width[i]); + printf (fmt, argv[i]); + } + printf ("\n"); + } + + /* Now print out stored data */ + if (nfiles < 1) + { + printf ("*** error: no input data corresponding to dfits output\n"); + return -1; + } + for (i = 0; i < nfiles; i++) + { + if (printnames) + { + sprintf (fmt, FMT_STRING, max_filnam); + printf (fmt, allrecords[i].filename); + } + for (kwnum = 0; kwnum < argc - 1; kwnum++) + { + sprintf (fmt, FMT_STRING, max_width[kwnum]); + if (allrecords[i].listkw[kwnum].present) + printf (fmt, allrecords[i].listkw[kwnum].value); + else + printf (fmt, " "); + } + printf ("\n"); + } + free (allrecords); + return 0; } /*----------------------------------------------------------------------------- @@ -218,11 +249,14 @@ int main(int argc, char *argv[]) extension, 0 otherwise. */ /*----------------------------------------------------------------------------*/ -static int isfilename(char * string) +static int +isfilename (char *string) { - if (!strncmp(string, "====>", 5)) return 1 ; - if (!strncmp(string, "===>", 4)) return 2 ; - return 0 ; + if (!strncmp (string, "====>", 5)) + return 1; + if (!strncmp (string, "===>", 4)) + return 2; + return 0; } /*----------------------------------------------------------------------------*/ @@ -234,13 +268,14 @@ static int isfilename(char * string) This is dfits dependent. */ /*----------------------------------------------------------------------------*/ -static void getfilename(char * line, char * word) +static void +getfilename (char *line, char *word) { - /* get filename from a dfits output */ - sscanf(line, "%*s %*s %s", word) ; - return ; + /* get filename from a dfits output */ + sscanf (line, "%*s %*s %s", word); + return; } - + /*----------------------------------------------------------------------------*/ /** @brief detects a if a keyword is present in a FITS line @@ -254,41 +289,45 @@ static void getfilename(char * line, char * word) otherwise, -1 is returned. */ /*----------------------------------------------------------------------------*/ -static int isdetectedkeyword( - char * line, - char * keywords[], - int nkeys) +static int +isdetectedkeyword (char *line, char *keywords[], int nkeys) { - char kw[MAX_STRING] ; - char esokw[MAX_STRING] ; - int i ; + char kw[MAX_STRING]; + char esokw[MAX_STRING]; + int i; - /* The keyword is up to the equal character, with trailing blanks removed */ - strcpy(kw, line) ; - strtok(kw, "=") ; - /* Now remove all trailing blanks (if any) */ - i = (int)strlen(kw) -1 ; - while (kw[i] == ' ') i -- ; - kw[i+1] = (char)0 ; - - /* Now compare what we got with what's available */ - for (i=0 ; i=80) search = 0 ; - else if ((line[c] == '/') && (quote == 0)) search = 0 ; - if (line[c] == '\'') quote = !quote ; - tmp[w++] = line[c++] ; + /* Copy the line till the slash '/' sign or the end of data is found. */ + while (search == 1) + { + if (c >= 80) + search = 0; + else if ((line[c] == '/') && (quote == 0)) + search = 0; + if (line[c] == '\'') + quote = !quote; + tmp[w++] = line[c++]; } - - /* NULL termination of the string */ - tmp[--w] = (char)0 ; - /* Return the keyword only : a diff is made between text fields and nbs. */ - if ((begin = strchr(tmp, '\'')) != (char*)NULL) { - /* A quote has been found: it is a string value */ - begin++ ; - end = strrchr(tmp, '\'') ; - length = (int)strlen(begin) - (int)strlen(end) ; - strncpy(word, begin, length) ; - } else { - /* No quote, just get the value (only one, no complex supported) */ - sscanf(tmp, "%s", word) ; + /* NULL termination of the string */ + tmp[--w] = (char) 0; + + /* Return the keyword only : a diff is made between text fields and nbs. */ + if ((begin = strchr (tmp, '\'')) != (char *) NULL) + { + /* A quote has been found: it is a string value */ + begin++; + end = strrchr (tmp, '\''); + length = (int) strlen (begin) - (int) strlen (end); + strncpy (word, begin, length); } - - return ; + else + { + /* No quote, just get the value (only one, no complex supported) */ + sscanf (tmp, "%s", word); + } + + return; } - diff --git a/qfits/saft/hierarch28.c b/qfits/saft/hierarch28.c index 4e22ca7..e384f1d 100644 --- a/qfits/saft/hierarch28.c +++ b/qfits/saft/hierarch28.c @@ -40,136 +40,136 @@ Function prototypes -----------------------------------------------------------------------------*/ -static char prog_desc[] = "header conversion from ESO to standard FITS" ; -static void usage(char *) ; -static int convert_eso_to_std_FITS(char *, char *) ; -static void free_keys(char **, int n) ; -static void strip_beg_end(char *) ; -static int search_and_replace_kw(char *, int, char **, char **, int) ; -static void search_rep(char *, char *, char *); -static void generate_default_convtab(void); -static char * convert_deg_to_str(double deg) ; +static char prog_desc[] = "header conversion from ESO to standard FITS"; +static void usage (char *); +static int convert_eso_to_std_FITS (char *, char *); +static void free_keys (char **, int n); +static void strip_beg_end (char *); +static int search_and_replace_kw (char *, int, char **, char **, int); +static void search_rep (char *, char *, char *); +static void generate_default_convtab (void); +static char *convert_deg_to_str (double deg); /*----------------------------------------------------------------------------- Static variables -----------------------------------------------------------------------------*/ static char CONVTAB_DEFAULT1[] = -"#\n" -"# Example of conversion table for hierarch28\n" -"#\n" -"# A note about this file's format:\n" -"# Any blank line or line starting with a hash is ignored.\n" -"# Declare the keyword names to search and replace with:\n" -"#\n" -"# OLDKEYWORD = NEWKEYWORD\n" -"#\n" -"# Spaces are allowed within keyword names, to allow e.g.:\n" -"#\n" -"# HIERARCH ESO DET NDIT = DET NDIT\n" -"#\n" -"# The most important restriction is that new keywords shall not be\n" -"# longer than the keywords they replace.\n" -"#\n" ; + "#\n" + "# Example of conversion table for hierarch28\n" + "#\n" + "# A note about this file's format:\n" + "# Any blank line or line starting with a hash is ignored.\n" + "# Declare the keyword names to search and replace with:\n" + "#\n" + "# OLDKEYWORD = NEWKEYWORD\n" + "#\n" + "# Spaces are allowed within keyword names, to allow e.g.:\n" + "#\n" + "# HIERARCH ESO DET NDIT = DET NDIT\n" + "#\n" + "# The most important restriction is that new keywords shall not be\n" + "# longer than the keywords they replace.\n" "#\n"; static char CONVTAB_DEFAULT2[] = -"#\n" -"# Translation table for basic keywords used by IRAF\n" -"# -------------------------------------------------\n" -"#\n" -"# Note: hierarch28 will replace keywords in the main header\n" -"# and also in extensions.\n" -"#\n" -"# Disclaimer:\n" -"# this table has been compiled to best knowledge of present\n" -"# IRAF packages. Please let us know of any addition/change.\n" -"#\n" -"\n" ; + "#\n" + "# Translation table for basic keywords used by IRAF\n" + "# -------------------------------------------------\n" + "#\n" + "# Note: hierarch28 will replace keywords in the main header\n" + "# and also in extensions.\n" + "#\n" + "# Disclaimer:\n" + "# this table has been compiled to best knowledge of present\n" + "# IRAF packages. Please let us know of any addition/change.\n" + "#\n" "\n"; static char CONVTAB_DEFAULT3[] = -"UTC = UT\n" -"LST = ST\n" -"RA = RA\n" -"DEC = DEC\n" -"\n" -"HIERARCH ESO TEL AIRM START = AIRMASS\n" -"HIERARCH ESO DPR TYPE = IMAGETYP\n" -"HIERARCH ESO INS FILT1 NAME = FILTER1\n" -"HIERARCH ESO INS SLIT2 NAME = SLIT\n" -"HIERARCH ESO INS GRIS1 NAME = GRISM\n" -"HIERARCH ESO INS GRAT NAME = GRAT\n" -"HIERARCH ESO INS GRAT1 NAME = GRAT1\n" -"HIERARCH ESO INS GRAT2 NAME = GRAT2\n" -"HIERARCH ESO INS GRAT WLEN = WLEN\n" -"HIERARCH ESO INS GRAT1 WLEN = WLEN1\n" -"HIERARCH ESO INS GRAT2 WLEN = WLEN2\n" -"HIERARCH ESO INS GRAT ORDER = ORDER\n" -"\n" ; + "UTC = UT\n" + "LST = ST\n" + "RA = RA\n" + "DEC = DEC\n" + "\n" + "HIERARCH ESO TEL AIRM START = AIRMASS\n" + "HIERARCH ESO DPR TYPE = IMAGETYP\n" + "HIERARCH ESO INS FILT1 NAME = FILTER1\n" + "HIERARCH ESO INS SLIT2 NAME = SLIT\n" + "HIERARCH ESO INS GRIS1 NAME = GRISM\n" + "HIERARCH ESO INS GRAT NAME = GRAT\n" + "HIERARCH ESO INS GRAT1 NAME = GRAT1\n" + "HIERARCH ESO INS GRAT2 NAME = GRAT2\n" + "HIERARCH ESO INS GRAT WLEN = WLEN\n" + "HIERARCH ESO INS GRAT1 WLEN = WLEN1\n" + "HIERARCH ESO INS GRAT2 WLEN = WLEN2\n" + "HIERARCH ESO INS GRAT ORDER = ORDER\n" "\n"; static char CONVTAB_DEFAULT4[] = -"#\n" -"# A note for IRAF users:\n" -"# Be aware also that the ESO convention names the keywords UTC and\n" -"# LST, whereas the IRAF convention is 'UT' and 'ST'.\n" -"#\n" -"# The ESO standard (see http://archive.eso.org/dicb) defines these\n" -"# keywords as floating point values with the units degrees for RA/DEC\n" -"# and elapsed seconds since midnight for UT/ST.\n" -"#\n" -"# In order to have this tranlation performed, add\n" -"# RA = RA\n" -"# DEC = DEC\n" -"# UTC = UT\n" -"# LST = ST\n" -"# to the conversion table.\n" -"#\n"; + "#\n" + "# A note for IRAF users:\n" + "# Be aware also that the ESO convention names the keywords UTC and\n" + "# LST, whereas the IRAF convention is 'UT' and 'ST'.\n" + "#\n" + "# The ESO standard (see http://archive.eso.org/dicb) defines these\n" + "# keywords as floating point values with the units degrees for RA/DEC\n" + "# and elapsed seconds since midnight for UT/ST.\n" + "#\n" + "# In order to have this tranlation performed, add\n" + "# RA = RA\n" + "# DEC = DEC\n" + "# UTC = UT\n" "# LST = ST\n" "# to the conversion table.\n" "#\n"; /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - char name_conv[NM_SIZ] ; - char name_in[NM_SIZ] ; + char name_conv[NM_SIZ]; + char name_in[NM_SIZ]; - if (argc<2) usage(argv[0]) ; - if (!strcmp(argv[1], "-g")) { - generate_default_convtab() ; - return 0 ; - } - - strcpy(name_in, argv[1]) ; - if (argc==3) { - strcpy(name_conv, argv[2]) ; - } else { - strcpy(name_conv, "table.conv") ; + if (argc < 2) + usage (argv[0]); + if (!strcmp (argv[1], "-g")) + { + generate_default_convtab (); + return 0; } - if (convert_eso_to_std_FITS(name_in, name_conv) != 0) { - fprintf(stderr, "error during conversion: aborting\n") ; + strcpy (name_in, argv[1]); + if (argc == 3) + { + strcpy (name_conv, argv[2]); } - return 0 ; + else + { + strcpy (name_conv, "table.conv"); + } + + if (convert_eso_to_std_FITS (name_in, name_conv) != 0) + { + fprintf (stderr, "error during conversion: aborting\n"); + } + return 0; } -static void usage(char * pname) +static void +usage (char *pname) { - printf( -"\n\n" -"hierarch28 (hierarch-to-eight)\n" -"%s : %s\n" -"use : %s [options] [table]\n" -"options are:\n" -"\t-g generates a generic table\n" -"\n" -"default conversion table name is 'table.conv'\n" -"\n" -"More help can be found in the comments included in the default\n" -"conversion table. Generate one with the -g option and read it.\n" -"\n\n", - pname, prog_desc, pname); - exit(0) ; + printf ("\n\n" + "hierarch28 (hierarch-to-eight)\n" + "%s : %s\n" + "use : %s [options] [table]\n" + "options are:\n" + "\t-g generates a generic table\n" + "\n" + "default conversion table name is 'table.conv'\n" + "\n" + "More help can be found in the comments included in the default\n" + "conversion table. Generate one with the -g option and read it.\n" + "\n\n", pname, prog_desc, pname); + exit (0); } @@ -185,132 +185,140 @@ static void usage(char * pname) keyword values are also modified to follow the IRAF convention. */ /*----------------------------------------------------------------------------*/ -static int convert_eso_to_std_FITS(char * name_in, char * name_conv) +static int +convert_eso_to_std_FITS (char *name_in, char *name_conv) { - FILE * convtab ; - int nkeys ; - int i ; - char ** key_in ; - char ** key_out ; - int fd ; - char * buf ; - char line[NM_SIZ] ; - char kw1[FITS_LINE], - kw2[FITS_LINE] ; - int lineno ; - int fs ; - struct stat fileinfo ; + FILE *convtab; + int nkeys; + int i; + char **key_in; + char **key_out; + int fd; + char *buf; + char line[NM_SIZ]; + char kw1[FITS_LINE], kw2[FITS_LINE]; + int lineno; + int fs; + struct stat fileinfo; - /* Read conversion table and translate it to key_in, key_out */ - if ((convtab = fopen(name_conv, "r")) == NULL) { - fprintf(stderr, "cannot open conversion table: %s\n", name_conv) ; - return -1 ; + /* Read conversion table and translate it to key_in, key_out */ + if ((convtab = fopen (name_conv, "r")) == NULL) + { + fprintf (stderr, "cannot open conversion table: %s\n", name_conv); + return -1; } - /* First, count how many keywords we need to translate */ - nkeys = 0 ; - while (fgets(line, FITS_LINE, convtab)!=NULL) { - if ((line[0] != '#') && (line[0] != '\n')) { - nkeys ++ ; - } + /* First, count how many keywords we need to translate */ + nkeys = 0; + while (fgets (line, FITS_LINE, convtab) != NULL) + { + if ((line[0] != '#') && (line[0] != '\n')) + { + nkeys++; + } } - rewind(convtab) ; + rewind (convtab); - /* Allocate space to store keyword info */ - key_in = malloc(nkeys * sizeof(char*)) ; - key_out = malloc(nkeys * sizeof(char*)) ; + /* Allocate space to store keyword info */ + key_in = malloc (nkeys * sizeof (char *)); + key_out = malloc (nkeys * sizeof (char *)); - /* Now read the file through and get the keywords */ - i = 0 ; - lineno = 0 ; - while (fgets(line, FITS_LINE, convtab)!=NULL) { - lineno++ ; - if ((line[0]!='#') && (line[0]!='\n')) { - if (sscanf(line, "%[^=] = %[^;#]", kw1, kw2)!=2) { - fprintf(stderr, - "*** error parsing table file %s\n", name_conv); - fprintf(stderr, "line: %d\n", lineno) ; - free_keys(key_in, i) ; - free_keys(key_out, i) ; - fclose(convtab) ; - return -1 ; - } - strip_beg_end(kw1) ; - strip_beg_end(kw2) ; - if (strlen(kw2)>strlen(kw1)) { - fprintf(stderr, - "*** error in conversion table %s (line %d)\n", - name_conv, lineno); - fprintf(stderr, - "*** error: dest keyword is longer than original\n"); - fprintf(stderr, "orig: [%s] dest: [%s]\n", kw1, kw2); - fclose(convtab) ; - free_keys(key_in, i) ; - free_keys(key_out, i) ; - return -1 ; - } - key_in[i] = strdup(kw1) ; - key_out[i] = strdup(kw2) ; - i++ ; - } + /* Now read the file through and get the keywords */ + i = 0; + lineno = 0; + while (fgets (line, FITS_LINE, convtab) != NULL) + { + lineno++; + if ((line[0] != '#') && (line[0] != '\n')) + { + if (sscanf (line, "%[^=] = %[^;#]", kw1, kw2) != 2) + { + fprintf (stderr, + "*** error parsing table file %s\n", name_conv); + fprintf (stderr, "line: %d\n", lineno); + free_keys (key_in, i); + free_keys (key_out, i); + fclose (convtab); + return -1; + } + strip_beg_end (kw1); + strip_beg_end (kw2); + if (strlen (kw2) > strlen (kw1)) + { + fprintf (stderr, + "*** error in conversion table %s (line %d)\n", + name_conv, lineno); + fprintf (stderr, + "*** error: dest keyword is longer than original\n"); + fprintf (stderr, "orig: [%s] dest: [%s]\n", kw1, kw2); + fclose (convtab); + free_keys (key_in, i); + free_keys (key_out, i); + return -1; + } + key_in[i] = strdup (kw1); + key_out[i] = strdup (kw2); + i++; + } } - fclose(convtab) ; + fclose (convtab); - /* Print out some information about what is being done */ - printf("\n\n") ; - printf("*** hierarch28\n") ; - printf("\n") ; - printf("searching %s and replacing the following keywords:\n", name_in) ; - for (i=0 ; i\t[%s]\n", key_in[i], key_out[i]) ; + /* Print out some information about what is being done */ + printf ("\n\n"); + printf ("*** hierarch28\n"); + printf ("\n"); + printf ("searching %s and replacing the following keywords:\n", name_in); + for (i = 0; i < nkeys; i++) + { + printf ("\t[%s]\t=>\t[%s]\n", key_in[i], key_out[i]); } - printf("\n\n") ; + printf ("\n\n"); - /* mmap the input file entirely */ - if (stat(name_in, &fileinfo)!=0) { - fprintf(stderr, "*** error: accessing file [%s]\n", name_in); - free_keys(key_in, nkeys) ; - free_keys(key_out, nkeys) ; - return -1 ; + /* mmap the input file entirely */ + if (stat (name_in, &fileinfo) != 0) + { + fprintf (stderr, "*** error: accessing file [%s]\n", name_in); + free_keys (key_in, nkeys); + free_keys (key_out, nkeys); + return -1; } - fs = (int)fileinfo.st_size ; - if (fs < 1) { - fprintf(stderr, "error getting FITS header size for %s\n", name_in); - free_keys(key_in, nkeys) ; - free_keys(key_out, nkeys) ; - return -1 ; + fs = (int) fileinfo.st_size; + if (fs < 1) + { + fprintf (stderr, "error getting FITS header size for %s\n", name_in); + free_keys (key_in, nkeys); + free_keys (key_out, nkeys); + return -1; } - fd = open(name_in, O_RDWR) ; - if (fd == -1) { - fprintf(stderr, "cannot open %s: aborting\n", name_in) ; - free_keys(key_in, nkeys) ; - free_keys(key_out, nkeys) ; - return -1 ; + fd = open (name_in, O_RDWR); + if (fd == -1) + { + fprintf (stderr, "cannot open %s: aborting\n", name_in); + free_keys (key_in, nkeys); + free_keys (key_out, nkeys); + return -1; } - buf = (char*)mmap(0, - fs, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, - 0) ; - if (buf == (char*)-1) { - perror("mmap") ; - fprintf(stderr, "cannot mmap file: %s\n", name_in) ; - free_keys(key_in, nkeys) ; - free_keys(key_out, nkeys) ; - close(fd) ; - return -1 ; + buf = (char *) mmap (0, fs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buf == (char *) -1) + { + perror ("mmap"); + fprintf (stderr, "cannot mmap file: %s\n", name_in); + free_keys (key_in, nkeys); + free_keys (key_out, nkeys); + close (fd); + return -1; } - /* Apply search and replace for the input keyword lists */ - if (search_and_replace_kw(buf, fs, key_in, key_out, nkeys) != 0) { - fprintf(stderr, "error while doing search and replace\n") ; + /* Apply search and replace for the input keyword lists */ + if (search_and_replace_kw (buf, fs, key_in, key_out, nkeys) != 0) + { + fprintf (stderr, "error while doing search and replace\n"); } - free_keys(key_in, nkeys) ; - free_keys(key_out, nkeys) ; - close(fd) ; - munmap(buf, fs) ; - return 0 ; + free_keys (key_in, nkeys); + free_keys (key_out, nkeys); + close (fd); + munmap (buf, fs); + return 0; } /*----------------------------------------------------------------------------*/ @@ -325,19 +333,22 @@ static int convert_eso_to_std_FITS(char * name_in, char * name_conv) characters (like FITS keywords). */ /*----------------------------------------------------------------------------*/ -static void strip_beg_end(char * s) +static void +strip_beg_end (char *s) { - int beg, len ; + int beg, len; - beg = 0 ; - while (!isalnum((unsigned char)(s[beg]))) beg++ ; + beg = 0; + while (!isalnum ((unsigned char) (s[beg]))) + beg++; - len = (int)strlen(s) -1 ; - while (!isalnum((unsigned char)(s[len]))) len -- ; + len = (int) strlen (s) - 1; + while (!isalnum ((unsigned char) (s[len]))) + len--; - strncpy(s, s+beg, len-beg+1) ; - s[len-beg+1] = (char)0 ; - return ; + strncpy (s, s + beg, len - beg + 1); + s[len - beg + 1] = (char) 0; + return; } /*----------------------------------------------------------------------------*/ @@ -351,13 +362,16 @@ static void strip_beg_end(char * s) keys and the master table pointer. */ /*----------------------------------------------------------------------------*/ -static void free_keys(char ** keyt, int n) +static void +free_keys (char **keyt, int n) { - int i ; + int i; - if (n<1) return ; - for (i=0 ; i <8|16|32|-32|-64>\n", argv[0]) ; - printf("\n") ; - printf("\t is a valid FITS file in the current directory\n") ; - printf("\t is the name of the output file\n") ; - printf("\t<8|16|32|-32|-64> is the output file pixel type\n") ; - printf("\n\n") ; - return 0 ; - } - ptype = (int)atoi(argv[3]) ; - check_all_types(ptype) ; - if (!strcmp(argv[1], argv[2])) { - fprintf(stderr, "cannot convert a file to itself\n") ; - fprintf(stderr, "specify another name for the output\n") ; - return -1 ; - } - return convert_fits_pixel_depth(argv[1], argv[2], ptype) ; + if (argc != 4) + { + printf ("\n\n"); + printf ("use: %s <8|16|32|-32|-64>\n", argv[0]); + printf ("\n"); + printf ("\t is a valid FITS file in the current directory\n"); + printf ("\t is the name of the output file\n"); + printf ("\t<8|16|32|-32|-64> is the output file pixel type\n"); + printf ("\n\n"); + return 0; + } + ptype = (int) atoi (argv[3]); + check_all_types (ptype); + if (!strcmp (argv[1], argv[2])) + { + fprintf (stderr, "cannot convert a file to itself\n"); + fprintf (stderr, "specify another name for the output\n"); + return -1; + } + return convert_fits_pixel_depth (argv[1], argv[2], ptype); } /*----------------------------------------------------------------------------- @@ -162,172 +165,198 @@ int main(int argc, char *argv[]) Heavy use of mmap() to speed up the process */ /*----------------------------------------------------------------------------*/ -static int convert_fits_pixel_depth( - char * name_in, - char * name_out, - int ptype_out) +static int +convert_fits_pixel_depth (char *name_in, char *name_out, int ptype_out) { - int fd_in, - fd_out ; - char * buf_in ; - char * buf_out ; - int fsize_in, - fsize_out, - header_size, - padd_size ; - int ptype_in ; - int bpp_out ; - int npix ; - char * zero ; - int yet_to_dump ; - int buf_dump ; - - /* Open input file and get pixel depth and number of pixels */ - fsize_in = filesize(name_in) ; - header_size = get_FITS_header_size(name_in) ; - if ((fd_in = open(name_in, O_RDONLY)) == -1) { - fprintf(stderr, "cannot open file %s: aborting\n", name_in) ; - return -1 ; - } - buf_in = (char*)mmap(0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0) ; - if (buf_in == (char*)-1) { - perror("mmap") ; - fprintf(stderr, "cannot mmap file: %s\n", name_in) ; - close(fd_in) ; - return -1 ; - } - ptype_in = get_bitpix(buf_in) ; - if (ptype_in == 0) { - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1 ; - } - if (ptype_in == -32) { - if (!IEEE_FLOAT_COMPAT(compat_flags)) { - fprintf(stderr, - "this machine does not support IEEE floating point values\n"); - fprintf(stderr, - "cannot convert from -32 type\n"); - exit(-1) ; - } - } - if (ptype_in == -64) { - if (!IEEE_DOUBLE_COMPAT(compat_flags)) { - fprintf(stderr, - "this machine does not support IEEE double values\n"); - fprintf(stderr, - "cannot convert from -64 type\n"); - exit(-1) ; - } - } + int fd_in, fd_out; + char *buf_in; + char *buf_out; + int fsize_in, fsize_out, header_size, padd_size; + int ptype_in; + int bpp_out; + int npix; + char *zero; + int yet_to_dump; + int buf_dump; - /* Compute the size of the output file: header size+pixel area+padding */ - npix = get_npix(buf_in) ; - if (npix < 1) { - close(fd_in) ; - munmap(buf_in, fsize_in) ; - return -1 ; + /* Open input file and get pixel depth and number of pixels */ + fsize_in = filesize (name_in); + header_size = get_FITS_header_size (name_in); + if ((fd_in = open (name_in, O_RDONLY)) == -1) + { + fprintf (stderr, "cannot open file %s: aborting\n", name_in); + return -1; + } + buf_in = (char *) mmap (0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0); + if (buf_in == (char *) -1) + { + perror ("mmap"); + fprintf (stderr, "cannot mmap file: %s\n", name_in); + close (fd_in); + return -1; + } + ptype_in = get_bitpix (buf_in); + if (ptype_in == 0) + { + close (fd_in); + munmap (buf_in, fsize_in); + return -1; + } + if (ptype_in == -32) + { + if (!IEEE_FLOAT_COMPAT (compat_flags)) + { + fprintf (stderr, + "this machine does not support IEEE floating point values\n"); + fprintf (stderr, "cannot convert from -32 type\n"); + exit (-1); } - bpp_out = ptype_out / 8 ; - if (bpp_out < 0) { - bpp_out = - bpp_out ; + } + if (ptype_in == -64) + { + if (!IEEE_DOUBLE_COMPAT (compat_flags)) + { + fprintf (stderr, + "this machine does not support IEEE double values\n"); + fprintf (stderr, "cannot convert from -64 type\n"); + exit (-1); } - fsize_out = header_size + npix * bpp_out ; - if ((fsize_out % FITS_BLSZ) == 0) { - padd_size = 0 ; - } else { - padd_size = FITS_BLSZ - fsize_out % FITS_BLSZ ; - } - fsize_out += padd_size ; + } - /* Now create the output file and fill it with zeros, then mmap it. */ - /* The permissions are rw-rw-r-- by default. */ - if ((fd_out=creat(name_out,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH))==-1){ - perror("creat") ; - fprintf(stderr, "cannot create file %s: aborting\n", name_out) ; - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1 ; - } - zero = calloc(ONE_MEGABYTE, 1) ; - yet_to_dump = fsize_out ; - if (fsize_out < ONE_MEGABYTE) { - buf_dump = fsize_out ; - } else { - buf_dump = ONE_MEGABYTE ; - } - while (yet_to_dump>0) { - if (write(fd_out, zero, buf_dump) != buf_dump) { - perror("write") ; - fprintf(stderr, "error writing to the output file: aborting\n") ; - close(fd_in) ; close(fd_out) ; munmap(buf_in, fsize_in) ; - free(zero) ; - return -1 ; - } - yet_to_dump -= buf_dump ; - if (yet_to_dump > ONE_MEGABYTE) { - buf_dump = ONE_MEGABYTE ; - } else { - buf_dump = yet_to_dump ; - } - } - free(zero) ; - close(fd_out) ; - if ((fd_out = open(name_out, O_RDWR)) == -1) { - perror("open") ; - fprintf(stderr, "cannot reopen file %s for writing\n", name_out); - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1 ; - } + /* Compute the size of the output file: header size+pixel area+padding */ + npix = get_npix (buf_in); + if (npix < 1) + { + close (fd_in); + munmap (buf_in, fsize_in); + return -1; + } + bpp_out = ptype_out / 8; + if (bpp_out < 0) + { + bpp_out = -bpp_out; + } + fsize_out = header_size + npix * bpp_out; + if ((fsize_out % FITS_BLSZ) == 0) + { + padd_size = 0; + } + else + { + padd_size = FITS_BLSZ - fsize_out % FITS_BLSZ; + } + fsize_out += padd_size; - buf_out = (char*)mmap(0, - fsize_out, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd_out, - 0) ; - if (buf_out == (char*)-1) { - perror("mmap") ; - fprintf(stderr, "cannot mmap file: %s\n", name_out) ; - munmap(buf_in, fsize_in) ; close(fd_in) ; close(fd_out) ; - return -1 ; + /* Now create the output file and fill it with zeros, then mmap it. */ + /* The permissions are rw-rw-r-- by default. */ + if ((fd_out = + creat (name_out, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) == -1) + { + perror ("creat"); + fprintf (stderr, "cannot create file %s: aborting\n", name_out); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; + } + zero = calloc (ONE_MEGABYTE, 1); + yet_to_dump = fsize_out; + if (fsize_out < ONE_MEGABYTE) + { + buf_dump = fsize_out; + } + else + { + buf_dump = ONE_MEGABYTE; + } + while (yet_to_dump > 0) + { + if (write (fd_out, zero, buf_dump) != buf_dump) + { + perror ("write"); + fprintf (stderr, "error writing to the output file: aborting\n"); + close (fd_in); + close (fd_out); + munmap (buf_in, fsize_in); + free (zero); + return -1; } - - /* Copy FITS header from input to output, modify BITPIX */ - memcpy(buf_out, buf_in, header_size) ; - if (set_bitpix(buf_out, ptype_out)!=0) { - fprintf(stderr, "error writing BITPIX in output: aborting\n"); - munmap(buf_in, fsize_in) ; munmap(buf_out, fsize_out) ; - close(fd_in) ; close(fd_out) ; - return -1 ; + yet_to_dump -= buf_dump; + if (yet_to_dump > ONE_MEGABYTE) + { + buf_dump = ONE_MEGABYTE; } - - /* Now write pixels */ - write_pixbuf(buf_in + header_size, - buf_out + header_size, - npix, - ptype_in, - ptype_out) ; - - /* Blank-pad the output file if needed */ - if (padd_size) { - if (lseek(fd_out, fsize_out-padd_size, SEEK_SET)==-1) { - perror("lseek"); - fprintf(stderr, "error seeking output file: aborting\n"); - } else { - zero = calloc(padd_size, 1) ; - if (write(fd_out, zero, 1)==-1) { - perror("write") ; - fprintf(stderr, "error writing into output file: aborting\n"); - } - free(zero) ; - } + else + { + buf_dump = yet_to_dump; } + } + free (zero); + close (fd_out); + if ((fd_out = open (name_out, O_RDWR)) == -1) + { + perror ("open"); + fprintf (stderr, "cannot reopen file %s for writing\n", name_out); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; + } - /* Close, unmap, goodbye */ - close(fd_in) ; - close(fd_out) ; - munmap(buf_in, fsize_in) ; - munmap(buf_out, fsize_out) ; - return 0 ; + buf_out = (char *) mmap (0, + fsize_out, + PROT_READ | PROT_WRITE, MAP_SHARED, fd_out, 0); + if (buf_out == (char *) -1) + { + perror ("mmap"); + fprintf (stderr, "cannot mmap file: %s\n", name_out); + munmap (buf_in, fsize_in); + close (fd_in); + close (fd_out); + return -1; + } + + /* Copy FITS header from input to output, modify BITPIX */ + memcpy (buf_out, buf_in, header_size); + if (set_bitpix (buf_out, ptype_out) != 0) + { + fprintf (stderr, "error writing BITPIX in output: aborting\n"); + munmap (buf_in, fsize_in); + munmap (buf_out, fsize_out); + close (fd_in); + close (fd_out); + return -1; + } + + /* Now write pixels */ + write_pixbuf (buf_in + header_size, + buf_out + header_size, npix, ptype_in, ptype_out); + + /* Blank-pad the output file if needed */ + if (padd_size) + { + if (lseek (fd_out, fsize_out - padd_size, SEEK_SET) == -1) + { + perror ("lseek"); + fprintf (stderr, "error seeking output file: aborting\n"); + } + else + { + zero = calloc (padd_size, 1); + if (write (fd_out, zero, 1) == -1) + { + perror ("write"); + fprintf (stderr, "error writing into output file: aborting\n"); + } + free (zero); + } + } + + /* Close, unmap, goodbye */ + close (fd_in); + close (fd_out); + munmap (buf_in, fsize_in); + munmap (buf_out, fsize_out); + return 0; } /*----------------------------------------------------------------------------*/ @@ -339,31 +368,38 @@ static int convert_fits_pixel_depth( characters are found per line. */ /*----------------------------------------------------------------------------*/ -static int get_FITS_header_size(char * name) +static int +get_FITS_header_size (char *name) { - FILE * in ; - char line[81] ; - int found = 0 ; - int count ; - int hs ; + FILE *in; + char line[81]; + int found = 0; + int count; + int hs; - if ((in = fopen(name, "r")) == NULL) { - fprintf(stderr, "cannot open %s: aborting\n", name) ; - return 0 ; + if ((in = fopen (name, "r")) == NULL) + { + fprintf (stderr, "cannot open %s: aborting\n", name); + return 0; } - count = 0 ; - while (!found) { - if (fread(line, 1, 80, in)!=80) break ; - count ++ ; - if (!strncmp(line, "END ", 4)) found = 1 ; + count = 0; + while (!found) + { + if (fread (line, 1, 80, in) != 80) + break; + count++; + if (!strncmp (line, "END ", 4)) + found = 1; } - fclose(in); + fclose (in); - if (!found) return 0 ; - /* The size is the cards nb x 80, rounded to the nextmultiple of 2880 */ - hs = count * 80 ; - if ((hs % FITS_BLSZ) != 0) hs = (1+(hs/FITS_BLSZ)) * FITS_BLSZ ; - return hs ; + if (!found) + return 0; + /* The size is the cards nb x 80, rounded to the nextmultiple of 2880 */ + hs = count * 80; + if ((hs % FITS_BLSZ) != 0) + hs = (1 + (hs / FITS_BLSZ)) * FITS_BLSZ; + return hs; } /*----------------------------------------------------------------------------*/ @@ -374,15 +410,18 @@ static int get_FITS_header_size(char * name) Strongly non portable. Only on Unix systems! */ /*----------------------------------------------------------------------------*/ -static int filesize(char *filename) +static int +filesize (char *filename) { - int size ; - struct stat fileinfo ; + int size; + struct stat fileinfo; - /* POSIX compliant */ - if (stat(filename, &fileinfo) != 0) size = (int)0 ; - else size = (int)fileinfo.st_size ; - return size ; + /* POSIX compliant */ + if (stat (filename, &fileinfo) != 0) + size = (int) 0; + else + size = (int) fileinfo.st_size; + return size; } /*----------------------------------------------------------------------------*/ @@ -392,26 +431,26 @@ static int filesize(char *filename) @return int 8 16 32 -32 or -64 or 0 if cannot find it */ /*----------------------------------------------------------------------------*/ -static int get_bitpix(char * buf) +static int +get_bitpix (char *buf) { - int bitpix ; - char * where ; + int bitpix; + char *where; - where = strstr(buf, "BITPIX") ; - if (where == NULL) { - fprintf(stderr, "cannot find BITPIX in header: aborting\n") ; - return 0 ; - } - sscanf(where, "%*[^=] = %d", &bitpix) ; - /* Check the returned value makes sense */ - if ((bitpix != 8) && - (bitpix != 16) && - (bitpix != 32) && - (bitpix != -32) && - (bitpix != -64)) { - bitpix = 0 ; - } - return bitpix ; + where = strstr (buf, "BITPIX"); + if (where == NULL) + { + fprintf (stderr, "cannot find BITPIX in header: aborting\n"); + return 0; + } + sscanf (where, "%*[^=] = %d", &bitpix); + /* Check the returned value makes sense */ + if ((bitpix != 8) && + (bitpix != 16) && (bitpix != 32) && (bitpix != -32) && (bitpix != -64)) + { + bitpix = 0; + } + return bitpix; } /*----------------------------------------------------------------------------*/ @@ -422,21 +461,23 @@ static int get_bitpix(char * buf) @return int 0 if Ok, anything else otherwise */ /*----------------------------------------------------------------------------*/ -static int set_bitpix(char * buf, int ptype) +static int +set_bitpix (char *buf, int ptype) { - char * where ; - char replace[81] ; + char *where; + char replace[81]; - where = strstr(buf, "BITPIX") ; - if (where == NULL) { - fprintf(stderr, "cannot find BITPIX in header: aborting\n") ; - return -1 ; - } - sprintf(replace, - "BITPIX = % 3d / Bits per pixel ", - ptype) ; - memcpy(where, replace, 80) ; - return 0 ; + where = strstr (buf, "BITPIX"); + if (where == NULL) + { + fprintf (stderr, "cannot find BITPIX in header: aborting\n"); + return -1; + } + sprintf (replace, + "BITPIX = % 3d / Bits per pixel ", + ptype); + memcpy (where, replace, 80); + return 0; } /*----------------------------------------------------------------------------*/ @@ -447,42 +488,47 @@ static int set_bitpix(char * buf, int ptype) Does not support extensions! */ /*----------------------------------------------------------------------------*/ -static int get_npix(char * buf) +static int +get_npix (char *buf) { - int naxes ; - int npix ; - int naxis ; - char * where ; - char lookfor[80] ; - int i ; + int naxes; + int npix; + int naxis; + char *where; + char lookfor[80]; + int i; - where = strstr(buf, "NAXIS") ; - if (where == NULL) { - fprintf(stderr, "cannot find NAXIS in header: aborting\n") ; - return 0 ; + where = strstr (buf, "NAXIS"); + if (where == NULL) + { + fprintf (stderr, "cannot find NAXIS in header: aborting\n"); + return 0; + } + sscanf (where, "%*[^=] = %d", &naxes); + if ((naxes < 1) || (naxes > 999)) + { + fprintf (stderr, "illegal value for %s: %d\n", lookfor, naxes); + return 0; + } + npix = 1; + for (i = 1; i <= naxes; i++) + { + sprintf (lookfor, "NAXIS%d", i); + where = strstr (buf, lookfor); + if (where == NULL) + { + fprintf (stderr, "cannot find %s in header: aborting\n", lookfor); + return 0; } - sscanf(where, "%*[^=] = %d", &naxes) ; - if ((naxes<1) || (naxes>999)) { - fprintf(stderr, "illegal value for %s: %d\n", lookfor, naxes) ; - return 0 ; + sscanf (where, "%*[^=] = %d", &naxis); + if (naxis < 1) + { + fprintf (stderr, "error: found %s=%d\n", lookfor, naxis); + return 0; } - npix = 1 ; - for (i=1 ; i<=naxes ; i++) { - sprintf(lookfor, "NAXIS%d", i) ; - where = strstr(buf, lookfor) ; - if (where == NULL) { - fprintf(stderr, "cannot find %s in header: aborting\n", - lookfor) ; - return 0 ; - } - sscanf(where, "%*[^=] = %d", &naxis) ; - if (naxis<1) { - fprintf(stderr, "error: found %s=%d\n", lookfor, naxis); - return 0 ; - } - npix *= naxis ; - } - return npix ; + npix *= naxis; + } + return npix; } /*----------------------------------------------------------------------------*/ @@ -498,724 +544,907 @@ static int get_npix(char * buf) Pixel format conversion on the fly if needed. */ /*----------------------------------------------------------------------------*/ -static int write_pixbuf( - char * bi, - char * bo, - int npix, - int ptype_in, - int ptype_out) +static int +write_pixbuf (char *bi, char *bo, int npix, int ptype_in, int ptype_out) { - int bpp ; - register int i ; - register char * ip ; - register char * op ; - double d ; - float f ; - char a2[2] ; - char a4[4] ; - char a8[8] ; + int bpp; + register int i; + register char *ip; + register char *op; + double d; + float f; + char a2[2]; + char a4[4]; + char a8[8]; - /* - * Trivial case of identical copies - * 8 to 8 - * 16 to 16 - * 32 to 32 - * -32 to-32 - * -64 to-64 - */ + /* + * Trivial case of identical copies + * 8 to 8 + * 16 to 16 + * 32 to 32 + * -32 to-32 + * -64 to-64 + */ - if (ptype_in == ptype_out) { - /* How many bytes per pixel? */ - bpp = ptype_in / 8 ; if (bpp < 0) bpp = - bpp ; - /* Copy the buffer as it is and return */ - memcpy(bo, bi, npix * bpp) ; - return 0 ; + if (ptype_in == ptype_out) + { + /* How many bytes per pixel? */ + bpp = ptype_in / 8; + if (bpp < 0) + bpp = -bpp; + /* Copy the buffer as it is and return */ + memcpy (bo, bi, npix * bpp); + return 0; + } + + /* Use registers to boost speed */ + ip = bi; + op = bo; + + /* ---------- 8 bit to something */ + + /* + * 8 to 16 + * one byte in input becomes the LSB in the 2 output bytes + * we transfer it directly in the LSB, this is independent from + * machine endian-ness. + */ + + if ((ptype_in == 8) && (ptype_out == 16)) + { + for (i = 0; i < npix; i++) + { + op++; + *op++ |= *ip++; } + return 0; + } - /* Use registers to boost speed */ - ip = bi ; - op = bo ; + /* + * 8 to 32 + * One byte in input becomes the LSB in the 4 output bytes + * we transfer it directly in the LSB, this is independent from + * machine endian-ness. + */ - /* ---------- 8 bit to something */ + if ((ptype_in == 8) && (ptype_out == 32)) + { + for (i = 0; i < npix; i++) + { + op++; + op++; + op++; + *op++ |= *ip++; + } + return 0; + } - /* - * 8 to 16 - * one byte in input becomes the LSB in the 2 output bytes - * we transfer it directly in the LSB, this is independent from - * machine endian-ness. - */ + /* + * 8 to -32 + * Conversion achieved through cast of the input byte into a float + * this means we use the local float type, need to know endian-ness + */ - if ((ptype_in == 8) && (ptype_out == 16)) { - for (i=0 ; i8 we try to find if the + * input is negative by looking at the MSB and in that case clip to + * zero. If the MSB is cleared but any other of the upper 24 bits is + * set, the number is greater than 0xff and is clipped to 0xff. + */ + if ((ptype_in == 32) && (ptype_out == 8)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + for (i = 0; i < npix; i++) + { + /* + * if first bit is set, the number is negative on 32 bits, + * should be clipped to 0 on 8 bits + */ + if (*ip & 0x80) + { + *op++ = 0; + } + else if ((byte) * ip | (byte) * (ip + 1) | (byte) * (ip + 2)) + { + /* + * if any bit is set in the first 24 bits, the number is + * greater than 0xff, should be clipped to 0xff + */ + *op++ = (byte) 0xff; + } + else + { + *op++ = *(ip + 3); + } + ip += 4; + } + return 0; + } + + /* + * 32 to 16 + * Loss of the 16 upper bits. + * We convert the input to a local signed long to see if it + * overflows a signed short. If it does, it is clipped. + */ + if ((ptype_in == 32) && (ptype_out == 16)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + if ((*(long32 *) ip) > 32767) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + ip += 4; } - return 0 ; - } - - /* - * 8 to -64 - * Conversion achieved through cast of the input byte into a double - * this means we use the local float type, need to know endian-ness - */ - if ((ptype_in == 8) && (ptype_out == -64)) { - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i 32767) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + ip += 4; } - return 0 ; - } - - /* - * 16 to -32 - * Conversion to IEEE float is done through a cast of the input - * number. Need to know local endian-ness. - */ - if ((ptype_in == 16) && (ptype_out == -32)) { - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i8 we try to find if the - * input is negative by looking at the MSB and in that case clip to - * zero. If the MSB is cleared but any other of the upper 24 bits is - * set, the number is greater than 0xff and is clipped to 0xff. - */ - if ((ptype_in == 32) && (ptype_out == 8)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - for (i=0 ; i 255.0) + { + *op++ = (byte) 0xff; } - return 0 ; - } - - /* - * 32 to 16 - * Loss of the 16 upper bits. - * We convert the input to a local signed long to see if it - * overflows a signed short. If it does, it is clipped. - */ - if ((ptype_in == 32) && (ptype_out == 16)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i 32767) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - ip += 4 ; - } else if ((*(long32*)ip) < -32768) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - ip += 4 ; - } else { - ip+=2 ; - *op++ = *ip++ ; - *op++ = *ip++ ; - } - } - } else { - for (i=0 ; i 32767) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - ip += 4 ; - } else if ((*(long32*)&a4[0]) < -32768) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - ip += 4 ; - } else { - ip+=2 ; - *op++ = *ip++ ; - *op++ = *ip++ ; - } - } + else if (f < 0.0) + { + *op++ = (byte) 0x00; } - return 0 ; - } - - /* - * 32 to -32 - * We cast the input to a local float, then output it to disk. - */ - if ((ptype_in == 32) && (ptype_out == -32)) { - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i 255.0) + { + *op++ = (byte) 0xff; } - return 0 ; - } - - /* ---------- -32 to something */ - - /* - * -32 to 8 - * Construct the input float through cast (need to know endian-ness). - * Clip it to [0..255], round the value to the smaller integer. - */ - if ((ptype_in == -32) && (ptype_out == 8)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (f<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)(f+0.5) ; - } - ip += 4; - } - } else { - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (f<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)(f+0.5) ; - } - ip += 4; - } + else if (f < 0.0) + { + *op++ = (byte) 0x00; } - - return 0 ; - } - - /* - * -32 to 16 - * Construct the input float through cast (need to know endian-ness) - * Clip it to [-32768..32767], round the value to the smaller - * integer. - */ - if ((ptype_in == -32) && (ptype_out == 16)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i32767.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - } else if (f<-32768.0) { - *op++ = (byte)0x80 ; - *op++ = 0x00 ; - } else { - *op++ = (short16)f >> 8 ; - *op++ = (short16)f & (byte)0xff ; - } - ip+=4 ; - } - } else { - for (i=0 ; i32767.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - } else if (f<-32768.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (short16)f >> 8 ; - *op++ = (short16)f & (byte)0xff ; - } - ip+=4 ; - } + else + { + *op++ = (byte) (f + 0.5); } - return 0 ; + ip += 4; + } } - /* - * -32 to 32 - * Construct the input float through cast (need to know endian-ness) - * Clip it to [-2147483648 .. 2147483647], then round to the smaller - * integer. - */ - if ((ptype_in == -32) && (ptype_out == 32)) { - fprintf(stderr, "warning: probable loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i2147483647.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - } else if (f<-2147483648.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (long32)f >> 24 ; - *op++ = ((long32)f >> 16) & 0xff ; - *op++ = ((long32)f >> 8) & 0xff ; - *op++ = (long32)f & 0xff ; - } - ip+=4 ; - } - } else { - for (i=0 ; i2147483647.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - } else if (f<-2147483648.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (long32)f >> 24 ; - *op++ = ((long32)f >> 16) & 0xff ; - *op++ = ((long32)f >> 8) & 0xff ; - *op++ = (long32)f & 0xff ; - } - ip+=4 ; - } + return 0; + } + + /* + * -32 to 16 + * Construct the input float through cast (need to know endian-ness) + * Clip it to [-32768..32767], round the value to the smaller + * integer. + */ + if ((ptype_in == -32) && (ptype_out == 16)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + f = *((float *) ip); + if (f > 32767.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; } - return 0 ; - } - - /* - * -32 to -64 - * Conversion achieved through cast. Need to know endian-ness. - */ - if ((ptype_in == -32) && (ptype_out == -64)) { - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (d<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)(d+0.5) ; - } - ip+=8 ; - } - } else { - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (d<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)(d+0.5) ; - } - ip+=8 ; - } + else + { + *op++ = (short16) f >> 8; + *op++ = (short16) f & (byte) 0xff; } - return 0 ; + ip += 4; + } } - - /* - * -64 to 16 - * Heavy loss of input precision! - * The input double is constructed through swap and cast. It is then - * clipped to [-32768..32767] and rounded to the smaller integer. - */ - if ((ptype_in == -64) && (ptype_out == 16)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i32767.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - } else if (d<-32768.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (short16)d >> 8 ; - *op++ = (short16)d & (byte)0xff ; - } - ip+=8 ; - } - } else { - for (i=0 ; i32767.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - } else if (d<-32768.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (short16)d >> 8 ; - *op++ = (short16)d & (byte)0xff ; - } - ip+=8 ; - } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a4, ip, 4); + swap_bytes (a4, 4); + f = *((float *) &a4[0]); + if (f > 32767.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; } - return 0 ; - } - - /* - * -64 to 32 - * Heavy loss of input precision! - * The input double is constructed through swap and cast. It is then - * clipped to [-2147483648..2147483647] and rounded to the smaller - * integer. - */ - if ((ptype_in == -64) && (ptype_out == 32)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i2147483647.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - } else if (d<-2147483648.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (long32)d >> 24 ; - *op++ = ((long32)d >> 16) & (byte)0xff ; - *op++ = ((long32)d >> 8) & (byte)0xff ; - *op++ = (long32)d & (byte)0xff ; - } - ip+=8 ; - } - } else { - for (i=0 ; i2147483647.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - } else if (d<-2147483648.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - } else { - *op++ = (long32)d >> 24 ; - *op++ = ((long32)d >> 16) & 0xff ; - *op++ = ((long32)d >> 8) & 0xff ; - *op++ = (long32)d & 0xff ; - } - ip+=8 ; - } + else if (f < -32768.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; } - return 0 ; - } - - /* - * -64 to -32 - * Heavy loss of input precision! - * The input double is constructed through swap and cast. It is then - * simply cast to a float using the local algorithm for this - * conversion. - */ - if ((ptype_in == -64) && (ptype_out == -32)) { - fprintf(stderr, "warning: loss of precision in output\n") ; - if (ENDIAN_NESS(compat_flags) == ENDIAN_MOTOROLA) { - for (i=0 ; i> 8; + *op++ = (short16) f & (byte) 0xff; } - return 0 ; + ip += 4; + } } - return 0 ; + return 0; + } + + /* + * -32 to 32 + * Construct the input float through cast (need to know endian-ness) + * Clip it to [-2147483648 .. 2147483647], then round to the smaller + * integer. + */ + if ((ptype_in == -32) && (ptype_out == 32)) + { + fprintf (stderr, "warning: probable loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + f = *((float *) ip); + if (f > 2147483647.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + } + else if (f < -2147483648.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + } + else + { + *op++ = (long32) f >> 24; + *op++ = ((long32) f >> 16) & 0xff; + *op++ = ((long32) f >> 8) & 0xff; + *op++ = (long32) f & 0xff; + } + ip += 4; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a4, ip, 4); + swap_bytes (a4, 4); + f = *((float *) &a4[0]); + if (f > 2147483647.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + } + else if (f < -2147483648.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + } + else + { + *op++ = (long32) f >> 24; + *op++ = ((long32) f >> 16) & 0xff; + *op++ = ((long32) f >> 8) & 0xff; + *op++ = (long32) f & 0xff; + } + ip += 4; + } + } + return 0; + } + + /* + * -32 to -64 + * Conversion achieved through cast. Need to know endian-ness. + */ + if ((ptype_in == -32) && (ptype_out == -64)) + { + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + d = (double) (*(float *) ip); + memcpy (op, &d, 8); + ip += 4; + op += 8; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a4, ip, 4); + swap_bytes (a4, 4); + d = (double) (*(float *) &a4[0]); + swap_bytes (&d, sizeof (double)); + memcpy (op, &d, 8); + ip += 4; + op += 8; + } + } + return 0; + } + + /* ---------- -64 to something */ + + /* + * -64 to 8 + * Heavy loss of input precision! + * The input double is constructed through swap and cast. It is then + * clipped to [0..255] and rounded to the smaller integer. + */ + if ((ptype_in == -64) && (ptype_out == 8)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + d = *((double *) ip); + if (d > 255.0) + { + *op++ = (byte) 0xff; + } + else if (d < 0.0) + { + *op++ = (byte) 0x00; + } + else + { + *op++ = (byte) (d + 0.5); + } + ip += 8; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a8, ip, 8); + swap_bytes (a8, 8); + d = *((double *) &a8[0]); + if (d > 255.0) + { + *op++ = (byte) 0xff; + } + else if (d < 0.0) + { + *op++ = (byte) 0x00; + } + else + { + *op++ = (byte) (d + 0.5); + } + ip += 8; + } + } + return 0; + } + + /* + * -64 to 16 + * Heavy loss of input precision! + * The input double is constructed through swap and cast. It is then + * clipped to [-32768..32767] and rounded to the smaller integer. + */ + if ((ptype_in == -64) && (ptype_out == 16)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + d = *((double *) ip); + if (d > 32767.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + } + else if (d < -32768.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + } + else + { + *op++ = (short16) d >> 8; + *op++ = (short16) d & (byte) 0xff; + } + ip += 8; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a8, ip, 8); + swap_bytes (a8, 8); + d = *((double *) &a8[0]); + if (d > 32767.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + } + else if (d < -32768.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + } + else + { + *op++ = (short16) d >> 8; + *op++ = (short16) d & (byte) 0xff; + } + ip += 8; + } + } + return 0; + } + + /* + * -64 to 32 + * Heavy loss of input precision! + * The input double is constructed through swap and cast. It is then + * clipped to [-2147483648..2147483647] and rounded to the smaller + * integer. + */ + if ((ptype_in == -64) && (ptype_out == 32)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + d = *((double *) ip); + if (d > 2147483647.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + } + else if (d < -2147483648.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + } + else + { + *op++ = (long32) d >> 24; + *op++ = ((long32) d >> 16) & (byte) 0xff; + *op++ = ((long32) d >> 8) & (byte) 0xff; + *op++ = (long32) d & (byte) 0xff; + } + ip += 8; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a8, ip, 8); + swap_bytes (a8, 8); + d = *((double *) &a8[0]); + if (d > 2147483647.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + } + else if (d < -2147483648.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + } + else + { + *op++ = (long32) d >> 24; + *op++ = ((long32) d >> 16) & 0xff; + *op++ = ((long32) d >> 8) & 0xff; + *op++ = (long32) d & 0xff; + } + ip += 8; + } + } + return 0; + } + + /* + * -64 to -32 + * Heavy loss of input precision! + * The input double is constructed through swap and cast. It is then + * simply cast to a float using the local algorithm for this + * conversion. + */ + if ((ptype_in == -64) && (ptype_out == -32)) + { + fprintf (stderr, "warning: loss of precision in output\n"); + if (ENDIAN_NESS (compat_flags) == ENDIAN_MOTOROLA) + { + for (i = 0; i < npix; i++) + { + f = (float) (*(double *) ip); + memcpy (op, &f, 4); + ip += 8; + op += 4; + } + } + else + { + for (i = 0; i < npix; i++) + { + memcpy (a8, ip, 8); + swap_bytes (a8, 8); + f = (float) (*(double *) &a8[0]); + swap_bytes (&f, sizeof (float)); + memcpy (op, &f, 4); + ip += 8; + op += 4; + } + } + return 0; + } + return 0; } -static int bits_per_byte(void) +static int +bits_per_byte (void) { - unsigned char c=1 ; - int i=0 ; - while (c) { c <<=1 ; i++ ; } - return i ; + unsigned char c = 1; + int i = 0; + while (c) + { + c <<= 1; + i++; + } + return i; } /*----------------------------------------------------------------------------*/ @@ -1227,52 +1456,57 @@ static int bits_per_byte(void) bit 2 stands for IEEE double compatible (1=yes, 0=no) */ /*----------------------------------------------------------------------------*/ -static int test_machine_formats(void) +static int +test_machine_formats (void) { - ushort16 ps = 0xff ; - int endian = ENDIAN_UNKNOWN ; - unsigned flags = 0; - float f ; - double d ; - byte * b ; + ushort16 ps = 0xff; + int endian = ENDIAN_UNKNOWN; + unsigned flags = 0; + float f; + double d; + byte *b; - /* Test endian-ness for this machine */ - endian = ((*((char*)(&ps))) ? ENDIAN_INTEL : ENDIAN_MOTOROLA ) ; - flags |= endian ; + /* Test endian-ness for this machine */ + endian = ((*((char *) (&ps))) ? ENDIAN_INTEL : ENDIAN_MOTOROLA); + flags |= endian; - /* Test whether the internal float format is IEEE 32bit floating */ - if (sizeof(float)==4) { - f = (float)2.5e-16 ; - b = (byte*)(&f) ; - if (endian == ENDIAN_INTEL) { - swap_bytes(&f, sizeof(float)) ; - } - if ((b[0] == (byte)0x25) && - (b[1] == (byte)0x90) && - (b[2] == (byte)0x1d) && - (b[3] == (byte)0x7d)) { - flags |= 2 ; - } + /* Test whether the internal float format is IEEE 32bit floating */ + if (sizeof (float) == 4) + { + f = (float) 2.5e-16; + b = (byte *) (&f); + if (endian == ENDIAN_INTEL) + { + swap_bytes (&f, sizeof (float)); + } + if ((b[0] == (byte) 0x25) && + (b[1] == (byte) 0x90) && + (b[2] == (byte) 0x1d) && (b[3] == (byte) 0x7d)) + { + flags |= 2; + } } - if (sizeof(double)==8) { - d = (double)3.14e32 ; - b = (byte*)&d ; - if (endian == ENDIAN_INTEL) { - swap_bytes(&d, sizeof(double)) ; - } - if ((b[0] == (byte)0x46) && - (b[1] == (byte)0xae) && - (b[2] == (byte)0xf6) && - (b[3] == (byte)0x79) && - (b[4] == (byte)0x70) && - (b[5] == (byte)0xae) && - (b[6] == (byte)0xec) && - (b[7] == (byte)0xd7)) { - flags |= 4 ; - } + if (sizeof (double) == 8) + { + d = (double) 3.14e32; + b = (byte *) & d; + if (endian == ENDIAN_INTEL) + { + swap_bytes (&d, sizeof (double)); + } + if ((b[0] == (byte) 0x46) && + (b[1] == (byte) 0xae) && + (b[2] == (byte) 0xf6) && + (b[3] == (byte) 0x79) && + (b[4] == (byte) 0x70) && + (b[5] == (byte) 0xae) && + (b[6] == (byte) 0xec) && (b[7] == (byte) 0xd7)) + { + flags |= 4; + } } - return flags ; + return flags; } /*----------------------------------------------------------------------------*/ @@ -1286,17 +1520,19 @@ static int test_machine_formats(void) ABCDEFGH -> HGFEDCBA */ /*----------------------------------------------------------------------------*/ -static void swap_bytes(void * p, int s) +static void +swap_bytes (void *p, int s) { - byte tmp, *a, *b ; + byte tmp, *a, *b; - a = (byte*)p ; - b = a + s ; + a = (byte *) p; + b = a + s; - while (a \n", argv[0]) ; - printf("\n") ; - printf("\t is a valid FITS file in the current directory\n") ; - printf("\t is the name of the output file\n") ; - printf("\n\n") ; - return 0 ; + if (argc < 3) + { + printf ("\n\n"); + printf ("use: %s \n", argv[0]); + printf ("\n"); + printf ("\t is a valid FITS file in the current directory\n"); + printf ("\t is the name of the output file\n"); + printf ("\n\n"); + return 0; } - if (!strcmp(argv[1], argv[2])) { - fprintf(stderr, "cannot convert a file to itself\n") ; - fprintf(stderr, "specify another name for the output\n") ; - return -1 ; + if (!strcmp (argv[1], argv[2])) + { + fprintf (stderr, "cannot convert a file to itself\n"); + fprintf (stderr, "specify another name for the output\n"); + return -1; } - return dump_pix(argv[1], argv[2]); + return dump_pix (argv[1], argv[2]); } /*----------------------------------------------------------------------------*/ @@ -90,108 +93,117 @@ int main(int argc, char *argv[]) Heavy use of mmap() to speed up the process */ /*----------------------------------------------------------------------------*/ -static int dump_pix( - char * name_in, - char * name_out) +static int +dump_pix (char *name_in, char *name_out) { - int fd_in, - fd_out ; - char * buf_in ; - char * buf_out ; - int fsize_in, - fsize_out, - header_size ; - int npix ; - - /* - * Open input file and get information we need: - * - pixel depth - * - number of pixels - */ + int fd_in, fd_out; + char *buf_in; + char *buf_out; + int fsize_in, fsize_out, header_size; + int npix; - fsize_in = filesize(name_in) ; - header_size = get_FITS_header_size(name_in) ; - if ((fd_in = open(name_in, O_RDONLY)) == -1) { - fprintf(stderr, "cannot open file %s: aborting\n", name_in) ; - return -1 ; + /* + * Open input file and get information we need: + * - pixel depth + * - number of pixels + */ + + fsize_in = filesize (name_in); + header_size = get_FITS_header_size (name_in); + if ((fd_in = open (name_in, O_RDONLY)) == -1) + { + fprintf (stderr, "cannot open file %s: aborting\n", name_in); + return -1; } - buf_in = (char*)mmap(0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0) ; - if (buf_in == (char*)-1) { - perror("mmap") ; - fprintf(stderr, "cannot mmap file: %s\n", name_in) ; - close(fd_in) ; - return -1 ; + buf_in = (char *) mmap (0, fsize_in, PROT_READ, MAP_SHARED, fd_in, 0); + if (buf_in == (char *) -1) + { + perror ("mmap"); + fprintf (stderr, "cannot mmap file: %s\n", name_in); + close (fd_in); + return -1; } - if (get_bitpix(buf_in) != -32) { - fprintf(stderr, "only 32-bit IEEE floating point format supported\n"); - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1 ; + if (get_bitpix (buf_in) != -32) + { + fprintf (stderr, "only 32-bit IEEE floating point format supported\n"); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; } - /* - * Compute the size of the output file - * same header size, + pixel area + blank padding - */ - npix = get_npix(buf_in) ; - if (npix < 1) { - fprintf(stderr, "cannot compute number of pixels\n"); - close(fd_in) ; - munmap(buf_in, fsize_in) ; - return -1 ; + /* + * Compute the size of the output file + * same header size, + pixel area + blank padding + */ + npix = get_npix (buf_in); + if (npix < 1) + { + fprintf (stderr, "cannot compute number of pixels\n"); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; } - fsize_out = npix * 4 ; - /* - * Now create the output file and fill it with zeros, then mmap it. - * The permissions are rw-rw-r-- by default. - */ - if ((fd_out=creat(name_out,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH))==-1){ - perror("creat") ; - fprintf(stderr, "cannot create file %s: aborting\n", name_out) ; - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1 ; + fsize_out = npix * 4; + /* + * Now create the output file and fill it with zeros, then mmap it. + * The permissions are rw-rw-r-- by default. + */ + if ((fd_out = + creat (name_out, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) == -1) + { + perror ("creat"); + fprintf (stderr, "cannot create file %s: aborting\n", name_out); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; } - buf_out = malloc(fsize_out) ; - if (buf_out == NULL) { - fprintf(stderr, "not enough memory\n"); - fprintf(stderr, "failed to allocate %d bytes\n", fsize_out); - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1; + buf_out = malloc (fsize_out); + if (buf_out == NULL) + { + fprintf (stderr, "not enough memory\n"); + fprintf (stderr, "failed to allocate %d bytes\n", fsize_out); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; } - write(fd_out, buf_out, fsize_out); - close(fd_out); - free(buf_out); + write (fd_out, buf_out, fsize_out); + close (fd_out); + free (buf_out); - fd_out = open(name_out, O_RDWR); - if (fd_out==-1) { - fprintf(stderr, "error opening file %s\n", name_out); - close(fd_in) ; munmap(buf_in, fsize_in) ; - return -1; + fd_out = open (name_out, O_RDWR); + if (fd_out == -1) + { + fprintf (stderr, "error opening file %s\n", name_out); + close (fd_in); + munmap (buf_in, fsize_in); + return -1; } - buf_out = (char*)mmap(0, - fsize_out, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd_out, - 0) ; - if (buf_out == (char*)-1) { - perror("mmap") ; - fprintf(stderr, "cannot mmap file: %s\n", name_out) ; - munmap(buf_in, fsize_in) ; close(fd_in) ; close(fd_out) ; - return -1 ; + buf_out = (char *) mmap (0, + fsize_out, + PROT_READ | PROT_WRITE, MAP_SHARED, fd_out, 0); + if (buf_out == (char *) -1) + { + perror ("mmap"); + fprintf (stderr, "cannot mmap file: %s\n", name_out); + munmap (buf_in, fsize_in); + close (fd_in); + close (fd_out); + return -1; } - /* - * Copy FITS header from input to output, modify BITPIX - */ - memcpy(buf_out, buf_in+header_size, fsize_out) ; - /* - * Close, unmap, goodbye - */ - close(fd_in) ; - close(fd_out) ; - munmap(buf_in, fsize_in) ; - munmap(buf_out, fsize_out) ; - return 0 ; + /* + * Copy FITS header from input to output, modify BITPIX + */ + memcpy (buf_out, buf_in + header_size, fsize_out); + /* + * Close, unmap, goodbye + */ + close (fd_in); + close (fd_out); + munmap (buf_in, fsize_in); + munmap (buf_out, fsize_out); + return 0; } /*----------------------------------------------------------------------------*/ @@ -203,40 +215,47 @@ static int dump_pix( 80 characters are found per line. */ /*----------------------------------------------------------------------------*/ -static int get_FITS_header_size(char * name) +static int +get_FITS_header_size (char *name) { - FILE * in ; - char line[81] ; - int found = 0 ; - int count ; - int hs ; + FILE *in; + char line[81]; + int found = 0; + int count; + int hs; - if ((in = fopen(name, "r")) == NULL) { - fprintf(stderr, "cannot open %s: aborting\n", name) ; - return 0 ; + if ((in = fopen (name, "r")) == NULL) + { + fprintf (stderr, "cannot open %s: aborting\n", name); + return 0; } - count = 0 ; - while (!found) { - if (fread(line, 1, 80, in)!=80) { - break ; - } - count ++ ; - if (!strncmp(line, "END ", 4)) { - found = 1 ; - } + count = 0; + while (!found) + { + if (fread (line, 1, 80, in) != 80) + { + break; + } + count++; + if (!strncmp (line, "END ", 4)) + { + found = 1; + } } - fclose(in); + fclose (in); - if (!found) return 0 ; - /* - * The size is the number of found cards times 80, - * rounded to the closest higher multiple of 2880. - */ - hs = count * 80 ; - if ((hs % FITS_BLSZ) != 0) { - hs = (1+(hs/FITS_BLSZ)) * FITS_BLSZ ; + if (!found) + return 0; + /* + * The size is the number of found cards times 80, + * rounded to the closest higher multiple of 2880. + */ + hs = count * 80; + if ((hs % FITS_BLSZ) != 0) + { + hs = (1 + (hs / FITS_BLSZ)) * FITS_BLSZ; } - return hs ; + return hs; } /*----------------------------------------------------------------------------*/ @@ -247,18 +266,22 @@ static int get_FITS_header_size(char * name) Strongly non portable. Only on Unix systems! */ /*----------------------------------------------------------------------------*/ -static int filesize(char *filename) +static int +filesize (char *filename) { - int size ; - struct stat fileinfo ; + int size; + struct stat fileinfo; - /* POSIX compliant */ - if (stat(filename, &fileinfo) != 0) { - size = (int)0 ; - } else { - size = (int)fileinfo.st_size ; + /* POSIX compliant */ + if (stat (filename, &fileinfo) != 0) + { + size = (int) 0; } - return size ; + else + { + size = (int) fileinfo.st_size; + } + return size; } /*----------------------------------------------------------------------------*/ @@ -268,28 +291,28 @@ static int filesize(char *filename) @return int 8 16 32 -32 or -64 or 0 if cannot find it */ /*----------------------------------------------------------------------------*/ -static int get_bitpix(char * buf) +static int +get_bitpix (char *buf) { - int bitpix ; - char * where ; + int bitpix; + char *where; - where = strstr(buf, "BITPIX") ; - if (where == NULL) { - fprintf(stderr, "cannot find BITPIX in header: aborting\n") ; - return 0 ; + where = strstr (buf, "BITPIX"); + if (where == NULL) + { + fprintf (stderr, "cannot find BITPIX in header: aborting\n"); + return 0; } - sscanf(where, "%*[^=] = %d", &bitpix) ; - /* - * Check the returned value makes sense - */ - if ((bitpix != 8) && - (bitpix != 16) && - (bitpix != 32) && - (bitpix != -32) && - (bitpix != -64)) { - bitpix = 0 ; + sscanf (where, "%*[^=] = %d", &bitpix); + /* + * Check the returned value makes sense + */ + if ((bitpix != 8) && + (bitpix != 16) && (bitpix != 32) && (bitpix != -32) && (bitpix != -64)) + { + bitpix = 0; } - return bitpix ; + return bitpix; } /*----------------------------------------------------------------------------*/ @@ -300,41 +323,45 @@ static int get_bitpix(char * buf) Does not support extensions! */ /*----------------------------------------------------------------------------*/ -static int get_npix(char * buf) +static int +get_npix (char *buf) { - int naxes ; - int npix ; - int naxis ; - char * where ; - char lookfor[80] ; - int i ; + int naxes; + int npix; + int naxis; + char *where; + char lookfor[80]; + int i; - where = strstr(buf, "NAXIS") ; - if (where == NULL) { - fprintf(stderr, "cannot find NAXIS in header: aborting\n") ; - return 0 ; + where = strstr (buf, "NAXIS"); + if (where == NULL) + { + fprintf (stderr, "cannot find NAXIS in header: aborting\n"); + return 0; } - sscanf(where, "%*[^=] = %d", &naxes) ; - if ((naxes<1) || (naxes>999)) { - fprintf(stderr, "illegal value for %s: %d\n", lookfor, naxes) ; - return 0 ; + sscanf (where, "%*[^=] = %d", &naxes); + if ((naxes < 1) || (naxes > 999)) + { + fprintf (stderr, "illegal value for %s: %d\n", lookfor, naxes); + return 0; } - npix = 1 ; - for (i=1 ; i<=naxes ; i++) { - sprintf(lookfor, "NAXIS%d", i) ; - where = strstr(buf, lookfor) ; - if (where == NULL) { - fprintf(stderr, "cannot find %s in header: aborting\n", - lookfor) ; - return 0 ; - } - sscanf(where, "%*[^=] = %d", &naxis) ; - if (naxis<1) { - fprintf(stderr, "error: found %s=%d\n", lookfor, naxis); - return 0 ; - } - npix *= (int)naxis ; + npix = 1; + for (i = 1; i <= naxes; i++) + { + sprintf (lookfor, "NAXIS%d", i); + where = strstr (buf, lookfor); + if (where == NULL) + { + fprintf (stderr, "cannot find %s in header: aborting\n", lookfor); + return 0; + } + sscanf (where, "%*[^=] = %d", &naxis); + if (naxis < 1) + { + fprintf (stderr, "error: found %s=%d\n", lookfor, naxis); + return 0; + } + npix *= (int) naxis; } - return npix ; + return npix; } - diff --git a/qfits/src/byteswap.c b/qfits/src/byteswap.c index 9a59a75..b37ea0e 100644 --- a/qfits/src/byteswap.c +++ b/qfits/src/byteswap.c @@ -51,15 +51,16 @@ modifying the passed argument. Assembler included for x86 architectures. */ /*----------------------------------------------------------------------------*/ -unsigned short __NOTRACE__ swap_bytes_16(unsigned short w) +unsigned short __NOTRACE__ +swap_bytes_16 (unsigned short w) { #ifdef CPU_X86 - __asm("xchgb %b0,%h0" : - "=q" (w) : - "0" (w)); - return w ; +__asm ("xchgb %b0,%h0": +"=q" (w): +"0" (w)); + return w; #else - return (((w) & 0x00ff) << 8 | ((w) & 0xff00) >> 8); + return (((w) & 0x00ff) << 8 | ((w) & 0xff00) >> 8); #endif } @@ -74,23 +75,22 @@ unsigned short __NOTRACE__ swap_bytes_16(unsigned short w) and optimized for processors above 386. */ /*----------------------------------------------------------------------------*/ -unsigned int __NOTRACE__ swap_bytes_32(unsigned int dw) +unsigned int __NOTRACE__ +swap_bytes_32 (unsigned int dw) { #ifdef CPU_X86 #if CPU_X86 > 386 - __asm("bswap %0": - "=r" (dw) : +__asm ("bswap %0": +"=r" (dw): #else - __asm("xchgb %b0,%h0\n" - " rorl $16,%0\n" - " xchgb %b0,%h0": - "=q" (dw) : +__asm ("xchgb %b0,%h0\n" " rorl $16,%0\n" " xchgb %b0,%h0": +"=q" (dw): #endif - "0" (dw)); - return dw ; +"0" (dw)); + return dw; #else - return ((((dw) & 0xff000000) >> 24) | (((dw) & 0x00ff0000) >> 8) | - (((dw) & 0x0000ff00) << 8) | (((dw) & 0x000000ff) << 24)); + return ((((dw) & 0xff000000) >> 24) | (((dw) & 0x00ff0000) >> 8) | + (((dw) & 0x0000ff00) << 8) | (((dw) & 0x000000ff) << 24)); #endif } @@ -107,17 +107,19 @@ unsigned int __NOTRACE__ swap_bytes_32(unsigned int dw) everywhere this file compiles. */ /*----------------------------------------------------------------------------*/ -void __NOTRACE__ swap_bytes(void * p, int s) +void __NOTRACE__ +swap_bytes (void *p, int s) { - unsigned char tmp, *a, *b ; + unsigned char tmp, *a, *b; - a = (unsigned char*)p ; - b = a + s ; + a = (unsigned char *) p; + b = a + s; - while (a=qfits_cache_entries) { - return -1 ; - } + /* Early exit: all entries have been browsed */ + if (n >= qfits_cache_entries) + { + return -1; } - return -1 ; + } + return -1; } #if QFITS_CACHE_DEBUG -void qfits_cache_dump(void) +void +qfits_cache_dump (void) { - int i, j ; + int i, j; - printf("qfits: dumping cache...\n"); + printf ("qfits: dumping cache...\n"); - printf("cache contains %d entries\n", qfits_cache_entries); - for (i=0 ; i entry: %d\n", i); - printf("qfits: name %s\n", qfits_cache[i].name); - printf("qfits: exts %d\n", qfits_cache[i].exts); - printf("qfits: size %d\n", qfits_cache[i].fsize); - printf("qfits: ohdr %d\n" - "qfits: shdr %d\n" - "qfits: data %d\n" - "qfits: dsiz %d\n", - qfits_cache[i].ohdr[0], - qfits_cache[i].shdr[0], - qfits_cache[i].data[0], - qfits_cache[i].dsiz[0]); - if (qfits_cache[i].exts>0) { - for (j=1 ; j<=qfits_cache[i].exts ; j++) { - printf("qfits: %s [%d]\n", qfits_cache[i].name, j); - printf("qfits: ohdr %d\n" - "qfits: shdr %d\n" - "qfits: data %d\n" - "qfits: dsiz %d\n", - qfits_cache[i].ohdr[j], - qfits_cache[i].shdr[j], - qfits_cache[i].data[j], - qfits_cache[i].dsiz[j]); - } - } - } + printf ("cache contains %d entries\n", qfits_cache_entries); + for (i = 0; i < QFITS_CACHESZ; i++) + { + if (qfits_cache[i].name != NULL) + { + printf ("qfits: -----> entry: %d\n", i); + printf ("qfits: name %s\n", qfits_cache[i].name); + printf ("qfits: exts %d\n", qfits_cache[i].exts); + printf ("qfits: size %d\n", qfits_cache[i].fsize); + printf ("qfits: ohdr %d\n" + "qfits: shdr %d\n" + "qfits: data %d\n" + "qfits: dsiz %d\n", + qfits_cache[i].ohdr[0], + qfits_cache[i].shdr[0], + qfits_cache[i].data[0], qfits_cache[i].dsiz[0]); + if (qfits_cache[i].exts > 0) + { + for (j = 1; j <= qfits_cache[i].exts; j++) + { + printf ("qfits: %s [%d]\n", qfits_cache[i].name, j); + printf ("qfits: ohdr %d\n" + "qfits: shdr %d\n" + "qfits: data %d\n" + "qfits: dsiz %d\n", + qfits_cache[i].ohdr[j], + qfits_cache[i].shdr[j], + qfits_cache[i].data[j], qfits_cache[i].dsiz[j]); + } + } } - return ; + } + return; } #endif @@ -309,71 +322,80 @@ void qfits_cache_dump(void) of the FITS file. */ /*----------------------------------------------------------------------------*/ -int qfits_query(char * filename, int what) +int +qfits_query (char *filename, int what) { - int rank ; - int which ; - int answer ; + int rank; + int which; + int answer; - qdebug( - printf("qfits: cache req %s\n", filename); + qdebug (printf ("qfits: cache req %s\n", filename); + ); + if ((rank = qfits_is_cached (filename)) == -1) + { + rank = qfits_cache_add (filename); + } + if (rank == -1) + { + qdebug (printf ("qfits: error adding %s to cache\n", filename); ); - if ((rank=qfits_is_cached(filename))==-1) { - rank = qfits_cache_add(filename); - } - if (rank==-1) { - qdebug( - printf("qfits: error adding %s to cache\n", filename); - ); - return -1 ; - } + return -1; + } - /* See what was requested */ - answer=-1 ; - if (what & QFITS_QUERY_N_EXT) { - answer = qfits_cache[rank].exts ; - qdebug( - printf("qfits: query n_exts\n"); - printf("qfits: -> %d\n", answer); - ); - } else if (what & QFITS_QUERY_HDR_START) { - which = what & (~QFITS_QUERY_HDR_START); - if (which>=0 && which<=qfits_cache[rank].exts) { - answer = qfits_cache[rank].ohdr[which] * FITS_BLOCK_SIZE ; - } - qdebug( - printf("qfits: query offset to header %d\n", which); - printf("qfits: -> %d (%d bytes)\n", answer/2880, answer); - ); - } else if (what & QFITS_QUERY_DAT_START) { - which = what & (~QFITS_QUERY_DAT_START); - if (which>=0 && which<=qfits_cache[rank].exts) { - answer = qfits_cache[rank].data[which] * FITS_BLOCK_SIZE ; - } - qdebug( - printf("qfits: query offset to data %d\n", which); - printf("qfits: -> %d (%d bytes)\n", answer/2880, answer); - ); - } else if (what & QFITS_QUERY_HDR_SIZE) { - which = what & (~QFITS_QUERY_HDR_SIZE); - if (which>=0 && which<=qfits_cache[rank].exts) { - answer = qfits_cache[rank].shdr[which] * FITS_BLOCK_SIZE ; - } - qdebug( - printf("qfits: query sizeof header %d\n", which); - printf("qfits: -> %d (%d bytes)\n", answer/2880, answer); - ); - } else if (what & QFITS_QUERY_DAT_SIZE) { - which = what & (~QFITS_QUERY_DAT_SIZE); - if (which>=0 && which<=qfits_cache[rank].exts) { - answer = qfits_cache[rank].dsiz[which] * FITS_BLOCK_SIZE ; - } - qdebug( - printf("qfits: query sizeof data %d\n", which); - printf("qfits: -> %d (%d bytes)\n", answer/2880, answer); - ); + /* See what was requested */ + answer = -1; + if (what & QFITS_QUERY_N_EXT) + { + answer = qfits_cache[rank].exts; + qdebug (printf ("qfits: query n_exts\n"); + printf ("qfits: -> %d\n", answer); + ); + } + else if (what & QFITS_QUERY_HDR_START) + { + which = what & (~QFITS_QUERY_HDR_START); + if (which >= 0 && which <= qfits_cache[rank].exts) + { + answer = qfits_cache[rank].ohdr[which] * FITS_BLOCK_SIZE; } - return answer ; + qdebug (printf ("qfits: query offset to header %d\n", which); + printf ("qfits: -> %d (%d bytes)\n", answer / 2880, answer); + ); + } + else if (what & QFITS_QUERY_DAT_START) + { + which = what & (~QFITS_QUERY_DAT_START); + if (which >= 0 && which <= qfits_cache[rank].exts) + { + answer = qfits_cache[rank].data[which] * FITS_BLOCK_SIZE; + } + qdebug (printf ("qfits: query offset to data %d\n", which); + printf ("qfits: -> %d (%d bytes)\n", answer / 2880, answer); + ); + } + else if (what & QFITS_QUERY_HDR_SIZE) + { + which = what & (~QFITS_QUERY_HDR_SIZE); + if (which >= 0 && which <= qfits_cache[rank].exts) + { + answer = qfits_cache[rank].shdr[which] * FITS_BLOCK_SIZE; + } + qdebug (printf ("qfits: query sizeof header %d\n", which); + printf ("qfits: -> %d (%d bytes)\n", answer / 2880, answer); + ); + } + else if (what & QFITS_QUERY_DAT_SIZE) + { + which = what & (~QFITS_QUERY_DAT_SIZE); + if (which >= 0 && which <= qfits_cache[rank].exts) + { + answer = qfits_cache[rank].dsiz[which] * FITS_BLOCK_SIZE; + } + qdebug (printf ("qfits: query sizeof data %d\n", which); + printf ("qfits: -> %d (%d bytes)\n", answer / 2880, answer); + ); + } + return answer; } /*----------------------------------------------------------------------------*/ @@ -402,366 +424,387 @@ int qfits_query(char * filename, int what) and extension data start. */ /*----------------------------------------------------------------------------*/ -static int qfits_cache_add(char * filename) +static int +qfits_cache_add (char *filename) { - FILE * in ; - int off_hdr[QFITS_MAX_EXTS]; - int off_dat[QFITS_MAX_EXTS]; - char buf[FITS_BLOCK_SIZE] ; - char * buf_c ; - int n_blocks ; - int found_it ; - int xtend ; - int naxis ; - char * read_val ; - int last ; - int end_of_file ; - int data_bytes ; - int skip_blocks ; - struct stat sta ; - int seeked ; - int i ; + FILE *in; + int off_hdr[QFITS_MAX_EXTS]; + int off_dat[QFITS_MAX_EXTS]; + char buf[FITS_BLOCK_SIZE]; + char *buf_c; + int n_blocks; + int found_it; + int xtend; + int naxis; + char *read_val; + int last; + int end_of_file; + int data_bytes; + int skip_blocks; + struct stat sta; + int seeked; + int i; - qfits_cache_cell * qc ; + qfits_cache_cell *qc; - /* Initialize cache if not done yet (done only once) */ - if (qfits_cache_init==0) { - qfits_cache_init++ ; - qfits_cache_activate(); + /* Initialize cache if not done yet (done only once) */ + if (qfits_cache_init == 0) + { + qfits_cache_init++; + qfits_cache_activate (); } - /* Stat file to get its size */ - if (stat(filename, &sta)!=0) { - qdebug( - printf("qfits: cannot stat file %s\n", filename); - ); - return -1 ; - } - - /* Open input file */ - if ((in=fopen(filename, "r"))==NULL) { - qdebug( - printf("qfits: cannot open file %s\n", filename); - ); - return -1 ; - } - - /* Read first block in */ - if (fread(buf, 1, FITS_BLOCK_SIZE, in)!=FITS_BLOCK_SIZE) { - qdebug( - printf("qfits: error reading first block from %s\n", filename); - ); - fclose(in); - return -1 ; - } - /* Identify FITS magic number */ - if (buf[0]!='S' || - buf[1]!='I' || - buf[2]!='M' || - buf[3]!='P' || - buf[4]!='L' || - buf[5]!='E' || - buf[6]!=' ' || - buf[7]!=' ' || - buf[8]!='=') { - qdebug( - printf("qfits: file %s is not FITS\n", filename); - ); - fclose(in); - return -1 ; - } - - /* - * Browse through file to identify primary HDU size and see if there - * might be some extensions. The size of the primary data zone will - * also be estimated from the gathering of the NAXIS?? values and - * BITPIX. - */ - - /* Rewind input file, END card might be in first block */ - rewind(in); - - /* Initialize all counters */ - n_blocks = 0 ; - found_it = 0 ; - xtend = 0 ; - naxis = 0 ; - data_bytes = 1 ; - - /* Start looking for END card */ - while (found_it==0) { - /* Read one FITS block */ - if (fread(buf, 1, FITS_BLOCK_SIZE, in)!=FITS_BLOCK_SIZE) { - qdebug( - printf("qfits: error reading file %s\n", filename); - ); - fclose(in); - return -1 ; - } - n_blocks ++ ; - /* Browse through current block */ - buf_c = buf ; - for (i=0 ; i= QFITS_CACHESZ) { - qfits_cache_last = 0 ; + /* Stat file to get its size */ + if (stat (filename, &sta) != 0) + { + qdebug (printf ("qfits: cannot stat file %s\n", filename); + ); + return -1; } - /* Alias to current pointer in cache for easier reading */ - qc = &(qfits_cache[qfits_cache_last]); - /* Clean cache cell if needed */ - if (qc->name!=NULL) { - free(qc->name) ; - qc->name = NULL ; - free(qc->ohdr); - free(qc->data); - free(qc->shdr); - free(qc->dsiz); - qfits_cache_entries -- ; + /* Open input file */ + if ((in = fopen (filename, "r")) == NULL) + { + qdebug (printf ("qfits: cannot open file %s\n", filename); + ); + return -1; } - - /* Initialize cache cell */ - qc->exts=0 ; - qc->name = strdup(filename); - qc->inode= sta.st_ino ; - /* Set first HDU offsets */ - off_hdr[0] = 0 ; - off_dat[0] = n_blocks ; - - /* Last is the pointer to the last added extension, plus one. */ - last = 1 ; + /* Read first block in */ + if (fread (buf, 1, FITS_BLOCK_SIZE, in) != FITS_BLOCK_SIZE) + { + qdebug (printf ("qfits: error reading first block from %s\n", filename); + ); + fclose (in); + return -1; + } + /* Identify FITS magic number */ + if (buf[0] != 'S' || + buf[1] != 'I' || + buf[2] != 'M' || + buf[3] != 'P' || + buf[4] != 'L' || + buf[5] != 'E' || buf[6] != ' ' || buf[7] != ' ' || buf[8] != '=') + { + qdebug (printf ("qfits: file %s is not FITS\n", filename); + ); + fclose (in); + return -1; + } - if (xtend) { - /* Look for extensions */ - qdebug( - printf("qfits: searching for extensions in %s\n", filename); + /* + * Browse through file to identify primary HDU size and see if there + * might be some extensions. The size of the primary data zone will + * also be estimated from the gathering of the NAXIS?? values and + * BITPIX. + */ + + /* Rewind input file, END card might be in first block */ + rewind (in); + + /* Initialize all counters */ + n_blocks = 0; + found_it = 0; + xtend = 0; + naxis = 0; + data_bytes = 1; + + /* Start looking for END card */ + while (found_it == 0) + { + /* Read one FITS block */ + if (fread (buf, 1, FITS_BLOCK_SIZE, in) != FITS_BLOCK_SIZE) + { + qdebug (printf ("qfits: error reading file %s\n", filename); + ); + fclose (in); + return -1; + } + n_blocks++; + /* Browse through current block */ + buf_c = buf; + for (i = 0; i < FITS_NCARDS; i++) + { + + /* Look for BITPIX keyword */ + if (buf_c[0] == 'B' && + buf_c[1] == 'I' && + buf_c[2] == 'T' && + buf_c[3] == 'P' && + buf_c[4] == 'I' && buf_c[5] == 'X' && buf_c[6] == ' ') + { + read_val = qfits_getvalue (buf_c); + data_bytes *= (int) atoi (read_val) / 8; + if (data_bytes < 0) + data_bytes *= -1; + } + else + /* Look for NAXIS keyword */ + if (buf_c[0] == 'N' && + buf_c[1] == 'A' && + buf_c[2] == 'X' && buf_c[3] == 'I' && buf_c[4] == 'S') + { + + if (buf_c[5] == ' ') + { + /* NAXIS keyword */ + read_val = qfits_getvalue (buf_c); + naxis = (int) atoi (read_val); + } + else + { + /* NAXIS?? keyword (axis size) */ + read_val = qfits_getvalue (buf_c); + data_bytes *= (int) atoi (read_val); + } + } + else + /* Look for EXTEND keyword */ + if (buf_c[0] == 'E' && + buf_c[1] == 'X' && + buf_c[2] == 'T' && + buf_c[3] == 'E' && + buf_c[4] == 'N' && buf_c[5] == 'D' && buf_c[6] == ' ') + { + /* The EXTEND keyword is present: might be some extensions */ + read_val = qfits_getvalue (buf_c); + if (read_val[0] == 'T' || read_val[0] == '1') + { + xtend = 1; + } + } + else + /* Look for END keyword */ + if (buf_c[0] == 'E' && + buf_c[1] == 'N' && buf_c[2] == 'D' && buf_c[3] == ' ') + { + found_it = 1; + } + buf_c += FITS_LINESZ; + } + } + + /* + * Prepare qfits cache for addition of a new entry + */ + qfits_cache_last++; + /* Rotate buffer if needed */ + if (qfits_cache_last >= QFITS_CACHESZ) + { + qfits_cache_last = 0; + } + /* Alias to current pointer in cache for easier reading */ + qc = &(qfits_cache[qfits_cache_last]); + + /* Clean cache cell if needed */ + if (qc->name != NULL) + { + free (qc->name); + qc->name = NULL; + free (qc->ohdr); + free (qc->data); + free (qc->shdr); + free (qc->dsiz); + qfits_cache_entries--; + } + + /* Initialize cache cell */ + qc->exts = 0; + qc->name = strdup (filename); + qc->inode = sta.st_ino; + + /* Set first HDU offsets */ + off_hdr[0] = 0; + off_dat[0] = n_blocks; + + /* Last is the pointer to the last added extension, plus one. */ + last = 1; + + if (xtend) + { + /* Look for extensions */ + qdebug (printf ("qfits: searching for extensions in %s\n", filename); + ); + + /* + * Register all extension offsets + */ + end_of_file = 0; + while (end_of_file == 0) + { + /* + * Skip the previous data section if pixels were declared + */ + if (naxis > 0) + { + /* Skip as many blocks as there are declared pixels */ + skip_blocks = data_bytes / FITS_BLOCK_SIZE; + if ((data_bytes % FITS_BLOCK_SIZE) != 0) + { + skip_blocks++; + } + seeked = fseek (in, skip_blocks * FITS_BLOCK_SIZE, SEEK_CUR); + if (seeked < 0) + { + qdebug (printf ("qfits: error seeking file %s\n", filename); + ); + free (qc->name); + fclose (in); + return -1; + } + /* Increase counter of current seen blocks. */ + n_blocks += skip_blocks; + } + + /* Look for extension start */ + found_it = 0; + while ((found_it == 0) && (end_of_file == 0)) + { + if (fread (buf, 1, FITS_BLOCK_SIZE, in) != FITS_BLOCK_SIZE) + { + /* Reached end of file */ + end_of_file = 1; + break; + } + n_blocks++; + /* Search for XTENSION at block top */ + if (buf[0] == 'X' && + buf[1] == 'T' && + buf[2] == 'E' && + buf[3] == 'N' && + buf[4] == 'S' && + buf[5] == 'I' && + buf[6] == 'O' && buf[7] == 'N' && buf[8] == '=') + { + /* Got an extension */ + found_it = 1; + off_hdr[last] = n_blocks - 1; + } + } + if (end_of_file) + break; + + /* + * Look for extension END + * Rewind one block backwards, END might be in same section as + * XTENSION start. + */ + if (fseek (in, -FITS_BLOCK_SIZE, SEEK_CUR) == -1) + { + qdebug (printf ("qfits: error fseeking file backwards\n"); ); - - /* - * Register all extension offsets - */ - end_of_file = 0 ; - while (end_of_file==0) { - /* - * Skip the previous data section if pixels were declared - */ - if (naxis>0) { - /* Skip as many blocks as there are declared pixels */ - skip_blocks = data_bytes/FITS_BLOCK_SIZE ; - if ((data_bytes % FITS_BLOCK_SIZE)!=0) { - skip_blocks ++ ; - } - seeked = fseek(in, skip_blocks*FITS_BLOCK_SIZE, SEEK_CUR); - if (seeked<0) { - qdebug( - printf("qfits: error seeking file %s\n", filename); - ); - free(qc->name); - fclose(in); - return -1 ; - } - /* Increase counter of current seen blocks. */ - n_blocks += skip_blocks ; - } - - /* Look for extension start */ - found_it=0 ; - while ((found_it==0) && (end_of_file==0)) { - if (fread(buf,1,FITS_BLOCK_SIZE,in)!=FITS_BLOCK_SIZE) { - /* Reached end of file */ - end_of_file=1 ; - break ; - } - n_blocks ++ ; - /* Search for XTENSION at block top */ - if (buf[0]=='X' && - buf[1]=='T' && - buf[2]=='E' && - buf[3]=='N' && - buf[4]=='S' && - buf[5]=='I' && - buf[6]=='O' && - buf[7]=='N' && - buf[8]=='=') { - /* Got an extension */ - found_it=1 ; - off_hdr[last] = n_blocks-1 ; - } - } - if (end_of_file) break ; - - /* - * Look for extension END - * Rewind one block backwards, END might be in same section as - * XTENSION start. - */ - if (fseek(in, -FITS_BLOCK_SIZE, SEEK_CUR)==-1) { - qdebug( - printf("qfits: error fseeking file backwards\n"); - ) ; - free(qc->name); - fclose(in); - return -1 ; - } - n_blocks -- ; - found_it=0 ; - data_bytes = 1 ; - naxis = 0 ; - while ((found_it==0) && (end_of_file==0)) { - if (fread(buf,1,FITS_BLOCK_SIZE,in)!=FITS_BLOCK_SIZE) { - qdebug( - printf("qfits: XTENSION without END in %s\n", filename); - ); - end_of_file=1; - break ; - } - n_blocks++ ; - - /* Browse current block */ - buf_c = buf ; - for (i=0 ; iexts ++ ; - break ; - } - buf_c+=FITS_LINESZ ; - } - } + free (qc->name); + fclose (in); + return -1; + } + n_blocks--; + found_it = 0; + data_bytes = 1; + naxis = 0; + while ((found_it == 0) && (end_of_file == 0)) + { + if (fread (buf, 1, FITS_BLOCK_SIZE, in) != FITS_BLOCK_SIZE) + { + qdebug (printf + ("qfits: XTENSION without END in %s\n", filename); + ); + end_of_file = 1; + break; } - } + n_blocks++; - /* Close file */ - fclose(in); + /* Browse current block */ + buf_c = buf; + for (i = 0; i < FITS_NCARDS; i++) + { + /* Look for BITPIX keyword */ + if (buf_c[0] == 'B' && + buf_c[1] == 'I' && + buf_c[2] == 'T' && + buf_c[3] == 'P' && + buf_c[4] == 'I' && buf_c[5] == 'X' && buf_c[6] == ' ') + { + read_val = qfits_getvalue (buf_c); + data_bytes *= (int) atoi (read_val) / 8; + if (data_bytes < 0) + data_bytes *= -1; + } + else + /* Look for NAXIS keyword */ + if (buf_c[0] == 'N' && + buf_c[1] == 'A' && + buf_c[2] == 'X' && buf_c[3] == 'I' && buf_c[4] == 'S') + { - /* Allocate buffers in cache */ - qc->ohdr = malloc(last * sizeof(int)); - qc->data = malloc(last * sizeof(int)); - qc->shdr = malloc(last * sizeof(int)); - qc->dsiz = malloc(last * sizeof(int)); - /* Store retrieved pointers in the cache */ - for (i=0 ; iohdr[i] = off_hdr[i]; - qc->data[i] = off_dat[i]; - - /* Sizes */ - qc->shdr[i] = off_dat[i] - off_hdr[i] ; - if (i==last-1) { - qc->dsiz[i] = (sta.st_size/FITS_BLOCK_SIZE) - off_dat[i] ; - } else { - qc->dsiz[i] = off_hdr[i+1] - off_dat[i] ; + if (buf_c[5] == ' ') + { + /* NAXIS keyword */ + read_val = qfits_getvalue (buf_c); + naxis = (int) atoi (read_val); + } + else + { + /* NAXIS?? keyword (axis size) */ + read_val = qfits_getvalue (buf_c); + data_bytes *= (int) atoi (read_val); + } + } + else + /* Look for END keyword */ + if (buf_c[0] == 'E' && + buf_c[1] == 'N' && buf_c[2] == 'D' && buf_c[3] == ' ') + { + /* Got the END card */ + found_it = 1; + /* Update registered extension list */ + off_dat[last] = n_blocks; + last++; + qc->exts++; + break; + } + buf_c += FITS_LINESZ; } + } } - qc->fsize = sta.st_size / FITS_BLOCK_SIZE ; - /* Add last modification date */ - qc->mtime = sta.st_mtime ; - qc->filesize = sta.st_size ; - qc->ctime = sta.st_ctime ; - qfits_cache_entries ++ ; + } - qdebug( - qfits_cache_dump(); + /* Close file */ + fclose (in); + + /* Allocate buffers in cache */ + qc->ohdr = malloc (last * sizeof (int)); + qc->data = malloc (last * sizeof (int)); + qc->shdr = malloc (last * sizeof (int)); + qc->dsiz = malloc (last * sizeof (int)); + /* Store retrieved pointers in the cache */ + for (i = 0; i < last; i++) + { + /* Offsets to start */ + qc->ohdr[i] = off_hdr[i]; + qc->data[i] = off_dat[i]; + + /* Sizes */ + qc->shdr[i] = off_dat[i] - off_hdr[i]; + if (i == last - 1) + { + qc->dsiz[i] = (sta.st_size / FITS_BLOCK_SIZE) - off_dat[i]; + } + else + { + qc->dsiz[i] = off_hdr[i + 1] - off_dat[i]; + } + } + qc->fsize = sta.st_size / FITS_BLOCK_SIZE; + /* Add last modification date */ + qc->mtime = sta.st_mtime; + qc->filesize = sta.st_size; + qc->ctime = sta.st_ctime; + qfits_cache_entries++; + + qdebug (qfits_cache_dump (); ); - /* Return index of the added file in the cache */ - return qfits_cache_last ; + /* Return index of the added file in the cache */ + return qfits_cache_last; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/cache.h b/qfits/src/cache.h index 22fb13b..0603518 100644 --- a/qfits/src/cache.h +++ b/qfits/src/cache.h @@ -56,7 +56,7 @@ fairly small, you should not need to care too much about this. */ /*----------------------------------------------------------------------------*/ -void qfits_cache_purge(void); +void qfits_cache_purge (void); /* */ /*----------------------------------------------------------------------------*/ @@ -116,7 +116,7 @@ void qfits_cache_purge(void); of the FITS file. */ /*----------------------------------------------------------------------------*/ -int qfits_query(char * filename, int what); +int qfits_query (char *filename, int what); #endif /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/expkey.c b/qfits/src/expkey.c index b3d7fa1..a5cf146 100644 --- a/qfits/src/expkey.c +++ b/qfits/src/expkey.c @@ -40,20 +40,23 @@ @return string */ /*----------------------------------------------------------------------------*/ -static char * expkey_strupc(const char * s) +static char * +expkey_strupc (const char *s) { - static char l[ASCIILINESZ+1]; - int i ; + static char l[ASCIILINESZ + 1]; + int i; - if (s==NULL) return NULL ; - memset(l, 0, ASCIILINESZ+1); - i=0 ; - while (s[i] && i */ #endif diff --git a/qfits/src/fits_h.c b/qfits/src/fits_h.c index dd1f702..887765d 100644 --- a/qfits/src/fits_h.c +++ b/qfits/src/fits_h.c @@ -59,18 +59,19 @@ to know about this struct. */ /*----------------------------------------------------------------------------*/ -typedef struct _keytuple_ { +typedef struct _keytuple_ +{ - char * key ; /** Key: unique string in a list */ - char * val ; /** Value, always as a string */ - char * com ; /** Comment associated to key */ - char * lin ; /** Initial line in FITS header if applicable */ - int typ ; /** Key type */ + char *key; /** Key: unique string in a list */ + char *val; /** Value, always as a string */ + char *com; /** Comment associated to key */ + char *lin; /** Initial line in FITS header if applicable */ + int typ; /** Key type */ /** Implemented as a doubly-linked list */ - struct _keytuple_ * next ; - struct _keytuple_ * prev ; -} keytuple ; + struct _keytuple_ *next; + struct _keytuple_ *prev; +} keytuple; /*----------------------------------------------------------------------------*/ /** @@ -83,68 +84,69 @@ typedef struct _keytuple_ { module. */ /*----------------------------------------------------------------------------*/ -typedef enum _keytype_ { - keytype_undef =0, +typedef enum _keytype_ +{ + keytype_undef = 0, - keytype_top =1, + keytype_top = 1, - /* Mandatory keywords */ - /* All FITS files */ - keytype_bitpix =2, - keytype_naxis =3, + /* Mandatory keywords */ + /* All FITS files */ + keytype_bitpix = 2, + keytype_naxis = 3, - keytype_naxis1 =11, - keytype_naxis2 =12, - keytype_naxis3 =13, - keytype_naxis4 =14, - keytype_naxisi =20, - /* Random groups only */ - keytype_group =30, - /* Extensions */ - keytype_pcount =31, - keytype_gcount =32, - /* Main header */ - keytype_extend =33, - /* Images */ - keytype_bscale =34, - keytype_bzero =35, - /* Tables */ - keytype_tfields =36, - keytype_tbcoli =40, - keytype_tformi =41, + keytype_naxis1 = 11, + keytype_naxis2 = 12, + keytype_naxis3 = 13, + keytype_naxis4 = 14, + keytype_naxisi = 20, + /* Random groups only */ + keytype_group = 30, + /* Extensions */ + keytype_pcount = 31, + keytype_gcount = 32, + /* Main header */ + keytype_extend = 33, + /* Images */ + keytype_bscale = 34, + keytype_bzero = 35, + /* Tables */ + keytype_tfields = 36, + keytype_tbcoli = 40, + keytype_tformi = 41, - /* Other primary keywords */ - keytype_primary =100, + /* Other primary keywords */ + keytype_primary = 100, - /* HIERARCH ESO keywords ordered according to DICB */ - keytype_hierarch_dpr =200, - keytype_hierarch_obs =201, - keytype_hierarch_tpl =202, - keytype_hierarch_gen =203, - keytype_hierarch_tel =204, - keytype_hierarch_ins =205, - keytype_hierarch_det =206, - keytype_hierarch_log =207, - keytype_hierarch_pro =208, - /* Other HIERARCH keywords */ - keytype_hierarch =300, + /* HIERARCH ESO keywords ordered according to DICB */ + keytype_hierarch_dpr = 200, + keytype_hierarch_obs = 201, + keytype_hierarch_tpl = 202, + keytype_hierarch_gen = 203, + keytype_hierarch_tel = 204, + keytype_hierarch_ins = 205, + keytype_hierarch_det = 206, + keytype_hierarch_log = 207, + keytype_hierarch_pro = 208, + /* Other HIERARCH keywords */ + keytype_hierarch = 300, - /* HISTORY and COMMENT */ - keytype_history =400, - keytype_comment =500, - /* END */ - keytype_end =1000 -} keytype ; + /* HISTORY and COMMENT */ + keytype_history = 400, + keytype_comment = 500, + /* END */ + keytype_end = 1000 +} keytype; /*----------------------------------------------------------------------------- Private to this module -----------------------------------------------------------------------------*/ -static keytuple * keytuple_new(const char *, const char *, const char - *, const char *); -static void keytuple_del(keytuple * k); -static void keytuple_dmp(keytuple * k); -static keytype keytuple_type(const char * key); +static keytuple *keytuple_new (const char *, const char *, const char + *, const char *); +static void keytuple_del (keytuple * k); +static void keytuple_dmp (keytuple * k); +static keytype keytuple_type (const char *key); /*----------------------------------------------------------------------------- Private functions @@ -165,40 +167,45 @@ static keytype keytuple_type(const char * key); */ /*----------------------------------------------------------------------------*/ -static keytuple * keytuple_new( - const char * key, - const char * val, - const char * com, - const char * lin) +static keytuple * +keytuple_new (const char *key, + const char *val, const char *com, const char *lin) { - keytuple * k ; + keytuple *k; - if (key==NULL) return NULL ; + if (key == NULL) + return NULL; - /* Allocate space for new structure */ - k = malloc(sizeof(keytuple)); - /* Hook a copy of the new key */ - k->key = strdup(qfits_expand_keyword(key)) ; - /* Hook a copy of the value if defined */ - k->val = NULL ; - if (val!=NULL) { - if (strlen(val)>0) k->val = strdup(val); - } - /* Hook a copy of the comment if defined */ - k->com = NULL ; - if (com!=NULL) { - if (strlen(com)>0) k->com = strdup(com) ; - } - /* Hook a copy of the initial line if defined */ - k->lin = NULL ; - if (lin!=NULL) { - if (strlen(lin)>0) k->lin = strdup(lin); - } - k->next = NULL ; - k->prev = NULL ; - k->typ = keytuple_type(key); + /* Allocate space for new structure */ + k = malloc (sizeof (keytuple)); + /* Hook a copy of the new key */ + k->key = strdup (qfits_expand_keyword (key)); + /* Hook a copy of the value if defined */ + k->val = NULL; + if (val != NULL) + { + if (strlen (val) > 0) + k->val = strdup (val); + } + /* Hook a copy of the comment if defined */ + k->com = NULL; + if (com != NULL) + { + if (strlen (com) > 0) + k->com = strdup (com); + } + /* Hook a copy of the initial line if defined */ + k->lin = NULL; + if (lin != NULL) + { + if (strlen (lin) > 0) + k->lin = strdup (lin); + } + k->next = NULL; + k->prev = NULL; + k->typ = keytuple_type (key); - return k; + return k; } /*----------------------------------------------------------------------------*/ @@ -213,43 +220,74 @@ static keytuple * keytuple_new( */ /*----------------------------------------------------------------------------*/ -static keytype keytuple_type(const char * key) +static keytype +keytuple_type (const char *key) { - keytype kt ; + keytype kt; - kt = keytype_undef ; - /* Assign type to key tuple */ - if (!strcmp(key, "SIMPLE") || !strcmp(key, "XTENSION")) kt = keytype_top ; - else if (!strcmp(key, "END")) kt = keytype_end ; - else if (!strcmp(key, "BITPIX")) kt = keytype_bitpix ; - else if (!strcmp(key, "NAXIS")) kt = keytype_naxis ; - else if (!strcmp(key, "NAXIS1")) kt = keytype_naxis1 ; - else if (!strcmp(key, "NAXIS2")) kt = keytype_naxis2 ; - else if (!strcmp(key, "NAXIS3")) kt = keytype_naxis3 ; - else if (!strcmp(key, "NAXIS4")) kt = keytype_naxis4 ; - else if (!strncmp(key, "NAXIS", 5)) kt = keytype_naxisi ; - else if (!strcmp(key, "GROUP")) kt = keytype_group ; - else if (!strcmp(key, "PCOUNT")) kt = keytype_pcount ; - else if (!strcmp(key, "GCOUNT")) kt = keytype_gcount ; - else if (!strcmp(key, "EXTEND")) kt = keytype_extend ; - else if (!strcmp(key, "BSCALE")) kt = keytype_bscale ; - else if (!strcmp(key, "BZERO")) kt = keytype_bzero ; - else if (!strcmp(key, "TFIELDS")) kt = keytype_tfields ; - else if (!strncmp(key, "TBCOL", 5)) kt = keytype_tbcoli ; - else if (!strncmp(key, "TFORM", 5)) kt = keytype_tformi ; - else if (!strncmp(key, "HIERARCH ESO DPR", 16)) kt = keytype_hierarch_dpr ; - else if (!strncmp(key, "HIERARCH ESO OBS", 16)) kt = keytype_hierarch_obs ; - else if (!strncmp(key, "HIERARCH ESO TPL", 16)) kt = keytype_hierarch_tpl ; - else if (!strncmp(key, "HIERARCH ESO GEN", 16)) kt = keytype_hierarch_gen ; - else if (!strncmp(key, "HIERARCH ESO TEL", 16)) kt = keytype_hierarch_tel ; - else if (!strncmp(key, "HIERARCH ESO INS", 16)) kt = keytype_hierarch_ins ; - else if (!strncmp(key, "HIERARCH ESO LOG", 16)) kt = keytype_hierarch_log ; - else if (!strncmp(key, "HIERARCH ESO PRO", 16)) kt = keytype_hierarch_pro ; - else if (!strncmp(key, "HIERARCH", 8)) kt = keytype_hierarch ; - else if (!strcmp(key, "HISTORY")) kt = keytype_history ; - else if (!strcmp(key, "COMMENT")) kt = keytype_comment ; - else if ((int)strlen(key)<9) kt = keytype_primary ; - return kt ; + kt = keytype_undef; + /* Assign type to key tuple */ + if (!strcmp (key, "SIMPLE") || !strcmp (key, "XTENSION")) + kt = keytype_top; + else if (!strcmp (key, "END")) + kt = keytype_end; + else if (!strcmp (key, "BITPIX")) + kt = keytype_bitpix; + else if (!strcmp (key, "NAXIS")) + kt = keytype_naxis; + else if (!strcmp (key, "NAXIS1")) + kt = keytype_naxis1; + else if (!strcmp (key, "NAXIS2")) + kt = keytype_naxis2; + else if (!strcmp (key, "NAXIS3")) + kt = keytype_naxis3; + else if (!strcmp (key, "NAXIS4")) + kt = keytype_naxis4; + else if (!strncmp (key, "NAXIS", 5)) + kt = keytype_naxisi; + else if (!strcmp (key, "GROUP")) + kt = keytype_group; + else if (!strcmp (key, "PCOUNT")) + kt = keytype_pcount; + else if (!strcmp (key, "GCOUNT")) + kt = keytype_gcount; + else if (!strcmp (key, "EXTEND")) + kt = keytype_extend; + else if (!strcmp (key, "BSCALE")) + kt = keytype_bscale; + else if (!strcmp (key, "BZERO")) + kt = keytype_bzero; + else if (!strcmp (key, "TFIELDS")) + kt = keytype_tfields; + else if (!strncmp (key, "TBCOL", 5)) + kt = keytype_tbcoli; + else if (!strncmp (key, "TFORM", 5)) + kt = keytype_tformi; + else if (!strncmp (key, "HIERARCH ESO DPR", 16)) + kt = keytype_hierarch_dpr; + else if (!strncmp (key, "HIERARCH ESO OBS", 16)) + kt = keytype_hierarch_obs; + else if (!strncmp (key, "HIERARCH ESO TPL", 16)) + kt = keytype_hierarch_tpl; + else if (!strncmp (key, "HIERARCH ESO GEN", 16)) + kt = keytype_hierarch_gen; + else if (!strncmp (key, "HIERARCH ESO TEL", 16)) + kt = keytype_hierarch_tel; + else if (!strncmp (key, "HIERARCH ESO INS", 16)) + kt = keytype_hierarch_ins; + else if (!strncmp (key, "HIERARCH ESO LOG", 16)) + kt = keytype_hierarch_log; + else if (!strncmp (key, "HIERARCH ESO PRO", 16)) + kt = keytype_hierarch_pro; + else if (!strncmp (key, "HIERARCH", 8)) + kt = keytype_hierarch; + else if (!strcmp (key, "HISTORY")) + kt = keytype_history; + else if (!strcmp (key, "COMMENT")) + kt = keytype_comment; + else if ((int) strlen (key) < 9) + kt = keytype_primary; + return kt; } /*----------------------------------------------------------------------------*/ @@ -260,14 +298,20 @@ static keytype keytuple_type(const char * key) Keytuple destructor. */ /*----------------------------------------------------------------------------*/ -static void keytuple_del(keytuple * k) +static void +keytuple_del (keytuple * k) { - if (k==NULL) return ; - if (k->key) free(k->key); - if (k->val) free(k->val); - if (k->com) free(k->com); - if (k->lin) free(k->lin); - free(k); + if (k == NULL) + return; + if (k->key) + free (k->key); + if (k->val) + free (k->val); + if (k->com) + free (k->com); + if (k->lin) + free (k->lin); + free (k); } /*----------------------------------------------------------------------------*/ @@ -280,15 +324,19 @@ static void keytuple_del(keytuple * k) purposes only. */ /*----------------------------------------------------------------------------*/ -static void keytuple_dmp(keytuple * k) +static void +keytuple_dmp (keytuple * k) { - if (!k) return ; - printf("[%s]=[", k->key); - if (k->val) printf("%s", k->val); - printf("]"); - if (k->com) printf("/[%s]", k->com); - printf("\n"); - return ; + if (!k) + return; + printf ("[%s]=[", k->key); + if (k->val) + printf ("%s", k->val); + printf ("]"); + if (k->com) + printf ("/[%s]", k->com); + printf ("\n"); + return; } /*----------------------------------------------------------------------------- @@ -304,14 +352,15 @@ static void keytuple_dmp(keytuple * k) an allocated linked-list handler with an empty card list. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_new(void) +qfits_header * +qfits_header_new (void) { - qfits_header * h ; - h = malloc(sizeof(qfits_header)); - h->first = NULL ; - h->last = NULL ; - h->n = 0 ; - return h; + qfits_header *h; + h = malloc (sizeof (qfits_header)); + h->first = NULL; + h->last = NULL; + h->n = 0; + return h; } /*----------------------------------------------------------------------------*/ @@ -325,13 +374,14 @@ qfits_header * qfits_header_new(void) */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_default(void) +qfits_header * +qfits_header_default (void) { - qfits_header * h ; - h = qfits_header_new() ; - qfits_header_append(h, "SIMPLE", "T", "Fits format", NULL); - qfits_header_append(h, "END", NULL, NULL, NULL); - return h; + qfits_header *h; + h = qfits_header_new (); + qfits_header_append (h, "SIMPLE", "T", "Fits format", NULL); + qfits_header_append (h, "END", NULL, NULL, NULL); + return h; } /*----------------------------------------------------------------------------*/ @@ -350,46 +400,49 @@ qfits_header * qfits_header_default(void) are allowed to get NULL values. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add( - qfits_header * hdr, - char * key, - char * val, - char * com, - char * lin) +void +qfits_header_add (qfits_header * hdr, + char *key, char *val, char *com, char *lin) { - keytuple * k ; - keytuple * kbf ; - keytuple * first ; - keytuple * last ; + keytuple *k; + keytuple *kbf; + keytuple *first; + keytuple *last; - if (hdr==NULL || key==NULL) return ; - if (hdr->n<2) return ; + if (hdr == NULL || key == NULL) + return; + if (hdr->n < 2) + return; - first = (keytuple*)hdr->first ; - last = (keytuple*)hdr->last ; + first = (keytuple *) hdr->first; + last = (keytuple *) hdr->last; - if (((keytype)first->typ != keytype_top) || - ((keytype)last->typ != keytype_end)) return ; - - /* Create new key tuple */ - k = keytuple_new(key, val, com, lin); + if (((keytype) first->typ != keytype_top) || + ((keytype) last->typ != keytype_end)) + return; - /* Find the last keytuple with same key type */ - kbf = first ; - while (kbf!=NULL) { - if ((k->typ>=kbf->typ) && (k->typnext->typ)) break ; - kbf = kbf->next ; - } - if (kbf==NULL) kbf = last->prev ; + /* Create new key tuple */ + k = keytuple_new (key, val, com, lin); - /* Hook it into list */ - k->next = kbf->next ; - (kbf->next)->prev = k ; - kbf->next = k ; - k->prev = kbf ; + /* Find the last keytuple with same key type */ + kbf = first; + while (kbf != NULL) + { + if ((k->typ >= kbf->typ) && (k->typ < kbf->next->typ)) + break; + kbf = kbf->next; + } + if (kbf == NULL) + kbf = last->prev; - hdr->n ++ ; - return ; + /* Hook it into list */ + k->next = kbf->next; + (kbf->next)->prev = k; + kbf->next = k; + k->prev = kbf; + + hdr->n++; + return; } /*----------------------------------------------------------------------------*/ @@ -408,36 +461,37 @@ void qfits_header_add( can be NULL, except after and key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add_after( - qfits_header * hdr, - char * after, - char * key, - char * val, - char * com, - char * lin) +void +qfits_header_add_after (qfits_header * hdr, + char *after, + char *key, char *val, char *com, char *lin) { - keytuple * kreq; - keytuple * k; - char * exp_after ; + keytuple *kreq; + keytuple *k; + char *exp_after; - if (hdr==NULL || after==NULL || key==NULL) return ; + if (hdr == NULL || after == NULL || key == NULL) + return; - exp_after = qfits_expand_keyword(after); - /* Locate where the entry is requested */ - kreq = (keytuple*)(hdr->first) ; - while (kreq!=NULL) { - if (!strcmp(kreq->key, exp_after)) break ; - kreq = kreq->next ; - } - if (kreq==NULL) return ; - k = keytuple_new(key, val, com, lin); + exp_after = qfits_expand_keyword (after); + /* Locate where the entry is requested */ + kreq = (keytuple *) (hdr->first); + while (kreq != NULL) + { + if (!strcmp (kreq->key, exp_after)) + break; + kreq = kreq->next; + } + if (kreq == NULL) + return; + k = keytuple_new (key, val, com, lin); - k->next = kreq->next ; - kreq->next->prev = k ; - kreq->next = k ; - k->prev = kreq ; - hdr->n ++ ; - return ; + k->next = kreq->next; + kreq->next->prev = k; + kreq->next = k; + k->prev = kreq; + hdr->n++; + return; } /*----------------------------------------------------------------------------*/ @@ -454,30 +508,30 @@ void qfits_header_add_after( NULL except key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_append( - qfits_header * hdr, - const char * key, - const char * val, - const char * com, - const char * lin) +void +qfits_header_append (qfits_header * hdr, + const char *key, + const char *val, const char *com, const char *lin) { - keytuple * k; - keytuple * last ; + keytuple *k; + keytuple *last; - if (hdr==NULL || key==NULL) return ; + if (hdr == NULL || key == NULL) + return; - k = keytuple_new(key, val, com, lin); - if (hdr->n==0) { - hdr->first = hdr->last = k ; - hdr->n = 1 ; - return ; - } - last = (keytuple*)hdr->last ; - last->next = k ; - k->prev = last ; - hdr->last = k ; - hdr->n++ ; - return ; + k = keytuple_new (key, val, com, lin); + if (hdr->n == 0) + { + hdr->first = hdr->last = k; + hdr->n = 1; + return; + } + last = (keytuple *) hdr->last; + last->next = k; + k->prev = last; + hdr->last = k; + hdr->n++; + return; } /*----------------------------------------------------------------------------*/ @@ -491,29 +545,36 @@ void qfits_header_append( the key is removed. */ /*----------------------------------------------------------------------------*/ -void qfits_header_del(qfits_header * hdr, char * key) +void +qfits_header_del (qfits_header * hdr, char *key) { - keytuple * k ; - char * xkey ; + keytuple *k; + char *xkey; - if (hdr==NULL || key==NULL) return ; + if (hdr == NULL || key == NULL) + return; - xkey = qfits_expand_keyword(key); - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strcmp(k->key, xkey)) break ; - k = k->next ; - } - if (k==NULL) - return ; - if(k == hdr->first) { - hdr->first = k->next ; - } else { - k->prev->next = k->next ; - k->next->prev = k->prev ; + xkey = qfits_expand_keyword (key); + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strcmp (k->key, xkey)) + break; + k = k->next; } - keytuple_del(k); - return ; + if (k == NULL) + return; + if (k == hdr->first) + { + hdr->first = k->next; + } + else + { + k->prev->next = k->next; + k->next->prev = k->prev; + } + keytuple_del (k); + return; } /*----------------------------------------------------------------------------*/ @@ -530,38 +591,46 @@ void qfits_header_del(qfits_header * hdr, char * key) line is set to NULL in the card. */ /*----------------------------------------------------------------------------*/ -void qfits_header_mod( - qfits_header * hdr, - char * key, - char * val, - char * com) +void +qfits_header_mod (qfits_header * hdr, char *key, char *val, char *com) { - keytuple * k ; - char * xkey ; + keytuple *k; + char *xkey; - if (hdr==NULL || key==NULL) return ; + if (hdr == NULL || key == NULL) + return; - xkey = qfits_expand_keyword(key); - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strcmp(k->key, xkey)) break ; - k=k->next ; - } - if (k==NULL) return ; - - if (k->val) free(k->val); - if (k->com) free(k->com); - if (k->lin) free(k->lin); - k->val = NULL ; - k->com = NULL ; - k->lin = NULL ; - if (val) { - if (strlen(val)>0) k->val = strdup(val); + xkey = qfits_expand_keyword (key); + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strcmp (k->key, xkey)) + break; + k = k->next; } - if (com) { - if (strlen(com)>0) k->com = strdup(com); + if (k == NULL) + return; + + if (k->val) + free (k->val); + if (k->com) + free (k->com); + if (k->lin) + free (k->lin); + k->val = NULL; + k->com = NULL; + k->lin = NULL; + if (val) + { + if (strlen (val) > 0) + k->val = strdup (val); } - return ; + if (com) + { + if (strlen (com) > 0) + k->com = strdup (com); + } + return; } /*----------------------------------------------------------------------------*/ @@ -571,66 +640,78 @@ void qfits_header_mod( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_header_sort(qfits_header ** hdr) +int +qfits_header_sort (qfits_header ** hdr) { - qfits_header * sorted ; - keytuple * k ; - keytuple * kbf ; - keytuple * next ; - keytuple * last ; + qfits_header *sorted; + keytuple *k; + keytuple *kbf; + keytuple *next; + keytuple *last; - /* Test entries */ - if (hdr == NULL) return -1 ; - if (*hdr == NULL) return -1 ; - if ((*hdr)->n < 2) return 0 ; - - /* Create the new FITS header */ - sorted = qfits_header_new() ; + /* Test entries */ + if (hdr == NULL) + return -1; + if (*hdr == NULL) + return -1; + if ((*hdr)->n < 2) + return 0; - /* Move the first keytuple to the sorted empty header */ - k = (keytuple*)(*hdr)->first ; - next = k->next ; - sorted->first = sorted->last = k ; - k->next = k->prev = NULL ; - sorted->n = 1 ; - - /* Loop over the other tuples */ - while (next != NULL) { - k = next ; - next = k->next ; + /* Create the new FITS header */ + sorted = qfits_header_new (); - /* Find k's place in sorted */ - kbf = (keytuple*)sorted->first ; - while (kbf!=NULL) { - if (k->typ < kbf->typ) break ; - kbf = kbf->next ; - } - - /* Hook k into sorted list */ - if (kbf == NULL) { - /* k is last in sorted */ - last = sorted->last ; - sorted->last = k ; - k->next = NULL ; - k->prev = last ; - last->next = k ; - } else { - /* k goes just before kbf */ - k->next = kbf ; - k->prev = kbf->prev ; - if (kbf->prev != NULL) (kbf->prev)->next = k ; - else sorted->first = k ; - kbf->prev = k ; - } - (sorted->n) ++ ; + /* Move the first keytuple to the sorted empty header */ + k = (keytuple *) (*hdr)->first; + next = k->next; + sorted->first = sorted->last = k; + k->next = k->prev = NULL; + sorted->n = 1; + + /* Loop over the other tuples */ + while (next != NULL) + { + k = next; + next = k->next; + + /* Find k's place in sorted */ + kbf = (keytuple *) sorted->first; + while (kbf != NULL) + { + if (k->typ < kbf->typ) + break; + kbf = kbf->next; } - /* Replace the input header by the sorted one */ - (*hdr)->first = (*hdr)->last = NULL ; - qfits_header_destroy(*hdr) ; - *hdr = sorted ; - - return 0 ; + /* Hook k into sorted list */ + if (kbf == NULL) + { + /* k is last in sorted */ + last = sorted->last; + sorted->last = k; + k->next = NULL; + k->prev = last; + last->next = k; + } + else + { + /* k goes just before kbf */ + k->next = kbf; + k->prev = kbf->prev; + if (kbf->prev != NULL) + (kbf->prev)->next = k; + else + sorted->first = k; + kbf->prev = k; + } + (sorted->n)++; + } + + /* Replace the input header by the sorted one */ + (*hdr)->first = (*hdr)->last = NULL; + qfits_header_destroy (*hdr); + *hdr = sorted; + + return 0; } /*----------------------------------------------------------------------------*/ @@ -643,20 +724,23 @@ int qfits_header_sort(qfits_header ** hdr) header. The returned header must be freed using qfits_header_destroy. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_copy(qfits_header * src) +qfits_header * +qfits_header_copy (qfits_header * src) { - qfits_header * fh_copy ; - keytuple * k ; + qfits_header *fh_copy; + keytuple *k; - if (src==NULL) return NULL ; + if (src == NULL) + return NULL; - fh_copy = qfits_header_new(); - k = (keytuple*)src->first ; - while (k!=NULL) { - qfits_header_append(fh_copy, k->key, k->val, k->com, k->lin) ; - k = k->next ; - } - return fh_copy ; + fh_copy = qfits_header_new (); + k = (keytuple *) src->first; + while (k != NULL) + { + qfits_header_append (fh_copy, k->key, k->val, k->com, k->lin); + k = k->next; + } + return fh_copy; } /*----------------------------------------------------------------------------*/ @@ -669,20 +753,24 @@ qfits_header * qfits_header_copy(qfits_header * src) freed and set to NULL. Useful when a header needs to be reformatted. */ /*----------------------------------------------------------------------------*/ -void qfits_header_touchall(qfits_header * hdr) +void +qfits_header_touchall (qfits_header * hdr) { - keytuple * k ; + keytuple *k; - if (hdr==NULL) return ; - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (k->lin!=NULL) { - free(k->lin); - k->lin=NULL ; - } - k=k->next ; + if (hdr == NULL) + return; + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (k->lin != NULL) + { + free (k->lin); + k->lin = NULL; } - return ; + k = k->next; + } + return; } /*----------------------------------------------------------------------------*/ @@ -693,18 +781,21 @@ void qfits_header_touchall(qfits_header * hdr) Dump a FITS header to stdout. Mostly for debugging purposes. */ /*----------------------------------------------------------------------------*/ -void qfits_header_consoledump(qfits_header * hdr) +void +qfits_header_consoledump (qfits_header * hdr) { - keytuple * k ; + keytuple *k; - if (hdr==NULL) return ; - k = (keytuple*)hdr->first ; - printf("------------------------------------\n"); - while (k!=NULL) { - keytuple_dmp(k) ; - k=k->next ; - } - return ; + if (hdr == NULL) + return; + k = (keytuple *) hdr->first; + printf ("------------------------------------\n"); + while (k != NULL) + { + keytuple_dmp (k); + k = k->next; + } + return; } /*----------------------------------------------------------------------------*/ @@ -716,21 +807,24 @@ void qfits_header_consoledump(qfits_header * hdr) Frees all memory associated to a given qfits_header object. */ /*----------------------------------------------------------------------------*/ -void qfits_header_destroy(qfits_header * hdr) +void +qfits_header_destroy (qfits_header * hdr) { - keytuple * k ; - keytuple * kn ; + keytuple *k; + keytuple *kn; - if (hdr==NULL) return ; + if (hdr == NULL) + return; - k = (keytuple*)hdr->first ; - while (k!=NULL) { - kn = k->next ; - keytuple_del(k); - k = kn ; - } - free(hdr); - return ; + k = (keytuple *) hdr->first; + while (k != NULL) + { + kn = k->next; + keytuple_del (k); + k = kn; + } + free (hdr); + return; } /*----------------------------------------------------------------------------*/ @@ -747,21 +841,26 @@ void qfits_header_destroy(qfits_header * hdr) Returns NULL if no matching key is found or no value is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getstr(qfits_header * hdr, const char * key) +char * +qfits_header_getstr (qfits_header * hdr, const char *key) { - keytuple * k ; - char * xkey ; + keytuple *k; + char *xkey; - if (hdr==NULL || key==NULL) return NULL ; + if (hdr == NULL || key == NULL) + return NULL; - xkey = qfits_expand_keyword(key); - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strcmp(k->key, xkey)) break ; - k=k->next ; - } - if (k==NULL) return NULL ; - return k->val ; + xkey = qfits_expand_keyword (key); + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strcmp (k->key, xkey)) + break; + k = k->next; + } + if (k == NULL) + return NULL; + return k->val; } /*----------------------------------------------------------------------------*/ @@ -786,19 +885,24 @@ char * qfits_header_getstr(qfits_header * hdr, const char * key) @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_header_findmatch(qfits_header * hdr, char * key) +char * +qfits_header_findmatch (qfits_header * hdr, char *key) { - keytuple * k ; + keytuple *k; - if (hdr==NULL || key==NULL) return NULL ; + if (hdr == NULL || key == NULL) + return NULL; - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strncmp(k->key, key, (int)strlen(key))) break ; - k=k->next ; - } - if (k==NULL) return NULL ; - return k->key ; + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strncmp (k->key, key, (int) strlen (key))) + break; + k = k->next; + } + if (k == NULL) + return NULL; + return k->key; } /*----------------------------------------------------------------------------*/ @@ -838,41 +942,51 @@ char * qfits_header_findmatch(qfits_header * hdr, char * key) available in this module. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getitem( - qfits_header * hdr, - int idx, - char * key, - char * val, - char * com, - char * lin) +int +qfits_header_getitem (qfits_header * hdr, + int idx, char *key, char *val, char *com, char *lin) { - keytuple * k ; - int count ; + keytuple *k; + int count; - if (hdr==NULL) return -1 ; - if (key==NULL && val==NULL && com==NULL && lin==NULL) return 0 ; - if (idx<0 || idx>hdr->n) return -1 ; + if (hdr == NULL) + return -1; + if (key == NULL && val == NULL && com == NULL && lin == NULL) + return 0; + if (idx < 0 || idx > hdr->n) + return -1; - count=0 ; - k = (keytuple*)hdr->first ; - while (countnext ; - count++ ; - } - if (key!=NULL) strcpy(key, k->key); - if (val!=NULL) { - if (k->val!=NULL) strcpy(val, k->val); - else val[0]=0 ; - } - if (com!=NULL) { - if (k->com!=NULL) strcpy(com, k->com); - else com[0]=0 ; - } - if (lin!=NULL) { - if (k->lin!=NULL) strcpy(lin, k->lin); - else lin[0]=0 ; - } - return 0 ; + count = 0; + k = (keytuple *) hdr->first; + while (count < idx) + { + k = k->next; + count++; + } + if (key != NULL) + strcpy (key, k->key); + if (val != NULL) + { + if (k->val != NULL) + strcpy (val, k->val); + else + val[0] = 0; + } + if (com != NULL) + { + if (k->com != NULL) + strcpy (com, k->com); + else + com[0] = 0; + } + if (lin != NULL) + { + if (k->lin != NULL) + strcpy (lin, k->lin); + else + lin[0] = 0; + } + return 0; } /*----------------------------------------------------------------------------*/ @@ -889,21 +1003,26 @@ int qfits_header_getitem( Returns NULL if no matching key is found or no line is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getline(qfits_header * hdr, char * key) +char * +qfits_header_getline (qfits_header * hdr, char *key) { - keytuple * k ; - char * xkey ; + keytuple *k; + char *xkey; - if (hdr==NULL || key==NULL) return NULL ; + if (hdr == NULL || key == NULL) + return NULL; - xkey = qfits_expand_keyword(key); - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strcmp(k->key, xkey)) break ; - k=k->next ; - } - if (k==NULL) return NULL ; - return k->lin ; + xkey = qfits_expand_keyword (key); + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strcmp (k->key, xkey)) + break; + k = k->next; + } + if (k == NULL) + return NULL; + return k->lin; } /*----------------------------------------------------------------------------*/ @@ -921,21 +1040,26 @@ char * qfits_header_getline(qfits_header * hdr, char * key) Returns NULL if no matching key is found or no comment is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getcom(qfits_header * hdr, char * key) +char * +qfits_header_getcom (qfits_header * hdr, char *key) { - keytuple * k ; - char * xkey ; + keytuple *k; + char *xkey; - if (hdr==NULL || key==NULL) return NULL ; + if (hdr == NULL || key == NULL) + return NULL; - xkey = qfits_expand_keyword(key); - k = (keytuple*)hdr->first ; - while (k!=NULL) { - if (!strcmp(k->key, xkey)) break ; - k=k->next ; - } - if (k==NULL) return NULL ; - return k->com ; + xkey = qfits_expand_keyword (key); + k = (keytuple *) hdr->first; + while (k != NULL) + { + if (!strcmp (k->key, xkey)) + break; + k = k->next; + } + if (k == NULL) + return NULL; + return k->com; } /*----------------------------------------------------------------------------*/ @@ -951,17 +1075,21 @@ char * qfits_header_getcom(qfits_header * hdr, char * key) attached. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getint(qfits_header * hdr, const char * key, int errval) +int +qfits_header_getint (qfits_header * hdr, const char *key, int errval) { - char * c ; - int d ; + char *c; + int d; - if (hdr==NULL || key==NULL) return errval ; + if (hdr == NULL || key == NULL) + return errval; - c = qfits_header_getstr(hdr, key); - if (c==NULL) return errval ; - if (sscanf(c, "%d", &d)!=1) return errval ; - return d ; + c = qfits_header_getstr (hdr, key); + if (c == NULL) + return errval; + if (sscanf (c, "%d", &d) != 1) + return errval; + return d; } /*----------------------------------------------------------------------------*/ @@ -977,15 +1105,18 @@ int qfits_header_getint(qfits_header * hdr, const char * key, int errval) attached. */ /*----------------------------------------------------------------------------*/ -double qfits_header_getdouble(qfits_header * hdr, const char * key, double errval) +double +qfits_header_getdouble (qfits_header * hdr, const char *key, double errval) { - char * c ; + char *c; - if (hdr==NULL || key==NULL) return errval ; + if (hdr == NULL || key == NULL) + return errval; - c = qfits_header_getstr(hdr, key); - if (c==NULL) return errval ; - return atof(c); + c = qfits_header_getstr (hdr, key); + if (c == NULL) + return errval; + return atof (c); } /*----------------------------------------------------------------------------*/ @@ -1010,25 +1141,35 @@ double qfits_header_getdouble(qfits_header * hdr, const char * key, double errva beginning with a 'n' (no), a 'f' (false) or the digit '0'. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) +int +qfits_header_getboolean (qfits_header * hdr, const char *key, int errval) { - char * c ; - int ret ; + char *c; + int ret; - if (hdr==NULL || key==NULL) return errval ; + if (hdr == NULL || key == NULL) + return errval; - c = qfits_header_getstr(hdr, key); - if (c==NULL) return errval ; - if (strlen(c)<1) return errval ; + c = qfits_header_getstr (hdr, key); + if (c == NULL) + return errval; + if (strlen (c) < 1) + return errval; - if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') { - ret = 1 ; - } else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') { - ret = 0 ; - } else { - ret = errval ; - } - return ret; + if (c[0] == 'y' || c[0] == 'Y' || c[0] == '1' || c[0] == 't' || c[0] == 'T') + { + ret = 1; + } + else if (c[0] == 'n' || c[0] == 'N' || c[0] == '0' || c[0] == 'f' + || c[0] == 'F') + { + ret = 0; + } + else + { + ret = errval; + } + return ret; } /*----------------------------------------------------------------------------*/ @@ -1045,35 +1186,37 @@ int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) If conservative is set to 0, a new line will be formatted. */ /*----------------------------------------------------------------------------*/ -static int qfits_header_makeline( - char * line, - keytuple * k, - int conservative) +static int +qfits_header_makeline (char *line, keytuple * k, int conservative) { - char blankline[81]; - int i ; + char blankline[81]; + int i; - if (line==NULL || k==NULL) return -1 ; + if (line == NULL || k == NULL) + return -1; - /* If a previous line information is there, use it as is */ - if (conservative) { - if (k->lin != NULL) { - memcpy(line, k->lin, 80); - line[80]=(char)0; - return 0 ; - } + /* If a previous line information is there, use it as is */ + if (conservative) + { + if (k->lin != NULL) + { + memcpy (line, k->lin, 80); + line[80] = (char) 0; + return 0; } - /* Got to build keyword from scratch */ - memset(blankline, 0, 81); - keytuple2str(blankline, k->key, k->val, k->com); - memset(line, ' ', 80); - i=0 ; - while (blankline[i] != (char)0) { - line[i] = blankline[i] ; - i++ ; } - line[80]=(char)0; - return 0; + /* Got to build keyword from scratch */ + memset (blankline, 0, 81); + keytuple2str (blankline, k->key, k->val, k->com); + memset (line, ' ', 80); + i = 0; + while (blankline[i] != (char) 0) + { + line[i] = blankline[i]; + i++; + } + line[80] = (char) 0; + return 0; } /*----------------------------------------------------------------------------*/ @@ -1090,114 +1233,136 @@ static int qfits_header_makeline( Formatting is done according to FITS standard. */ /*----------------------------------------------------------------------------*/ -void keytuple2str( - char * line, - char * key, - char * val, - char * com) +void +keytuple2str (char *line, char *key, char *val, char *com) { - 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 ; + 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 ; + if (line == NULL || key == NULL) + return; - /* Set the line with zeroes */ - memset(line, ' ', 80); - if (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 ; + /* 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); } - /* 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 ; + else + { + sprintf (safe_line, "%-8.8s= / %-48s", key, + ccom); } + strncpy (line, safe_line, 80); + line[80] = (char) 0; + 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 ; + /* 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++; + } - /* 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 ; - } + 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); - /* 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 ; + /* Null-terminate in any case */ + line[80] = (char) 0; + return; } /*----------------------------------------------------------------------------*/ @@ -1209,39 +1374,44 @@ void keytuple2str( Dumps a FITS header to an opened file pointer. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump( - qfits_header * hdr, - FILE * out) +int +qfits_header_dump (qfits_header * hdr, FILE * out) { - keytuple * k ; - char line[81]; - int n_out ; + keytuple *k; + char line[81]; + int n_out; - if (hdr==NULL) return -1 ; - if (out==NULL) out=stdout ; + if (hdr == NULL) + return -1; + if (out == NULL) + out = stdout; - k = (keytuple*)hdr->first ; - n_out = 0 ; - while (k!=NULL) { - /* Make line from information in the node */ - qfits_header_makeline(line, k, 1); - if ((fwrite(line, 1, 80, out))!=80) { - fprintf(stderr, "error dumping FITS header"); - return -1 ; - } - n_out ++; - k=k->next; + k = (keytuple *) hdr->first; + n_out = 0; + while (k != NULL) + { + /* Make line from information in the node */ + qfits_header_makeline (line, k, 1); + if ((fwrite (line, 1, 80, out)) != 80) + { + fprintf (stderr, "error dumping FITS header"); + return -1; + } + n_out++; + k = k->next; } - /* If printing out to a regular file, blank pad */ - if (out!=stdout && out!=stderr) { - /* Blank-pad the output */ - memset(line, ' ', 80); - while (n_out % 36) { - fwrite(line, 1, 80, out); - n_out++ ; - } + /* If printing out to a regular file, blank pad */ + if (out != stdout && out != stderr) + { + /* Blank-pad the output */ + memset (line, ' ', 80); + while (n_out % 36) + { + fwrite (line, 1, 80, out); + n_out++; + } } - return 0 ; + return 0; } /*----------------------------------------------------------------------------*/ @@ -1255,28 +1425,30 @@ int qfits_header_dump( This function is meant to create hdr files. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump_hdr( - qfits_header * hdr, - FILE * out) +int +qfits_header_dump_hdr (qfits_header * hdr, FILE * out) { - keytuple * k ; - char line[81]; - int n_out ; + keytuple *k; + char line[81]; + int n_out; - if (hdr==NULL) return -1 ; - if (out==NULL) out=stdout ; + if (hdr == NULL) + return -1; + if (out == NULL) + out = stdout; - k = (keytuple*)hdr->first ; - n_out = 0 ; - while (k!=NULL) { - /* Make line from information in the node */ - qfits_header_makeline(line, k, 1); - fprintf(out, line) ; - fprintf(out, "\n") ; - n_out ++; - k=k->next; + k = (keytuple *) hdr->first; + n_out = 0; + while (k != NULL) + { + /* Make line from information in the node */ + qfits_header_makeline (line, k, 1); + fprintf (out, line); + fprintf (out, "\n"); + n_out++; + k = k->next; } - return 0 ; + return 0; } /*----------------------------------------------------------------------------*/ @@ -1295,40 +1467,44 @@ int qfits_header_dump_hdr( 'hsize'. The returned block must be deallocated using free(). */ /*----------------------------------------------------------------------------*/ -char * qfits_header_to_memblock(qfits_header * fh, int * hsize) +char * +qfits_header_to_memblock (qfits_header * fh, int *hsize) { - char * buf ; - int sz ; - int nblocks ; - int ncards ; - keytuple * k ; - char * start ; + char *buf; + int sz; + int nblocks; + int ncards; + keytuple *k; + char *start; - if (fh==NULL || hsize==NULL) return NULL ; + if (fh == NULL || hsize == NULL) + return NULL; - /* Count number of blocks in input (1 block is 36 cards) */ - ncards = fh->n ; - nblocks = 1 ; - while (ncards>36) { - nblocks ++ ; - ncards -= 36 ; - } - sz = nblocks * 2880 ; - /* Allocate buffer and fill it with blanks */ - buf = malloc(sz * sizeof(char)); - memset(buf, ' ', sz); + /* Count number of blocks in input (1 block is 36 cards) */ + ncards = fh->n; + nblocks = 1; + while (ncards > 36) + { + nblocks++; + ncards -= 36; + } + sz = nblocks * 2880; + /* Allocate buffer and fill it with blanks */ + buf = malloc (sz * sizeof (char)); + memset (buf, ' ', sz); - /* Iterate on all cards in the header, dump them into buf */ - start = buf ; - k = (keytuple*)fh->first ; - while (k!=NULL) { - qfits_header_makeline(start, k, 1); - k=k->next ; - start+=80 ; - } - /* Update output buffer size */ - *hsize = sz ; - return buf ; + /* Iterate on all cards in the header, dump them into buf */ + start = buf; + k = (keytuple *) fh->first; + while (k != NULL) + { + qfits_header_makeline (start, k, 1); + k = k->next; + start += 80; + } + /* Update output buffer size */ + *hsize = sz; + return buf; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/fits_h.h b/qfits/src/fits_h.h index fe259a7..8fa30aa 100644 --- a/qfits/src/fits_h.h +++ b/qfits/src/fits_h.h @@ -23,7 +23,8 @@ #define FITS_HEADER_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*----------------------------------------------------------------------------- @@ -50,11 +51,12 @@ extern "C" { accessor functions. */ /*----------------------------------------------------------------------------*/ -typedef struct qfits_header { - void * first ; /* Pointer to list start */ - void * last ; /* Pointer to list end */ - int n ; /* Number of cards in list */ -} qfits_header ; + typedef struct qfits_header + { + void *first; /* Pointer to list start */ + void *last; /* Pointer to list end */ + int n; /* Number of cards in list */ + } qfits_header; /*----------------------------------------------------------------------------- Function ANSI prototypes @@ -69,7 +71,7 @@ typedef struct qfits_header { an allocated linked-list handler with an empty card list. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_new(void) ; + qfits_header *qfits_header_new (void); /*----------------------------------------------------------------------------*/ /** @@ -81,7 +83,7 @@ qfits_header * qfits_header_new(void) ; (SIMPLE=T) and the last one (END). */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_default(void) ; + qfits_header *qfits_header_default (void); /*----------------------------------------------------------------------------*/ /** @@ -99,12 +101,8 @@ qfits_header * qfits_header_default(void) ; are allowed to get NULL values. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add( - qfits_header * hdr, - char * key, - char * val, - char * com, - char * lin) ; + void qfits_header_add (qfits_header * hdr, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -122,13 +120,9 @@ void qfits_header_add( can be NULL, except after and key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_add_after( - qfits_header * hdr, - char * after, - char * key, - char * val, - char * com, - char * lin) ; + void qfits_header_add_after (qfits_header * hdr, + char *after, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -144,12 +138,10 @@ void qfits_header_add_after( NULL except key. */ /*----------------------------------------------------------------------------*/ -void qfits_header_append( - qfits_header * hdr, - const char * key, - const char * val, - const char * com, - const char * lin) ; + void qfits_header_append (qfits_header * hdr, + const char *key, + const char *val, + const char *com, const char *lin); /*----------------------------------------------------------------------------*/ /** @@ -162,7 +154,7 @@ void qfits_header_append( the key is removed. */ /*----------------------------------------------------------------------------*/ -void qfits_header_del(qfits_header * hdr, char * key) ; + void qfits_header_del (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -178,7 +170,7 @@ void qfits_header_del(qfits_header * hdr, char * key) ; line is set to NULL in the card. */ /*----------------------------------------------------------------------------*/ -void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ; + void qfits_header_mod (qfits_header * hdr, char *key, char *val, char *com); /*----------------------------------------------------------------------------*/ /** @@ -187,7 +179,7 @@ void qfits_header_mod(qfits_header * hdr, char * key, char * val, char * com) ; @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_header_sort(qfits_header ** hdr) ; + int qfits_header_sort (qfits_header ** hdr); /*----------------------------------------------------------------------------*/ /** @@ -199,7 +191,7 @@ int qfits_header_sort(qfits_header ** hdr) ; header. The returned header must be freed using qfits_header_destroy. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_copy(qfits_header * src) ; + qfits_header *qfits_header_copy (qfits_header * src); /*----------------------------------------------------------------------------*/ /** @@ -211,7 +203,7 @@ qfits_header * qfits_header_copy(qfits_header * src) ; freed and set to NULL. Useful when a header needs to be reformatted. */ /*----------------------------------------------------------------------------*/ -void qfits_header_touchall(qfits_header * hdr) ; + void qfits_header_touchall (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -222,7 +214,7 @@ void qfits_header_touchall(qfits_header * hdr) ; Dump a FITS header to stdout. Mostly for debugging purposes. */ /*----------------------------------------------------------------------------*/ -void qfits_header_consoledump(qfits_header * hdr) ; + void qfits_header_consoledump (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -233,7 +225,7 @@ void qfits_header_consoledump(qfits_header * hdr) ; Frees all memory associated to a given qfits_header object. */ /*----------------------------------------------------------------------------*/ -void qfits_header_destroy(qfits_header * hdr) ; + void qfits_header_destroy (qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -249,7 +241,7 @@ void qfits_header_destroy(qfits_header * hdr) ; Returns NULL if no matching key is found or no value is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getstr(qfits_header * hdr, const char * key) ; + char *qfits_header_getstr (qfits_header * hdr, const char *key); /*----------------------------------------------------------------------------*/ /** @@ -273,7 +265,7 @@ char * qfits_header_getstr(qfits_header * hdr, const char * key) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_header_findmatch(qfits_header * hdr, char * key) ; + char *qfits_header_findmatch (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -312,13 +304,9 @@ char * qfits_header_findmatch(qfits_header * hdr, char * key) ; available in this module. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getitem( - qfits_header * hdr, - int idx, - char * key, - char * val, - char * com, - char * lin) ; + int qfits_header_getitem (qfits_header * hdr, + int idx, + char *key, char *val, char *com, char *lin); /*----------------------------------------------------------------------------*/ /** @@ -334,7 +322,7 @@ int qfits_header_getitem( Returns NULL if no matching key is found or no line is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getline(qfits_header * hdr, char * key) ; + char *qfits_header_getline (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -351,7 +339,7 @@ char * qfits_header_getline(qfits_header * hdr, char * key) ; Returns NULL if no matching key is found or no comment is attached. */ /*----------------------------------------------------------------------------*/ -char * qfits_header_getcom(qfits_header * hdr, char * key) ; + char *qfits_header_getcom (qfits_header * hdr, char *key); /*----------------------------------------------------------------------------*/ /** @@ -366,7 +354,7 @@ char * qfits_header_getcom(qfits_header * hdr, char * key) ; attached. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ; + int qfits_header_getint (qfits_header * hdr, const char *key, int errval); /*----------------------------------------------------------------------------*/ /** @@ -381,7 +369,8 @@ int qfits_header_getint(qfits_header * hdr, const char * key, int errval) ; attached. */ /*----------------------------------------------------------------------------*/ -double qfits_header_getdouble(qfits_header * hdr, const char * key, double errval) ; + double qfits_header_getdouble (qfits_header * hdr, const char *key, + double errval); /*----------------------------------------------------------------------------*/ /** @@ -405,7 +394,8 @@ double qfits_header_getdouble(qfits_header * hdr, const char * key, double errva beginning with a 'n' (no), a 'f' (false) or the digit '0'. */ /*----------------------------------------------------------------------------*/ -int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ; + int qfits_header_getboolean (qfits_header * hdr, const char *key, + int errval); /*----------------------------------------------------------------------------*/ /** @@ -421,7 +411,7 @@ int qfits_header_getboolean(qfits_header * hdr, const char * key, int errval) ; Formatting is done according to FITS standard. */ /*----------------------------------------------------------------------------*/ -void keytuple2str(char * line, char * key, char * val, char * com) ; + void keytuple2str (char *line, char *key, char *val, char *com); /*----------------------------------------------------------------------------*/ /** @@ -433,7 +423,7 @@ void keytuple2str(char * line, char * key, char * val, char * com) ; Dumps a FITS header to an opened file pointer. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump(qfits_header * hdr, FILE * out) ; + int qfits_header_dump (qfits_header * hdr, FILE * out); /*----------------------------------------------------------------------------*/ /** @@ -446,7 +436,7 @@ int qfits_header_dump(qfits_header * hdr, FILE * out) ; This function is meant to create hdr files. */ /*----------------------------------------------------------------------------*/ -int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ; + int qfits_header_dump_hdr (qfits_header * hdr, FILE * out); /*----------------------------------------------------------------------------*/ /** @@ -464,7 +454,7 @@ int qfits_header_dump_hdr(qfits_header * hdr, FILE * out) ; 'hsize'. The returned block must be deallocated using free(). */ /*----------------------------------------------------------------------------*/ -char * qfits_header_to_memblock(qfits_header * fh, int * hsize) ; + char *qfits_header_to_memblock (qfits_header * fh, int *hsize); /* */ #ifdef __cplusplus diff --git a/qfits/src/fits_md5.c b/qfits/src/fits_md5.c index dbb260a..e6df2dc 100644 --- a/qfits/src/fits_md5.c +++ b/qfits/src/fits_md5.c @@ -58,121 +58,128 @@ of error. */ /*----------------------------------------------------------------------------*/ -char * qfits_datamd5(char * filename) +char * +qfits_datamd5 (char *filename) { - static char datamd5[MD5HASHSZ+1] ; - struct MD5Context ctx ; - unsigned char digest[16] ; - FILE * in ; - char buf[FITS_BLOCK_SIZE]; - char * buf_c ; - int i ; - int in_header ; - int check_fits ; + static char datamd5[MD5HASHSZ + 1]; + struct MD5Context ctx; + unsigned char digest[16]; + FILE *in; + char buf[FITS_BLOCK_SIZE]; + char *buf_c; + int i; + int in_header; + int check_fits; - /* Check entries */ - if (filename==NULL) return NULL ; - /* Open input file */ - if ((in=fopen(filename, "r"))==NULL) { - qfits_error("cannot open file %s", filename); - return NULL ; + /* Check entries */ + if (filename == NULL) + return NULL; + /* Open input file */ + if ((in = fopen (filename, "r")) == NULL) + { + qfits_error ("cannot open file %s", filename); + return NULL; } - /* Initialize all variables */ - MD5Init(&ctx); - in_header=1 ; - check_fits=0 ; - /* Loop over input file */ - while (fread(buf, 1, FITS_BLOCK_SIZE, in)==FITS_BLOCK_SIZE) { - /* First time in the loop: check the file is FITS */ - if (check_fits==0) { - /* Examine first characters in block */ - if (buf[0]!='S' || - buf[1]!='I' || - buf[2]!='M' || - buf[3]!='P' || - buf[4]!='L' || - buf[5]!='E' || - buf[6]!=' ' || - buf[7]!=' ' || - buf[8]!='=') { - qfits_error("file [%s] is not FITS\n", filename); - fclose(in); - return NULL ; - } else { - check_fits=1 ; - } - } - if (in_header) { - buf_c = buf ; - for (i=0 ; i */ #endif diff --git a/qfits/src/fits_p.c b/qfits/src/fits_p.c index c872358..5f3211c 100644 --- a/qfits/src/fits_p.c +++ b/qfits/src/fits_p.c @@ -50,63 +50,73 @@ allocated in this function, so do not modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getkey(char * line) +char * +qfits_getkey (char *line) { - static char key[81]; - int i ; + static char key[81]; + int i; - if (line==NULL) { + if (line == NULL) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getkey: NULL input line\n"); + printf ("qfits_getkey: NULL input line\n"); #endif - return NULL ; - } + return NULL; + } - /* 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 ; - } + /* 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; + } - memset(key, 0, 81); - /* General case: look for the first equal sign */ - i=0 ; - while (line[i]!='=' && i<80) i++ ; - if (i>=80) { + memset (key, 0, 81); + /* General case: look for the first equal sign */ + i = 0; + while (line[i] != '=' && i < 80) + i++; + if (i >= 80) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getkey: cannot find equal sign\n"); + printf ("qfits_getkey: cannot find equal sign\n"); #endif - return NULL ; - } - i-- ; - /* Equal sign found, now backtrack on blanks */ - while (line[i]==' ' && i>=0) i-- ; - if (i<=0) { + return NULL; + } + i--; + /* Equal sign found, now backtrack on blanks */ + while (line[i] == ' ' && i >= 0) + i--; + if (i <= 0) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getkey: error backtracking on blanks\n"); + printf ("qfits_getkey: error backtracking on blanks\n"); #endif - return NULL ; - } - i++ ; + return NULL; + } + i++; - /* Copy relevant characters into output buffer */ - strncpy(key, line, i) ; - /* Null-terminate the string */ - key[i+1] = (char)0; - return key ; + /* Copy relevant characters into output buffer */ + strncpy (key, line, i); + /* Null-terminate the string */ + key[i + 1] = (char) 0; + return key; } /*----------------------------------------------------------------------------*/ @@ -121,124 +131,145 @@ char * qfits_getkey(char * line) modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getvalue(char * line) +char * +qfits_getvalue (char *line) { - static char value[81] ; - int i ; - int from, to ; - int inq ; + static char value[81]; + int i; + int from, to; + int inq; - if (line==NULL) { + if (line == NULL) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getvalue: NULL input line\n"); + printf ("qfits_getvalue: NULL input line\n"); #endif - return NULL ; - } - - /* Special cases */ - - /* 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 (to80) { -#ifdef DEBUG_FITSHEADER - printf("qfits_getvalue: no equal sign found on line\n"); -#endif - return NULL ; - } - i++ ; - while (line[i]==' ' && i<80) i++ ; - if (i>80) { -#ifdef DEBUG_FITSHEADER - printf("qfits_getvalue: no value past the equal sign\n"); -#endif - return NULL ; - } - 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++; + return NULL; } - i-- ; - /* Backtrack on blanks */ - while (line[i]==' ' && i>=0) i-- ; - if (i<0) { -#ifdef DEBUG_FITSHEADER - printf("qfits_getvalue: error backtracking on blanks\n"); -#endif - return NULL ; - } - to=i ; + /* Special cases */ - if (to= 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) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getvalue: from>to?\n"); - printf("line=[%s]\n", line); + printf ("qfits_getvalue: inconsistent value search in COMMENT\n"); #endif - return NULL ; + 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 ; + /* 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) + { +#ifdef DEBUG_FITSHEADER + printf ("qfits_getvalue: no equal sign found on line\n"); +#endif + return NULL; + } + i++; + while (line[i] == ' ' && i < 80) + i++; + if (i > 80) + { +#ifdef DEBUG_FITSHEADER + printf ("qfits_getvalue: no value past the equal sign\n"); +#endif + return NULL; + } + 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--; + + /* Backtrack on blanks */ + while (line[i] == ' ' && i >= 0) + i--; + if (i < 0) + { +#ifdef DEBUG_FITSHEADER + printf ("qfits_getvalue: error backtracking on blanks\n"); +#endif + return NULL; + } + to = i; + + if (to < from) + { +#ifdef DEBUG_FITSHEADER + printf ("qfits_getvalue: from>to?\n"); + printf ("line=[%s]\n", line); +#endif + 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; } /*----------------------------------------------------------------------------*/ @@ -253,74 +284,87 @@ char * qfits_getvalue(char * line) modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getcomment(char * line) +char * +qfits_getcomment (char *line) { - static char comment[81]; - int i ; - int from, to ; - int inq ; + static char comment[81]; + int i; + int from, to; + int inq; - if (line==NULL) { + if (line == NULL) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getcomment: null line in input\n"); + printf ("qfits_getcomment: null line in input\n"); #endif - return NULL ; - } + return NULL; + } - /* 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 ; + /* 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) { + memset (comment, 0, 81); + /* Get past the keyword */ + i = 0; + while (line[i] != '=' && i < 80) + i++; + if (i >= 80) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getcomment: no equal sign on line\n"); + printf ("qfits_getcomment: no equal sign on line\n"); #endif - 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) { -#ifdef DEBUG_FITSHEADER - printf("qfits_getcomment: no slash found on line\n"); -#endif - return NULL ; - } - i++ ; - /* Get past the first blanks */ - while (line[i]==' ') i++ ; - from=i ; + return NULL; + } + i++; - /* Now backtrack from the end of the line to the first non-blank char */ - to=79 ; - while (line[to]==' ') to-- ; - - if (to= 80) + { #ifdef DEBUG_FITSHEADER - printf("qfits_getcomment: from>to?\n"); + printf ("qfits_getcomment: no slash found on line\n"); #endif - 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 ; + 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) + { +#ifdef DEBUG_FITSHEADER + printf ("qfits_getcomment: from>to?\n"); +#endif + 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; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/fits_p.h b/qfits/src/fits_p.h index b3dcd8d..6302fb0 100644 --- a/qfits/src/fits_p.h +++ b/qfits/src/fits_p.h @@ -22,7 +22,8 @@ #define FITSEP_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* */ @@ -41,7 +42,7 @@ extern "C" { allocated in this function, so do not modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getkey(char * line) ; + char *qfits_getkey (char *line); /*----------------------------------------------------------------------------*/ /** @@ -55,7 +56,7 @@ char * qfits_getkey(char * line) ; modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getvalue(char * line) ; + char *qfits_getvalue (char *line); /*----------------------------------------------------------------------------*/ /** @@ -69,7 +70,7 @@ char * qfits_getvalue(char * line) ; modify or try to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_getcomment(char * line) ; + char *qfits_getcomment (char *line); /* */ #ifdef __cplusplus diff --git a/qfits/src/fits_rw.c b/qfits/src/fits_rw.c index 1045c60..400b936 100644 --- a/qfits/src/fits_rw.c +++ b/qfits/src/fits_rw.c @@ -35,14 +35,17 @@ /*----------------------------------------------------------------------------- Private to this module -----------------------------------------------------------------------------*/ -static int is_blank_line(char * s) +static int +is_blank_line (char *s) { - int i ; + int i; - for (i=0 ; i<(int)strlen(s) ; i++) { - if (s[i]!=' ') return 0 ; + for (i = 0; i < (int) strlen (s); i++) + { + if (s[i] != ' ') + return 0; } - return 1 ; + return 1; } /*----------------------------------------------------------------------------- @@ -70,10 +73,11 @@ static int is_blank_line(char * s) Value, comment, and original line might be NULL pointers. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read(char * filename) +qfits_header * +qfits_header_read (char *filename) { - /* Forward job to readext */ - return qfits_header_readext(filename, 0); + /* Forward job to readext */ + return qfits_header_readext (filename, 0); } /*----------------------------------------------------------------------------*/ @@ -88,76 +92,86 @@ qfits_header * qfits_header_read(char * filename) a hdr file. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr(char * filename) +qfits_header * +qfits_header_read_hdr (char *filename) { - qfits_header * hdr ; - FILE * in ; - char line[81]; - char * key, - * val, - * com ; - int i, j ; + qfits_header *hdr; + FILE *in; + char line[81]; + char *key, *val, *com; + int i, j; - /* Check input */ - if (filename==NULL) return NULL ; + /* Check input */ + if (filename == NULL) + return NULL; - /* Initialise */ - key = val = com = NULL ; + /* Initialise */ + key = val = com = NULL; - /* Open the file */ - if ((in=fopen(filename, "r"))==NULL) { - qfits_error("cannot read [%s]", filename) ; - return NULL ; + /* Open the file */ + if ((in = fopen (filename, "r")) == NULL) + { + qfits_error ("cannot read [%s]", filename); + return NULL; } - - /* Create the header */ - hdr = qfits_header_new() ; - - /* Go through the file */ - while (fgets(line, 81, in)!=NULL) { - for (i=0 ; i<81 ; i++) { - if (line[i] == '\n') { - for (j=i ; j<81 ; j++) line[j] = ' ' ; - line[80] = (char)0 ; - break ; - } - } - if (!strcmp(line, "END")) { - line[3] = ' '; - line[4] = (char)0 ; - } - - /* Rule out blank lines */ - if (!is_blank_line(line)) { - /* Get key, value, comment for the current line */ - key = qfits_getkey(line); - val = qfits_getvalue(line); - com = qfits_getcomment(line); + /* Create the header */ + hdr = qfits_header_new (); - /* If key or value cannot be found, trigger an error */ - if (key==NULL) { - qfits_header_destroy(hdr); - fclose(in) ; - return NULL ; - } - /* Append card to linked-list */ - qfits_header_append(hdr, key, val, com, NULL); - } + /* Go through the file */ + while (fgets (line, 81, in) != NULL) + { + for (i = 0; i < 81; i++) + { + if (line[i] == '\n') + { + for (j = i; j < 81; j++) + line[j] = ' '; + line[80] = (char) 0; + break; + } + } + if (!strcmp (line, "END")) + { + line[3] = ' '; + line[4] = (char) 0; + } + + /* Rule out blank lines */ + if (!is_blank_line (line)) + { + + /* Get key, value, comment for the current line */ + key = qfits_getkey (line); + val = qfits_getvalue (line); + com = qfits_getcomment (line); + + /* If key or value cannot be found, trigger an error */ + if (key == NULL) + { + qfits_header_destroy (hdr); + fclose (in); + return NULL; + } + /* Append card to linked-list */ + qfits_header_append (hdr, key, val, com, NULL); + } } - fclose(in) ; + fclose (in); - /* The last key should be 'END' */ - if (strlen(key)!=3) { - qfits_header_destroy(hdr); - return NULL ; - } - if (key[0]!='E' || key[1]!='N' || key[2]!='D') { - qfits_header_destroy(hdr); - return NULL ; + /* The last key should be 'END' */ + if (strlen (key) != 3) + { + qfits_header_destroy (hdr); + return NULL; } - - return hdr ; + if (key[0] != 'E' || key[1] != 'N' || key[2] != 'D') + { + qfits_header_destroy (hdr); + return NULL; + } + + return hdr; } /*----------------------------------------------------------------------------*/ @@ -171,74 +185,81 @@ qfits_header * qfits_header_read_hdr(char * filename) object. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr_string( - unsigned char * hdr_str, - int nb_char) +qfits_header * +qfits_header_read_hdr_string (unsigned char *hdr_str, int nb_char) { - qfits_header * hdr ; - char line[81]; - char * key, - * val, - * com ; - int ind ; - int i, j ; + qfits_header *hdr; + char line[81]; + char *key, *val, *com; + int ind; + int i, j; - /* Check input */ - if (hdr_str==NULL) return NULL ; + /* Check input */ + if (hdr_str == NULL) + return NULL; - /* Initialise */ - key = val = com = NULL ; + /* Initialise */ + key = val = com = NULL; - /* Create the header */ - hdr = qfits_header_new() ; - - /* Go through the file */ - ind = 0 ; - while (ind <= nb_char - 80) { - strncpy(line, (char*)hdr_str + ind, 80) ; - line[80] = (char)0 ; - for (i=0 ; i<81 ; i++) { - if (line[i] == '\n') { - for (j=i ; j<81 ; j++) line[j] = ' ' ; - line[80] = (char)0 ; - break ; - } - } - if (!strcmp(line, "END")) { - line[3] = ' '; - line[4] = (char)0 ; - } - - /* Rule out blank lines */ - if (!is_blank_line(line)) { + /* Create the header */ + hdr = qfits_header_new (); - /* Get key, value, comment for the current line */ - key = qfits_getkey(line); - val = qfits_getvalue(line); - com = qfits_getcomment(line); + /* Go through the file */ + ind = 0; + while (ind <= nb_char - 80) + { + strncpy (line, (char *) hdr_str + ind, 80); + line[80] = (char) 0; + for (i = 0; i < 81; i++) + { + if (line[i] == '\n') + { + for (j = i; j < 81; j++) + line[j] = ' '; + line[80] = (char) 0; + break; + } + } + if (!strcmp (line, "END")) + { + line[3] = ' '; + line[4] = (char) 0; + } - /* If key or value cannot be found, trigger an error */ - if (key==NULL) { - qfits_header_destroy(hdr); - return NULL ; - } - /* Append card to linked-list */ - qfits_header_append(hdr, key, val, com, NULL); - } - ind += 80 ; + /* Rule out blank lines */ + if (!is_blank_line (line)) + { + + /* Get key, value, comment for the current line */ + key = qfits_getkey (line); + val = qfits_getvalue (line); + com = qfits_getcomment (line); + + /* If key or value cannot be found, trigger an error */ + if (key == NULL) + { + qfits_header_destroy (hdr); + return NULL; + } + /* Append card to linked-list */ + qfits_header_append (hdr, key, val, com, NULL); + } + ind += 80; } - /* The last key should be 'END' */ - if (strlen(key)!=3) { - qfits_header_destroy(hdr); - return NULL ; - } - if (key[0]!='E' || key[1]!='N' || key[2]!='D') { - qfits_header_destroy(hdr); - return NULL ; + /* The last key should be 'END' */ + if (strlen (key) != 3) + { + qfits_header_destroy (hdr); + return NULL; } - - return hdr ; + if (key[0] != 'E' || key[1] != 'N' || key[2] != 'D') + { + qfits_header_destroy (hdr); + return NULL; + } + + return hdr; } /*----------------------------------------------------------------------------*/ @@ -255,80 +276,85 @@ qfits_header * qfits_header_read_hdr_string( Returns NULL in case of error. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_readext(char * filename, int xtnum) +qfits_header * +qfits_header_readext (char *filename, int xtnum) { - qfits_header* hdr ; - int n_ext ; - char line[81]; - char * where ; - char * start ; - char * key, - * val, - * com ; - int seg_start ; - int seg_size ; - size_t size ; + qfits_header *hdr; + int n_ext; + char line[81]; + char *where; + char *start; + char *key, *val, *com; + int seg_start; + int seg_size; + size_t size; - /* Check input */ - if (filename==NULL || xtnum<0) - return NULL ; + /* Check input */ + if (filename == NULL || xtnum < 0) + return NULL; - /* Check that there are enough extensions */ - if (xtnum>0) { - n_ext = qfits_query_n_ext(filename); - if (xtnum>n_ext) { - return NULL ; - } + /* Check that there are enough extensions */ + if (xtnum > 0) + { + n_ext = qfits_query_n_ext (filename); + if (xtnum > n_ext) + { + return NULL; + } } - /* Get offset to the extension header */ - if (qfits_get_hdrinfo(filename, xtnum, &seg_start, &seg_size)!=0) { - return NULL ; + /* Get offset to the extension header */ + if (qfits_get_hdrinfo (filename, xtnum, &seg_start, &seg_size) != 0) + { + return NULL; } - /* Memory-map the input file */ - start = falloc(filename, seg_start, &size) ; - if (start==NULL) return NULL ; + /* Memory-map the input file */ + start = falloc (filename, seg_start, &size); + if (start == NULL) + return NULL; - hdr = qfits_header_new() ; - where = start ; - while (1) { - memcpy(line, where, 80); - line[80] = (char)0; + hdr = qfits_header_new (); + where = start; + while (1) + { + memcpy (line, where, 80); + line[80] = (char) 0; - /* Rule out blank lines */ - if (!is_blank_line(line)) { + /* Rule out blank lines */ + if (!is_blank_line (line)) + { - /* Get key, value, comment for the current line */ - key = qfits_getkey(line); - val = qfits_getvalue(line); - com = qfits_getcomment(line); + /* Get key, value, comment for the current line */ + key = qfits_getkey (line); + val = qfits_getvalue (line); + com = qfits_getcomment (line); - /* If key or value cannot be found, trigger an error */ - if (key==NULL) { - qfits_header_destroy(hdr); - hdr = NULL ; - break ; - } - /* Append card to linked-list */ - qfits_header_append(hdr, key, val, com, line); - /* Check for END keyword */ - if (strlen(key)==3) - if (key[0]=='E' && - key[1]=='N' && - key[2]=='D') - break ; - } - where += 80 ; - /* If reaching the end of file, trigger an error */ - if ((int)(where-start)>=(int)(seg_size+80)) { - qfits_header_destroy(hdr); - hdr = NULL ; - break ; - } + /* If key or value cannot be found, trigger an error */ + if (key == NULL) + { + qfits_header_destroy (hdr); + hdr = NULL; + break; + } + /* Append card to linked-list */ + qfits_header_append (hdr, key, val, com, line); + /* Check for END keyword */ + if (strlen (key) == 3) + if (key[0] == 'E' && key[1] == 'N' && key[2] == 'D') + break; + } + where += 80; + /* If reaching the end of file, trigger an error */ + if ((int) (where - start) >= (int) (seg_size + 80)) + { + qfits_header_destroy (hdr); + hdr = NULL; + break; + } } - fdealloc(start, seg_start, size) ; - return hdr ; + fdealloc (start, seg_start, size); + return hdr; } /*----------------------------------------------------------------------------*/ @@ -341,34 +367,38 @@ qfits_header * qfits_header_readext(char * filename, int xtnum) for the file size to reach a multiple of 2880, as required by FITS. */ /*----------------------------------------------------------------------------*/ -void qfits_zeropad(char * filename) +void +qfits_zeropad (char *filename) { - struct stat sta ; - int size ; - int remaining; - FILE * out ; - char * buf; + struct stat sta; + int size; + int remaining; + FILE *out; + char *buf; - if (filename==NULL) return ; + if (filename == NULL) + return; - /* Get file size in bytes */ - if (stat(filename, &sta)!=0) { - return ; + /* Get file size in bytes */ + if (stat (filename, &sta) != 0) + { + return; } - size = (int)sta.st_size ; - /* Compute number of zeros to pad */ - remaining = size % FITS_BLOCK_SIZE ; - if (remaining==0) return ; - remaining = FITS_BLOCK_SIZE - remaining ; + size = (int) sta.st_size; + /* Compute number of zeros to pad */ + remaining = size % FITS_BLOCK_SIZE; + if (remaining == 0) + return; + remaining = FITS_BLOCK_SIZE - remaining; - /* Open file, dump zeros, exit */ - if ((out=fopen(filename, "a"))==NULL) - return ; - buf = calloc(remaining, sizeof(char)); - fwrite(buf, 1, remaining, out); - fclose(out); - free(buf); - return ; + /* Open file, dump zeros, exit */ + if ((out = fopen (filename, "a")) == NULL) + return; + buf = calloc (remaining, sizeof (char)); + fwrite (buf, 1, remaining, out); + fclose (out); + free (buf); + return; } /*----------------------------------------------------------------------------*/ @@ -381,28 +411,31 @@ void qfits_zeropad(char * filename) 0 else. If the file does not exist, returns -1. */ /*----------------------------------------------------------------------------*/ -int is_fits_file(char *filename) +int +is_fits_file (char *filename) { - FILE * fp ; - char * magic ; - int isfits ; + FILE *fp; + char *magic; + int isfits; - if (filename==NULL) return -1 ; - if ((fp = fopen(filename, "r"))==NULL) { - qfits_error("cannot open file [%s]", filename) ; - return -1 ; + if (filename == NULL) + return -1; + if ((fp = fopen (filename, "r")) == NULL) + { + qfits_error ("cannot open file [%s]", filename); + return -1; } - magic = calloc(FITS_MAGIC_SZ+1, sizeof(char)) ; - fread(magic, 1, FITS_MAGIC_SZ, fp) ; - fclose(fp) ; - magic[FITS_MAGIC_SZ] = (char)0 ; - if (strstr(magic, FITS_MAGIC)!=NULL) - isfits = 1 ; - else - isfits = 0 ; - free(magic) ; - return isfits ; + magic = calloc (FITS_MAGIC_SZ + 1, sizeof (char)); + fread (magic, 1, FITS_MAGIC_SZ, fp); + fclose (fp); + magic[FITS_MAGIC_SZ] = (char) 0; + if (strstr (magic, FITS_MAGIC) != NULL) + isfits = 1; + else + isfits = 0; + free (magic); + return isfits; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/fits_rw.h b/qfits/src/fits_rw.h index 92a666a..ffac719 100644 --- a/qfits/src/fits_rw.h +++ b/qfits/src/fits_rw.h @@ -19,7 +19,8 @@ #define FITS_RW_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*----------------------------------------------------------------------------- @@ -55,7 +56,7 @@ extern "C" { Value, comment, and original line might be NULL pointers. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read(char * filename) ; + qfits_header *qfits_header_read (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -69,7 +70,7 @@ qfits_header * qfits_header_read(char * filename) ; a hdr file. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr(char * filename) ; + qfits_header *qfits_header_read_hdr (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -82,9 +83,8 @@ qfits_header * qfits_header_read_hdr(char * filename) ; object. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_read_hdr_string( - unsigned char * hdr_str, - int nb_char) ; + qfits_header *qfits_header_read_hdr_string (unsigned char *hdr_str, + int nb_char); /*----------------------------------------------------------------------------*/ /** @@ -100,7 +100,7 @@ qfits_header * qfits_header_read_hdr_string( Returns NULL in case of error. */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_header_readext(char * filename, int xtnum) ; + qfits_header *qfits_header_readext (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -112,7 +112,7 @@ qfits_header * qfits_header_readext(char * filename, int xtnum) ; for the file size to reach a multiple of 2880, as required by FITS. */ /*----------------------------------------------------------------------------*/ -void qfits_zeropad(char * filename) ; + void qfits_zeropad (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -124,7 +124,7 @@ void qfits_zeropad(char * filename) ; 0 else. If the file does not exist, returns -1. */ /*----------------------------------------------------------------------------*/ -int is_fits_file(char *filename) ; + int is_fits_file (char *filename); /* */ #ifdef __cplusplus diff --git a/qfits/src/fits_std.h b/qfits/src/fits_std.h index f7ad336..9e06ab0 100644 --- a/qfits/src/fits_std.h +++ b/qfits/src/fits_std.h @@ -63,7 +63,7 @@ ((x) == BPP_16_SIGNED) ? 2 : \ ((x) == BPP_32_SIGNED) ? 4 : \ ((x) == BPP_IEEE_FLOAT) ? 4 : \ - ((x) == BPP_IEEE_DOUBLE) ? 8 : 0 ) + ((x) == BPP_IEEE_DOUBLE) ? 8 : 0 ) /* */ #endif /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/get_name.c b/qfits/src/get_name.c index db1fbc2..e9abcd5 100644 --- a/qfits/src/get_name.c +++ b/qfits/src/get_name.c @@ -89,33 +89,35 @@ @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_dir_name(char * filename) +char * +qfits_get_dir_name (char *filename) { - static char path[MAXNAMESZ]; - char *last_slash; + static char path[MAXNAMESZ]; + char *last_slash; - if (strlen(filename)>MAXNAMESZ) return NULL ; - strcpy(path, filename); - /* Find last '/'. */ - last_slash = path != NULL ? strrchr (path, '/') : NULL; + if (strlen (filename) > MAXNAMESZ) + return NULL; + strcpy (path, filename); + /* Find last '/'. */ + last_slash = path != NULL ? strrchr (path, '/') : NULL; - if (last_slash == path) + if (last_slash == path) /* The last slash is the first character in the string. We have to - return "/". */ - ++last_slash; - else if (last_slash != NULL && last_slash[1] == '\0') - /* The '/' is the last character, we have to look further. */ - last_slash = memchr (path, last_slash - path, '/'); + return "/". */ + ++last_slash; + else if (last_slash != NULL && last_slash[1] == '\0') + /* The '/' is the last character, we have to look further. */ + last_slash = memchr (path, last_slash - path, '/'); - if (last_slash != NULL) - /* Terminate the path. */ - last_slash[0] = '\0'; - else - /* This assignment is ill-designed but the XPG specs require to - return a string containing "." in any case no directory part is - found and so a static and constant string is required. */ - strcpy(path, "."); - return path; + if (last_slash != NULL) + /* Terminate the path. */ + last_slash[0] = '\0'; + else + /* This assignment is ill-designed but the XPG specs require to + return a string containing "." in any case no directory part is + found and so a static and constant string is required. */ + strcpy (path, "."); + return path; } /*----------------------------------------------------------------------------*/ @@ -138,11 +140,12 @@ char * qfits_get_dir_name(char * filename) @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_base_name(const char *filename) +char * +qfits_get_base_name (const char *filename) { - char *p ; - p = strrchr (filename, '/'); - return p ? p + 1 : (char *) filename; + char *p; + p = strrchr (filename, '/'); + return p ? p + 1 : (char *) filename; } /*----------------------------------------------------------------------------*/ @@ -176,26 +179,29 @@ char * qfits_get_base_name(const char *filename) to free it or modify its contents. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_root_name(char * filename) +char * +qfits_get_root_name (char *filename) { - static char path[MAXNAMESZ+1]; - char * lastdot ; + static char path[MAXNAMESZ + 1]; + char *lastdot; - if (strlen(filename)>MAXNAMESZ) return NULL ; - memset(path, MAXNAMESZ, 0); - strcpy(path, filename); - lastdot = strrchr(path, '.'); - if (lastdot == NULL) return path ; - if ((!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".FITS")) || - (!strcmp(lastdot, ".paf")) || (!strcmp(lastdot, ".PAF")) || - (!strcmp(lastdot, ".dat")) || (!strcmp(lastdot, ".DAT")) || - (!strcmp(lastdot, ".txt")) || (!strcmp(lastdot, ".TXT")) || - (!strcmp(lastdot, ".tfits")) || (!strcmp(lastdot, ".TFITS")) || - (!strcmp(lastdot, ".ascii")) || (!strcmp(lastdot, ".ASCII"))) + if (strlen (filename) > MAXNAMESZ) + return NULL; + memset (path, MAXNAMESZ, 0); + strcpy (path, filename); + lastdot = strrchr (path, '.'); + if (lastdot == NULL) + return path; + if ((!strcmp (lastdot, ".fits")) || (!strcmp (lastdot, ".FITS")) || + (!strcmp (lastdot, ".paf")) || (!strcmp (lastdot, ".PAF")) || + (!strcmp (lastdot, ".dat")) || (!strcmp (lastdot, ".DAT")) || + (!strcmp (lastdot, ".txt")) || (!strcmp (lastdot, ".TXT")) || + (!strcmp (lastdot, ".tfits")) || (!strcmp (lastdot, ".TFITS")) || + (!strcmp (lastdot, ".ascii")) || (!strcmp (lastdot, ".ASCII"))) { - lastdot[0] = (char)0; + lastdot[0] = (char) 0; } - return path ; + return path; } /*----------------------------------------------------------------------------*/ @@ -219,11 +225,12 @@ char * qfits_get_root_name(char * filename) @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_ext_name(char * filename) +char * +qfits_get_ext_name (char *filename) { - char * p; - p = strrchr(filename, '.'); - return p ? p+1 : NULL ; + char *p; + p = strrchr (filename, '.'); + return p ? p + 1 : NULL; } /*----------------------------------------------------------------------------*/ @@ -240,17 +247,20 @@ char * qfits_get_ext_name(char * filename) string which first character is a null character. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_login_name(void) +char * +qfits_get_login_name (void) { - struct passwd * pw ; - static char name[32]; + struct passwd *pw; + static char name[32]; - pw = getpwuid(getuid()); - if (pw!=NULL) { - strcpy(name, pw->pw_name); - } else { - name[0]=0 ; + pw = getpwuid (getuid ()); + if (pw != NULL) + { + strcpy (name, pw->pw_name); } - return name ; + else + { + name[0] = 0; + } + return name; } - diff --git a/qfits/src/get_name.h b/qfits/src/get_name.h index e62df31..8f3fd47 100644 --- a/qfits/src/get_name.h +++ b/qfits/src/get_name.h @@ -20,7 +20,8 @@ #define GET_NAME_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif @@ -51,7 +52,7 @@ extern "C" { @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_dir_name(char * filename) ; + char *qfits_get_dir_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -73,7 +74,7 @@ char * qfits_get_dir_name(char * filename) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_base_name(const char *filename) ; + char *qfits_get_base_name (const char *filename); /*----------------------------------------------------------------------------*/ /** @@ -105,7 +106,7 @@ char * qfits_get_base_name(const char *filename) ; to free it or modify its contents. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_root_name(char * filename) ; + char *qfits_get_root_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -128,7 +129,7 @@ char * qfits_get_root_name(char * filename) ; @endverbatim */ /*----------------------------------------------------------------------------*/ -char * qfits_get_ext_name(char * filename) ; + char *qfits_get_ext_name (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -144,7 +145,7 @@ char * qfits_get_ext_name(char * filename) ; point to a string which first character is a null character. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_login_name(void) ; + char *qfits_get_login_name (void); /* */ #ifdef __cplusplus diff --git a/qfits/src/ieeefp-compat.c b/qfits/src/ieeefp-compat.c index 6122211..908e759 100644 --- a/qfits/src/ieeefp-compat.c +++ b/qfits/src/ieeefp-compat.c @@ -38,85 +38,94 @@ #ifndef WORDS_BIGENDIAN /* Little endian ordering */ -typedef union _ieee_double_pattern_ { - double d ; - struct { - unsigned int lsw ; - unsigned int msw ; - } p ; -} ieee_double_pattern ; +typedef union _ieee_double_pattern_ +{ + double d; + struct + { + unsigned int lsw; + unsigned int msw; + } p; +} ieee_double_pattern; #else /* Big endian ordering */ -typedef union _ieee_double_pattern_ { - double d ; - struct { - unsigned int msw ; - unsigned int lsw ; - } p ; -} ieee_double_pattern ; +typedef union _ieee_double_pattern_ +{ + double d; + struct + { + unsigned int msw; + unsigned int lsw; + } p; +} ieee_double_pattern; #endif -typedef union _ieee_float_pattern_ { - float f ; - int i ; -} ieee_float_pattern ; +typedef union _ieee_float_pattern_ +{ + float f; + int i; +} ieee_float_pattern; /*----------------------------------------------------------------------------- Function codes -----------------------------------------------------------------------------*/ -int _qfits_isnanf(float f) +int +_qfits_isnanf (float f) { - ieee_float_pattern ip ; - int ix ; + ieee_float_pattern ip; + int ix; - ip.f = f ; - ix = ip.i ; - ix &= 0x7fffffff ; - ix = 0x7f800000 - ix ; - return (int)(((unsigned int)(ix))>>31); + ip.f = f; + ix = ip.i; + ix &= 0x7fffffff; + ix = 0x7f800000 - ix; + return (int) (((unsigned int) (ix)) >> 31); } -int _qfits_isinff(float f) +int +_qfits_isinff (float f) { - ieee_float_pattern ip ; - int ix, t ; + ieee_float_pattern ip; + int ix, t; - ip.f = f ; - ix = ip.i ; - t = ix & 0x7fffffff; - t ^= 0x7f800000; - t |= -t; - return ~(t >> 31) & (ix >> 30); + ip.f = f; + ix = ip.i; + t = ix & 0x7fffffff; + t ^= 0x7f800000; + t |= -t; + return ~(t >> 31) & (ix >> 30); } -int _qfits_isnand(double d) +int +_qfits_isnand (double d) { - ieee_double_pattern id ; - int hx, lx ; + ieee_double_pattern id; + int hx, lx; - id.d = d ; - lx = id.p.lsw ; - hx = id.p.msw ; + id.d = d; + lx = id.p.lsw; + hx = id.p.msw; - hx &= 0x7fffffff; - hx |= (unsigned int)(lx|(-lx))>>31; - hx = 0x7ff00000 - hx; - return (int)(((unsigned int)hx)>>31); + hx &= 0x7fffffff; + hx |= (unsigned int) (lx | (-lx)) >> 31; + hx = 0x7ff00000 - hx; + return (int) (((unsigned int) hx) >> 31); } -int _qfits_isinfd(double d) +int +_qfits_isinfd (double d) { - ieee_double_pattern id ; - int hx, lx ; + ieee_double_pattern id; + int hx, lx; - id.d = d ; - lx = id.p.lsw ; - hx = id.p.msw ; + id.d = d; + lx = id.p.lsw; + hx = id.p.msw; - lx |= (hx & 0x7fffffff) ^ 0x7ff00000; - lx |= -lx; - return ~(lx >> 31) & (hx >> 30); + lx |= (hx & 0x7fffffff) ^ 0x7ff00000; + lx |= -lx; + return ~(lx >> 31) & (hx >> 30); } @@ -133,88 +142,99 @@ int _qfits_isinfd(double d) #ifndef WORDS_BIGENDIAN /* Little endian patterns */ -static unsigned char fnan_pat[] = {0, 0, 0xc0, 0x7f}; -static unsigned char dnan_pat[] = {0, 0, 0, 0, 0, 0, 0xf8, 0x7f}; -static unsigned char finf_pat[] = {0, 0, 0x80, 0x7f}; -static unsigned char dinf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f}; -static unsigned char fminf_pat[] = {0, 0, 0x80, 0xff}; +static unsigned char fnan_pat[] = { 0, 0, 0xc0, 0x7f }; +static unsigned char dnan_pat[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f }; +static unsigned char finf_pat[] = { 0, 0, 0x80, 0x7f }; +static unsigned char dinf_pat[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }; +static unsigned char fminf_pat[] = { 0, 0, 0x80, 0xff }; + /* static unsigned char dminf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0xff}; */ -static unsigned char dminf_pat[] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f}; +static unsigned char dminf_pat[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }; #else /* Big endian patterns */ -static unsigned char fnan_pat[] = {0x7f, 0xc0, 0, 0}; -static unsigned char dnan_pat[] = {0x7f, 0xf8, 0, 0, 0, 0, 0, 0}; -static unsigned char finf_pat[] = {0x7f, 0x80, 0, 0}; -static unsigned char dinf_pat[] = {0x7f, 0xf0, 0, 0, 0, 0, 0, 0}; -static unsigned char fminf_pat[] = {0xff, 0x80, 0, 0}; -static unsigned char dminf_pat[] = {0xff, 0xf0, 0, 0, 0, 0, 0, 0}; +static unsigned char fnan_pat[] = { 0x7f, 0xc0, 0, 0 }; +static unsigned char dnan_pat[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 }; +static unsigned char finf_pat[] = { 0x7f, 0x80, 0, 0 }; +static unsigned char dinf_pat[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }; +static unsigned char fminf_pat[] = { 0xff, 0x80, 0, 0 }; +static unsigned char dminf_pat[] = { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 }; #endif -static void hexdump(void * p, int s) +static void +hexdump (void *p, int s) { - unsigned char * c ; - int i ; + unsigned char *c; + int i; - c=(unsigned char*)p ; + c = (unsigned char *) p; #ifndef WORDS_BIGENDIAN - for (i=s-1 ; i>=0 ; i--) { + for (i = s - 1; i >= 0; i--) + { #else - for (i=0 ; i */ #endif diff --git a/qfits/src/md5.c b/qfits/src/md5.c index 97719cb..9d356c9 100644 --- a/qfits/src/md5.c +++ b/qfits/src/md5.c @@ -31,13 +31,13 @@ -----------------------------------------------------------------------------*/ #include "md5.h" -#include +#include /*----------------------------------------------------------------------------- Function prototypes -----------------------------------------------------------------------------*/ -static void byteReverse(unsigned char *buf, unsigned longs) ; +static void byteReverse (unsigned char *buf, unsigned longs); /*----------------------------------------------------------------------------- Function codes @@ -46,123 +46,135 @@ static void byteReverse(unsigned char *buf, unsigned longs) ; /* * Note: this code is harmless on little-endian machines. */ -static void byteReverse(unsigned char *buf, unsigned longs) +static void +byteReverse (unsigned char *buf, unsigned longs) { - word32 t; - do { - t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(word32 *) buf = t; - buf += 4; - } while (--longs); + word32 t; + do + { + t = (word32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + *(word32 *) buf = t; + buf += 4; + } + while (--longs); } /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ -void MD5Init(struct MD5Context *ctx) +void +MD5Init (struct MD5Context *ctx) { - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; - ctx->bits[0] = 0; - ctx->bits[1] = 0; + ctx->bits[0] = 0; + ctx->bits[1] = 0; } /* * Update context to reflect the concatenation of another buffer full * of bytes. */ -void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) +void +MD5Update (struct MD5Context *ctx, unsigned char const *buf, unsigned len) { - register word32 t; + register word32 t; - /* Update bitcount */ + /* Update bitcount */ - t = ctx->bits[0]; - if ((ctx->bits[0] = t + ((word32) len << 3)) < t) - ctx->bits[1]++; /* Carry from low to high */ - ctx->bits[1] += len >> 29; + t = ctx->bits[0]; + if ((ctx->bits[0] = t + ((word32) len << 3)) < t) + ctx->bits[1]++; /* Carry from low to high */ + ctx->bits[1] += len >> 29; - t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ + t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ - /* Handle any leading odd-sized chunks */ + /* Handle any leading odd-sized chunks */ - if (t) { - unsigned char *p = (unsigned char *) ctx->in + t; + if (t) + { + unsigned char *p = (unsigned char *) ctx->in + t; - t = 64 - t; - if (len < t) { - memmove(p, buf, len); - return; + t = 64 - t; + if (len < t) + { + memmove (p, buf, len); + return; } - memmove(p, buf, t); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); - buf += t; - len -= t; + memmove (p, buf, t); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); + buf += t; + len -= t; } - /* Process data in 64-byte chunks */ + /* Process data in 64-byte chunks */ - while (len >= 64) { - memmove(ctx->in, buf, 64); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); - buf += 64; - len -= 64; + while (len >= 64) + { + memmove (ctx->in, buf, 64); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); + buf += 64; + len -= 64; } - /* Handle any remaining bytes of data. */ + /* Handle any remaining bytes of data. */ - memmove(ctx->in, buf, len); + memmove (ctx->in, buf, len); } /* * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -void MD5Final(unsigned char digest[16], struct MD5Context *ctx) +void +MD5Final (unsigned char digest[16], struct MD5Context *ctx) { - unsigned int count; - unsigned char *p; + unsigned int count; + unsigned char *p; - /* Compute number of bytes mod 64 */ - count = (ctx->bits[0] >> 3) & 0x3F; + /* Compute number of bytes mod 64 */ + count = (ctx->bits[0] >> 3) & 0x3F; - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = ctx->in + count; - *p++ = 0x80; + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + p = ctx->in + count; + *p++ = 0x80; - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; + /* Bytes of padding needed to make 64 bytes */ + count = 64 - 1 - count; - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(p, 0, count); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (word32 *) ctx->in); + /* Pad out to 56 mod 64 */ + if (count < 8) + { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset (p, 0, count); + byteReverse (ctx->in, 16); + MD5Transform (ctx->buf, (word32 *) ctx->in); - /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset(p, 0, count - 8); + /* Now fill the next block with 56 bytes */ + memset (ctx->in, 0, 56); } - byteReverse(ctx->in, 14); + else + { + /* Pad block to 56 bytes */ + memset (p, 0, count - 8); + } + byteReverse (ctx->in, 14); - /* Append length in bits and transform */ - ((word32 *) ctx->in)[14] = ctx->bits[0]; - ((word32 *) ctx->in)[15] = ctx->bits[1]; + /* Append length in bits and transform */ + ((word32 *) ctx->in)[14] = ctx->bits[0]; + ((word32 *) ctx->in)[15] = ctx->bits[1]; - MD5Transform(ctx->buf, (word32 *) ctx->in); - byteReverse((unsigned char *) ctx->buf, 4); - memmove(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ + MD5Transform (ctx->buf, (word32 *) ctx->in); + byteReverse ((unsigned char *) ctx->buf, 4); + memmove (digest, ctx->buf, 16); + memset (ctx, 0, sizeof (ctx)); /* In case it's sensitive */ } /* The four core functions - F1 is optimized somewhat */ @@ -182,86 +194,88 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ -void MD5Transform(word32 buf[4], word32 const in[16]) +void +MD5Transform (word32 buf[4], word32 const in[16]) { - register word32 a, b, c, d; + register word32 a, b, c, d; - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; - MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); + MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7); + MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12); + MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17); + MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22); + MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7); + MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12); + MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17); + MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22); + MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7); + MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12); + MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17); + MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22); + MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7); + MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12); + MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17); + MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22); - MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); + MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5); + MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9); + MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14); + MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); + MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5); + MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9); + MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14); + MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); + MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5); + MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9); + MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14); + MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20); + MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5); + MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); + MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14); + MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); - MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); + MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4); + MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11); + MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16); + MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23); + MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4); + MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); + MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); + MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23); + MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4); + MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11); + MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16); + MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23); + MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4); + MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11); + MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); + MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23); - MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); + MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6); + MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10); + MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15); + MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21); + MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6); + MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); + MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15); + MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21); + MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); + MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); + MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15); + MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21); + MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6); + MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10); + MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); + MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21); - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; } + /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/md5.h b/qfits/src/md5.h index d1d5e7c..9316ee6 100644 --- a/qfits/src/md5.h +++ b/qfits/src/md5.h @@ -1,19 +1,20 @@ #ifndef MD5_H #define MD5_H -typedef unsigned int word32 ; +typedef unsigned int word32; -struct MD5Context { - word32 buf[4]; - word32 bits[2]; - unsigned char in[64]; +struct MD5Context +{ + word32 buf[4]; + word32 bits[2]; + unsigned char in[64]; }; -void MD5Init(struct MD5Context *context); -void MD5Update(struct MD5Context *context, unsigned char const *buf, - unsigned len); -void MD5Final(unsigned char digest[16], struct MD5Context *context); -void MD5Transform(word32 buf[4], word32 const in[16]); +void MD5Init (struct MD5Context *context); +void MD5Update (struct MD5Context *context, unsigned char const *buf, + unsigned len); +void MD5Final (unsigned char digest[16], struct MD5Context *context); +void MD5Transform (word32 buf[4], word32 const in[16]); /* * This is needed to make RSAREF happy on some MS-DOS compilers. diff --git a/qfits/src/pafs.c b/qfits/src/pafs.c index cc80f03..8ecd248 100644 --- a/qfits/src/pafs.c +++ b/qfits/src/pafs.c @@ -33,7 +33,7 @@ /*----------------------------------------------------------------------------- Function prototypes -----------------------------------------------------------------------------*/ -static char * qfits_strcrop(char * s) ; +static char *qfits_strcrop (char *s); /*----------------------------------------------------------------------------- Function codes @@ -65,32 +65,31 @@ static char * qfits_strcrop(char * s) ; the file. */ /*----------------------------------------------------------------------------*/ -FILE * qfits_paf_print_header( - char * filename, - char * paf_id, - char * paf_desc, - char * login_name, - char * datetime) +FILE * +qfits_paf_print_header (char *filename, + char *paf_id, + char *paf_desc, char *login_name, char *datetime) { - FILE * paf ; - - if ((paf=fopen(filename, "w"))==NULL) { - qfits_error("cannot create PAF file [%s]", filename); - return NULL ; - } - fprintf(paf, "PAF.HDR.START ;# start of header\n"); - fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n"); - fprintf(paf, "PAF.ID \"%s\"\n", paf_id); - fprintf(paf, "PAF.NAME \"%s\"\n", filename); - fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc); - fprintf(paf, "PAF.CRTE.NAME \"%s\"\n", login_name) ; - fprintf(paf, "PAF.CRTE.DAYTIM \"%s\"\n", datetime) ; - fprintf(paf, "PAF.LCHG.NAME \"%s\"\n", login_name) ; - fprintf(paf, "PAF.LCHG.DAYTIM \"%s\"\n", datetime) ; - fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n"); - fprintf(paf, "PAF.HDR.END ;# end of header\n"); - fprintf(paf, "\n"); - return paf ; + FILE *paf; + + if ((paf = fopen (filename, "w")) == NULL) + { + qfits_error ("cannot create PAF file [%s]", filename); + return NULL; + } + fprintf (paf, "PAF.HDR.START ;# start of header\n"); + fprintf (paf, "PAF.TYPE \"pipeline product\" ;\n"); + fprintf (paf, "PAF.ID \"%s\"\n", paf_id); + fprintf (paf, "PAF.NAME \"%s\"\n", filename); + fprintf (paf, "PAF.DESC \"%s\"\n", paf_desc); + fprintf (paf, "PAF.CRTE.NAME \"%s\"\n", login_name); + fprintf (paf, "PAF.CRTE.DAYTIM \"%s\"\n", datetime); + fprintf (paf, "PAF.LCHG.NAME \"%s\"\n", login_name); + fprintf (paf, "PAF.LCHG.DAYTIM \"%s\"\n", datetime); + fprintf (paf, "PAF.CHCK.CHECKSUM \"\"\n"); + fprintf (paf, "PAF.HDR.END ;# end of header\n"); + fprintf (paf, "\n"); + return paf; } /*----------------------------------------------------------------------------*/ @@ -107,57 +106,65 @@ FILE * qfits_paf_print_header( If the key is not found, this function returns NULL. */ /*----------------------------------------------------------------------------*/ -char * qfits_paf_query( - char * filename, - char * key) +char * +qfits_paf_query (char *filename, char *key) { - static char value[ASCIILINESZ]; - FILE * paf ; - char line[ASCIILINESZ+1]; - char val[ASCIILINESZ+1]; - char head[ASCIILINESZ+1]; - int found ; - int len ; + static char value[ASCIILINESZ]; + FILE *paf; + char line[ASCIILINESZ + 1]; + char val[ASCIILINESZ + 1]; + char head[ASCIILINESZ + 1]; + int found; + int len; - /* Check inputs */ - if (filename==NULL || key==NULL) return NULL ; + /* Check inputs */ + if (filename == NULL || key == NULL) + return NULL; - /* Check PAF validity */ - if (qfits_is_paf_file(filename)!=1) { - qfits_error("not a PAF file: [%s]", filename); - return NULL ; - } + /* Check PAF validity */ + if (qfits_is_paf_file (filename) != 1) + { + qfits_error ("not a PAF file: [%s]", filename); + return NULL; + } - /* Open file and read it */ - paf = fopen(filename, "r"); - if (paf==NULL) { - qfits_error("opening [%s]", filename); - return NULL ; + /* Open file and read it */ + paf = fopen (filename, "r"); + if (paf == NULL) + { + qfits_error ("opening [%s]", filename); + return NULL; + } + + found = 0; + while (fgets (line, ASCIILINESZ, paf) != NULL) + { + sscanf (line, "%[^ ]", head); + if (!strcmp (head, key)) + { + /* Get value */ + sscanf (line, "%*[^ ] %[^;]", value); + found++; + break; } - - found = 0 ; - while (fgets(line, ASCIILINESZ, paf)!=NULL) { - sscanf(line, "%[^ ]", head); - if (!strcmp(head, key)) { - /* Get value */ - sscanf(line, "%*[^ ] %[^;]", value); - found ++ ; - break ; - } - } - if (!found) return NULL ; - - /* Remove trailing blanks */ - strcpy(val, qfits_strcrop(value)); - /* Get rid of possible quotes */ - len = strlen(val); - if (val[0]=='\"' && val[len-1]=='\"') { - strncpy(value, val+1, len-2); - value[len-2]=(char)0; - } else { - strcpy(value, val); - } - return value ; + } + if (!found) + return NULL; + + /* Remove trailing blanks */ + strcpy (val, qfits_strcrop (value)); + /* Get rid of possible quotes */ + len = strlen (val); + if (val[0] == '\"' && val[len - 1] == '\"') + { + strncpy (value, val + 1, len - 2); + value[len - 2] = (char) 0; + } + else + { + strcpy (value, val); + } + return value; } /*----------------------------------------------------------------------------*/ @@ -170,34 +177,40 @@ char * qfits_paf_query( is checked with the presence of PAF.HDR.START at the beginning */ /*----------------------------------------------------------------------------*/ -int qfits_is_paf_file(char * filename) +int +qfits_is_paf_file (char *filename) { - FILE * fp ; - int is_paf ; - char line[ASCIILINESZ] ; - - if (filename==NULL) return -1 ; + FILE *fp; + int is_paf; + char line[ASCIILINESZ]; - /* Initialize is_paf */ - is_paf = 0 ; + if (filename == NULL) + return -1; - /* Open file */ - if ((fp = fopen(filename, "r"))==NULL) { - qfits_error("cannot open file [%s]", filename) ; - return -1 ; + /* Initialize is_paf */ + is_paf = 0; + + /* Open file */ + if ((fp = fopen (filename, "r")) == NULL) + { + qfits_error ("cannot open file [%s]", filename); + return -1; + } + + /* Parse file */ + while (fgets (line, ASCIILINESZ, fp) != NULL) + { + if (line[0] != '#') + { + if (!strncmp (line, PAF_MAGIC, PAF_MAGIC_SZ)) + is_paf = 1; + (void) fclose (fp); + return is_paf; } + } - /* Parse file */ - while (fgets(line, ASCIILINESZ, fp) != NULL) { - if (line[0] != '#') { - if (!strncmp(line, PAF_MAGIC, PAF_MAGIC_SZ)) is_paf = 1 ; - (void)fclose(fp) ; - return is_paf ; - } - } - - (void)fclose(fp) ; - return is_paf ; + (void) fclose (fp); + return is_paf; } /*----------------------------------------------------------------------------*/ @@ -214,22 +227,25 @@ int qfits_is_paf_file(char * filename) (not re-entrant). */ /*----------------------------------------------------------------------------*/ -static char * qfits_strcrop(char * s) +static char * +qfits_strcrop (char *s) { - static char l[ASCIILINESZ+1]; - char * last ; + static char l[ASCIILINESZ + 1]; + char *last; - if (s==NULL) return NULL ; - memset(l, 0, ASCIILINESZ+1); - strcpy(l, s); - last = l + strlen(l); - while (last > l) { - if (!isspace((int)*(last-1))) - break ; - last -- ; + if (s == NULL) + return NULL; + memset (l, 0, ASCIILINESZ + 1); + strcpy (l, s); + last = l + strlen (l); + while (last > l) + { + if (!isspace ((int) *(last - 1))) + break; + last--; } - *last = (char)0; - return l ; + *last = (char) 0; + return l; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/pafs.h b/qfits/src/pafs.h index a0f6f12..567ee30 100644 --- a/qfits/src/pafs.h +++ b/qfits/src/pafs.h @@ -21,7 +21,8 @@ #define PAFS_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*----------------------------------------------------------------------------- @@ -65,12 +66,10 @@ extern "C" { the file. */ /*----------------------------------------------------------------------------*/ -FILE * qfits_paf_print_header( - char * filename, - char * paf_id, - char * paf_desc, - char * login_name, - char * datetime) ; + FILE *qfits_paf_print_header (char *filename, + char *paf_id, + char *paf_desc, + char *login_name, char *datetime); /*----------------------------------------------------------------------------*/ /** @@ -86,9 +85,7 @@ FILE * qfits_paf_print_header( If the key is not found, this function returns NULL. */ /*----------------------------------------------------------------------------*/ -char * qfits_paf_query( - char * filename, - char * key) ; + char *qfits_paf_query (char *filename, char *key); /*----------------------------------------------------------------------------*/ /** @@ -100,7 +97,7 @@ char * qfits_paf_query( is checked with the presence of PAF.HDR.START at the beginning */ /*----------------------------------------------------------------------------*/ -int qfits_is_paf_file(char * filename) ; + int qfits_is_paf_file (char *filename); /* */ #ifdef __cplusplus diff --git a/qfits/src/pixio.c b/qfits/src/pixio.c index 0780557..789ce5d 100644 --- a/qfits/src/pixio.c +++ b/qfits/src/pixio.c @@ -73,240 +73,256 @@ is performed by qfits_loadpix() afterwards. */ /*----------------------------------------------------------------------------*/ -int qfitsloader_init(qfitsloader * ql) +int +qfitsloader_init (qfitsloader * ql) { - qfits_header * fh ; + qfits_header *fh; - int n_ext ; - int seg_start ; - int seg_size ; - int bitpix, naxis, naxis1, naxis2, naxis3 ; - char * xt_type ; - char * sval ; - struct stat sta ; + int n_ext; + int seg_start; + int seg_size; + int bitpix, naxis, naxis1, naxis2, naxis3; + char *xt_type; + char *sval; + struct stat sta; #if QFITS_DEBUGLOADERINIT==1 - int expsize ; + int expsize; #endif - /* Check passed object is allocated */ - if (ql==NULL) { - qfits_error("pixio: NULL loader"); - return -1 ; + /* Check passed object is allocated */ + if (ql == NULL) + { + qfits_error ("pixio: NULL loader"); + return -1; } - /* Object must contain a filename */ - if (ql->filename == NULL) { - qfits_error("pixio: NULL filename in loader"); - return -1 ; + /* Object must contain a filename */ + if (ql->filename == NULL) + { + qfits_error ("pixio: NULL filename in loader"); + return -1; } - /* Check requested file exists and contains data */ - if (stat(ql->filename, &sta)!=0) { - qfits_error("no such file: %s", ql->filename); - return -1 ; + /* Check requested file exists and contains data */ + if (stat (ql->filename, &sta) != 0) + { + qfits_error ("no such file: %s", ql->filename); + return -1; } - if (sta.st_size<1) { - qfits_error("empty file: %s", ql->filename); - return -1 ; + if (sta.st_size < 1) + { + qfits_error ("empty file: %s", ql->filename); + return -1; } - /* Requested extension number must be positive */ - if (ql->xtnum<0) { - qfits_error("pixio: negative xtnum in loader"); - return -1 ; + /* Requested extension number must be positive */ + if (ql->xtnum < 0) + { + qfits_error ("pixio: negative xtnum in loader"); + return -1; } - /* Requested returned pixel type must be legal */ - if ((ql->ptype!=PTYPE_INT) && - (ql->ptype!=PTYPE_FLOAT) && - (ql->ptype!=PTYPE_DOUBLE)) { - qfits_error("pixio: invalid ptype in loader"); - return -1 ; + /* Requested returned pixel type must be legal */ + if ((ql->ptype != PTYPE_INT) && + (ql->ptype != PTYPE_FLOAT) && (ql->ptype != PTYPE_DOUBLE)) + { + qfits_error ("pixio: invalid ptype in loader"); + return -1; } - /* Check requested file is FITS */ - if (is_fits_file(ql->filename)!=1) { - qfits_error("pixio: not a FITS file: %s", ql->filename); - return -1 ; + /* Check requested file is FITS */ + if (is_fits_file (ql->filename) != 1) + { + qfits_error ("pixio: not a FITS file: %s", ql->filename); + return -1; } - /* Get number of extensions for this file */ - n_ext = qfits_query_n_ext(ql->filename); - if (n_ext==-1) { - qfits_error("pixio: cannot get number of extensions in %s", - ql->filename); - return -1 ; + /* Get number of extensions for this file */ + n_ext = qfits_query_n_ext (ql->filename); + if (n_ext == -1) + { + qfits_error ("pixio: cannot get number of extensions in %s", + ql->filename); + return -1; } - /* Check requested extension falls within range */ - if (ql->xtnum > n_ext) { - qfits_error("pixio: requested extension %d but file %s has %d\n", - ql->xtnum, - ql->filename, - n_ext); - return -1 ; + /* Check requested extension falls within range */ + if (ql->xtnum > n_ext) + { + qfits_error ("pixio: requested extension %d but file %s has %d\n", + ql->xtnum, ql->filename, n_ext); + return -1; } - ql->exts = n_ext ; - /* Get segment offset and size for the requested buffer */ - if (qfits_get_datinfo(ql->filename, - ql->xtnum, - &seg_start, - &seg_size)!=0) { - qfits_error("pixio: cannot get seginfo for %s extension %d", - ql->filename, - ql->xtnum); - return -1 ; + ql->exts = n_ext; + /* Get segment offset and size for the requested buffer */ + if (qfits_get_datinfo (ql->filename, ql->xtnum, &seg_start, &seg_size) != 0) + { + qfits_error ("pixio: cannot get seginfo for %s extension %d", + ql->filename, ql->xtnum); + return -1; } - /* Check segment size is consistent with file size */ - if (sta.st_size < (seg_start+seg_size)) { - return -1 ; + /* Check segment size is consistent with file size */ + if (sta.st_size < (seg_start + seg_size)) + { + return -1; } - ql->seg_start = seg_start ; - ql->seg_size = seg_size ; + ql->seg_start = seg_start; + ql->seg_size = seg_size; - /* Get file header */ - fh = qfits_header_readext(ql->filename, ql->xtnum); - if (fh==NULL) { - qfits_error("pixio: cannot read header from ext %d in %s", - ql->xtnum, - ql->filename); - return -1 ; + /* Get file header */ + fh = qfits_header_readext (ql->filename, ql->xtnum); + if (fh == NULL) + { + qfits_error ("pixio: cannot read header from ext %d in %s", + ql->xtnum, ql->filename); + return -1; } - /* If the requested image is within an extension */ - if (ql->xtnum>0) { - /* Check extension is an image */ - xt_type = qfits_header_getstr(fh, "XTENSION"); - if (xt_type==NULL) { - qfits_error("pixio: cannot read extension type for ext %d in %s", - ql->xtnum, - ql->filename); - qfits_header_destroy(fh); - return -1 ; - } - xt_type = qfits_pretty_string(xt_type); - if (strcmp(xt_type, "IMAGE")) { - qfits_error( - "pixio: not an image -- extension %d in %s has type [%s]", - ql->xtnum, - ql->filename, - xt_type); - qfits_header_destroy(fh); - return -1 ; - } + /* If the requested image is within an extension */ + if (ql->xtnum > 0) + { + /* Check extension is an image */ + xt_type = qfits_header_getstr (fh, "XTENSION"); + if (xt_type == NULL) + { + qfits_error ("pixio: cannot read extension type for ext %d in %s", + ql->xtnum, ql->filename); + qfits_header_destroy (fh); + return -1; + } + xt_type = qfits_pretty_string (xt_type); + if (strcmp (xt_type, "IMAGE")) + { + qfits_error + ("pixio: not an image -- extension %d in %s has type [%s]", + ql->xtnum, ql->filename, xt_type); + qfits_header_destroy (fh); + return -1; + } } - /* Get file root informations */ - bitpix = qfits_header_getint(fh, "BITPIX", -1); - naxis = qfits_header_getint(fh, "NAXIS", -1); - naxis1 = qfits_header_getint(fh, "NAXIS1", -1); - naxis2 = qfits_header_getint(fh, "NAXIS2", -1); - naxis3 = qfits_header_getint(fh, "NAXIS3", -1); + /* Get file root informations */ + bitpix = qfits_header_getint (fh, "BITPIX", -1); + naxis = qfits_header_getint (fh, "NAXIS", -1); + naxis1 = qfits_header_getint (fh, "NAXIS1", -1); + naxis2 = qfits_header_getint (fh, "NAXIS2", -1); + naxis3 = qfits_header_getint (fh, "NAXIS3", -1); - /* Get BSCALE and BZERO if available */ - sval = qfits_header_getstr(fh, "BSCALE"); - if (sval==NULL) { - ql->bscale = 1.0 ; - } else { - ql->bscale = atof(sval); + /* Get BSCALE and BZERO if available */ + sval = qfits_header_getstr (fh, "BSCALE"); + if (sval == NULL) + { + ql->bscale = 1.0; } - sval = qfits_header_getstr(fh, "BZERO"); - if (sval==NULL) { - ql->bzero = 0.0 ; - } else { - ql->bzero = atof(sval); + else + { + ql->bscale = atof (sval); + } + sval = qfits_header_getstr (fh, "BZERO"); + if (sval == NULL) + { + ql->bzero = 0.0; + } + else + { + ql->bzero = atof (sval); } - /* Destroy header */ - qfits_header_destroy(fh); + /* Destroy header */ + qfits_header_destroy (fh); - /* Check BITPIX is present */ - if (bitpix==-1) { - qfits_error("pixio: missing BITPIX in file %s", ql->filename); - return -1 ; + /* Check BITPIX is present */ + if (bitpix == -1) + { + qfits_error ("pixio: missing BITPIX in file %s", ql->filename); + return -1; } - /* Check BITPIX is valid */ - if ((bitpix!= 8) && - (bitpix!= 16) && - (bitpix!= 32) && - (bitpix!= -32) && - (bitpix!= -64)) { - qfits_error("pixio: invalid BITPIX (%d) in file %s", - bitpix, - ql->filename); - return -1 ; + /* Check BITPIX is valid */ + if ((bitpix != 8) && + (bitpix != 16) && (bitpix != 32) && (bitpix != -32) && (bitpix != -64)) + { + qfits_error ("pixio: invalid BITPIX (%d) in file %s", + bitpix, ql->filename); + return -1; } - ql->bitpix = bitpix ; + ql->bitpix = bitpix; - /* Check NAXIS is present and valid */ - if (naxis<0) { - qfits_error("pixio: no NAXIS in file %s", ql->filename); - return -1 ; + /* Check NAXIS is present and valid */ + if (naxis < 0) + { + qfits_error ("pixio: no NAXIS in file %s", ql->filename); + return -1; } - if (naxis==0) { - qfits_error("pixio: no pixel in ext %d of file %s", - ql->xtnum, - ql->filename); - return -1 ; + if (naxis == 0) + { + qfits_error ("pixio: no pixel in ext %d of file %s", + ql->xtnum, ql->filename); + return -1; } - if (naxis>3) { - qfits_error("pixio: NAXIS>3 (%d) unsupported", naxis); - return -1 ; + if (naxis > 3) + { + qfits_error ("pixio: NAXIS>3 (%d) unsupported", naxis); + return -1; } - /* NAXIS1 must always be present */ - if (naxis1<0) { - qfits_error("pixio: no NAXIS1 in file %s", ql->filename); - return -1 ; + /* NAXIS1 must always be present */ + if (naxis1 < 0) + { + qfits_error ("pixio: no NAXIS1 in file %s", ql->filename); + return -1; } - /* Update dimension fields in loader struct */ - ql->lx = 1 ; - ql->ly = 1 ; - ql->np = 1 ; + /* Update dimension fields in loader struct */ + ql->lx = 1; + ql->ly = 1; + ql->np = 1; - switch (naxis) { - case 1: - ql->lx = naxis1 ; - break ; + switch (naxis) + { + case 1: + ql->lx = naxis1; + break; - case 3: - if (naxis3<0) { - qfits_error("pixio: no NAXIS3 in file %s", ql->filename); - return -1 ; - } - ql->np = naxis3 ; - /* No break statement: intended to go through next case */ + case 3: + if (naxis3 < 0) + { + qfits_error ("pixio: no NAXIS3 in file %s", ql->filename); + return -1; + } + ql->np = naxis3; + /* No break statement: intended to go through next case */ - case 2: - if (naxis2<0) { - qfits_error("pixio: no NAXIS2 in file %s", ql->filename); - return -1 ; - } - ql->ly = naxis2 ; - ql->lx = naxis1 ; - break ; + case 2: + if (naxis2 < 0) + { + qfits_error ("pixio: no NAXIS2 in file %s", ql->filename); + return -1; + } + ql->ly = naxis2; + ql->lx = naxis1; + break; } - /* Check that requested plane number falls within range */ - if (ql->pnum >= ql->np) { - qfits_error("pixio: requested plane %d but NAXIS3=%d", - ql->pnum, - ql->np); - return -1 ; + /* Check that requested plane number falls within range */ + if (ql->pnum >= ql->np) + { + qfits_error ("pixio: requested plane %d but NAXIS3=%d", + ql->pnum, ql->np); + return -1; } /* Disabled, should only be done in debug mode */ #if QFITS_DEBUGLOADERINIT==1 - /* Check segment size is consistent with declared size */ - expsize = BYTESPERPIXEL(ql->bitpix) * ql->lx * ql->ly * ql->np ; - /* Round up to next multiple of FITS_BLOCK_SIZE */ - if (expsize % FITS_BLOCK_SIZE) { - expsize = FITS_BLOCK_SIZE * (1 + (expsize/FITS_BLOCK_SIZE)) ; + /* Check segment size is consistent with declared size */ + expsize = BYTESPERPIXEL (ql->bitpix) * ql->lx * ql->ly * ql->np; + /* Round up to next multiple of FITS_BLOCK_SIZE */ + if (expsize % FITS_BLOCK_SIZE) + { + expsize = FITS_BLOCK_SIZE * (1 + (expsize / FITS_BLOCK_SIZE)); } - if (expsize != ql->seg_size) { - qfits_warning("invalid data segment size\n" - "found: %d blocks\n" - "expected: %d blocks\n", - ql->seg_size / 2880, - expsize / 2880); + if (expsize != ql->seg_size) + { + qfits_warning ("invalid data segment size\n" + "found: %d blocks\n" + "expected: %d blocks\n", + ql->seg_size / 2880, expsize / 2880); } #endif - /* Everything Ok, fields have been filled along. */ - /* Mark the structure as initialized */ - ql->_init = QFITSLOADERINIT_MAGIC ; - return 0 ; + /* Everything Ok, fields have been filled along. */ + /* Mark the structure as initialized */ + ql->_init = QFITSLOADERINIT_MAGIC; + return 0; } /*----------------------------------------------------------------------------*/ @@ -329,122 +345,131 @@ int qfitsloader_init(qfitsloader * ql) code before you make calls to the pixel loader here. */ /*----------------------------------------------------------------------------*/ -int qfits_loadpix(qfitsloader * ql) +int +qfits_loadpix (qfitsloader * ql) { - byte * fptr ; - size_t fsize ; - int npix ; - int datastart ; - int datasize ; - FILE * lfile ; - int dataread ; + byte *fptr; + size_t fsize; + int npix; + int datastart; + int datasize; + FILE *lfile; + int dataread; - /* Check inputs */ - if (ql==NULL) return -1 ; - if (ql->_init != QFITSLOADERINIT_MAGIC) { - qfits_error("pixio: called with unitialized obj"); - return -1 ; + /* Check inputs */ + if (ql == NULL) + return -1; + if (ql->_init != QFITSLOADERINIT_MAGIC) + { + qfits_error ("pixio: called with unitialized obj"); + return -1; } - /* Get raw pixel buffer from file */ - npix = ql->lx * ql->ly ; - datasize = npix * BYTESPERPIXEL(ql->bitpix); - datastart = ql->seg_start + ql->pnum * datasize ; + /* Get raw pixel buffer from file */ + npix = ql->lx * ql->ly; + datasize = npix * BYTESPERPIXEL (ql->bitpix); + datastart = ql->seg_start + ql->pnum * datasize; - /* Check loading mode */ - if (ql->map) { - /* Map the file in */ - fptr = (byte*)falloc(ql->filename, datastart, &fsize); - if (fptr==NULL) { - qfits_error("pixio: cannot falloc(%s)", ql->filename); - return -1 ; - } - } else { - /* - * Read using malloc() + fread() - * This ensures the input data are untouched. - */ - if ((lfile=fopen(ql->filename, "r"))==NULL) { - qfits_error("pixio: cannot open %s", ql->filename); - return -1 ; - } - if (fseek(lfile, datastart, SEEK_SET)!=0) { - qfits_error("pixio: cannot seek %s", ql->filename); - fclose(lfile); - return -1 ; - } - fptr = (byte*)malloc(datasize) ; - dataread = fread(fptr, sizeof(byte), datasize, lfile); - fclose(lfile); - if (dataread!=datasize) { - free(fptr) ; - qfits_error("pixio: cannot read from %s", ql->filename); - return -1 ; - } + /* Check loading mode */ + if (ql->map) + { + /* Map the file in */ + fptr = (byte *) falloc (ql->filename, datastart, &fsize); + if (fptr == NULL) + { + qfits_error ("pixio: cannot falloc(%s)", ql->filename); + return -1; + } + } + else + { + /* + * Read using malloc() + fread() + * This ensures the input data are untouched. + */ + if ((lfile = fopen (ql->filename, "r")) == NULL) + { + qfits_error ("pixio: cannot open %s", ql->filename); + return -1; + } + if (fseek (lfile, datastart, SEEK_SET) != 0) + { + qfits_error ("pixio: cannot seek %s", ql->filename); + fclose (lfile); + return -1; + } + fptr = (byte *) malloc (datasize); + dataread = fread (fptr, sizeof (byte), datasize, lfile); + fclose (lfile); + if (dataread != datasize) + { + free (fptr); + qfits_error ("pixio: cannot read from %s", ql->filename); + return -1; + } } - /* Initialize buffer pointers */ - ql->ibuf = NULL ; - ql->fbuf = NULL ; - ql->dbuf = NULL ; + /* Initialize buffer pointers */ + ql->ibuf = NULL; + ql->fbuf = NULL; + ql->dbuf = NULL; - /* - * Special cases: mapped file is identical to requested format. - * This is only possible on big-endian machines, since FITS is - * big-endian only. - */ + /* + * Special cases: mapped file is identical to requested format. + * This is only possible on big-endian machines, since FITS is + * big-endian only. + */ #ifdef WORDS_BIGENDIAN - if (ql->ptype==PTYPE_FLOAT && ql->bitpix==-32) { - ql->fbuf = (float*)fptr ; - return 0 ; + if (ql->ptype == PTYPE_FLOAT && ql->bitpix == -32) + { + ql->fbuf = (float *) fptr; + return 0; } - if (ql->ptype==PTYPE_DOUBLE && ql->bitpix==-64) { - ql->dbuf = (double*)fptr ; - return 0 ; + if (ql->ptype == PTYPE_DOUBLE && ql->bitpix == -64) + { + ql->dbuf = (double *) fptr; + return 0; } - if (ql->ptype==PTYPE_INT && ql->bitpix==32) { - ql->ibuf = (int*)fptr ; - return 0 ; + if (ql->ptype == PTYPE_INT && ql->bitpix == 32) + { + ql->ibuf = (int *) fptr; + return 0; } #endif - /* General case: fallback to dedicated conversion function */ - switch (ql->ptype) { - case PTYPE_FLOAT: - ql->fbuf = qfits_pixin_float( fptr, - npix, - ql->bitpix, - ql->bscale, - ql->bzero); - break ; + /* General case: fallback to dedicated conversion function */ + switch (ql->ptype) + { + case PTYPE_FLOAT: + ql->fbuf = qfits_pixin_float (fptr, + npix, ql->bitpix, ql->bscale, ql->bzero); + break; - case PTYPE_INT: - ql->ibuf = qfits_pixin_int( fptr, - npix, - ql->bitpix, - ql->bscale, - ql->bzero); - break ; + case PTYPE_INT: + ql->ibuf = qfits_pixin_int (fptr, + npix, ql->bitpix, ql->bscale, ql->bzero); + break; - case PTYPE_DOUBLE: - ql->dbuf = qfits_pixin_double( fptr, - npix, - ql->bitpix, - ql->bscale, - ql->bzero); - break ; + case PTYPE_DOUBLE: + ql->dbuf = qfits_pixin_double (fptr, + npix, ql->bitpix, ql->bscale, ql->bzero); + break; } - if (ql->map) { - fdealloc((char*)fptr, datastart, fsize) ; - } else { - free(fptr); + if (ql->map) + { + fdealloc ((char *) fptr, datastart, fsize); + } + else + { + free (fptr); } - if (ql->ibuf==NULL && ql->fbuf==NULL && ql->dbuf==NULL) { - qfits_error("pixio: error during conversion"); - return -1 ; + if (ql->ibuf == NULL && ql->fbuf == NULL && ql->dbuf == NULL) + { + qfits_error ("pixio: error during conversion"); + return -1; } - return 0 ; + return 0; } /*----------------------------------------------------------------------------*/ @@ -468,86 +493,89 @@ int qfits_loadpix(qfitsloader * ql) free on the returned pointer. */ /*----------------------------------------------------------------------------*/ -float * qfits_pixin_float( - byte * p_source, - int npix, - int bitpix, - double bscale, - double bzero) +float * +qfits_pixin_float (byte * p_source, + int npix, int bitpix, double bscale, double bzero) { - int i ; - float * baseptr ; - float * p_dest ; - double dpix ; - short spix ; - int lpix ; - float fpix ; - byte XLpix[8] ; - + int i; + float *baseptr; + float *p_dest; + double dpix; + short spix; + int lpix; + float fpix; + byte XLpix[8]; - baseptr = p_dest = malloc(npix * sizeof(float)); - switch (bitpix) { - case 8: - /* No swapping needed */ - for (i=0 ; ifilename==NULL) return -1 ; - switch (qd->ptype) { - case PTYPE_FLOAT: - if (qd->fbuf==NULL) return -1 ; - break ; + /* Check inputs */ + if (qd == NULL) + return -1; + if (qd->filename == NULL) + return -1; + switch (qd->ptype) + { + case PTYPE_FLOAT: + if (qd->fbuf == NULL) + return -1; + break; - case PTYPE_DOUBLE: - if (qd->dbuf==NULL) return -1 ; - break ; + case PTYPE_DOUBLE: + if (qd->dbuf == NULL) + return -1; + break; - case PTYPE_INT: - if (qd->ibuf==NULL) return -1 ; - break ; + case PTYPE_INT: + if (qd->ibuf == NULL) + return -1; + break; - default: - return -1 ; + default: + return -1; } - /* - * Special cases: input buffer is identical to requested format. - * This is only possible on big-endian machines, since FITS is - * big-endian only. - */ - buf_out = NULL ; - buf_free = 1 ; + /* + * Special cases: input buffer is identical to requested format. + * This is only possible on big-endian machines, since FITS is + * big-endian only. + */ + buf_out = NULL; + buf_free = 1; #ifdef WORDS_BIGENDIAN - if (qd->ptype==PTYPE_FLOAT && qd->out_ptype==-32) { - buf_out = (byte*)qd->fbuf ; - buf_free=0 ; - } else if (qd->ptype==PTYPE_DOUBLE && qd->out_ptype==-64) { - buf_out = (byte*)qd->dbuf ; - buf_free=0 ; - } else if (qd->ptype==PTYPE_INT && qd->out_ptype==32) { - buf_out = (byte*)qd->ibuf ; - buf_free=0 ; + if (qd->ptype == PTYPE_FLOAT && qd->out_ptype == -32) + { + buf_out = (byte *) qd->fbuf; + buf_free = 0; + } + else if (qd->ptype == PTYPE_DOUBLE && qd->out_ptype == -64) + { + buf_out = (byte *) qd->dbuf; + buf_free = 0; + } + else if (qd->ptype == PTYPE_INT && qd->out_ptype == 32) + { + buf_out = (byte *) qd->ibuf; + buf_free = 0; } #endif - buf_sz = qd->npix * BYTESPERPIXEL(qd->out_ptype); + buf_sz = qd->npix * BYTESPERPIXEL (qd->out_ptype); - /* General case */ - if (buf_out==NULL) { - switch (qd->ptype) { - /* Convert buffer */ - case PTYPE_FLOAT: - buf_out = qfits_pixdump_float( qd->fbuf, - qd->npix, - qd->out_ptype); - break ; + /* General case */ + if (buf_out == NULL) + { + switch (qd->ptype) + { + /* Convert buffer */ + case PTYPE_FLOAT: + buf_out = qfits_pixdump_float (qd->fbuf, qd->npix, qd->out_ptype); + break; - /* Convert buffer */ - case PTYPE_INT: - buf_out = qfits_pixdump_int( qd->ibuf, - qd->npix, - qd->out_ptype); - break ; + /* Convert buffer */ + case PTYPE_INT: + buf_out = qfits_pixdump_int (qd->ibuf, qd->npix, qd->out_ptype); + break; - /* Convert buffer */ - case PTYPE_DOUBLE: - buf_out = qfits_pixdump_double( qd->dbuf, - qd->npix, - qd->out_ptype); - break ; - } + /* Convert buffer */ + case PTYPE_DOUBLE: + buf_out = qfits_pixdump_double (qd->dbuf, qd->npix, qd->out_ptype); + break; + } } - if (buf_out==NULL) { - qfits_error("cannot dump pixel buffer"); - return -1 ; + if (buf_out == NULL) + { + qfits_error ("cannot dump pixel buffer"); + return -1; } - /* Dump buffer */ - if (!strncmp(qd->filename, "STDOUT", 6)) { - f_out = stdout ; - } else { - f_out = fopen(qd->filename, "a"); + /* Dump buffer */ + if (!strncmp (qd->filename, "STDOUT", 6)) + { + f_out = stdout; } - if (f_out==NULL) { - free(buf_out); - return -1 ; + else + { + f_out = fopen (qd->filename, "a"); } - fwrite(buf_out, buf_sz, 1, f_out); - if (buf_free) { - free(buf_out); + if (f_out == NULL) + { + free (buf_out); + return -1; } - if (f_out!=stdout) { - fclose(f_out); + fwrite (buf_out, buf_sz, 1, f_out); + if (buf_free) + { + free (buf_out); } - return 0 ; + if (f_out != stdout) + { + fclose (f_out); + } + return 0; } /*----------------------------------------------------------------------------*/ @@ -902,103 +951,125 @@ int qfits_pixdump(qfitsdumper * qd) using the free() function offered by xmemory. */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_float(float * buf, int npix, int ptype) +byte * +qfits_pixdump_float (float *buf, int npix, int ptype) { - byte * buf_out ; - register byte * op ; - int i ; - int lpix ; - short spix ; - double dpix ; + byte *buf_out; + register byte *op; + int i; + int lpix; + short spix; + double dpix; - buf_out = malloc(npix * BYTESPERPIXEL(ptype)); - op = buf_out ; - switch (ptype) { - case 8: - /* Convert from float to 8 bits */ - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (buf[i]<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)buf[i]; - } - } - break ; + buf_out = malloc (npix * BYTESPERPIXEL (ptype)); + op = buf_out; + switch (ptype) + { + case 8: + /* Convert from float to 8 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 255.0) + { + *op++ = (byte) 0xff; + } + else if (buf[i] < 0.0) + { + *op++ = (byte) 0x00; + } + else + { + *op++ = (byte) buf[i]; + } + } + break; - case 16: - /* Convert from float to 16 bits */ - for (i=0 ; i32767.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - } else if (buf[i]<-32768.0) { - *op++ = (byte)0x80 ; - *op++ = 0x00 ; - } else { - spix = (short)buf[i]; - *op++ = (spix >> 8) ; - *op++ = (spix & (byte)0xff) ; - } - } - break ; + case 16: + /* Convert from float to 16 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 32767.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + } + else if (buf[i] < -32768.0) + { + *op++ = (byte) 0x80; + *op++ = 0x00; + } + else + { + spix = (short) buf[i]; + *op++ = (spix >> 8); + *op++ = (spix & (byte) 0xff); + } + } + break; - case 32: - /* Convert from float to 32 bits */ - for (i=0 ; i 2147483647.0) { - *op++ = (byte)0x7f ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - *op++ = (byte)0xff ; - } else if (buf[i]<-2147483648.0) { - *op++ = (byte)0x80 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - *op++ = (byte)0x00 ; - } else { - lpix = (int)buf[i] ; - *op++ = (byte)(lpix >> 24) ; - *op++ = (byte)(lpix >> 16) & 0xff ; - *op++ = (byte)(lpix >> 8 ) & 0xff ; - *op++ = (byte)(lpix) & 0xff ; - } - } - break ; + case 32: + /* Convert from float to 32 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 2147483647.0) + { + *op++ = (byte) 0x7f; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + *op++ = (byte) 0xff; + } + else if (buf[i] < -2147483648.0) + { + *op++ = (byte) 0x80; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + *op++ = (byte) 0x00; + } + else + { + lpix = (int) buf[i]; + *op++ = (byte) (lpix >> 24); + *op++ = (byte) (lpix >> 16) & 0xff; + *op++ = (byte) (lpix >> 8) & 0xff; + *op++ = (byte) (lpix) & 0xff; + } + } + break; - case -32: - /* Convert from float to float */ - memcpy(op, buf, npix * sizeof(float)); + case -32: + /* Convert from float to float */ + memcpy (op, buf, npix * sizeof (float)); #ifndef WORDS_BIGENDIAN - for (i=0 ; i255) { - *op++ = (byte)0xff ; - } else if (buf[i]<0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)buf[i] ; - } - } - break ; + buf_out = malloc (npix * BYTESPERPIXEL (ptype)); + op = buf_out; + switch (ptype) + { + case 8: + /* Convert from int32 to 8 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 255) + { + *op++ = (byte) 0xff; + } + else if (buf[i] < 0) + { + *op++ = (byte) 0x00; + } + else + { + *op++ = (byte) buf[i]; + } + } + break; - case 16: - /* Convert from int32 to 16 bits */ - for (i=0 ; i32767) { - spix = 32767 ; - } else if (buf[i]<-32768) { - spix = -32768 ; - } else { - spix = (short)buf[i] ; - } + case 16: + /* Convert from int32 to 16 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 32767) + { + spix = 32767; + } + else if (buf[i] < -32768) + { + spix = -32768; + } + else + { + spix = (short) buf[i]; + } #ifndef WORDS_BIGENDIAN - swap_bytes(&spix, 2); + swap_bytes (&spix, 2); #endif - memcpy(op, &spix, 2); - op += 2 ; - } - break ; + memcpy (op, &spix, 2); + op += 2; + } + break; - case 32: - /* Convert from int32 to 32 bits */ - memcpy(op, buf, npix * sizeof(int)); + case 32: + /* Convert from int32 to 32 bits */ + memcpy (op, buf, npix * sizeof (int)); #ifndef WORDS_BIGENDIAN - for (i=0 ; i255.0) { - *op++ = (byte)0xff ; - } else if (buf[i]<0.0) { - *op++ = (byte)0x00 ; - } else { - *op++ = (byte)buf[i] ; - } - } - break ; + buf_out = malloc (npix * BYTESPERPIXEL (ptype)); + op = buf_out; + switch (ptype) + { + case 8: + /* Convert from double to 8 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 255.0) + { + *op++ = (byte) 0xff; + } + else if (buf[i] < 0.0) + { + *op++ = (byte) 0x00; + } + else + { + *op++ = (byte) buf[i]; + } + } + break; - case 16: - /* Convert from double to 16 bits */ - for (i=0 ; i32767.0) { - spix = 32767 ; - } else if (buf[i]<-32768.0) { - spix = -32768 ; - } else { - spix = (short)buf[i] ; - } + case 16: + /* Convert from double to 16 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 32767.0) + { + spix = 32767; + } + else if (buf[i] < -32768.0) + { + spix = -32768; + } + else + { + spix = (short) buf[i]; + } #ifndef WORDS_BIGENDIAN - swap_bytes(&spix, 2); + swap_bytes (&spix, 2); #endif - memcpy(op, &spix, 2); - op += 2 ; - } - break ; + memcpy (op, &spix, 2); + op += 2; + } + break; - case 32: - /* Convert from double to 32 bits */ - for (i=0 ; i 2147483647.0) { - lpix = 2147483647 ; - } else if (buf[i] < -2147483648.0) { - lpix = -2147483647 ; - } else { - lpix = (int)buf[i]; - } + case 32: + /* Convert from double to 32 bits */ + for (i = 0; i < npix; i++) + { + if (buf[i] > 2147483647.0) + { + lpix = 2147483647; + } + else if (buf[i] < -2147483648.0) + { + lpix = -2147483647; + } + else + { + lpix = (int) buf[i]; + } #ifndef WORDS_BIGENDIAN - swap_bytes(&lpix, 4); + swap_bytes (&lpix, 4); #endif - memcpy(op, &lpix, 4); - op += 4 ; - } - break ; + memcpy (op, &lpix, 4); + op += 4; + } + break; - case -32: - /* Convert from double to float */ - for (i=0 ; ifilename, - ql->xtnum, - ql->pnum, - ql->ptype, - ql->lx, - ql->ly, - ql->np, - ql->bitpix, - ql->seg_start, - ql->bscale, - ql->bzero, - ql->ibuf, - ql->fbuf, - ql->dbuf); + fprintf (stderr, + "file : %s\n" + "xtnum : %d\n" + "pnum : %d\n" + "ptype : %d\n" + "lx : %d\n" + "ly : %d\n" + "np : %d\n" + "bitpix : %d\n" + "seg_start : %d\n" + "bscale : %g\n" + "bzero : %g\n" + "ibuf : %p\n" + "fbuf : %p\n" + "dbuf : %p\n", + ql->filename, + ql->xtnum, + ql->pnum, + ql->ptype, + ql->lx, + ql->ly, + ql->np, + ql->bitpix, + ql->seg_start, + ql->bscale, ql->bzero, ql->ibuf, ql->fbuf, ql->dbuf); } -int main (int argc, char * argv[]) +int +main (int argc, char *argv[]) { - qfitsloader ql ; + qfitsloader ql; - if (argc<2) { - printf("use: %s \n", argv[0]); - return 1 ; + if (argc < 2) + { + printf ("use: %s \n", argv[0]); + return 1; } - ql.filename = argv[1] ; - ql.xtnum = 0 ; - ql.pnum = 0 ; - ql.ptype = PTYPE_FLOAT ; + ql.filename = argv[1]; + ql.xtnum = 0; + ql.pnum = 0; + ql.ptype = PTYPE_FLOAT; - if (qfits_loadpix(&ql)!=0) { - printf("error occurred during loading: abort\n"); - return -1 ; + if (qfits_loadpix (&ql) != 0) + { + printf ("error occurred during loading: abort\n"); + return -1; } - qfitsloader_dump(&ql); - /* xmemory_status(); */ - printf("pix[0]=%g\n" - "pix[100]=%g\n" - "pix[10000]=%g\n", - ql.fbuf[0], - ql.fbuf[100], - ql.fbuf[10000]); - free(ql.fbuf); + qfitsloader_dump (&ql); + /* xmemory_status(); */ + printf ("pix[0]=%g\n" + "pix[100]=%g\n" + "pix[10000]=%g\n", ql.fbuf[0], ql.fbuf[100], ql.fbuf[10000]); + free (ql.fbuf); - ql.ptype = PTYPE_INT ; - if (qfits_loadpix(&ql)!=0) { - printf("error occurred during loading: abort\n"); - return -1 ; + ql.ptype = PTYPE_INT; + if (qfits_loadpix (&ql) != 0) + { + printf ("error occurred during loading: abort\n"); + return -1; } - qfitsloader_dump(&ql); - /* xmemory_status(); */ - printf("pix[0]=%d\n" - "pix[100]=%d\n" - "pix[10000]=%d\n", - ql.ibuf[0], - ql.ibuf[100], - ql.ibuf[10000]); - free(ql.ibuf); + qfitsloader_dump (&ql); + /* xmemory_status(); */ + printf ("pix[0]=%d\n" + "pix[100]=%d\n" + "pix[10000]=%d\n", ql.ibuf[0], ql.ibuf[100], ql.ibuf[10000]); + free (ql.ibuf); - ql.ptype = PTYPE_DOUBLE ; - if (qfits_loadpix(&ql)!=0) { - printf("error occurred during loading: abort\n"); - return -1 ; + ql.ptype = PTYPE_DOUBLE; + if (qfits_loadpix (&ql) != 0) + { + printf ("error occurred during loading: abort\n"); + return -1; } - qfitsloader_dump(&ql); - /* xmemory_status(); */ - printf("pix[0]=%g\n" - "pix[100]=%g\n" - "pix[10000]=%g\n", - ql.dbuf[0], - ql.dbuf[100], - ql.dbuf[10000]); - free(ql.dbuf); + qfitsloader_dump (&ql); + /* xmemory_status(); */ + printf ("pix[0]=%g\n" + "pix[100]=%g\n" + "pix[10000]=%g\n", ql.dbuf[0], ql.dbuf[100], ql.dbuf[10000]); + free (ql.dbuf); - return 0 ; + return 0; } #endif /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/pixio.h b/qfits/src/pixio.h index 4374afd..b4655ae 100644 --- a/qfits/src/pixio.h +++ b/qfits/src/pixio.h @@ -53,7 +53,7 @@ for readability. */ /*----------------------------------------------------------------------------*/ -typedef unsigned char byte ; +typedef unsigned char byte; /*----------------------------------------------------------------------------*/ /** @@ -126,49 +126,50 @@ int main(int argc, char * argv[]) @endcode */ /*----------------------------------------------------------------------------*/ -typedef struct qfitsloader { +typedef struct qfitsloader +{ /** Private field to see if structure has been initialized */ - int _init ; - + int _init; + /** input: Name of the file you want to read pixels from */ - char * filename ; + char *filename; /** input: xtension number you want to read */ - int xtnum ; + int xtnum; /** input: Index of the plane you want, from 0 to np-1 */ - int pnum ; + int pnum; /** input: Pixel type you want (PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE) */ - int ptype ; + int ptype; /** input: Guarantee file copy or allow file mapping */ - int map ; + int map; /** output: Total number of extensions found in file */ - int exts ; + int exts; /** output: Size in X of the requested plane */ - int lx ; + int lx; /** output: Size in Y of the requested plane */ - int ly ; + int ly; /** output: Number of planes present in this extension */ - int np ; + int np; /** output: BITPIX for this extension */ - int bitpix ; + int bitpix; /** output: Start of the data segment (in bytes) for your request */ - int seg_start ; + int seg_start; /** output: Size of the data segment (in bytes) for your request */ - int seg_size ; + int seg_size; /** output: BSCALE found for this extension */ - double bscale ; + double bscale; /** output: BZERO found for this extension */ - double bzero ; + double bzero; /** output: Pointer to pixel buffer loaded as integer values */ - int * ibuf ; + int *ibuf; /** output: Pointer to pixel buffer loaded as float values */ - float * fbuf ; + float *fbuf; /** output: Pointer to pixel buffer loaded as double values */ - double * dbuf ; + double *dbuf; -} qfitsloader ; +} qfitsloader; /*----------------------------------------------------------------------------*/ @@ -215,25 +216,26 @@ typedef struct qfitsloader { could have been re-directed). */ /*----------------------------------------------------------------------------*/ -typedef struct qfitsdumper { +typedef struct qfitsdumper +{ /** Name of the file to dump to, "STDOUT" to dump to stdout */ - char * filename ; + char *filename; /** Number of pixels in the buffer to dump */ - int npix ; + int npix; /** Buffer type: PTYPE_FLOAT, PTYPE_INT or PTYPE_DOUBLE */ - int ptype ; + int ptype; /** Pointer to input integer pixel buffer */ - int * ibuf ; + int *ibuf; /** Pointer to input float pixel buffer */ - float * fbuf ; + float *fbuf; /** Pointer to input double pixel buffer */ - double * dbuf ; + double *dbuf; /** Requested BITPIX in output FITS file */ - int out_ptype ; -} qfitsdumper ; + int out_ptype; +} qfitsdumper; /*----------------------------------------------------------------------------- @@ -265,7 +267,7 @@ typedef struct qfitsdumper { is performed by qfits_loadpix() afterwards. */ /*----------------------------------------------------------------------------*/ -int qfitsloader_init(qfitsloader * ql); +int qfitsloader_init (qfitsloader * ql); /*----------------------------------------------------------------------------*/ /** @@ -287,7 +289,7 @@ int qfitsloader_init(qfitsloader * ql); code before you make calls to the pixel loader here. */ /*----------------------------------------------------------------------------*/ -int qfits_loadpix(qfitsloader * ql); +int qfits_loadpix (qfitsloader * ql); /*----------------------------------------------------------------------------*/ /** @@ -310,7 +312,7 @@ int qfits_loadpix(qfitsloader * ql); free on the returned pointer. */ /*----------------------------------------------------------------------------*/ -float * qfits_pixin_float (byte *, int, int, double, double); +float *qfits_pixin_float (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -333,7 +335,7 @@ float * qfits_pixin_float (byte *, int, int, double, double); free on the returned pointer. */ /*----------------------------------------------------------------------------*/ -int * qfits_pixin_int (byte *, int, int, double, double); +int *qfits_pixin_int (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -354,9 +356,9 @@ int * qfits_pixin_int (byte *, int, int, double, double); The returned buffer must be deallocated using the free() offered by xmemory. It is enough to #include "xmemory.h" before calling free on the returned pointer. - */ + */ /*----------------------------------------------------------------------------*/ -double * qfits_pixin_double(byte *, int, int, double, double); +double *qfits_pixin_double (byte *, int, int, double, double); /*----------------------------------------------------------------------------*/ /** @@ -393,7 +395,7 @@ double * qfits_pixin_double(byte *, int, int, double, double); will be performed to stdout. */ /*----------------------------------------------------------------------------*/ -int qfits_pixdump(qfitsdumper * qd) ; +int qfits_pixdump (qfitsdumper * qd); /*----------------------------------------------------------------------------*/ /** @@ -407,9 +409,9 @@ int qfits_pixdump(qfitsdumper * qd) ; suitable for dumping to a FITS file (i.e. big-endian, in the requested pixel type). The returned pointer must be deallocated using the free() function offered by xmemory. - */ + */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_float(float * buf, int npix, int ptype); +byte *qfits_pixdump_float (float *buf, int npix, int ptype); /*----------------------------------------------------------------------------*/ /** @@ -425,7 +427,7 @@ byte * qfits_pixdump_float(float * buf, int npix, int ptype); using the free() function offered by xmemory. */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_int(int * buf, int npix, int ptype); +byte *qfits_pixdump_int (int *buf, int npix, int ptype); /*----------------------------------------------------------------------------*/ /** @@ -441,7 +443,7 @@ byte * qfits_pixdump_int(int * buf, int npix, int ptype); using the free() function offered by xmemory. */ /*----------------------------------------------------------------------------*/ -byte * qfits_pixdump_double(double * buf, int npix, int ptype); +byte *qfits_pixdump_double (double *buf, int npix, int ptype); /* */ #endif diff --git a/qfits/src/qerror.c b/qfits/src/qerror.c index 55c8d72..bf155f6 100644 --- a/qfits/src/qerror.c +++ b/qfits/src/qerror.c @@ -41,16 +41,21 @@ -----------------------------------------------------------------------------*/ /* Type of a display function only defined for legibility here */ -typedef void (*qfits_err_dispfunc)(char *) ; +typedef void (*qfits_err_dispfunc) (char *); /* Default display function prints out msg to stderr */ -static void qfits_err_display_stderr(char * s) -{ fprintf(stderr, "qfits: %s\n", s); } +static void +qfits_err_display_stderr (char *s) +{ + fprintf (stderr, "qfits: %s\n", s); +} + /* Static control structure, completely private */ -static struct { - qfits_err_dispfunc disp[QFITS_ERR_MAXERRDISP] ; - int n ; - int active ; -} qfits_err_control = {{qfits_err_display_stderr}, 1, 0} ; +static struct +{ + qfits_err_dispfunc disp[QFITS_ERR_MAXERRDISP]; + int n; + int active; +} qfits_err_control = { {qfits_err_display_stderr}, 1, 0 }; /*----------------------------------------------------------------------------- Function codes @@ -64,21 +69,24 @@ static struct { It calls registered display functions one after another. */ /*----------------------------------------------------------------------------*/ -static void qfits_err_main_display(char * msg) +static void +qfits_err_main_display (char *msg) { - int i ; + int i; - /* Check if there is a message in input */ - if (msg==NULL) - return ; + /* Check if there is a message in input */ + if (msg == NULL) + return; - /* Loop on all registered functions and call them */ - for (i=0 ; i */ /* Public warning/error functions */ -void qfits_warning(const char *fmt, ...); -void qfits_error(const char *fmt, ...); +void qfits_warning (const char *fmt, ...); +void qfits_error (const char *fmt, ...); #endif /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/simple.c b/qfits/src/simple.c index 9cd1284..653aca3 100644 --- a/qfits/src/simple.c +++ b/qfits/src/simple.c @@ -50,14 +50,14 @@ */ /** A regular expression matching a floating-point number */ static char regex_float[] = - "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$"; + "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$"; /** A regular expression matching an integer */ static char regex_int[] = "^[+-]?[0-9]+$"; /** A regular expression matching a complex number (int or float) */ static char regex_cmp[] = -"^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?[ ]+[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$"; + "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?[ ]+[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eEdD][+-]?[0-9]+)?$"; /*----------------------------------------------------------------------------- Function codes @@ -90,9 +90,10 @@ static char regex_cmp[] = Returns NULL in case the requested keyword cannot be found. */ /*----------------------------------------------------------------------------*/ -char * qfits_query_hdr(char * filename, const char * keyword) +char * +qfits_query_hdr (char *filename, const char *keyword) { - return qfits_query_ext(filename, keyword, 0); + return qfits_query_ext (filename, keyword, 0); } /*----------------------------------------------------------------------------*/ @@ -108,90 +109,99 @@ char * qfits_query_hdr(char * filename, const char * keyword) strictly identical to qfits_query_hdr(). */ /*----------------------------------------------------------------------------*/ -char * qfits_query_ext(char * filename, const char * keyword, int xtnum) +char * +qfits_query_ext (char *filename, const char *keyword, int xtnum) { - char * exp_key ; - char * where ; - char * start ; - char * value ; - char test1, test2 ; - int i ; - int len ; - int different ; - int seg_start ; - int seg_size ; - long bufcount ; - size_t size ; + char *exp_key; + char *where; + char *start; + char *value; + char test1, test2; + int i; + int len; + int different; + int seg_start; + int seg_size; + long bufcount; + size_t size; - /* Bulletproof entries */ - if (filename==NULL || keyword==NULL || xtnum<0) return NULL ; + /* Bulletproof entries */ + if (filename == NULL || keyword == NULL || xtnum < 0) + return NULL; - /* Expand keyword */ - exp_key = qfits_expand_keyword(keyword); + /* Expand keyword */ + exp_key = qfits_expand_keyword (keyword); - /* - * Find out offsets to the required extension - * Record the xtension start and stop offsets - */ - if (qfits_get_hdrinfo(filename, xtnum, &seg_start, &seg_size)==-1) { - return NULL ; + /* + * Find out offsets to the required extension + * Record the xtension start and stop offsets + */ + if (qfits_get_hdrinfo (filename, xtnum, &seg_start, &seg_size) == -1) + { + return NULL; } - /* - * Get a hand on requested buffer - */ + /* + * Get a hand on requested buffer + */ - start = falloc(filename, seg_start, &size); - if (start==NULL) return NULL ; + start = falloc (filename, seg_start, &size); + if (start == NULL) + return NULL; - /* - * Look for keyword in header - */ + /* + * Look for keyword in header + */ - bufcount=0 ; - where = start ; - len = (int)strlen(exp_key); - while (1) { - different=0 ; - for (i=0 ; iseg_size) { - /* File is damaged or not FITS: bailout */ - fdealloc(start, seg_start, size) ; - return NULL ; - } + bufcount = 0; + where = start; + len = (int) strlen (exp_key); + while (1) + { + different = 0; + for (i = 0; i < len; i++) + { + if (where[i] != exp_key[i]) + { + different++; + break; + } + } + if (!different) + { + /* Get 2 chars after keyword */ + test1 = where[len]; + test2 = where[len + 1]; + /* If first subsequent character is the equal sign, bingo. */ + if (test1 == '=') + break; + /* If subsequent char is equal sign, bingo */ + if (test1 == ' ' && (test2 == '=' || test2 == ' ')) + break; + } + /* Watch out for header end */ + if ((where[0] == 'E') && + (where[1] == 'N') && (where[2] == 'D') && (where[3] == ' ')) + { + /* Detected header end */ + fdealloc (start, seg_start, size); + return NULL; + } + /* Forward one line */ + where += 80; + bufcount += 80; + if (bufcount > seg_size) + { + /* File is damaged or not FITS: bailout */ + fdealloc (start, seg_start, size); + return NULL; + } } - /* Found the keyword, now get its value */ - value = qfits_getvalue(where); - fdealloc(start, seg_start, size) ; - return value; + /* Found the keyword, now get its value */ + value = qfits_getvalue (where); + fdealloc (start, seg_start, size); + return value; } /*----------------------------------------------------------------------------*/ @@ -203,9 +213,10 @@ char * qfits_query_ext(char * filename, const char * keyword, int xtnum) extension is found, and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_n_ext(char * filename) +int +qfits_query_n_ext (char *filename) { - return qfits_query(filename, QFITS_QUERY_N_EXT); + return qfits_query (filename, QFITS_QUERY_N_EXT); } /*----------------------------------------------------------------------------*/ @@ -218,45 +229,54 @@ int qfits_query_n_ext(char * filename) and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_nplanes(char * filename, int extnum) +int +qfits_query_nplanes (char *filename, int extnum) { - char * sval ; - int next ; - int naxes ; - int nplanes ; + char *sval; + int next; + int naxes; + int nplanes; - /* Check file existence */ - if (filename == NULL) return -1 ; - /* Check validity of extnum */ - next = qfits_query_n_ext(filename) ; - if (extnum>next) { - qfits_error("invalid extension specified") ; - return -1 ; + /* Check file existence */ + if (filename == NULL) + return -1; + /* Check validity of extnum */ + next = qfits_query_n_ext (filename); + if (extnum > next) + { + qfits_error ("invalid extension specified"); + return -1; + } + + /* Find the number of axes */ + naxes = 0; + if ((sval = qfits_query_ext (filename, "NAXIS", extnum)) == NULL) + { + qfits_error ("missing key in header: NAXIS"); + return -1; + } + naxes = atoi (sval); + + /* Check validity of naxes */ + if ((naxes < 2) || (naxes > 3)) + return -1; + + /* Two dimensions cube */ + if (naxes == 2) + nplanes = 1; + else + { + /* For 3D cubes, get the third dimension size */ + if ((sval = qfits_query_ext (filename, "NAXIS3", extnum)) == NULL) + { + qfits_error ("missing key in header: NAXIS3"); + return -1; } - - /* Find the number of axes */ - naxes = 0 ; - if ((sval = qfits_query_ext(filename, "NAXIS", extnum)) == NULL) { - qfits_error("missing key in header: NAXIS"); - return -1 ; - } - naxes = atoi(sval) ; - - /* Check validity of naxes */ - if ((naxes < 2) || (naxes > 3)) return -1 ; - - /* Two dimensions cube */ - if (naxes == 2) nplanes = 1 ; - else { - /* For 3D cubes, get the third dimension size */ - if ((sval = qfits_query_ext(filename, "NAXIS3", extnum))==NULL) { - qfits_error("missing key in header: NAXIS3"); - return -1 ; - } - nplanes = atoi(sval); - if (nplanes < 1) nplanes = 0 ; - } - return nplanes ; + nplanes = atoi (sval); + if (nplanes < 1) + nplanes = 0; + } + return nplanes; } /*----------------------------------------------------------------------------*/ @@ -278,51 +298,61 @@ int qfits_query_nplanes(char * filename, int extnum) */ /*----------------------------------------------------------------------------*/ #define PRETTY_STRING_STATICBUFS 8 -char * qfits_pretty_string(char * s) +char * +qfits_pretty_string (char *s) { - static char pretty_buf[PRETTY_STRING_STATICBUFS][81] ; - static int flip=0 ; - char * pretty ; - int i,j ; + static char pretty_buf[PRETTY_STRING_STATICBUFS][81]; + static int flip = 0; + char *pretty; + int i, j; - /* bulletproof */ - if (s==NULL) return NULL ; + /* bulletproof */ + if (s == NULL) + return NULL; - /* Switch between static buffers */ - pretty = pretty_buf[flip]; - flip++ ; - if (flip==PRETTY_STRING_STATICBUFS) - flip=0 ; - - pretty[0] = (char)0 ; - if (s[0]!='\'') return s ; + /* Switch between static buffers */ + pretty = pretty_buf[flip]; + flip++; + if (flip == PRETTY_STRING_STATICBUFS) + flip = 0; - /* skip first quote */ - i=1 ; - j=0 ; - /* trim left-side blanks */ - while (s[i]==' ') { - if (i==(int)strlen(s)) break ; - i++ ; + pretty[0] = (char) 0; + if (s[0] != '\'') + return s; + + /* skip first quote */ + i = 1; + j = 0; + /* trim left-side blanks */ + while (s[i] == ' ') + { + if (i == (int) strlen (s)) + break; + i++; } - if (i>=(int)(strlen(s)-1)) return pretty ; - /* copy string, changing double quotes to single ones */ - while (i<(int)strlen(s)) { - if (s[i]=='\'') { - i++ ; - } - pretty[j]=s[i]; - i++ ; - j++ ; - } - /* NULL-terminate the pretty string */ - pretty[j+1]=(char)0; - /* trim right-side blanks */ - j = (int)strlen(pretty)-1; - while (pretty[j]==' ') j-- ; - pretty[j+1]=(char)0; + if (i >= (int) (strlen (s) - 1)) return pretty; + /* copy string, changing double quotes to single ones */ + while (i < (int) strlen (s)) + { + if (s[i] == '\'') + { + i++; + } + pretty[j] = s[i]; + i++; + j++; + } + /* NULL-terminate the pretty string */ + pretty[j + 1] = (char) 0; + /* trim right-side blanks */ + j = (int) strlen (pretty) - 1; + while (pretty[j] == ' ') + j--; + pretty[j + 1] = (char) 0; + return pretty; } + #undef PRETTY_STRING_STATICBUFS /*----------------------------------------------------------------------------*/ @@ -334,13 +364,18 @@ char * qfits_pretty_string(char * s) Identifies if a FITS value is boolean. */ /*----------------------------------------------------------------------------*/ -int qfits_is_boolean(char * s) +int +qfits_is_boolean (char *s) { - if (s==NULL) return 0 ; - if (s[0]==0) return 0 ; - if ((int)strlen(s)>1) return 0 ; - if (s[0]=='T' || s[0]=='F') return 1 ; - return 0 ; + if (s == NULL) + return 0; + if (s[0] == 0) + return 0; + if ((int) strlen (s) > 1) + return 0; + if (s[0] == 'T' || s[0] == 'F') + return 1; + return 0; } /*----------------------------------------------------------------------------*/ @@ -352,20 +387,24 @@ int qfits_is_boolean(char * s) Identifies if a FITS value is an integer. */ /*----------------------------------------------------------------------------*/ -int qfits_is_int(char * s) +int +qfits_is_int (char *s) { - regex_t re_int ; - int status ; + regex_t re_int; + int status; - if (s==NULL) return 0 ; - if (s[0]==0) return 0 ; - if (regcomp(&re_int, ®ex_int[0], REG_EXTENDED|REG_NOSUB)!=0) { - qfits_error("internal error: compiling int rule"); - exit(-1); + if (s == NULL) + return 0; + if (s[0] == 0) + return 0; + if (regcomp (&re_int, ®ex_int[0], REG_EXTENDED | REG_NOSUB) != 0) + { + qfits_error ("internal error: compiling int rule"); + exit (-1); } - status = regexec(&re_int, s, 0, NULL, 0) ; - regfree(&re_int) ; - return (status) ? 0 : 1 ; + status = regexec (&re_int, s, 0, NULL, 0); + regfree (&re_int); + return (status) ? 0 : 1; } /*----------------------------------------------------------------------------*/ @@ -377,20 +416,24 @@ int qfits_is_int(char * s) Identifies if a FITS value is float. */ /*----------------------------------------------------------------------------*/ -int qfits_is_float(char * s) +int +qfits_is_float (char *s) { - regex_t re_float; - int status ; + regex_t re_float; + int status; - if (s==NULL) return 0 ; - if (s[0]==0) return 0 ; - if (regcomp(&re_float, ®ex_float[0], REG_EXTENDED|REG_NOSUB)!=0) { - qfits_error("internal error: compiling float rule"); - exit(-1); + if (s == NULL) + return 0; + if (s[0] == 0) + return 0; + if (regcomp (&re_float, ®ex_float[0], REG_EXTENDED | REG_NOSUB) != 0) + { + qfits_error ("internal error: compiling float rule"); + exit (-1); } - status = regexec(&re_float, s, 0, NULL, 0) ; - regfree(&re_float) ; - return (status) ? 0 : 1 ; + status = regexec (&re_float, s, 0, NULL, 0); + regfree (&re_float); + return (status) ? 0 : 1; } /*----------------------------------------------------------------------------*/ @@ -402,20 +445,24 @@ int qfits_is_float(char * s) Identifies if a FITS value is complex. */ /*----------------------------------------------------------------------------*/ -int qfits_is_complex(char * s) +int +qfits_is_complex (char *s) { - regex_t re_cmp ; - int status ; + regex_t re_cmp; + int status; - if (s==NULL) return 0 ; - if (s[0]==0) return 0 ; - if (regcomp(&re_cmp, ®ex_cmp[0], REG_EXTENDED|REG_NOSUB)!=0) { - qfits_error("internal error: compiling complex rule"); - exit(-1); + if (s == NULL) + return 0; + if (s[0] == 0) + return 0; + if (regcomp (&re_cmp, ®ex_cmp[0], REG_EXTENDED | REG_NOSUB) != 0) + { + qfits_error ("internal error: compiling complex rule"); + exit (-1); } - status = regexec(&re_cmp, s, 0, NULL, 0) ; - regfree(&re_cmp) ; - return (status) ? 0 : 1 ; + status = regexec (&re_cmp, s, 0, NULL, 0); + regfree (&re_cmp); + return (status) ? 0 : 1; } /*----------------------------------------------------------------------------*/ @@ -427,11 +474,14 @@ int qfits_is_complex(char * s) Identifies if a FITS value is a string. */ /*----------------------------------------------------------------------------*/ -int qfits_is_string(char * s) +int +qfits_is_string (char *s) { - if (s==NULL) return 0 ; - if (s[0]=='\'') return 1 ; - return 0 ; + if (s == NULL) + return 0; + if (s[0] == '\'') + return 1; + return 0; } /*----------------------------------------------------------------------------*/ @@ -450,14 +500,20 @@ int qfits_is_string(char * s) - QFITS_STRING (5) for a FITS string. */ /*----------------------------------------------------------------------------*/ -int qfits_get_type(char * s) +int +qfits_get_type (char *s) { - if (s==NULL) return QFITS_UNKNOWN ; - if (qfits_is_boolean(s)) return QFITS_BOOLEAN ; - if (qfits_is_int(s)) return QFITS_INT ; - if (qfits_is_float(s)) return QFITS_FLOAT ; - if (qfits_is_complex(s)) return QFITS_COMPLEX ; - return QFITS_STRING ; + if (s == NULL) + return QFITS_UNKNOWN; + if (qfits_is_boolean (s)) + return QFITS_BOOLEAN; + if (qfits_is_int (s)) + return QFITS_INT; + if (qfits_is_float (s)) + return QFITS_FLOAT; + if (qfits_is_complex (s)) + return QFITS_COMPLEX; + return QFITS_STRING; } /*----------------------------------------------------------------------------*/ @@ -479,26 +535,27 @@ int qfits_get_type(char * s) main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_hdrinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) +int +qfits_get_hdrinfo (char *filename, int xtnum, int *seg_start, int *seg_size) { - if (filename==NULL || xtnum<0 || (seg_start==NULL && seg_size==NULL)) { - return -1 ; + if (filename == NULL || xtnum < 0 + || (seg_start == NULL && seg_size == NULL)) + { + return -1; } - if (seg_start!=NULL) { - *seg_start = qfits_query(filename, QFITS_QUERY_HDR_START | xtnum); - if (*seg_start<0) - return -1 ; + if (seg_start != NULL) + { + *seg_start = qfits_query (filename, QFITS_QUERY_HDR_START | xtnum); + if (*seg_start < 0) + return -1; } - if (seg_size!=NULL) { - *seg_size = qfits_query(filename, QFITS_QUERY_HDR_SIZE | xtnum); - if (*seg_size<0) - return -1 ; + if (seg_size != NULL) + { + *seg_size = qfits_query (filename, QFITS_QUERY_HDR_SIZE | xtnum); + if (*seg_size < 0) + return -1; } - return 0 ; + return 0; } /*----------------------------------------------------------------------------*/ @@ -520,26 +577,27 @@ int qfits_get_hdrinfo( main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_datinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) +int +qfits_get_datinfo (char *filename, int xtnum, int *seg_start, int *seg_size) { - if (filename==NULL || xtnum<0 || (seg_start==NULL && seg_size==NULL)) { - return -1 ; + if (filename == NULL || xtnum < 0 + || (seg_start == NULL && seg_size == NULL)) + { + return -1; } - if (seg_start!=NULL) { - *seg_start = qfits_query(filename, QFITS_QUERY_DAT_START | xtnum); - if (*seg_start<0) - return -1 ; + if (seg_start != NULL) + { + *seg_start = qfits_query (filename, QFITS_QUERY_DAT_START | xtnum); + if (*seg_start < 0) + return -1; } - if (seg_size!=NULL) { - *seg_size = qfits_query(filename, QFITS_QUERY_DAT_SIZE | xtnum); - if (*seg_size<0) - return -1 ; + if (seg_size != NULL) + { + *seg_size = qfits_query (filename, QFITS_QUERY_DAT_SIZE | xtnum); + if (*seg_size < 0) + return -1; } - return 0 ; + return 0; } /*----------------------------------------------------------------------------*/ @@ -550,72 +608,74 @@ int qfits_get_datinfo( @return Allocated string containing the card or NULL */ /*----------------------------------------------------------------------------*/ -char * qfits_query_card( - char * filename, - char * keyword) +char * +qfits_query_card (char *filename, char *keyword) { - char * exp_key ; - int fd ; - char * buf ; - char * buf2 ; - char * where ; - int hs ; - char * card ; + char *exp_key; + int fd; + char *buf; + char *buf2; + char *where; + int hs; + char *card; - /* Bulletproof entries */ - if (filename==NULL || keyword==NULL) return NULL ; + /* Bulletproof entries */ + if (filename == NULL || keyword == NULL) + return NULL; - /* Expand keyword */ - exp_key = qfits_expand_keyword(keyword) ; + /* 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 ; + /* 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 ; + 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 = 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); - free(buf2) ; - return NULL ; - } - if ((where-buf2)%80) where++ ; - } while ((where-buf2)%80) ; - - where = buf + (int)(where - buf2) ; - - /* Create the card */ - card = malloc(81*sizeof(char)) ; - strncpy(card, where, 80) ; - card[80] = (char)0 ; + /* Apply search for the input keyword */ + buf2 = 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); + free (buf2); + return NULL; + } + if ((where - buf2) % 80) + where++; + } + while ((where - buf2) % 80); - /* Free and return */ - close(fd) ; - munmap(buf, hs) ; - free(buf2) ; - return card ; + where = buf + (int) (where - buf2); + + /* Create the card */ + card = malloc (81 * sizeof (char)); + strncpy (card, where, 80); + card[80] = (char) 0; + + /* Free and return */ + close (fd); + munmap (buf, hs); + free (buf2); + return card; } /*----------------------------------------------------------------------------*/ @@ -637,76 +697,77 @@ char * qfits_query_card( Returns 0 if everything worked Ok, -1 otherwise. */ /*----------------------------------------------------------------------------*/ -int qfits_replace_card( - char * filename, - const char * keyword, - char * substitute) +int +qfits_replace_card (char *filename, const char *keyword, char *substitute) { - char * exp_key ; - int fd ; - char * buf ; - char * buf2 ; - char * where ; - int hs ; + char *exp_key; + int fd; + char *buf; + char *buf2; + char *where; + int hs; - /* Bulletproof entries */ - if (filename==NULL || keyword==NULL || substitute==NULL) return -1 ; + /* Bulletproof entries */ + if (filename == NULL || keyword == NULL || substitute == NULL) + return -1; - /* Expand keyword */ - exp_key = qfits_expand_keyword(keyword); - /* - * Memory-map the FITS header of the input file - */ + /* 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 -1 ; + qfits_get_hdrinfo (filename, 0, NULL, &hs); + if (hs < 1) + { + qfits_error ("error getting FITS header size for %s", filename); + return -1; } - fd = open(filename, O_RDWR) ; - if (fd == -1) { - return -1 ; + fd = open (filename, O_RDWR); + if (fd == -1) + { + return -1; } - buf = (char*)mmap(0, - hs, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, - 0) ; - if (buf == (char*)-1) { - perror("mmap") ; - close(fd) ; - return -1 ; + buf = (char *) mmap (0, hs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buf == (char *) -1) + { + perror ("mmap"); + close (fd); + return -1; } - /* Apply search and replace for the input keyword lists */ - buf2 = 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); - free(buf2) ; - return -1 ; - } - if ((where-buf2)%80) where++ ; - } while ((where-buf2)%80) ; - - where = buf + (int)(where - buf2) ; - - /* Replace current placeholder by blanks */ - memset(where, ' ', 80); - /* Copy substitute into placeholder */ - memcpy(where, substitute, strlen(substitute)); + /* Apply search and replace for the input keyword lists */ + buf2 = 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); + free (buf2); + return -1; + } + if ((where - buf2) % 80) + where++; + } + while ((where - buf2) % 80); - close(fd) ; - munmap(buf, hs) ; - free(buf2) ; - return 0 ; + where = buf + (int) (where - buf2); + + /* Replace current placeholder by blanks */ + memset (where, ' ', 80); + /* Copy substitute into placeholder */ + memcpy (where, substitute, strlen (substitute)); + + close (fd); + munmap (buf, hs); + free (buf2); + return 0; } /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/simple.h b/qfits/src/simple.h index d42d345..8b716a8 100644 --- a/qfits/src/simple.h +++ b/qfits/src/simple.h @@ -22,7 +22,8 @@ #define SIMPLE_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*----------------------------------------------------------------------------- @@ -82,7 +83,7 @@ extern "C" { Returns NULL in case the requested keyword cannot be found. */ /*----------------------------------------------------------------------------*/ -char * qfits_query_hdr(char * filename, const char * keyword) ; + char *qfits_query_hdr (char *filename, const char *keyword); /*----------------------------------------------------------------------------*/ /** @@ -97,7 +98,7 @@ char * qfits_query_hdr(char * filename, const char * keyword) ; strictly identical to qfits_query_hdr(). */ /*----------------------------------------------------------------------------*/ -char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ; + char *qfits_query_ext (char *filename, const char *keyword, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -109,7 +110,7 @@ char * qfits_query_ext(char * filename, const char * keyword, int xtnum) ; extension is found, and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_n_ext(char * filename) ; + int qfits_query_n_ext (char *filename); /*----------------------------------------------------------------------------*/ /** @@ -121,7 +122,7 @@ int qfits_query_n_ext(char * filename) ; and -1 if an error occurred. */ /*----------------------------------------------------------------------------*/ -int qfits_query_nplanes(char * filename, int extnum) ; + int qfits_query_nplanes (char *filename, int extnum); /*----------------------------------------------------------------------------*/ /** @@ -141,7 +142,7 @@ int qfits_query_nplanes(char * filename, int extnum) ; */ /*----------------------------------------------------------------------------*/ -char * qfits_pretty_string(char * s) ; + char *qfits_pretty_string (char *s); /*----------------------------------------------------------------------------*/ /** @@ -152,7 +153,7 @@ char * qfits_pretty_string(char * s) ; Identifies if a FITS value is boolean. */ /*----------------------------------------------------------------------------*/ -int qfits_is_boolean(char * s) ; + int qfits_is_boolean (char *s); /*----------------------------------------------------------------------------*/ /** @@ -163,7 +164,7 @@ int qfits_is_boolean(char * s) ; Identifies if a FITS value is an integer. */ /*----------------------------------------------------------------------------*/ -int qfits_is_int(char * s) ; + int qfits_is_int (char *s); /*----------------------------------------------------------------------------*/ /** @@ -174,7 +175,7 @@ int qfits_is_int(char * s) ; Identifies if a FITS value is float. */ /*----------------------------------------------------------------------------*/ -int qfits_is_float(char * s) ; + int qfits_is_float (char *s); /*----------------------------------------------------------------------------*/ /** @@ -185,7 +186,7 @@ int qfits_is_float(char * s) ; Identifies if a FITS value is complex. */ /*----------------------------------------------------------------------------*/ -int qfits_is_complex(char * s) ; + int qfits_is_complex (char *s); /*----------------------------------------------------------------------------*/ /** @@ -196,7 +197,7 @@ int qfits_is_complex(char * s) ; Identifies if a FITS value is a string. */ /*----------------------------------------------------------------------------*/ -int qfits_is_string(char * s) ; + int qfits_is_string (char *s); /*----------------------------------------------------------------------------*/ /** @@ -214,7 +215,7 @@ int qfits_is_string(char * s) ; - QFITS_STRING (5) for a FITS string. */ /*----------------------------------------------------------------------------*/ -int qfits_get_type(char * s) ; + int qfits_get_type (char *s); /*----------------------------------------------------------------------------*/ /** @@ -235,11 +236,8 @@ int qfits_get_type(char * s) ; main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_hdrinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) ; + int qfits_get_hdrinfo (char *filename, + int xtnum, int *seg_start, int *seg_size); /*----------------------------------------------------------------------------*/ /** @@ -260,11 +258,8 @@ int qfits_get_hdrinfo( main header in the file. */ /*----------------------------------------------------------------------------*/ -int qfits_get_datinfo( - char * filename, - int xtnum, - int * seg_start, - int * seg_size) ; + int qfits_get_datinfo (char *filename, + int xtnum, int *seg_start, int *seg_size); /*----------------------------------------------------------------------------*/ /** @@ -274,9 +269,7 @@ int qfits_get_datinfo( @return Allocated string containing the card or NULL */ /*----------------------------------------------------------------------------*/ -char * qfits_query_card( - char * filename, - char * keyword) ; + char *qfits_query_card (char *filename, char *keyword); /*----------------------------------------------------------------------------*/ /** @@ -297,10 +290,8 @@ char * qfits_query_card( Returns 0 if everything worked Ok, -1 otherwise. */ /*----------------------------------------------------------------------------*/ -int qfits_replace_card( - char * filename, - const char * keyword, - char * substitute) ; + int qfits_replace_card (char *filename, + const char *keyword, char *substitute); /* */ #ifdef __cplusplus diff --git a/qfits/src/static_sz.h b/qfits/src/static_sz.h index 126061b..d836b30 100644 --- a/qfits/src/static_sz.h +++ b/qfits/src/static_sz.h @@ -19,7 +19,8 @@ #define STATIC_SZ_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* */ diff --git a/qfits/src/t_iso8601.c b/qfits/src/t_iso8601.c index 38017ea..5cbf4aa 100644 --- a/qfits/src/t_iso8601.c +++ b/qfits/src/t_iso8601.c @@ -75,8 +75,8 @@ Private to this module -----------------------------------------------------------------------------*/ -static long timer_to_date(time_t time_secs) ; -static long timer_to_time(time_t time_secs) ; +static long timer_to_date (time_t time_secs); +static long timer_to_time (time_t time_secs); /*----------------------------------------------------------------------------- Static Function code @@ -95,9 +95,10 @@ static long timer_to_time(time_t time_secs) ; Example: 19 Oct 2000 is returned as 20001019 */ /*----------------------------------------------------------------------------*/ -long qfits_date_now (void) +long +qfits_date_now (void) { - return (timer_to_date (time (NULL))); + return (timer_to_date (time (NULL))); } /*----------------------------------------------------------------------------*/ @@ -111,13 +112,13 @@ long qfits_date_now (void) Example: 15:36:12.84 is returned as 15361284 */ /*----------------------------------------------------------------------------*/ -long qfits_time_now(void) +long +qfits_time_now (void) { - struct timeval time_struct; + struct timeval time_struct; - gettimeofday (&time_struct, 0); - return (timer_to_time (time_struct.tv_sec) - + time_struct.tv_usec / 10000); + gettimeofday (&time_struct, 0); + return (timer_to_time (time_struct.tv_sec) + time_struct.tv_usec / 10000); } /*----------------------------------------------------------------------------*/ @@ -133,25 +134,31 @@ long qfits_time_now(void) (GMT). */ /*----------------------------------------------------------------------------*/ -static long timer_to_date(time_t time_secs) +static long +timer_to_date (time_t time_secs) { - struct tm *time_struct; + struct tm *time_struct; - if (time_secs == 0) { - return 0; - } else { - /* Convert into a long value CCYYMMDD */ - time_struct = localtime (&time_secs); - if (time_struct) { - time_struct-> tm_year += 1900; - return (MAKE_DATE ( time_struct-> tm_year / 100, - time_struct-> tm_year % 100, - time_struct-> tm_mon + 1, - time_struct-> tm_mday)); - } else { - return (19700101); - } + if (time_secs == 0) + { + return 0; + } + else + { + /* Convert into a long value CCYYMMDD */ + time_struct = localtime (&time_secs); + if (time_struct) + { + time_struct->tm_year += 1900; + return (MAKE_DATE (time_struct->tm_year / 100, + time_struct->tm_year % 100, + time_struct->tm_mon + 1, time_struct->tm_mday)); } + else + { + return (19700101); + } + } } /*----------------------------------------------------------------------------*/ @@ -167,24 +174,29 @@ static long timer_to_date(time_t time_secs) (GMT). */ /*----------------------------------------------------------------------------*/ -static long timer_to_time(time_t time_secs) +static long +timer_to_time (time_t time_secs) { - struct tm *time_struct; + struct tm *time_struct; - if (time_secs == 0) { - return 0; - } else { - /* Convert into a long value HHMMSS00 */ - time_struct = localtime (&time_secs); - if (time_struct) { - return (MAKE_TIME (time_struct-> tm_hour, - time_struct-> tm_min, - time_struct-> tm_sec, - 0)); - } else { - return 0; - } + if (time_secs == 0) + { + return 0; + } + else + { + /* Convert into a long value HHMMSS00 */ + time_struct = localtime (&time_secs); + if (time_struct) + { + return (MAKE_TIME (time_struct->tm_hour, + time_struct->tm_min, time_struct->tm_sec, 0)); } + else + { + return 0; + } + } } /*----------------------------------------------------------------------------- @@ -201,17 +213,16 @@ static long timer_to_time(time_t time_secs) in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_date_iso8601(void) +char * +qfits_get_date_iso8601 (void) { - static char date_iso8601[20] ; - long curdate ; + static char date_iso8601[20]; + long curdate; - curdate = qfits_date_now() ; - sprintf(date_iso8601, "%04d-%02d-%02d", - GET_CCYEAR(curdate), - GET_MONTH(curdate), - GET_DAY(curdate)); - return date_iso8601 ; + curdate = qfits_date_now (); + sprintf (date_iso8601, "%04d-%02d-%02d", + GET_CCYEAR (curdate), GET_MONTH (curdate), GET_DAY (curdate)); + return date_iso8601; } /*----------------------------------------------------------------------------*/ @@ -224,38 +235,37 @@ char * qfits_get_date_iso8601(void) statically allocated string in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_datetime_iso8601(void) +char * +qfits_get_datetime_iso8601 (void) { - static char date_iso8601[20] ; - long curdate ; - long curtime ; + static char date_iso8601[20]; + long curdate; + long curtime; - curdate = qfits_date_now() ; - curtime = qfits_time_now() ; + curdate = qfits_date_now (); + curtime = qfits_time_now (); - sprintf(date_iso8601, "%04d-%02d-%02dT%02d:%02d:%02d", - GET_CCYEAR(curdate), - GET_MONTH(curdate), - GET_DAY(curdate), - GET_HOUR(curtime), - GET_MINUTE(curtime), - GET_SECOND(curtime)); - return date_iso8601 ; + sprintf (date_iso8601, "%04d-%02d-%02dT%02d:%02d:%02d", + GET_CCYEAR (curdate), + GET_MONTH (curdate), + GET_DAY (curdate), + GET_HOUR (curtime), GET_MINUTE (curtime), GET_SECOND (curtime)); + return date_iso8601; } #ifdef TEST -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - printf( "date now %ld\n" - "time now %ld\n" - "date iso8601 %s\n" - "date/time iso 8601 %s\n", - qfits_date_now(), - qfits_time_now(), - qfits_get_date_iso8601(), - qfits_get_datetime_iso8601()); - - return 0 ; + printf ("date now %ld\n" + "time now %ld\n" + "date iso8601 %s\n" + "date/time iso 8601 %s\n", + qfits_date_now (), + qfits_time_now (), + qfits_get_date_iso8601 (), qfits_get_datetime_iso8601 ()); + + return 0; } #endif /* vim: set ts=4 et sw=4 tw=75 */ diff --git a/qfits/src/t_iso8601.h b/qfits/src/t_iso8601.h index 535ddb8..d6311db 100644 --- a/qfits/src/t_iso8601.h +++ b/qfits/src/t_iso8601.h @@ -23,7 +23,8 @@ #define T_ISO8601_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif @@ -46,7 +47,7 @@ extern "C" { Example: 19 Oct 2000 is returned as 20001019 */ /*----------------------------------------------------------------------------*/ -long qfits_date_now (void); + long qfits_date_now (void); /*----------------------------------------------------------------------------*/ /** @@ -58,7 +59,7 @@ long qfits_date_now (void); Example: 15:36:12.84 is returned as 15361284 */ /*----------------------------------------------------------------------------*/ -long qfits_time_now(void); + long qfits_time_now (void); /*----------------------------------------------------------------------------*/ /** @@ -70,7 +71,7 @@ long qfits_time_now(void); in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_date_iso8601(void); + char *qfits_get_date_iso8601 (void); /*----------------------------------------------------------------------------*/ /** @@ -82,7 +83,7 @@ char * qfits_get_date_iso8601(void); statically allocated string in the function, so no need to free it. */ /*----------------------------------------------------------------------------*/ -char * qfits_get_datetime_iso8601(void); + char *qfits_get_datetime_iso8601 (void); /* */ #ifdef __cplusplus diff --git a/qfits/src/tfits.c b/qfits/src/tfits.c index 0a1363d..679cb9d 100644 --- a/qfits/src/tfits.c +++ b/qfits/src/tfits.c @@ -49,16 +49,17 @@ Function prototypes -----------------------------------------------------------------------------*/ -static char * qfits_bintable_field_to_string(qfits_table *, int, int, int) ; -static char * qfits_asciitable_field_to_string(qfits_table *, int, int, int) ; -static char * qfits_build_format(qfits_col *) ; -static int qfits_table_append_bin_xtension(FILE *, qfits_table *, void **) ; -static int qfits_table_append_ascii_xtension(FILE *, qfits_table *, void **) ; -static int qfits_table_append_data(FILE *, qfits_table *, void **) ; -static int qfits_table_get_field_size(int, qfits_col *) ; -static int qfits_table_interpret_type(char *, int *, int*, tfits_type *, int) ; -static char * qfits_strstrip(char *); -static double qfits_str2dec(char *, int) ; +static char *qfits_bintable_field_to_string (qfits_table *, int, int, int); +static char *qfits_asciitable_field_to_string (qfits_table *, int, int, int); +static char *qfits_build_format (qfits_col *); +static int qfits_table_append_bin_xtension (FILE *, qfits_table *, void **); +static int qfits_table_append_ascii_xtension (FILE *, qfits_table *, void **); +static int qfits_table_append_data (FILE *, qfits_table *, void **); +static int qfits_table_get_field_size (int, qfits_col *); +static int qfits_table_interpret_type (char *, int *, int *, tfits_type *, + int); +static char *qfits_strstrip (char *); +static double qfits_str2dec (char *, int); /*----------------------------------------------------------------------------- Function codes @@ -73,23 +74,28 @@ static double qfits_str2dec(char *, int) ; Examines the requested extension and identifies the presence of a FITS table. */ /*----------------------------------------------------------------------------*/ -int qfits_is_table(char * filename, int xtnum) +int +qfits_is_table (char *filename, int xtnum) { - char * value ; - int ttype ; - - ttype = QFITS_INVALIDTABLE ; - value = qfits_query_ext(filename, "XTENSION", xtnum); - if (value==NULL) return ttype ; - - value = qfits_pretty_string(value); - if (!strcmp(value, "TABLE")) { - ttype = QFITS_ASCIITABLE; - } else if (!strcmp(value, "BINTABLE")) { - ttype = QFITS_BINTABLE; + char *value; + int ttype; + + ttype = QFITS_INVALIDTABLE; + value = qfits_query_ext (filename, "XTENSION", xtnum); + if (value == NULL) + return ttype; + + value = qfits_pretty_string (value); + if (!strcmp (value, "TABLE")) + { + ttype = QFITS_ASCIITABLE; } - return ttype; -} + else if (!strcmp (value, "BINTABLE")) + { + ttype = QFITS_BINTABLE; + } + return ttype; +} /*----------------------------------------------------------------------------*/ /** @@ -97,20 +103,21 @@ int qfits_is_table(char * filename, int xtnum) @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_prim_header_default(void) +qfits_header * +qfits_table_prim_header_default (void) { - qfits_header * fh ; + qfits_header *fh; - fh = qfits_header_new() ; + fh = qfits_header_new (); - qfits_header_append(fh, "SIMPLE", "T", "Standard FITS file", NULL) ; - qfits_header_append(fh, "BITPIX", "8", "ASCII or bytes array", NULL) ; - qfits_header_append(fh, "NAXIS", "0", "Minimal header", NULL) ; - qfits_header_append(fh, "EXTEND", "T", "There may be FITS ext", NULL); - qfits_header_append(fh, "BLOCKED", "T", "The file may be blocked", NULL) ; - qfits_header_append(fh, "END", NULL, NULL, NULL) ; + qfits_header_append (fh, "SIMPLE", "T", "Standard FITS file", NULL); + qfits_header_append (fh, "BITPIX", "8", "ASCII or bytes array", NULL); + qfits_header_append (fh, "NAXIS", "0", "Minimal header", NULL); + qfits_header_append (fh, "EXTEND", "T", "There may be FITS ext", NULL); + qfits_header_append (fh, "BLOCKED", "T", "The file may be blocked", NULL); + qfits_header_append (fh, "END", NULL, NULL, NULL); - return fh ; + return fh; } /*----------------------------------------------------------------------------*/ @@ -119,147 +126,176 @@ qfits_header * qfits_table_prim_header_default(void) @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_ext_header_default(qfits_table * t) +qfits_header * +qfits_table_ext_header_default (qfits_table * t) { - qfits_header * fh ; - qfits_col * curr_col ; - char str_val[FITS_LINESZ] ; - char str_val2[FITS_LINESZ] ; - char * date ; - int tab_width ; - int col_pos ; - int i ; + qfits_header *fh; + qfits_col *curr_col; + char str_val[FITS_LINESZ]; + char str_val2[FITS_LINESZ]; + char *date; + int tab_width; + int col_pos; + int i; - /* Compute the table width */ - if ((tab_width = qfits_compute_table_width(t)) == -1) { - qfits_error("cannot get the table width") ; - return NULL ; + /* Compute the table width */ + if ((tab_width = qfits_compute_table_width (t)) == -1) + { + qfits_error ("cannot get the table width"); + return NULL; } - /* Create fits header */ - if ((fh=qfits_header_new()) == NULL) { - qfits_error("cannot create new fits header") ; - return NULL ; + /* Create fits header */ + if ((fh = qfits_header_new ()) == NULL) + { + qfits_error ("cannot create new fits header"); + return NULL; + } + + /* Check the kind of table */ + if (t->tab_t == QFITS_BINTABLE) + { + + /* Write extension header */ + qfits_header_append (fh, "XTENSION", "BINTABLE", + "FITS Binary Table Extension", NULL); + qfits_header_append (fh, "BITPIX", "8", "8-bits character format", + NULL); + qfits_header_append (fh, "NAXIS", "2", "Tables are 2-D char. array", + NULL); + sprintf (str_val, "%d", tab_width); + qfits_header_append (fh, "NAXIS1", str_val, "Bytes in row", NULL); + sprintf (str_val, "%d", (int) (t->nr)); + qfits_header_append (fh, "NAXIS2", str_val, "No. of rows in table", + NULL); + qfits_header_append (fh, "PCOUNT", "0", "Parameter count always 0", + NULL); + qfits_header_append (fh, "GCOUNT", "1", "Group count always 1", NULL); + sprintf (str_val, "%d", (int) (t->nc)); + qfits_header_append (fh, "TFIELDS", str_val, "No. of col in table", + NULL); + /* Columns descriptors */ + curr_col = t->col; + for (i = 0; i < t->nc; i++) + { + sprintf (str_val, "TFORM%d", i + 1); + sprintf (str_val2, "'%s'", qfits_build_format (curr_col)); + qfits_header_append (fh, str_val, str_val2, "Format of field", + NULL); + + sprintf (str_val, "TTYPE%d", i + 1); + sprintf (str_val2, "%s", curr_col->tlabel); + qfits_header_append (fh, str_val, str_val2, "Field label", NULL); + + sprintf (str_val, "TUNIT%d", i + 1); + sprintf (str_val2, "%s", curr_col->tunit); + qfits_header_append (fh, str_val, str_val2, + "Physical unit of field", NULL); + if (curr_col->zero_present) + { + sprintf (str_val, "TZERO%d", i + 1); + sprintf (str_val2, "%f", curr_col->zero); + qfits_header_append (fh, str_val, str_val2, + "NULL value is defined", NULL); + } + if (curr_col->scale_present) + { + sprintf (str_val, "TSCAL%d", i + 1); + sprintf (str_val2, "%f", curr_col->scale); + qfits_header_append (fh, str_val, str_val2, "Scaling applied", + NULL); + } + curr_col++; } + qfits_header_append (fh, "ORIGIN", "ESO-QFITS", "Written by QFITS", + NULL); - /* Check the kind of table */ - if (t->tab_t == QFITS_BINTABLE) { - - /* Write extension header */ - qfits_header_append(fh, "XTENSION", "BINTABLE", - "FITS Binary Table Extension", NULL) ; - qfits_header_append(fh, "BITPIX", "8", "8-bits character format", NULL); - qfits_header_append(fh, "NAXIS", "2","Tables are 2-D char. array",NULL); - sprintf(str_val, "%d", tab_width) ; - qfits_header_append(fh, "NAXIS1", str_val, "Bytes in row", NULL) ; - sprintf(str_val, "%d", (int)(t->nr)) ; - qfits_header_append(fh, "NAXIS2", str_val, "No. of rows in table",NULL); - qfits_header_append(fh, "PCOUNT", "0", "Parameter count always 0",NULL); - qfits_header_append(fh, "GCOUNT", "1", "Group count always 1", NULL); - sprintf(str_val, "%d", (int)(t->nc)) ; - qfits_header_append(fh, "TFIELDS", str_val, "No. of col in table",NULL); - /* Columns descriptors */ - curr_col = t->col ; - for (i=0 ; inc ; i++) { - sprintf(str_val, "TFORM%d", i+1) ; - sprintf(str_val2, "'%s'", qfits_build_format(curr_col)) ; - qfits_header_append(fh, str_val, str_val2, "Format of field", NULL); - - sprintf(str_val, "TTYPE%d", i+1) ; - sprintf(str_val2, "%s", curr_col->tlabel) ; - qfits_header_append(fh, str_val, str_val2, "Field label", NULL) ; + date = qfits_get_datetime_iso8601 (); + sprintf (str_val, "'%s'", date); + qfits_header_append (fh, "DATE", str_val, "[UTC] Date of writing", + NULL); + qfits_header_append (fh, "END", NULL, NULL, NULL); - sprintf(str_val, "TUNIT%d", i+1) ; - sprintf(str_val2, "%s", curr_col->tunit) ; - qfits_header_append(fh, str_val, str_val2, "Physical unit of field", - NULL) ; - if (curr_col->zero_present) { - sprintf(str_val, "TZERO%d", i+1) ; - sprintf(str_val2, "%f", curr_col->zero) ; - qfits_header_append(fh, str_val, str_val2, - "NULL value is defined", NULL) ; - } - if (curr_col->scale_present) { - sprintf(str_val, "TSCAL%d", i+1) ; - sprintf(str_val2, "%f", curr_col->scale) ; - qfits_header_append(fh, str_val, str_val2, "Scaling applied", - NULL); - } - curr_col++ ; - } - qfits_header_append(fh,"ORIGIN","ESO-QFITS", "Written by QFITS", NULL); - - date = qfits_get_datetime_iso8601() ; - sprintf(str_val, "'%s'", date) ; - qfits_header_append(fh, "DATE", str_val, "[UTC] Date of writing", NULL); - qfits_header_append(fh, "END", NULL, NULL, NULL); - - } else if (t->tab_t == QFITS_ASCIITABLE) { - - /* Write extension header */ - qfits_header_append(fh, "XTENSION", "TABLE", - "FITS ASCII Table Extension", NULL) ; - qfits_header_append(fh, "BITPIX", "8", "8-bits character format", NULL); - qfits_header_append(fh, "NAXIS", "2", "ASCII table has 2 axes", NULL) ; - - /* Fill the header */ - sprintf(str_val, "%d", tab_width) ; - qfits_header_append(fh, "NAXIS1", str_val, "Characters in a row", NULL); - sprintf(str_val, "%d", (int)(t->nr)) ; - qfits_header_append(fh, "NAXIS2", str_val, "No. of rows in table",NULL); - qfits_header_append(fh, "PCOUNT", "0", "No group parameters", NULL) ; - qfits_header_append(fh, "GCOUNT", "1", "Only one group", NULL); - sprintf(str_val, "%d", (int)(t->nc)) ; - qfits_header_append(fh, "TFIELDS", str_val, "No. of col in table",NULL); - qfits_header_append(fh, "ORIGIN","ESO-QFITS","Written by QFITS",NULL); - date = qfits_get_datetime_iso8601() ; - sprintf(str_val, "'%s'", date) ; - qfits_header_append(fh, "DATE", str_val, "[UTC] Date of writing", NULL); - - /* Columns descriptors */ - curr_col = t->col ; - col_pos = 1 ; - for (i=0 ; inc ; i++) { - sprintf(str_val, "TTYPE%d", i+1) ; - sprintf(str_val2, "%s", curr_col->tlabel) ; - qfits_header_append(fh, str_val, str_val2, "Field label", NULL) ; - - sprintf(str_val, "TFORM%d", i+1) ; - sprintf(str_val2, "'%s'", qfits_build_format(curr_col)) ; - qfits_header_append(fh, str_val, str_val2, "Format of field", NULL); - - sprintf(str_val, "TBCOL%d", i+1) ; - sprintf(str_val2, "%d", col_pos) ; - qfits_header_append(fh, str_val, str_val2,"Start column of field", - NULL); - col_pos += curr_col->atom_nb ; - - sprintf(str_val, "TUNIT%d", i+1) ; - sprintf(str_val2, "%s", curr_col->tunit) ; - qfits_header_append(fh, str_val, str_val2, "Physical unit of field", - NULL) ; - if (curr_col->zero_present) { - sprintf(str_val, "TZERO%d", i+1) ; - sprintf(str_val2, "%f", curr_col->zero) ; - qfits_header_append(fh, str_val, str_val2, - "NULL value is defined", NULL) ; - } - if (curr_col->scale_present) { - sprintf(str_val, "TSCAL%d", i+1) ; - sprintf(str_val2, "%f", curr_col->scale) ; - qfits_header_append(fh, str_val, str_val2, "Scaling applied", - NULL); - } - curr_col++ ; - } - qfits_header_append(fh, "END", NULL, NULL, NULL); - - } else { - qfits_error("Table type not known") ; - qfits_header_destroy(fh) ; - return NULL ; } - return fh ; + else if (t->tab_t == QFITS_ASCIITABLE) + { + + /* Write extension header */ + qfits_header_append (fh, "XTENSION", "TABLE", + "FITS ASCII Table Extension", NULL); + qfits_header_append (fh, "BITPIX", "8", "8-bits character format", + NULL); + qfits_header_append (fh, "NAXIS", "2", "ASCII table has 2 axes", NULL); + + /* Fill the header */ + sprintf (str_val, "%d", tab_width); + qfits_header_append (fh, "NAXIS1", str_val, "Characters in a row", + NULL); + sprintf (str_val, "%d", (int) (t->nr)); + qfits_header_append (fh, "NAXIS2", str_val, "No. of rows in table", + NULL); + qfits_header_append (fh, "PCOUNT", "0", "No group parameters", NULL); + qfits_header_append (fh, "GCOUNT", "1", "Only one group", NULL); + sprintf (str_val, "%d", (int) (t->nc)); + qfits_header_append (fh, "TFIELDS", str_val, "No. of col in table", + NULL); + qfits_header_append (fh, "ORIGIN", "ESO-QFITS", "Written by QFITS", + NULL); + date = qfits_get_datetime_iso8601 (); + sprintf (str_val, "'%s'", date); + qfits_header_append (fh, "DATE", str_val, "[UTC] Date of writing", + NULL); + + /* Columns descriptors */ + curr_col = t->col; + col_pos = 1; + for (i = 0; i < t->nc; i++) + { + sprintf (str_val, "TTYPE%d", i + 1); + sprintf (str_val2, "%s", curr_col->tlabel); + qfits_header_append (fh, str_val, str_val2, "Field label", NULL); + + sprintf (str_val, "TFORM%d", i + 1); + sprintf (str_val2, "'%s'", qfits_build_format (curr_col)); + qfits_header_append (fh, str_val, str_val2, "Format of field", + NULL); + + sprintf (str_val, "TBCOL%d", i + 1); + sprintf (str_val2, "%d", col_pos); + qfits_header_append (fh, str_val, str_val2, "Start column of field", + NULL); + col_pos += curr_col->atom_nb; + + sprintf (str_val, "TUNIT%d", i + 1); + sprintf (str_val2, "%s", curr_col->tunit); + qfits_header_append (fh, str_val, str_val2, + "Physical unit of field", NULL); + if (curr_col->zero_present) + { + sprintf (str_val, "TZERO%d", i + 1); + sprintf (str_val2, "%f", curr_col->zero); + qfits_header_append (fh, str_val, str_val2, + "NULL value is defined", NULL); + } + if (curr_col->scale_present) + { + sprintf (str_val, "TSCAL%d", i + 1); + sprintf (str_val2, "%f", curr_col->scale); + qfits_header_append (fh, str_val, str_val2, "Scaling applied", + NULL); + } + curr_col++; + } + qfits_header_append (fh, "END", NULL, NULL, NULL); + + } + else + { + qfits_error ("Table type not known"); + qfits_header_destroy (fh); + return NULL; + } + return fh; } /*----------------------------------------------------------------------------*/ @@ -275,23 +311,20 @@ qfits_header * qfits_table_ext_header_default(qfits_table * t) qfits_table_close() */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_new( - char * filename, - int table_type, - int table_width, - int nb_cols, - int nb_raws) +qfits_table * +qfits_table_new (char *filename, + int table_type, int table_width, int nb_cols, int nb_raws) { - qfits_table * qt ; - qt = malloc(sizeof(qfits_table)) ; - (void)strcpy(qt->filename, filename) ; - qt->tab_t = table_type ; - qt->nc = nb_cols ; - qt->nr = nb_raws ; - qt->col = calloc(qt->nc, sizeof(qfits_col)) ; - qt->tab_w = table_width ; - - return qt ; + qfits_table *qt; + qt = malloc (sizeof (qfits_table)); + (void) strcpy (qt->filename, filename); + qt->tab_t = table_type; + qt->nc = nb_cols; + qt->nr = nb_raws; + qt->col = calloc (qt->nc, sizeof (qfits_col)); + qt->tab_w = table_width; + + return qt; } /*----------------------------------------------------------------------------*/ @@ -318,59 +351,56 @@ qfits_table * qfits_table_new( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_col_fill( - qfits_col * qc, - int atom_nb, - int atom_dec_nb, - int atom_size, - tfits_type atom_type, - char * label, - char * unit, - char * nullval, - char * disp, - int zero_present, - float zero, - int scale_present, - float scale, - int offset_beg) +int +qfits_col_fill (qfits_col * qc, + int atom_nb, + int atom_dec_nb, + int atom_size, + tfits_type atom_type, + char *label, + char *unit, + char *nullval, + char *disp, + int zero_present, + float zero, int scale_present, float scale, int offset_beg) { - /* Number of atoms per column */ - qc->atom_nb = atom_nb ; - - /* Number of decimals in a field in ASCII table (0 in BINTABLE) */ - qc->atom_dec_nb = atom_dec_nb ; - - /* Size in bytes of an atom */ - qc->atom_size = atom_size ; - - /* Data type in the column */ - qc->atom_type = atom_type ; - - /* Label of the column */ - (void)strcpy(qc->tlabel, label) ; - - /* Unit of the column data */ - (void)strcpy(qc->tunit, unit) ; - - /* Null value*/ - (void)strcpy(qc->nullval, nullval) ; + /* Number of atoms per column */ + qc->atom_nb = atom_nb; - /* How to display the data */ - (void)strcpy(qc->tdisp, disp) ; + /* Number of decimals in a field in ASCII table (0 in BINTABLE) */ + qc->atom_dec_nb = atom_dec_nb; - /* Default values for zero and scales */ - qc->zero_present = zero_present ; - qc->scale_present = scale_present ; - qc->zero = zero ; - qc->scale = scale ; + /* Size in bytes of an atom */ + qc->atom_size = atom_size; - /* Number of bytes between two consecutive fields of the same column */ - qc->off_beg = offset_beg ; - - /* A column is a priori readable */ - qc->readable = 1 ; + /* Data type in the column */ + qc->atom_type = atom_type; - return 0 ; + /* Label of the column */ + (void) strcpy (qc->tlabel, label); + + /* Unit of the column data */ + (void) strcpy (qc->tunit, unit); + + /* Null value */ + (void) strcpy (qc->nullval, nullval); + + /* How to display the data */ + (void) strcpy (qc->tdisp, disp); + + /* Default values for zero and scales */ + qc->zero_present = zero_present; + qc->scale_present = scale_present; + qc->zero = zero; + qc->scale = scale; + + /* Number of bytes between two consecutive fields of the same column */ + qc->off_beg = offset_beg; + + /* A column is a priori readable */ + qc->readable = 1; + + return 0; } /*----------------------------------------------------------------------------*/ @@ -384,242 +414,278 @@ int qfits_col_fill( newly allocated qfits_table structure. */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_open( - char * filename, - int xtnum) +qfits_table * +qfits_table_open (char *filename, int xtnum) { - qfits_table * tload ; - qfits_col * curr_col ; - char * str_val ; - char keyword[FITSVALSZ] ; - /* Table infos */ - int table_type ; - int nb_col ; - int table_width ; - int nb_raws ; - /* Column infos */ - char label[FITSVALSZ] ; - char unit[FITSVALSZ] ; - char disp[FITSVALSZ] ; - char nullval[FITSVALSZ] ; - int atom_nb ; - int atom_dec_nb ; - int atom_size ; - tfits_type atom_type ; - int offset_beg ; - int data_size ; - int theory_size ; - int zero_present ; - int scale_present ; - float zero ; - float scale ; - - /* For ASCII tables */ - int col_pos ; - int next_col_pos ; - - /* For X type */ - int nb_bits ; - - int i ; - - /* See if 'filename' is a fits file */ - if (is_fits_file(filename) != 1) { - qfits_error("[%s] is not FITS", filename) ; - return NULL ; - } - - /* Identify a table and get the table type : ASCII or BIN */ - if ((table_type = qfits_is_table(filename, xtnum))==0) { - qfits_error("[%s] extension %d is not a table", filename, xtnum) ; - return NULL ; - } - - /* Get number of columns and allocate them: nc <-> TFIELDS */ - if ((str_val = qfits_query_ext(filename, "TFIELDS", xtnum)) == NULL) { - qfits_error("cannot read TFIELDS in [%s]:[%d]", filename, xtnum) ; - return NULL ; - } - nb_col = atoi(str_val) ; + qfits_table *tload; + qfits_col *curr_col; + char *str_val; + char keyword[FITSVALSZ]; + /* Table infos */ + int table_type; + int nb_col; + int table_width; + int nb_raws; + /* Column infos */ + char label[FITSVALSZ]; + char unit[FITSVALSZ]; + char disp[FITSVALSZ]; + char nullval[FITSVALSZ]; + int atom_nb; + int atom_dec_nb; + int atom_size; + tfits_type atom_type; + int offset_beg; + int data_size; + int theory_size; + int zero_present; + int scale_present; + float zero; + float scale; - /* Get the width in bytes of the table */ - if ((str_val = qfits_query_ext(filename, "NAXIS1", xtnum)) == NULL) { - qfits_error("cannot read NAXIS1 in [%s]:[%d]", filename, xtnum) ; - return NULL ; - } - table_width = atoi(str_val) ; - - /* Get the number of raws */ - if ((str_val = qfits_query_ext(filename, "NAXIS2", xtnum)) == NULL) { - qfits_error("cannot read NAXIS2 in [%s]:[%d]", filename, xtnum) ; - return NULL ; - } - nb_raws = atoi(str_val) ; + /* For ASCII tables */ + int col_pos; + int next_col_pos; - /* Create the table object */ - tload = qfits_table_new(filename, table_type, table_width, nb_col, nb_raws); - - /* Initialize offset_beg */ - if (qfits_get_datinfo(filename, xtnum, &offset_beg, &data_size)!=0) { - qfits_error("cannot find data start in [%s]:[%d]", filename, xtnum); - qfits_table_close(tload); - return NULL ; - } - - /* Loop on all columns and get column descriptions */ - curr_col = tload->col ; - for (i=0 ; inc ; i++) { - /* label <-> TTYPE */ - sprintf(keyword, "TTYPE%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) { - label[0] = (char)0 ; - } else strcpy(label, qfits_pretty_string(str_val)) ; - - /* unit <-> TUNIT */ - sprintf(keyword, "TUNIT%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) { - unit[0] = (char)0 ; - } else strcpy(unit, qfits_pretty_string(str_val)) ; + /* For X type */ + int nb_bits; - /* disp <-> TDISP */ - sprintf(keyword, "TDISP%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) { - disp[0] = (char)0 ; - } else strcpy(disp, qfits_pretty_string(str_val)) ; + int i; - /* nullval <-> TNULL */ - sprintf(keyword, "TNULL%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) { - nullval[0] = (char)0 ; - } else strcpy(nullval, qfits_pretty_string(str_val)) ; - - /* atom_size, atom_nb, atom_dec_nb, atom_type <-> TFORM */ - sprintf(keyword, "TFORM%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL) { - qfits_error("cannot read [%s] in [%s]:[%d]", keyword, filename, - xtnum); - qfits_table_close(tload); - return NULL ; - } - /* Interpret the type in header */ - if (qfits_table_interpret_type(qfits_pretty_string(str_val), - &(atom_nb), - &(atom_dec_nb), - &(atom_type), - table_type) == -1) { - qfits_error("cannot interpret the type: %s", str_val) ; - qfits_table_close(tload) ; - return NULL ; - } - - /* Set atom_size */ - switch (atom_type) { - case TFITS_BIN_TYPE_A: - case TFITS_BIN_TYPE_L: - case TFITS_BIN_TYPE_B: - atom_size = 1 ; - break ; - case TFITS_BIN_TYPE_I: - atom_size = 2 ; - break ; - case TFITS_BIN_TYPE_J: - case TFITS_BIN_TYPE_E: - case TFITS_ASCII_TYPE_I: - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - atom_size = 4 ; - break ; - case TFITS_BIN_TYPE_C: - case TFITS_BIN_TYPE_P: - atom_size = 4 ; - atom_nb *= 2 ; - break ; - case TFITS_BIN_TYPE_D: - case TFITS_ASCII_TYPE_D: - atom_size = 8 ; - break ; - case TFITS_BIN_TYPE_M: - atom_size = 8 ; - atom_nb *= 2 ; - break ; - case TFITS_BIN_TYPE_X: - atom_size = 1 ; - nb_bits = atom_nb ; - atom_nb = (int)((nb_bits - 1)/ 8) + 1 ; - break ; - case TFITS_ASCII_TYPE_A: - atom_size = atom_nb ; - break ; - default: - qfits_error("unrecognized type") ; - qfits_table_close(tload) ; - return NULL ; - break ; - } - - /* zero <-> TZERO */ - sprintf(keyword, "TZERO%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) != NULL) { - zero = (float)atof(str_val) ; - zero_present = 1 ; - } else { - zero = (float)0.0 ; - zero_present = 0 ; - } - - /* scale <-> TSCAL */ - sprintf(keyword, "TSCAL%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum)) != NULL) { - scale = (float)atof(str_val) ; - scale_present = 1 ; - } else { - scale = (float)1.0 ; - scale_present = 0 ; - } - - /* Fill the current column object */ - qfits_col_fill(curr_col, atom_nb, atom_dec_nb, atom_size, atom_type, - label, unit, nullval, disp, zero_present, zero, scale_present, - scale, offset_beg) ; - - /* Compute offset_beg but for the last column */ - if (i < tload->nc - 1) { - if (table_type == QFITS_ASCIITABLE) { - /* column width <-> TBCOLi and TBCOLi+1 */ - sprintf(keyword, "TBCOL%d", i+1) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL) { - qfits_error("cannot read [%s] in [%s]", keyword, filename); - qfits_table_close(tload); - return NULL ; - } - col_pos = atoi(qfits_pretty_string(str_val)) ; - - sprintf(keyword, "TBCOL%d", i+2) ; - if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL){ - qfits_error("cannot read [%s] in [%s]", keyword, filename) ; - qfits_table_close(tload) ; - return NULL ; - } - next_col_pos = atoi(qfits_pretty_string(str_val)) ; - offset_beg += (int)(next_col_pos - col_pos) ; - } else if (table_type == QFITS_BINTABLE) { - offset_beg += atom_nb * atom_size ; - } - } - curr_col++ ; - } - - /* Check that the theoritical data size is not far from the measured */ - /* one by more than 2880 */ - theory_size = qfits_compute_table_width(tload)*tload->nr ; - if (data_size < theory_size) { - qfits_error("Uncoherent data sizes") ; - qfits_table_close(tload) ; - return NULL ; + /* See if 'filename' is a fits file */ + if (is_fits_file (filename) != 1) + { + qfits_error ("[%s] is not FITS", filename); + return NULL; } - - /* Return */ - return tload ; + + /* Identify a table and get the table type : ASCII or BIN */ + if ((table_type = qfits_is_table (filename, xtnum)) == 0) + { + qfits_error ("[%s] extension %d is not a table", filename, xtnum); + return NULL; + } + + /* Get number of columns and allocate them: nc <-> TFIELDS */ + if ((str_val = qfits_query_ext (filename, "TFIELDS", xtnum)) == NULL) + { + qfits_error ("cannot read TFIELDS in [%s]:[%d]", filename, xtnum); + return NULL; + } + nb_col = atoi (str_val); + + /* Get the width in bytes of the table */ + if ((str_val = qfits_query_ext (filename, "NAXIS1", xtnum)) == NULL) + { + qfits_error ("cannot read NAXIS1 in [%s]:[%d]", filename, xtnum); + return NULL; + } + table_width = atoi (str_val); + + /* Get the number of raws */ + if ((str_val = qfits_query_ext (filename, "NAXIS2", xtnum)) == NULL) + { + qfits_error ("cannot read NAXIS2 in [%s]:[%d]", filename, xtnum); + return NULL; + } + nb_raws = atoi (str_val); + + /* Create the table object */ + tload = + qfits_table_new (filename, table_type, table_width, nb_col, nb_raws); + + /* Initialize offset_beg */ + if (qfits_get_datinfo (filename, xtnum, &offset_beg, &data_size) != 0) + { + qfits_error ("cannot find data start in [%s]:[%d]", filename, xtnum); + qfits_table_close (tload); + return NULL; + } + + /* Loop on all columns and get column descriptions */ + curr_col = tload->col; + for (i = 0; i < tload->nc; i++) + { + /* label <-> TTYPE */ + sprintf (keyword, "TTYPE%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + label[0] = (char) 0; + } + else + strcpy (label, qfits_pretty_string (str_val)); + + /* unit <-> TUNIT */ + sprintf (keyword, "TUNIT%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + unit[0] = (char) 0; + } + else + strcpy (unit, qfits_pretty_string (str_val)); + + /* disp <-> TDISP */ + sprintf (keyword, "TDISP%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + disp[0] = (char) 0; + } + else + strcpy (disp, qfits_pretty_string (str_val)); + + /* nullval <-> TNULL */ + sprintf (keyword, "TNULL%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + nullval[0] = (char) 0; + } + else + strcpy (nullval, qfits_pretty_string (str_val)); + + /* atom_size, atom_nb, atom_dec_nb, atom_type <-> TFORM */ + sprintf (keyword, "TFORM%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + qfits_error ("cannot read [%s] in [%s]:[%d]", keyword, filename, + xtnum); + qfits_table_close (tload); + return NULL; + } + /* Interpret the type in header */ + if (qfits_table_interpret_type (qfits_pretty_string (str_val), + &(atom_nb), + &(atom_dec_nb), + &(atom_type), table_type) == -1) + { + qfits_error ("cannot interpret the type: %s", str_val); + qfits_table_close (tload); + return NULL; + } + + /* Set atom_size */ + switch (atom_type) + { + case TFITS_BIN_TYPE_A: + case TFITS_BIN_TYPE_L: + case TFITS_BIN_TYPE_B: + atom_size = 1; + break; + case TFITS_BIN_TYPE_I: + atom_size = 2; + break; + case TFITS_BIN_TYPE_J: + case TFITS_BIN_TYPE_E: + case TFITS_ASCII_TYPE_I: + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + atom_size = 4; + break; + case TFITS_BIN_TYPE_C: + case TFITS_BIN_TYPE_P: + atom_size = 4; + atom_nb *= 2; + break; + case TFITS_BIN_TYPE_D: + case TFITS_ASCII_TYPE_D: + atom_size = 8; + break; + case TFITS_BIN_TYPE_M: + atom_size = 8; + atom_nb *= 2; + break; + case TFITS_BIN_TYPE_X: + atom_size = 1; + nb_bits = atom_nb; + atom_nb = (int) ((nb_bits - 1) / 8) + 1; + break; + case TFITS_ASCII_TYPE_A: + atom_size = atom_nb; + break; + default: + qfits_error ("unrecognized type"); + qfits_table_close (tload); + return NULL; + break; + } + + /* zero <-> TZERO */ + sprintf (keyword, "TZERO%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) != NULL) + { + zero = (float) atof (str_val); + zero_present = 1; + } + else + { + zero = (float) 0.0; + zero_present = 0; + } + + /* scale <-> TSCAL */ + sprintf (keyword, "TSCAL%d", i + 1); + if ((str_val = qfits_query_ext (filename, keyword, xtnum)) != NULL) + { + scale = (float) atof (str_val); + scale_present = 1; + } + else + { + scale = (float) 1.0; + scale_present = 0; + } + + /* Fill the current column object */ + qfits_col_fill (curr_col, atom_nb, atom_dec_nb, atom_size, atom_type, + label, unit, nullval, disp, zero_present, zero, + scale_present, scale, offset_beg); + + /* Compute offset_beg but for the last column */ + if (i < tload->nc - 1) + { + if (table_type == QFITS_ASCIITABLE) + { + /* column width <-> TBCOLi and TBCOLi+1 */ + sprintf (keyword, "TBCOL%d", i + 1); + if ((str_val = + qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + qfits_error ("cannot read [%s] in [%s]", keyword, filename); + qfits_table_close (tload); + return NULL; + } + col_pos = atoi (qfits_pretty_string (str_val)); + + sprintf (keyword, "TBCOL%d", i + 2); + if ((str_val = + qfits_query_ext (filename, keyword, xtnum)) == NULL) + { + qfits_error ("cannot read [%s] in [%s]", keyword, filename); + qfits_table_close (tload); + return NULL; + } + next_col_pos = atoi (qfits_pretty_string (str_val)); + offset_beg += (int) (next_col_pos - col_pos); + } + else if (table_type == QFITS_BINTABLE) + { + offset_beg += atom_nb * atom_size; + } + } + curr_col++; + } + + /* Check that the theoritical data size is not far from the measured */ + /* one by more than 2880 */ + theory_size = qfits_compute_table_width (tload) * tload->nr; + if (data_size < theory_size) + { + qfits_error ("Uncoherent data sizes"); + qfits_table_close (tload); + return NULL; + } + + /* Return */ + return tload; } /*----------------------------------------------------------------------------*/ @@ -630,12 +696,16 @@ qfits_table * qfits_table_open( Frees all memory associated to a qfits_table structure. */ /*----------------------------------------------------------------------------*/ -void qfits_table_close(qfits_table * t) +void +qfits_table_close (qfits_table * t) { - if (t==NULL) return ; - if (t->nc>0) if (t->col!=NULL) free(t->col) ; - free(t); - return ; + if (t == NULL) + return; + if (t->nc > 0) + if (t->col != NULL) + free (t->col); + free (t); + return; } /*----------------------------------------------------------------------------*/ @@ -668,100 +738,119 @@ void qfits_table_close(qfits_table * t) */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column( - qfits_table * th, - int colnum, - int * selection) +unsigned char * +qfits_query_column (qfits_table * th, int colnum, int *selection) { - char * start ; - qfits_col * col ; - int field_size ; - unsigned char * array ; - unsigned char * r ; - unsigned char * inbuf ; - int table_width ; - int nb_rows ; - size_t size ; - int i ; - - if (th->tab_w == -1) { - /* Compute the table width in bytes */ - if ((table_width = qfits_compute_table_width(th)) == -1) { - qfits_error("cannot compute the table width") ; - return NULL ; - } - } else table_width = th->tab_w ; - - /* Compute the number of selected rows */ - nb_rows = 0 ; - if (selection == NULL) { - nb_rows = th->nr ; - } else { - for (i=0 ; inr ; i++) if (selection[i] == 1) nb_rows++ ; - } - - /* Pointer to requested column */ - col = th->col + colnum ; + char *start; + qfits_col *col; + int field_size; + unsigned char *array; + unsigned char *r; + unsigned char *inbuf; + int table_width; + int nb_rows; + size_t size; + int i; - /* Test if column is empty */ - if (nb_rows * col->atom_size * col->atom_nb == 0) col->readable = 0 ; - - /* Test if column is readable */ - if (col->readable == 0) return NULL ; - - /* Compute the size in bytes of one field stored in the file */ - if ((field_size=qfits_table_get_field_size(th->tab_t,col))==-1) return NULL; - - /* Load input file */ - if ((start=falloc(th->filename, 0, &size))==NULL) { - qfits_error("cannot open table for query [%s]", th->filename); - return NULL ; - } - - /* Allocate data array */ - array = malloc(nb_rows * field_size * sizeof(char)) ; - - /* Position the input pointer at the begining of the column data */ - r = array ; - inbuf = (unsigned char*)start + col->off_beg ; - - /* Copy the values in array */ - if (selection == NULL) { - /* No selection : get the complete column */ - for (i=0 ; inr ; i++) { - /* Copy all atoms on this field into array */ - memcpy(r, inbuf, field_size); - r += field_size ; - /* Jump to next line */ - inbuf += table_width ; - } - } else { - /* Get only the selected rows */ - for (i=0 ; inr ; i++) { - if (selection[i] == 1) { - /* Copy all atoms on this field into array */ - memcpy(r, inbuf, field_size); - r += field_size ; - } - /* Jump to next line */ - inbuf += table_width ; - } - } - fdealloc(start, 0, size) ; - - /* SWAP the bytes if necessary */ -#ifndef WORDS_BIGENDIAN - if (th->tab_t == QFITS_BINTABLE) { - r = array ; - for (i=0 ; iatom_nb ; i++) { - swap_bytes(r, col->atom_size); - r += col->atom_size ; - } + if (th->tab_w == -1) + { + /* Compute the table width in bytes */ + if ((table_width = qfits_compute_table_width (th)) == -1) + { + qfits_error ("cannot compute the table width"); + return NULL; } + } + else + table_width = th->tab_w; + + /* Compute the number of selected rows */ + nb_rows = 0; + if (selection == NULL) + { + nb_rows = th->nr; + } + else + { + for (i = 0; i < th->nr; i++) + if (selection[i] == 1) + nb_rows++; + } + + /* Pointer to requested column */ + col = th->col + colnum; + + /* Test if column is empty */ + if (nb_rows * col->atom_size * col->atom_nb == 0) + col->readable = 0; + + /* Test if column is readable */ + if (col->readable == 0) + return NULL; + + /* Compute the size in bytes of one field stored in the file */ + if ((field_size = qfits_table_get_field_size (th->tab_t, col)) == -1) + return NULL; + + /* Load input file */ + if ((start = falloc (th->filename, 0, &size)) == NULL) + { + qfits_error ("cannot open table for query [%s]", th->filename); + return NULL; + } + + /* Allocate data array */ + array = malloc (nb_rows * field_size * sizeof (char)); + + /* Position the input pointer at the begining of the column data */ + r = array; + inbuf = (unsigned char *) start + col->off_beg; + + /* Copy the values in array */ + if (selection == NULL) + { + /* No selection : get the complete column */ + for (i = 0; i < th->nr; i++) + { + /* Copy all atoms on this field into array */ + memcpy (r, inbuf, field_size); + r += field_size; + /* Jump to next line */ + inbuf += table_width; + } + } + else + { + /* Get only the selected rows */ + for (i = 0; i < th->nr; i++) + { + if (selection[i] == 1) + { + /* Copy all atoms on this field into array */ + memcpy (r, inbuf, field_size); + r += field_size; + } + /* Jump to next line */ + inbuf += table_width; + } + } + fdealloc (start, 0, size); + + /* SWAP the bytes if necessary */ +#ifndef WORDS_BIGENDIAN + if (th->tab_t == QFITS_BINTABLE) + { + r = array; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + swap_bytes (r, col->atom_size); + r += col->atom_size; + } + } #endif - /* Return allocated and converted array */ - return array ; + /* Return allocated and converted array */ + return array; } /*----------------------------------------------------------------------------*/ @@ -776,85 +865,95 @@ unsigned char * qfits_query_column( Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column_seq( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows) +unsigned char * +qfits_query_column_seq (qfits_table * th, + int colnum, int start_ind, int nb_rows) { - char * start ; - qfits_col * col ; - int field_size ; - unsigned char * array ; - unsigned char * r ; - unsigned char * inbuf ; - int table_width ; - size_t size ; - int i ; - - if (th->tab_w == -1) { - /* Compute the table width in bytes */ - if ((table_width = qfits_compute_table_width(th)) == -1) { - qfits_error("cannot compute the table width") ; - return NULL ; - } - } else table_width = th->tab_w ; - - /* Check the validity of start_ind and nb_rows */ - if ((start_ind<0) || (start_ind+nb_rows>th->nr)) { - qfits_error("bad start index and number of rows") ; - return NULL ; - } - - /* Pointer to requested column */ - col = th->col + colnum ; + char *start; + qfits_col *col; + int field_size; + unsigned char *array; + unsigned char *r; + unsigned char *inbuf; + int table_width; + size_t size; + int i; - /* Test if column is empty */ - if (nb_rows * col->atom_size * col->atom_nb == 0) col->readable = 0 ; - - /* Test if column is readable */ - if (col->readable == 0) return NULL ; - - /* Compute the size in bytes of one field stored in the file */ - if ((field_size=qfits_table_get_field_size(th->tab_t,col))==-1) return NULL; - - /* Load input file */ - if ((start=falloc(th->filename, 0, &size))==NULL) { - qfits_error("cannot open table for query [%s]", th->filename); - return NULL ; - } - - /* Allocate data array */ - array = malloc(nb_rows * field_size * sizeof(char)) ; - - /* Position the input pointer at the begining of the column data */ - r = array ; - inbuf = (unsigned char*)start + col->off_beg + table_width * start_ind ; - - /* Copy the values in array */ - /* Get only the selected rows */ - for (i=0 ; itab_t == QFITS_BINTABLE) { - r = array ; - for (i=0 ; iatom_nb ; i++) { - swap_bytes(r, col->atom_size); - r += col->atom_size ; - } + if (th->tab_w == -1) + { + /* Compute the table width in bytes */ + if ((table_width = qfits_compute_table_width (th)) == -1) + { + qfits_error ("cannot compute the table width"); + return NULL; } + } + else + table_width = th->tab_w; + + /* Check the validity of start_ind and nb_rows */ + if ((start_ind < 0) || (start_ind + nb_rows > th->nr)) + { + qfits_error ("bad start index and number of rows"); + return NULL; + } + + /* Pointer to requested column */ + col = th->col + colnum; + + /* Test if column is empty */ + if (nb_rows * col->atom_size * col->atom_nb == 0) + col->readable = 0; + + /* Test if column is readable */ + if (col->readable == 0) + return NULL; + + /* Compute the size in bytes of one field stored in the file */ + if ((field_size = qfits_table_get_field_size (th->tab_t, col)) == -1) + return NULL; + + /* Load input file */ + if ((start = falloc (th->filename, 0, &size)) == NULL) + { + qfits_error ("cannot open table for query [%s]", th->filename); + return NULL; + } + + /* Allocate data array */ + array = malloc (nb_rows * field_size * sizeof (char)); + + /* Position the input pointer at the begining of the column data */ + r = array; + inbuf = (unsigned char *) start + col->off_beg + table_width * start_ind; + + /* Copy the values in array */ + /* Get only the selected rows */ + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms on this field into array */ + memcpy (r, inbuf, field_size); + r += field_size; + /* Jump to next line */ + inbuf += table_width; + } + fdealloc (start, 0, size); + + /* SWAP the bytes if necessary */ +#ifndef WORDS_BIGENDIAN + if (th->tab_t == QFITS_BINTABLE) + { + r = array; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + swap_bytes (r, col->atom_size); + r += col->atom_size; + } + } #endif - /* Return allocated and converted array */ - return array ; + /* Return allocated and converted array */ + return array; } /*----------------------------------------------------------------------------*/ @@ -864,26 +963,31 @@ unsigned char * qfits_query_column_seq( @return the width (-1 in error case) */ /*----------------------------------------------------------------------------*/ -int qfits_compute_table_width(qfits_table * th) +int +qfits_compute_table_width (qfits_table * th) { - int width ; - qfits_col * curr_col ; - int i ; - - /* Initialize */ - width = 0 ; - - /* Loop on all columns and get column descriptions */ - curr_col = th->col ; - for (i=0 ; inc ; i++) { - if (th->tab_t == QFITS_ASCIITABLE) { - width += curr_col->atom_nb ; - } else if (th->tab_t == QFITS_BINTABLE) { - width += curr_col->atom_nb * curr_col->atom_size ; - } - curr_col++ ; + int width; + qfits_col *curr_col; + int i; + + /* Initialize */ + width = 0; + + /* Loop on all columns and get column descriptions */ + curr_col = th->col; + for (i = 0; i < th->nc; i++) + { + if (th->tab_t == QFITS_ASCIITABLE) + { + width += curr_col->atom_nb; + } + else if (th->tab_t == QFITS_BINTABLE) + { + width += curr_col->atom_nb * curr_col->atom_size; + } + curr_col++; } - return width ; + return width; } /*----------------------------------------------------------------------------*/ @@ -911,195 +1015,228 @@ int qfits_compute_table_width(qfits_table * th) the pixel loader here. */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_data( - qfits_table * th, - int colnum, - int * selection, - void * null_value) +void * +qfits_query_column_data (qfits_table * th, + int colnum, int *selection, void *null_value) { - void * out_array ; - qfits_col * col ; - int nb_rows ; - unsigned char * in_array ; - char * field ; + void *out_array; + qfits_col *col; + int nb_rows; + unsigned char *in_array; + char *field; - unsigned char ucnull ; - short snull ; - int inull ; - double dnull ; - float fnull ; + unsigned char ucnull; + short snull; + int inull; + double dnull; + float fnull; - int i ; - - /* Initialize */ - if (null_value == NULL) { - inull = (int)0 ; - snull = (short)0 ; - ucnull = (unsigned char)0 ; - fnull = (float)0.0 ; - dnull = (double)0.0 ; - } else { - inull = *(int*)null_value ; - snull = *(short*)null_value ; - ucnull = *(unsigned char*)null_value ; - fnull = *(float*)null_value ; - dnull = *(double*)null_value ; + int i; + + /* Initialize */ + if (null_value == NULL) + { + inull = (int) 0; + snull = (short) 0; + ucnull = (unsigned char) 0; + fnull = (float) 0.0; + dnull = (double) 0.0; + } + else + { + inull = *(int *) null_value; + snull = *(short *) null_value; + ucnull = *(unsigned char *) null_value; + fnull = *(float *) null_value; + dnull = *(double *) null_value; } - /* Get the number of selected rows */ - nb_rows = 0 ; - if (selection == NULL) { - nb_rows = th->nr ; - } else { - for (i=0 ; inr ; i++) if (selection[i] == 1) nb_rows++ ; + /* Get the number of selected rows */ + nb_rows = 0; + if (selection == NULL) + { + nb_rows = th->nr; + } + else + { + for (i = 0; i < th->nr; i++) + if (selection[i] == 1) + nb_rows++; } - /* Pointer to requested column */ - col = th->col+colnum ; + /* Pointer to requested column */ + col = th->col + colnum; - /* Test if column is readable */ - if (col->readable == 0) return NULL ; - - /* Handle each type separately */ - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - out_array = (char*)qfits_query_column(th, colnum, selection) ; - break ; + /* Test if column is readable */ + if (col->readable == 0) + return NULL; - case TFITS_ASCII_TYPE_I: - in_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - ((int*)out_array)[i] = inull ; - } else { - ((int*)out_array)[i] = (int)atoi(field) ; - } - } - free(field) ; - free(in_array) ; - break ; - - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - in_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - ((float*)out_array)[i] = fnull ; - } else { - /* Add the decimal handling */ - ((float*)out_array)[i]=(float)qfits_str2dec(field, - col->atom_dec_nb) ; - } - } - free(field) ; - free(in_array) ; - break ; - - case TFITS_ASCII_TYPE_D: - in_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, field)) { - ((double*)out_array)[i] = dnull ; - } else { - /* Add the decimal handling */ - ((double*)out_array)[i]=qfits_str2dec(field, col->atom_dec_nb) ; - } - } - free(field) ; - free(in_array) ; - - break ; - case TFITS_BIN_TYPE_A: - case TFITS_BIN_TYPE_L: - out_array = (char*)qfits_query_column(th, colnum, selection) ; - break ; - - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - out_array = (double*)qfits_query_column(th, colnum, selection) ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((double*)out_array)[i]) || - qfits_isinf(((double*)out_array)[i])) { - ((double*)out_array)[i] = dnull ; - } - } - break ; - - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - out_array = (float*)qfits_query_column(th, colnum, selection) ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((float*)out_array)[i]) || - qfits_isinf(((float*)out_array)[i])) { - ((float*)out_array)[i] = fnull ; - } - } - break ; - - case TFITS_BIN_TYPE_X: - out_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - break ; - - case TFITS_BIN_TYPE_B: - out_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval) == (int)((unsigned char*)out_array)[i])) { - ((unsigned char*)out_array)[i] = ucnull ; - } - } - break ; - - case TFITS_BIN_TYPE_I: - out_array = (short*)qfits_query_column(th, colnum, selection) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==(int)((short*)out_array)[i])) { - ((short*)out_array)[i] = snull ; - } - } - break ; - - case TFITS_BIN_TYPE_J: - out_array = (int*)qfits_query_column(th, colnum, selection) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==((int*)out_array)[i])) { - ((int*)out_array)[i] = inull ; - } - } - break ; - - case TFITS_BIN_TYPE_P: - out_array = (int*)qfits_query_column(th, colnum, selection) ; - break ; - - default: - qfits_error("unrecognized data type") ; - return NULL ; + /* Handle each type separately */ + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + out_array = (char *) qfits_query_column (th, colnum, selection); + break; + + case TFITS_ASCII_TYPE_I: + in_array = (unsigned char *) qfits_query_column (th, colnum, selection); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + ((int *) out_array)[i] = inull; + } + else + { + ((int *) out_array)[i] = (int) atoi (field); + } } - return out_array ; + free (field); + free (in_array); + break; + + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + in_array = (unsigned char *) qfits_query_column (th, colnum, selection); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + ((float *) out_array)[i] = fnull; + } + else + { + /* Add the decimal handling */ + ((float *) out_array)[i] = (float) qfits_str2dec (field, + col->atom_dec_nb); + } + } + free (field); + free (in_array); + break; + + case TFITS_ASCII_TYPE_D: + in_array = (unsigned char *) qfits_query_column (th, colnum, selection); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, field)) + { + ((double *) out_array)[i] = dnull; + } + else + { + /* Add the decimal handling */ + ((double *) out_array)[i] = + qfits_str2dec (field, col->atom_dec_nb); + } + } + free (field); + free (in_array); + + break; + case TFITS_BIN_TYPE_A: + case TFITS_BIN_TYPE_L: + out_array = (char *) qfits_query_column (th, colnum, selection); + break; + + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + out_array = (double *) qfits_query_column (th, colnum, selection); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((double *) out_array)[i]) || + qfits_isinf (((double *) out_array)[i])) + { + ((double *) out_array)[i] = dnull; + } + } + break; + + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + out_array = (float *) qfits_query_column (th, colnum, selection); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((float *) out_array)[i]) || + qfits_isinf (((float *) out_array)[i])) + { + ((float *) out_array)[i] = fnull; + } + } + break; + + case TFITS_BIN_TYPE_X: + out_array = + (unsigned char *) qfits_query_column (th, colnum, selection); + break; + + case TFITS_BIN_TYPE_B: + out_array = + (unsigned char *) qfits_query_column (th, colnum, selection); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((unsigned char *) out_array)[i])) + { + ((unsigned char *) out_array)[i] = ucnull; + } + } + break; + + case TFITS_BIN_TYPE_I: + out_array = (short *) qfits_query_column (th, colnum, selection); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((short *) out_array)[i])) + { + ((short *) out_array)[i] = snull; + } + } + break; + + case TFITS_BIN_TYPE_J: + out_array = (int *) qfits_query_column (th, colnum, selection); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == ((int *) out_array)[i])) + { + ((int *) out_array)[i] = inull; + } + } + break; + + case TFITS_BIN_TYPE_P: + out_array = (int *) qfits_query_column (th, colnum, selection); + break; + + default: + qfits_error ("unrecognized data type"); + return NULL; + } + return out_array; } /*----------------------------------------------------------------------------*/ @@ -1115,199 +1252,230 @@ void * qfits_query_column_data( of rows. Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_seq_data( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows, - void * null_value) +void * +qfits_query_column_seq_data (qfits_table * th, + int colnum, + int start_ind, int nb_rows, void *null_value) { - void * out_array ; - qfits_col * col ; - unsigned char * in_array ; - char * field ; + void *out_array; + qfits_col *col; + unsigned char *in_array; + char *field; - unsigned char ucnull ; - short snull ; - int inull ; - double dnull ; - float fnull ; + unsigned char ucnull; + short snull; + int inull; + double dnull; + float fnull; - int i ; - - /* Initialize */ - if (null_value == NULL) { - inull = (int)0 ; - snull = (short)0 ; - ucnull = (unsigned char)0 ; - fnull = (float)0.0 ; - dnull = (double)0.0 ; - } else { - inull = *(int*)null_value ; - snull = *(short*)null_value ; - ucnull = *(unsigned char*)null_value ; - fnull = *(float*)null_value ; - dnull = *(double*)null_value ; + int i; + + /* Initialize */ + if (null_value == NULL) + { + inull = (int) 0; + snull = (short) 0; + ucnull = (unsigned char) 0; + fnull = (float) 0.0; + dnull = (double) 0.0; + } + else + { + inull = *(int *) null_value; + snull = *(short *) null_value; + ucnull = *(unsigned char *) null_value; + fnull = *(float *) null_value; + dnull = *(double *) null_value; } - /* Pointer to requested column */ - col = th->col+colnum ; + /* Pointer to requested column */ + col = th->col + colnum; - /* Test if column is readable */ - if (col->readable == 0) return NULL ; - - /* Handle each type separately */ - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - out_array = (char*)qfits_query_column_seq(th, colnum, start_ind, - nb_rows) ; - break ; + /* Test if column is readable */ + if (col->readable == 0) + return NULL; - case TFITS_ASCII_TYPE_I: - in_array = (unsigned char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - ((int*)out_array)[i] = inull ; - } else { - ((int*)out_array)[i] = (int)atoi(field) ; - } - } - free(field) ; - free(in_array) ; - break ; - - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - in_array = (unsigned char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - ((float*)out_array)[i] = fnull ; - } else { - /* Add the decimal handling */ - ((float*)out_array)[i]=(float)qfits_str2dec(field, - col->atom_dec_nb) ; - } - } - free(field) ; - free(in_array) ; - break ; - - case TFITS_ASCII_TYPE_D: - in_array = (unsigned char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - out_array = malloc(nb_rows*col->atom_size); - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Write the data in out_array */ - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - ((double*)out_array)[i] = dnull ; - } else { - /* Add the decimal handling */ - ((double*)out_array)[i]=qfits_str2dec(field, col->atom_dec_nb) ; - } - } - free(field) ; - free(in_array) ; - break ; - - case TFITS_BIN_TYPE_A: - case TFITS_BIN_TYPE_L: - out_array = (char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - break ; - - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - out_array = (double*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((double*)out_array)[i]) || - qfits_isinf(((double*)out_array)[i])) { - ((double*)out_array)[i] = dnull ; - } - } - break ; - - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - out_array = (float*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((float*)out_array)[i]) || - qfits_isinf(((float*)out_array)[i])) { - ((float*)out_array)[i] = fnull ; - } - } - break ; - - case TFITS_BIN_TYPE_X: - out_array = (unsigned char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - break ; - - case TFITS_BIN_TYPE_B: - out_array = (unsigned char*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)== (int)((unsigned char*)out_array)[i])) { - ((unsigned char*)out_array)[i] = ucnull ; - } - } - break ; - - case TFITS_BIN_TYPE_I: - out_array = (short*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==(int)((short*)out_array)[i])) { - ((short*)out_array)[i] = snull ; - } - } - break ; - - case TFITS_BIN_TYPE_J: - out_array = (int*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==((int*)out_array)[i])) { - ((int*)out_array)[i] = inull ; - } - } - break ; - - case TFITS_BIN_TYPE_P: - out_array = (int*)qfits_query_column_seq(th, colnum, - start_ind, nb_rows) ; - break ; - - default: - qfits_error("unrecognized data type") ; - return NULL ; + /* Handle each type separately */ + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + out_array = (char *) qfits_query_column_seq (th, colnum, start_ind, + nb_rows); + break; + + case TFITS_ASCII_TYPE_I: + in_array = (unsigned char *) qfits_query_column_seq (th, colnum, + start_ind, + nb_rows); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + ((int *) out_array)[i] = inull; + } + else + { + ((int *) out_array)[i] = (int) atoi (field); + } } - return out_array ; + free (field); + free (in_array); + break; + + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + in_array = (unsigned char *) qfits_query_column_seq (th, colnum, + start_ind, + nb_rows); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + ((float *) out_array)[i] = fnull; + } + else + { + /* Add the decimal handling */ + ((float *) out_array)[i] = (float) qfits_str2dec (field, + col->atom_dec_nb); + } + } + free (field); + free (in_array); + break; + + case TFITS_ASCII_TYPE_D: + in_array = (unsigned char *) qfits_query_column_seq (th, colnum, + start_ind, + nb_rows); + out_array = malloc (nb_rows * col->atom_size); + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Write the data in out_array */ + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + ((double *) out_array)[i] = dnull; + } + else + { + /* Add the decimal handling */ + ((double *) out_array)[i] = + qfits_str2dec (field, col->atom_dec_nb); + } + } + free (field); + free (in_array); + break; + + case TFITS_BIN_TYPE_A: + case TFITS_BIN_TYPE_L: + out_array = (char *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + break; + + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + out_array = (double *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((double *) out_array)[i]) || + qfits_isinf (((double *) out_array)[i])) + { + ((double *) out_array)[i] = dnull; + } + } + break; + + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + out_array = (float *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((float *) out_array)[i]) || + qfits_isinf (((float *) out_array)[i])) + { + ((float *) out_array)[i] = fnull; + } + } + break; + + case TFITS_BIN_TYPE_X: + out_array = (unsigned char *) qfits_query_column_seq (th, colnum, + start_ind, + nb_rows); + break; + + case TFITS_BIN_TYPE_B: + out_array = (unsigned char *) qfits_query_column_seq (th, colnum, + start_ind, + nb_rows); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((unsigned char *) out_array)[i])) + { + ((unsigned char *) out_array)[i] = ucnull; + } + } + break; + + case TFITS_BIN_TYPE_I: + out_array = (short *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((short *) out_array)[i])) + { + ((short *) out_array)[i] = snull; + } + } + break; + + case TFITS_BIN_TYPE_J: + out_array = (int *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == ((int *) out_array)[i])) + { + ((int *) out_array)[i] = inull; + } + } + break; + + case TFITS_BIN_TYPE_P: + out_array = (int *) qfits_query_column_seq (th, colnum, + start_ind, nb_rows); + break; + + default: + qfits_error ("unrecognized data type"); + return NULL; + } + return out_array; } /*----------------------------------------------------------------------------*/ @@ -1321,155 +1489,179 @@ void * qfits_query_column_seq_data( @return array with 1 for NULLs and 0 for non-NULLs */ /*----------------------------------------------------------------------------*/ -int * qfits_query_column_nulls( - qfits_table * th, - int colnum, - int * selection, - int * nb_vals, - int * nb_nulls) +int * +qfits_query_column_nulls (qfits_table * th, + int colnum, + int *selection, int *nb_vals, int *nb_nulls) { - int * out_array ; - qfits_col * col ; - unsigned char * in_array ; - void * tmp_array ; - char * field ; - int nb_rows ; - int i ; + int *out_array; + qfits_col *col; + unsigned char *in_array; + void *tmp_array; + char *field; + int nb_rows; + int i; - /* Initialize */ - *nb_nulls = 0 ; - *nb_vals = 0 ; + /* Initialize */ + *nb_nulls = 0; + *nb_vals = 0; - /* Get the number of selected rows */ - nb_rows = 0 ; - if (selection == NULL) { - nb_rows = th->nr ; - } else { - for (i=0 ; inr ; i++) if (selection[i] == 1) nb_rows++ ; + /* Get the number of selected rows */ + nb_rows = 0; + if (selection == NULL) + { + nb_rows = th->nr; + } + else + { + for (i = 0; i < th->nr; i++) + if (selection[i] == 1) + nb_rows++; } - - /* Pointer to requested column */ - col = th->col+colnum ; - - /* Test if column is readable */ - if (col->readable == 0) return NULL ; - /* Handle each type separately */ - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - case TFITS_ASCII_TYPE_D: - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - case TFITS_ASCII_TYPE_I: - in_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows, sizeof(int)); - *nb_vals = nb_rows ; - field = malloc((col->atom_nb+1)*sizeof(char)) ; - for (i=0 ; iatom_nb], col->atom_nb); - field[col->atom_nb]=(char)0 ; - /* Test if a NULL val is encoutered */ - if (!strcmp(col->nullval, qfits_strstrip(field))) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - free(field) ; - if (in_array != NULL) free(in_array) ; - break ; - - case TFITS_BIN_TYPE_A: - /* No NULL values */ - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - break ; - - case TFITS_BIN_TYPE_L: - case TFITS_BIN_TYPE_X: - case TFITS_BIN_TYPE_P: - /* No NULL values */ - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - break ; - - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - tmp_array = (double*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((double*)tmp_array)[i]) || - qfits_isinf(((double*)tmp_array)[i])) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - if (tmp_array != NULL) free(tmp_array) ; - break ; - - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - tmp_array = (float*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - for (i=0 ; iatom_nb ; i++) { - if (qfits_isnan(((float*)tmp_array)[i]) || - qfits_isinf(((float*)tmp_array)[i])) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - if (tmp_array != NULL) free(tmp_array) ; - break ; - - case TFITS_BIN_TYPE_B: - tmp_array = (unsigned char*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==(int)((unsigned char*)tmp_array)[i])) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - if (tmp_array != NULL) free(tmp_array) ; - break ; - - case TFITS_BIN_TYPE_I: - tmp_array = (short*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==(int)((short*)tmp_array)[i])) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - if (tmp_array != NULL) free(tmp_array) ; - break ; - - case TFITS_BIN_TYPE_J: - tmp_array = (int*)qfits_query_column(th, colnum, selection) ; - out_array = calloc(nb_rows * col->atom_nb, sizeof(int)) ; - *nb_vals = nb_rows * col->atom_nb ; - for (i=0 ; iatom_nb ; i++) { - if (((col->nullval)[0] != (char)0) && - (atoi(col->nullval)==((int*)tmp_array)[i])) { - out_array[i] = 1 ; - (*nb_nulls)++ ; - } - } - if (tmp_array != NULL) free(tmp_array) ; - break ; - - default: - qfits_error("unrecognized data type") ; - return NULL ; + /* Pointer to requested column */ + col = th->col + colnum; + + /* Test if column is readable */ + if (col->readable == 0) + return NULL; + + /* Handle each type separately */ + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + case TFITS_ASCII_TYPE_D: + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + case TFITS_ASCII_TYPE_I: + in_array = (unsigned char *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows, sizeof (int)); + *nb_vals = nb_rows; + field = malloc ((col->atom_nb + 1) * sizeof (char)); + for (i = 0; i < nb_rows; i++) + { + /* Copy all atoms of the field into 'field' */ + memcpy (field, &in_array[i * col->atom_nb], col->atom_nb); + field[col->atom_nb] = (char) 0; + /* Test if a NULL val is encoutered */ + if (!strcmp (col->nullval, qfits_strstrip (field))) + { + out_array[i] = 1; + (*nb_nulls)++; + } } - return out_array ; + free (field); + if (in_array != NULL) + free (in_array); + break; + + case TFITS_BIN_TYPE_A: + /* No NULL values */ + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + break; + + case TFITS_BIN_TYPE_L: + case TFITS_BIN_TYPE_X: + case TFITS_BIN_TYPE_P: + /* No NULL values */ + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + break; + + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + tmp_array = (double *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((double *) tmp_array)[i]) || + qfits_isinf (((double *) tmp_array)[i])) + { + out_array[i] = 1; + (*nb_nulls)++; + } + } + if (tmp_array != NULL) + free (tmp_array); + break; + + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + tmp_array = (float *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (qfits_isnan (((float *) tmp_array)[i]) || + qfits_isinf (((float *) tmp_array)[i])) + { + out_array[i] = 1; + (*nb_nulls)++; + } + } + if (tmp_array != NULL) + free (tmp_array); + break; + + case TFITS_BIN_TYPE_B: + tmp_array = + (unsigned char *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((unsigned char *) tmp_array)[i])) + { + out_array[i] = 1; + (*nb_nulls)++; + } + } + if (tmp_array != NULL) + free (tmp_array); + break; + + case TFITS_BIN_TYPE_I: + tmp_array = (short *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == (int) ((short *) tmp_array)[i])) + { + out_array[i] = 1; + (*nb_nulls)++; + } + } + if (tmp_array != NULL) + free (tmp_array); + break; + + case TFITS_BIN_TYPE_J: + tmp_array = (int *) qfits_query_column (th, colnum, selection); + out_array = calloc (nb_rows * col->atom_nb, sizeof (int)); + *nb_vals = nb_rows * col->atom_nb; + for (i = 0; i < nb_rows * col->atom_nb; i++) + { + if (((col->nullval)[0] != (char) 0) && + (atoi (col->nullval) == ((int *) tmp_array)[i])) + { + out_array[i] = 1; + (*nb_nulls)++; + } + } + if (tmp_array != NULL) + free (tmp_array); + break; + + default: + qfits_error ("unrecognized data type"); + return NULL; + } + return out_array; } /*----------------------------------------------------------------------------*/ @@ -1481,57 +1673,67 @@ int * qfits_query_column_nulls( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_save_table_hdrdump( - void ** array, - qfits_table * table, - qfits_header * fh) +int +qfits_save_table_hdrdump (void **array, + qfits_table * table, qfits_header * fh) { - FILE * outfile ; - char * md5hash ; - char md5card[81]; + FILE *outfile; + char *md5hash; + char md5card[81]; - /* Open the destination file */ - if ((outfile = fopen(table->filename, "w")) == NULL) { - qfits_error("cannot open file [%s]", table->filename) ; - return -1 ; - } - /* Write the fits header in the file 'outname' */ - if (qfits_header_dump(fh, outfile) == -1) { - qfits_error("cannot dump header in file") ; - fclose(outfile) ; - return -1 ; - } - /* Append the extension */ - if (table->tab_t == QFITS_BINTABLE) { - if (qfits_table_append_bin_xtension(outfile, table, array) == -1) { - qfits_error("in writing fits table") ; - fclose(outfile) ; - return -1 ; - } - } else if (table->tab_t == QFITS_ASCIITABLE) { - if (qfits_table_append_ascii_xtension(outfile, table, array) == -1) { - qfits_error("in writing fits table") ; - fclose(outfile) ; - return -1 ; - } - } else { - qfits_error("Unrecognized table type") ; - fclose(outfile) ; - return -1 ; - } - fclose(outfile) ; - /* Update MD5 keyword */ - if (strcmp(table->filename, "STDOUT")) { - md5hash = qfits_datamd5(table->filename); - if (md5hash==NULL) { - qfits_error("computing MD5 signature for output file %s", - table->filename); - return -1 ; - } - sprintf(md5card, "DATAMD5 = '%s' / MD5 checksum", md5hash); - qfits_replace_card(table->filename, "DATAMD5", md5card); + /* Open the destination file */ + if ((outfile = fopen (table->filename, "w")) == NULL) + { + qfits_error ("cannot open file [%s]", table->filename); + return -1; } - return 0 ; + /* Write the fits header in the file 'outname' */ + if (qfits_header_dump (fh, outfile) == -1) + { + qfits_error ("cannot dump header in file"); + fclose (outfile); + return -1; + } + /* Append the extension */ + if (table->tab_t == QFITS_BINTABLE) + { + if (qfits_table_append_bin_xtension (outfile, table, array) == -1) + { + qfits_error ("in writing fits table"); + fclose (outfile); + return -1; + } + } + else if (table->tab_t == QFITS_ASCIITABLE) + { + if (qfits_table_append_ascii_xtension (outfile, table, array) == -1) + { + qfits_error ("in writing fits table"); + fclose (outfile); + return -1; + } + } + else + { + qfits_error ("Unrecognized table type"); + fclose (outfile); + return -1; + } + fclose (outfile); + /* Update MD5 keyword */ + if (strcmp (table->filename, "STDOUT")) + { + md5hash = qfits_datamd5 (table->filename); + if (md5hash == NULL) + { + qfits_error ("computing MD5 signature for output file %s", + table->filename); + return -1; + } + sprintf (md5card, "DATAMD5 = '%s' / MD5 checksum", md5hash); + qfits_replace_card (table->filename, "DATAMD5", md5card); + } + return 0; } /*----------------------------------------------------------------------------*/ @@ -1550,27 +1752,32 @@ int qfits_save_table_hdrdump( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension( - FILE * outfile, - qfits_table * t, - void ** data) +int +qfits_table_append_xtension (FILE * outfile, qfits_table * t, void **data) { - /* Append the extension */ - if (t->tab_t == QFITS_BINTABLE) { - if (qfits_table_append_bin_xtension(outfile, t, data) == -1) { - qfits_error("in writing fits table") ; - return -1 ; - } - } else if (t->tab_t == QFITS_ASCIITABLE) { - if (qfits_table_append_ascii_xtension(outfile, t, data) == -1) { - qfits_error("in writing fits table") ; - return -1 ; - } - } else { - qfits_error("Unrecognized table type") ; - return -1 ; + /* Append the extension */ + if (t->tab_t == QFITS_BINTABLE) + { + if (qfits_table_append_bin_xtension (outfile, t, data) == -1) + { + qfits_error ("in writing fits table"); + return -1; } - return 0 ; + } + else if (t->tab_t == QFITS_ASCIITABLE) + { + if (qfits_table_append_ascii_xtension (outfile, t, data) == -1) + { + qfits_error ("in writing fits table"); + return -1; + } + } + else + { + qfits_error ("Unrecognized table type"); + return -1; + } + return 0; } /*----------------------------------------------------------------------------*/ @@ -1589,20 +1796,20 @@ int qfits_table_append_xtension( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension_hdr( - FILE * outfile, - qfits_table * t, - void ** data, - qfits_header * hdr) +int +qfits_table_append_xtension_hdr (FILE * outfile, + qfits_table * t, + void **data, qfits_header * hdr) { - /* Write the fits header in the file */ - if (qfits_header_dump(hdr, outfile) == -1) { - qfits_error("cannot dump header in file") ; - return -1 ; - } + /* Write the fits header in the file */ + if (qfits_header_dump (hdr, outfile) == -1) + { + qfits_error ("cannot dump header in file"); + return -1; + } - /* Append the data to the file */ - return qfits_table_append_data(outfile, t, data) ; + /* Append the data to the file */ + return qfits_table_append_data (outfile, t, data); } /*----------------------------------------------------------------------------*/ @@ -1619,30 +1826,29 @@ int qfits_table_append_xtension_hdr( time, or for debugging puposes. */ /*----------------------------------------------------------------------------*/ -char * qfits_table_field_to_string( - qfits_table * table, - int col_id, - int row_id, - int use_zero_scale) +char * +qfits_table_field_to_string (qfits_table * table, + int col_id, int row_id, int use_zero_scale) { - char * str ; - - switch (table->tab_t) { - case QFITS_BINTABLE: - str = qfits_bintable_field_to_string(table, col_id, row_id, - use_zero_scale) ; - break ; + char *str; - case QFITS_ASCIITABLE: - str = qfits_asciitable_field_to_string(table, col_id, row_id, - use_zero_scale) ; - break ; - default: - qfits_error("Table type not recognized") ; - return NULL ; - break ; - } - return str ; + switch (table->tab_t) + { + case QFITS_BINTABLE: + str = qfits_bintable_field_to_string (table, col_id, row_id, + use_zero_scale); + break; + + case QFITS_ASCIITABLE: + str = qfits_asciitable_field_to_string (table, col_id, row_id, + use_zero_scale); + break; + default: + qfits_error ("Table type not recognized"); + return NULL; + break; + } + return str; } /*----------------------------------------------------------------------------*/ @@ -1657,97 +1863,108 @@ char * qfits_table_field_to_string( ASCII tables specific */ /*----------------------------------------------------------------------------*/ -static char * qfits_asciitable_field_to_string( - qfits_table * table, - int col_id, - int row_id, - int use_zero_scale) +static char * +qfits_asciitable_field_to_string (qfits_table * table, + int col_id, int row_id, int use_zero_scale) { - qfits_col * col ; - char * ccol ; - int * icol ; - float * fcol ; - double * dcol ; - char ctmp[512]; - char * stmp ; - int field_size ; - void * field ; - int * selection ; - - /* Test inputs */ - if (table->tab_t != QFITS_ASCIITABLE) return NULL ; - - /* Initialize */ - ctmp[0] = (char)0 ; + qfits_col *col; + char *ccol; + int *icol; + float *fcol; + double *dcol; + char ctmp[512]; + char *stmp; + int field_size; + void *field; + int *selection; - /* Set selection to select the requested row */ - selection = calloc(table->nr, sizeof(int)) ; - selection[row_id] = 1 ; - - /* Load the column data */ - if ((field = qfits_query_column_data(table, col_id, selection, - NULL)) == NULL) return NULL ; - free(selection) ; - - /* Set reference to the column */ - col = table->col + col_id ; - - /* Compute field size and allocate stmp */ - if (col->atom_nb > ELEMENT_MAX_DISPLAY_SIZE) field_size = col->atom_nb + 1 ; - else field_size = ELEMENT_MAX_DISPLAY_SIZE ; - stmp = malloc(field_size * sizeof(char)) ; - stmp[0] = (char)0 ; - - /* Get the string to write according to the type */ - switch(col->atom_type) { - case TFITS_ASCII_TYPE_A: - ccol = (char*)field ; - strncpy(ctmp, ccol, col->atom_nb); - ctmp[col->atom_nb] = (char)0; - strcpy(stmp, ctmp); - break ; - - case TFITS_ASCII_TYPE_I: - icol = (int*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - sprintf(stmp, "%f", (float)(col->zero + - (float)icol[0] * col->scale)) ; - } else { - sprintf(stmp, "%d", icol[0]); - } - break ; - - case TFITS_ASCII_TYPE_E: - case TFITS_ASCII_TYPE_F: - fcol = (float*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - sprintf(stmp, "%f", (float)(col->zero + - fcol[0] * col->scale)) ; - } else { - sprintf(stmp, "%f", fcol[0]); - } - break ; - - case TFITS_ASCII_TYPE_D: - dcol = (double*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - sprintf(stmp, "%f", (float)(col->zero + - (float)dcol[0] * col->scale)) ; - } else { - sprintf(stmp, "%g", dcol[0]) ; - } - break ; - default: - qfits_warning("Type not recognized") ; - break ; + /* Test inputs */ + if (table->tab_t != QFITS_ASCIITABLE) + return NULL; + + /* Initialize */ + ctmp[0] = (char) 0; + + /* Set selection to select the requested row */ + selection = calloc (table->nr, sizeof (int)); + selection[row_id] = 1; + + /* Load the column data */ + if ((field = qfits_query_column_data (table, col_id, selection, + NULL)) == NULL) + return NULL; + free (selection); + + /* Set reference to the column */ + col = table->col + col_id; + + /* Compute field size and allocate stmp */ + if (col->atom_nb > ELEMENT_MAX_DISPLAY_SIZE) + field_size = col->atom_nb + 1; + else + field_size = ELEMENT_MAX_DISPLAY_SIZE; + stmp = malloc (field_size * sizeof (char)); + stmp[0] = (char) 0; + + /* Get the string to write according to the type */ + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + ccol = (char *) field; + strncpy (ctmp, ccol, col->atom_nb); + ctmp[col->atom_nb] = (char) 0; + strcpy (stmp, ctmp); + break; + + case TFITS_ASCII_TYPE_I: + icol = (int *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + sprintf (stmp, "%f", (float) (col->zero + + (float) icol[0] * col->scale)); } + else + { + sprintf (stmp, "%d", icol[0]); + } + break; - /* Free and return */ - free(field) ; - return stmp ; + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + fcol = (float *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + sprintf (stmp, "%f", (float) (col->zero + fcol[0] * col->scale)); + } + else + { + sprintf (stmp, "%f", fcol[0]); + } + break; + + case TFITS_ASCII_TYPE_D: + dcol = (double *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + sprintf (stmp, "%f", (float) (col->zero + + (float) dcol[0] * col->scale)); + } + else + { + sprintf (stmp, "%g", dcol[0]); + } + break; + default: + qfits_warning ("Type not recognized"); + break; + } + + /* Free and return */ + free (field); + return stmp; } /*----------------------------------------------------------------------------*/ @@ -1762,236 +1979,271 @@ static char * qfits_asciitable_field_to_string( BIN tables specific */ /*----------------------------------------------------------------------------*/ -static char * qfits_bintable_field_to_string( - qfits_table * table, - int col_id, - int row_id, - int use_zero_scale) +static char * +qfits_bintable_field_to_string (qfits_table * table, + int col_id, int row_id, int use_zero_scale) { - qfits_col * col ; - unsigned char * uccol ; - char * ccol ; - int * icol ; - short * scol ; - float * fcol ; - double * dcol ; - char ctmp[512]; - char * stmp ; - int field_size ; - void * field ; - int * selection ; + qfits_col *col; + unsigned char *uccol; + char *ccol; + int *icol; + short *scol; + float *fcol; + double *dcol; + char ctmp[512]; + char *stmp; + int field_size; + void *field; + int *selection; - int i ; + int i; - /* Test inputs */ - if (table->tab_t != QFITS_BINTABLE) return NULL ; + /* Test inputs */ + if (table->tab_t != QFITS_BINTABLE) + return NULL; - /* Initialize */ - ctmp[0] = (char)0 ; - - /* Set selection to select the requested row */ - selection = calloc(table->nr, sizeof(int)) ; - selection[row_id] = 1 ; - - /* Load the data column */ - if ((field = qfits_query_column_data(table, col_id, selection, - NULL)) == NULL) { - free(selection) ; - return NULL ; + /* Initialize */ + ctmp[0] = (char) 0; + + /* Set selection to select the requested row */ + selection = calloc (table->nr, sizeof (int)); + selection[row_id] = 1; + + /* Load the data column */ + if ((field = qfits_query_column_data (table, col_id, selection, + NULL)) == NULL) + { + free (selection); + return NULL; } - free(selection) ; - - /* Set reference to the column */ - col = table->col + col_id ; + free (selection); - /* Compute field size and allocate stmp */ - field_size = col->atom_nb * ELEMENT_MAX_DISPLAY_SIZE ; - stmp = malloc(field_size * sizeof(char)) ; - stmp[0] = (char)0 ; - - /* Get the string to write according to the type */ - switch(col->atom_type) { - case TFITS_BIN_TYPE_A: - ccol = (char*)field ; - strncpy(ctmp, ccol, col->atom_size * col->atom_nb) ; - ctmp[col->atom_size*col->atom_nb] = (char)0 ; - strcpy(stmp, ctmp) ; - break ; + /* Set reference to the column */ + col = table->col + col_id; - case TFITS_BIN_TYPE_B: - uccol = (unsigned char*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%f, ", (float)(col->zero + - (float)uccol[i] * col->scale)) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", (float)(col->zero + - (float)uccol[col->atom_nb-1]*col->scale)) ; - strcat(stmp, ctmp) ; - } else { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%d, ", (int)uccol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp,"%d",(int)uccol[col->atom_nb-1]); - strcat(stmp, ctmp) ; - } - break ; + /* Compute field size and allocate stmp */ + field_size = col->atom_nb * ELEMENT_MAX_DISPLAY_SIZE; + stmp = malloc (field_size * sizeof (char)); + stmp[0] = (char) 0; - case TFITS_BIN_TYPE_D: - case TFITS_BIN_TYPE_M: - dcol = (double*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%g, ", (double)((double)col->zero + - dcol[i] * (double)col->scale)) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%g", (double)((double)col->zero + - dcol[col->atom_nb-1] * (double)col->scale)); - strcat(stmp, ctmp) ; - } else { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%g, ", dcol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%g", dcol[col->atom_nb-1]) ; - strcat(stmp, ctmp) ; - } - break ; + /* Get the string to write according to the type */ + switch (col->atom_type) + { + case TFITS_BIN_TYPE_A: + ccol = (char *) field; + strncpy (ctmp, ccol, col->atom_size * col->atom_nb); + ctmp[col->atom_size * col->atom_nb] = (char) 0; + strcpy (stmp, ctmp); + break; - case TFITS_BIN_TYPE_E: - case TFITS_BIN_TYPE_C: - fcol = (float*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%f, ", (float)(col->zero + - (float)fcol[i] * col->scale)) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", (float)(col->zero + - (float)fcol[col->atom_nb-1] * col->scale)) ; - strcat(stmp, ctmp) ; - } else { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%f, ", fcol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", fcol[col->atom_nb-1]) ; - strcat(stmp, ctmp) ; - } - break ; + case TFITS_BIN_TYPE_B: + uccol = (unsigned char *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%f, ", (float) (col->zero + + (float) uccol[i] * col->scale)); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", (float) (col->zero + + (float) uccol[col->atom_nb - + 1] * col->scale)); + strcat (stmp, ctmp); + } + else + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%d, ", (int) uccol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", (int) uccol[col->atom_nb - 1]); + strcat (stmp, ctmp); + } + break; - case TFITS_BIN_TYPE_I: - scol = (short*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%f, ", (float)(col->zero + - (float)scol[i] * col->scale)) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", (float)(col->zero + - (float)scol[col->atom_nb-1] * col->scale)) ; - strcat(stmp, ctmp) ; - } else { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%d, ", (int)scol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d",(int)scol[col->atom_nb-1]); - strcat(stmp, ctmp) ; - } - break ; + case TFITS_BIN_TYPE_D: + case TFITS_BIN_TYPE_M: + dcol = (double *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%g, ", (double) ((double) col->zero + + dcol[i] * + (double) col->scale)); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%g", (double) ((double) col->zero + + dcol[col->atom_nb - + 1] * (double) col->scale)); + strcat (stmp, ctmp); + } + else + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%g, ", dcol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%g", dcol[col->atom_nb - 1]); + strcat (stmp, ctmp); + } + break; - case TFITS_BIN_TYPE_J: - icol = (int*)field ; - /* Two cases: use col->zero and col->scale or not */ - if (col->zero_present && col->scale_present && use_zero_scale) { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%f, ", (float)(col->zero + - (float)icol[i] * col->scale)) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%f", (float)(col->zero + - (float)icol[col->atom_nb-1] * col->scale)) ; - strcat(stmp, ctmp) ; - } else { - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%d, ", (int)icol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d",(int)icol[col->atom_nb-1]); - strcat(stmp, ctmp) ; - } - break ; + case TFITS_BIN_TYPE_E: + case TFITS_BIN_TYPE_C: + fcol = (float *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%f, ", (float) (col->zero + + (float) fcol[i] * col->scale)); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", (float) (col->zero + + (float) fcol[col->atom_nb - + 1] * col->scale)); + strcat (stmp, ctmp); + } + else + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%f, ", fcol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", fcol[col->atom_nb - 1]); + strcat (stmp, ctmp); + } + break; - case TFITS_BIN_TYPE_L: - ccol = (char*)field ; - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%c, ", ccol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%c", ccol[col->atom_nb-1]) ; - strcat(stmp, ctmp) ; - break ; + case TFITS_BIN_TYPE_I: + scol = (short *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%f, ", (float) (col->zero + + (float) scol[i] * col->scale)); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", (float) (col->zero + + (float) scol[col->atom_nb - + 1] * col->scale)); + strcat (stmp, ctmp); + } + else + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%d, ", (int) scol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", (int) scol[col->atom_nb - 1]); + strcat (stmp, ctmp); + } + break; - case TFITS_BIN_TYPE_X: - uccol = (unsigned char*)field ; - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%d, ", uccol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d", uccol[col->atom_nb-1]) ; - strcat(stmp, ctmp) ; - break ; + case TFITS_BIN_TYPE_J: + icol = (int *) field; + /* Two cases: use col->zero and col->scale or not */ + if (col->zero_present && col->scale_present && use_zero_scale) + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%f, ", (float) (col->zero + + (float) icol[i] * col->scale)); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%f", (float) (col->zero + + (float) icol[col->atom_nb - + 1] * col->scale)); + strcat (stmp, ctmp); + } + else + { + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%d, ", (int) icol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", (int) icol[col->atom_nb - 1]); + strcat (stmp, ctmp); + } + break; - case TFITS_BIN_TYPE_P: - icol = (int*)field ; - /* For each atom of the column */ - for (i=0 ; iatom_nb-1 ; i++) { - sprintf(ctmp, "%d, ", (int)icol[i]) ; - strcat(stmp, ctmp) ; - } - /* Handle the last atom differently: no ',' */ - sprintf(ctmp, "%d",(int)icol[col->atom_nb-1]); - strcat(stmp, ctmp) ; - break ; + case TFITS_BIN_TYPE_L: + ccol = (char *) field; + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%c, ", ccol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%c", ccol[col->atom_nb - 1]); + strcat (stmp, ctmp); + break; - default: - qfits_warning("Type not recognized") ; - break ; + case TFITS_BIN_TYPE_X: + uccol = (unsigned char *) field; + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%d, ", uccol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", uccol[col->atom_nb - 1]); + strcat (stmp, ctmp); + break; + + case TFITS_BIN_TYPE_P: + icol = (int *) field; + /* For each atom of the column */ + for (i = 0; i < col->atom_nb - 1; i++) + { + sprintf (ctmp, "%d, ", (int) icol[i]); + strcat (stmp, ctmp); + } + /* Handle the last atom differently: no ',' */ + sprintf (ctmp, "%d", (int) icol[col->atom_nb - 1]); + strcat (stmp, ctmp); + break; + + default: + qfits_warning ("Type not recognized"); + break; } - free(field) ; - return stmp ; + free (field); + return stmp; } /*----------------------------------------------------------------------------*/ @@ -2008,26 +2260,30 @@ static char * qfits_bintable_field_to_string( each function call (not re-entrant). */ /*----------------------------------------------------------------------------*/ -static char * qfits_strstrip(char * s) +static char * +qfits_strstrip (char *s) { - static char l[ASCIILINESZ+1]; - char * last ; + static char l[ASCIILINESZ + 1]; + char *last; - if (s==NULL) return NULL ; + if (s == NULL) + return NULL; - while (isspace((int)*s) && *s) s++; + while (isspace ((int) *s) && *s) + s++; - memset(l, 0, ASCIILINESZ+1); - strcpy(l, s); - last = l + strlen(l); - while (last > l) { - if (!isspace((int)*(last-1))) - break ; - last -- ; + memset (l, 0, ASCIILINESZ + 1); + strcpy (l, s); + last = l + strlen (l); + while (last > l) + { + if (!isspace ((int) *(last - 1))) + break; + last--; } - *last = (char)0; + *last = (char) 0; - return (char*)l ; + return (char *) l; } /*----------------------------------------------------------------------------*/ @@ -2040,22 +2296,24 @@ static char * qfits_strstrip(char * s) This is handled by this function. */ /*----------------------------------------------------------------------------*/ -static double qfits_str2dec( - char * to_format, - int nb_dec) +static double +qfits_str2dec (char *to_format, int nb_dec) { - double val ; - int i ; - - /* Test entries */ - if (to_format == NULL) return 0.00 ; - - val = (double)atof(to_format) ; - /* First handle case where there are no decimals or the dot is there */ - if ((strstr(to_format, ".") == NULL) && (nb_dec > 0)) { - for (i=0 ; i 0)) + { + for (i = 0; i < nb_dec; i++) + val /= 10; } - return val ; + return val; } /*----------------------------------------------------------------------------*/ @@ -2071,57 +2329,99 @@ static double qfits_str2dec( This functions reads the input string and uses it to update nb and type */ /*----------------------------------------------------------------------------*/ -static int qfits_table_interpret_type( - char * str, - int * nb, - int * dec_nb, - tfits_type * type, - int table_type) +static int +qfits_table_interpret_type (char *str, + int *nb, + int *dec_nb, tfits_type * type, int table_type) { - char type_c ; - - *dec_nb = 0 ; - if (table_type == QFITS_BINTABLE) { - if (sscanf(str, "%d%c", nb, &type_c) == 0) { - /* nb is 1 by default */ - if (sscanf(str, "%c", &type_c) == 0) { - qfits_error("cannot interpret this type: %s", str) ; - return -1 ; - } - *nb = 1 ; - } - switch(type_c) { - case 'A': *type = TFITS_BIN_TYPE_A ; break ; - case 'B': *type = TFITS_BIN_TYPE_B ; break ; - case 'C': *type = TFITS_BIN_TYPE_C ; break ; - case 'D': *type = TFITS_BIN_TYPE_D ; break ; - case 'E': *type = TFITS_BIN_TYPE_E ; break ; - case 'I': *type = TFITS_BIN_TYPE_I ; break ; - case 'J': *type = TFITS_BIN_TYPE_J ; break ; - case 'L': *type = TFITS_BIN_TYPE_L ; break ; - case 'M': *type = TFITS_BIN_TYPE_M ; break ; - case 'P': *type = TFITS_BIN_TYPE_P ; break ; - case 'X': *type = TFITS_BIN_TYPE_X ; break ; - default: return -1 ; - } - } else if (table_type == QFITS_ASCIITABLE) { - if (sscanf(str, "%c%d.%d", &type_c, nb, dec_nb) == 0) { - qfits_error("cannot interpret this type: %s", str) ; - return -1 ; - } - switch(type_c) { - case 'A': *type = TFITS_ASCII_TYPE_A ; break ; - case 'D': *type = TFITS_ASCII_TYPE_D ; break ; - case 'E': *type = TFITS_ASCII_TYPE_E ; break ; - case 'F': *type = TFITS_ASCII_TYPE_F ; break ; - case 'I': *type = TFITS_ASCII_TYPE_I ; break ; - default: return -1 ; - } - } else { - qfits_error("unrecognized table type") ; - return -1 ; + char type_c; + + *dec_nb = 0; + if (table_type == QFITS_BINTABLE) + { + if (sscanf (str, "%d%c", nb, &type_c) == 0) + { + /* nb is 1 by default */ + if (sscanf (str, "%c", &type_c) == 0) + { + qfits_error ("cannot interpret this type: %s", str); + return -1; + } + *nb = 1; } - return 0 ; + switch (type_c) + { + case 'A': + *type = TFITS_BIN_TYPE_A; + break; + case 'B': + *type = TFITS_BIN_TYPE_B; + break; + case 'C': + *type = TFITS_BIN_TYPE_C; + break; + case 'D': + *type = TFITS_BIN_TYPE_D; + break; + case 'E': + *type = TFITS_BIN_TYPE_E; + break; + case 'I': + *type = TFITS_BIN_TYPE_I; + break; + case 'J': + *type = TFITS_BIN_TYPE_J; + break; + case 'L': + *type = TFITS_BIN_TYPE_L; + break; + case 'M': + *type = TFITS_BIN_TYPE_M; + break; + case 'P': + *type = TFITS_BIN_TYPE_P; + break; + case 'X': + *type = TFITS_BIN_TYPE_X; + break; + default: + return -1; + } + } + else if (table_type == QFITS_ASCIITABLE) + { + if (sscanf (str, "%c%d.%d", &type_c, nb, dec_nb) == 0) + { + qfits_error ("cannot interpret this type: %s", str); + return -1; + } + switch (type_c) + { + case 'A': + *type = TFITS_ASCII_TYPE_A; + break; + case 'D': + *type = TFITS_ASCII_TYPE_D; + break; + case 'E': + *type = TFITS_ASCII_TYPE_E; + break; + case 'F': + *type = TFITS_ASCII_TYPE_F; + break; + case 'I': + *type = TFITS_ASCII_TYPE_I; + break; + default: + return -1; + } + } + else + { + qfits_error ("unrecognized table type"); + return -1; + } + return 0; } /*----------------------------------------------------------------------------*/ @@ -2131,41 +2431,67 @@ static int qfits_table_interpret_type( @return The string to write to TFORM */ /*----------------------------------------------------------------------------*/ -static char * qfits_build_format(qfits_col * col) +static char * +qfits_build_format (qfits_col * col) { - static char sval[10] ; - int nb ; - - switch (col->atom_type) { - case TFITS_ASCII_TYPE_A: - nb=sprintf(sval, "A%d.%d", col->atom_nb, col->atom_dec_nb) ; break ; - case TFITS_ASCII_TYPE_D: - nb=sprintf(sval, "D%d.%d", col->atom_nb, col->atom_dec_nb) ; break ; - case TFITS_ASCII_TYPE_E: - nb=sprintf(sval, "E%d.%d", col->atom_nb, col->atom_dec_nb) ; break ; - case TFITS_ASCII_TYPE_I: - nb=sprintf(sval, "I%d.%d", col->atom_nb, col->atom_dec_nb) ; break ; - case TFITS_ASCII_TYPE_F: - nb=sprintf(sval, "F%d.%d", col->atom_nb, col->atom_dec_nb) ; break ; - case TFITS_BIN_TYPE_D: nb=sprintf(sval, "%dD", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_E: nb=sprintf(sval, "%dE", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_I: nb=sprintf(sval, "%dI", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_A: nb=sprintf(sval, "%dA", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_B: nb=sprintf(sval, "%dB", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_C: nb=sprintf(sval, "%dC", - (int)(col->atom_nb/2)) ; break ; - case TFITS_BIN_TYPE_J: nb=sprintf(sval, "%dJ", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_L: nb=sprintf(sval, "%dL", col->atom_nb) ; break ; - case TFITS_BIN_TYPE_M: nb=sprintf(sval, "%dM", - (int)(col->atom_nb/2)) ; break ; - case TFITS_BIN_TYPE_P: nb=sprintf(sval, "%dP", - (int)(col->atom_nb/2)) ; break ; - case TFITS_BIN_TYPE_X: nb=sprintf(sval, "%dX", - 8*col->atom_nb) ; break ; - default: return NULL ; - } - sval[nb] = (char)0 ; - return sval ; + static char sval[10]; + int nb; + + switch (col->atom_type) + { + case TFITS_ASCII_TYPE_A: + nb = sprintf (sval, "A%d.%d", col->atom_nb, col->atom_dec_nb); + break; + case TFITS_ASCII_TYPE_D: + nb = sprintf (sval, "D%d.%d", col->atom_nb, col->atom_dec_nb); + break; + case TFITS_ASCII_TYPE_E: + nb = sprintf (sval, "E%d.%d", col->atom_nb, col->atom_dec_nb); + break; + case TFITS_ASCII_TYPE_I: + nb = sprintf (sval, "I%d.%d", col->atom_nb, col->atom_dec_nb); + break; + case TFITS_ASCII_TYPE_F: + nb = sprintf (sval, "F%d.%d", col->atom_nb, col->atom_dec_nb); + break; + case TFITS_BIN_TYPE_D: + nb = sprintf (sval, "%dD", col->atom_nb); + break; + case TFITS_BIN_TYPE_E: + nb = sprintf (sval, "%dE", col->atom_nb); + break; + case TFITS_BIN_TYPE_I: + nb = sprintf (sval, "%dI", col->atom_nb); + break; + case TFITS_BIN_TYPE_A: + nb = sprintf (sval, "%dA", col->atom_nb); + break; + case TFITS_BIN_TYPE_B: + nb = sprintf (sval, "%dB", col->atom_nb); + break; + case TFITS_BIN_TYPE_C: + nb = sprintf (sval, "%dC", (int) (col->atom_nb / 2)); + break; + case TFITS_BIN_TYPE_J: + nb = sprintf (sval, "%dJ", col->atom_nb); + break; + case TFITS_BIN_TYPE_L: + nb = sprintf (sval, "%dL", col->atom_nb); + break; + case TFITS_BIN_TYPE_M: + nb = sprintf (sval, "%dM", (int) (col->atom_nb / 2)); + break; + case TFITS_BIN_TYPE_P: + nb = sprintf (sval, "%dP", (int) (col->atom_nb / 2)); + break; + case TFITS_BIN_TYPE_X: + nb = sprintf (sval, "%dX", 8 * col->atom_nb); + break; + default: + return NULL; + } + sval[nb] = (char) 0; + return sval; } /*----------------------------------------------------------------------------*/ @@ -2184,29 +2510,29 @@ static char * qfits_build_format(qfits_col * col) Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -static int qfits_table_append_bin_xtension( - FILE * outfile, - qfits_table * t, - void ** data) +static int +qfits_table_append_bin_xtension (FILE * outfile, qfits_table * t, void **data) { - qfits_header * fh ; + qfits_header *fh; - if ((fh=qfits_table_ext_header_default(t)) == NULL) { - qfits_error("cannot create new fits header") ; - return -1 ; - } + if ((fh = qfits_table_ext_header_default (t)) == NULL) + { + qfits_error ("cannot create new fits header"); + return -1; + } - /* Write the fits header in the file */ - if (qfits_header_dump(fh, outfile) == -1) { - qfits_error("cannot dump header in file") ; - qfits_header_destroy(fh) ; - fclose(outfile) ; - return -1 ; - } - qfits_header_destroy(fh) ; + /* Write the fits header in the file */ + if (qfits_header_dump (fh, outfile) == -1) + { + qfits_error ("cannot dump header in file"); + qfits_header_destroy (fh); + fclose (outfile); + return -1; + } + qfits_header_destroy (fh); - /* Append the data to the file */ - return qfits_table_append_data(outfile, t, data) ; + /* Append the data to the file */ + return qfits_table_append_data (outfile, t, data); } /*----------------------------------------------------------------------------*/ @@ -2227,29 +2553,30 @@ static int qfits_table_append_bin_xtension( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -static int qfits_table_append_ascii_xtension( - FILE * outfile, - qfits_table * t, - void ** data) +static int +qfits_table_append_ascii_xtension (FILE * outfile, + qfits_table * t, void **data) { - qfits_header * fh ; - - if ((fh=qfits_table_ext_header_default(t)) == NULL) { - qfits_error("cannot create new fits header") ; - return -1 ; - } + qfits_header *fh; - /* Write the fits header in the file */ - if (qfits_header_dump(fh, outfile) == -1) { - qfits_error("cannot dump header in file") ; - qfits_header_destroy(fh) ; - return -1 ; - } - qfits_header_destroy(fh) ; - - /* Append the data to the file */ - return qfits_table_append_data(outfile, t, data) ; -} + if ((fh = qfits_table_ext_header_default (t)) == NULL) + { + qfits_error ("cannot create new fits header"); + return -1; + } + + /* Write the fits header in the file */ + if (qfits_header_dump (fh, outfile) == -1) + { + qfits_error ("cannot dump header in file"); + qfits_header_destroy (fh); + return -1; + } + qfits_header_destroy (fh); + + /* Append the data to the file */ + return qfits_table_append_data (outfile, t, data); +} /*----------------------------------------------------------------------------*/ /** @@ -2264,116 +2591,130 @@ static int qfits_table_append_ascii_xtension( The output is then padded to reach a multiple of 2880 bytes in size. */ /*----------------------------------------------------------------------------*/ -static int qfits_table_append_data( - FILE * outfile, - qfits_table * t, - void ** data) +static int +qfits_table_append_data (FILE * outfile, qfits_table * t, void **data) { - qfits_col * curr_col ; - char field[1024] ; - char * line ; - unsigned char ** array ; - unsigned char * r ; - unsigned char * inbuf ; - int writt_char ; - int nb_blanks ; - int field_size ; - int i, j ; + qfits_col *curr_col; + char field[1024]; + char *line; + unsigned char **array; + unsigned char *r; + unsigned char *inbuf; + int writt_char; + int nb_blanks; + int field_size; + int i, j; - /* Write DATA */ - array = malloc(t->nc*sizeof(unsigned char *)) ; + /* Write DATA */ + array = malloc (t->nc * sizeof (unsigned char *)); - curr_col = t->col ; - for (i=0 ; inc ; i++) { - /* Compute the field size */ - field_size = qfits_table_get_field_size(t->tab_t, curr_col) ; + curr_col = t->col; + for (i = 0; i < t->nc; i++) + { + /* Compute the field size */ + field_size = qfits_table_get_field_size (t->tab_t, curr_col); - /* Copy data from data to array (unsigned char) */ - array[i] = malloc(t->nr * field_size) ; - r = (unsigned char *)array[i] ; - inbuf = (unsigned char *)(data[i]) ; + /* Copy data from data to array (unsigned char) */ + array[i] = malloc (t->nr * field_size); + r = (unsigned char *) array[i]; + inbuf = (unsigned char *) (data[i]); - /* Copy the data */ - if (t->tab_t == QFITS_ASCIITABLE) { - /* ASCII table */ - for (j=0 ; jnr ; j++) { - switch(curr_col->atom_type) { - case TFITS_ASCII_TYPE_A : - strncpy(field, (char*)inbuf, curr_col->atom_nb) ; - field[curr_col->atom_nb] = (char)0 ; - inbuf += curr_col->atom_nb ; - break ; - case TFITS_ASCII_TYPE_D : - memset(field, ' ', curr_col->atom_nb) ; - sprintf(field, "%g", ((double*)data[i])[j]) ; - field[curr_col->atom_nb] = (char)0 ; - break ; - case TFITS_ASCII_TYPE_E : - case TFITS_ASCII_TYPE_F : - memset(field, ' ', curr_col->atom_nb) ; - sprintf(field, "%f", ((float*)data[i])[j]) ; - field[curr_col->atom_nb] = (char)0 ; - break ; - case TFITS_ASCII_TYPE_I : - memset(field, ' ', curr_col->atom_nb) ; - sprintf(field, "%d", ((int*)data[i])[j]) ; - field[curr_col->atom_nb] = (char)0 ; - break ; - default: - break ; - } - memcpy(r, field, curr_col->atom_nb) ; - r += (curr_col->atom_nb) ; - } - } else if (t->tab_t == QFITS_BINTABLE) { - /* BINARY table */ - for (j=0 ; jnr ; j++) { - memcpy(r, inbuf, field_size) ; - inbuf += field_size ; - r += field_size ; - } + /* Copy the data */ + if (t->tab_t == QFITS_ASCIITABLE) + { + /* ASCII table */ + for (j = 0; j < t->nr; j++) + { + switch (curr_col->atom_type) + { + case TFITS_ASCII_TYPE_A: + strncpy (field, (char *) inbuf, curr_col->atom_nb); + field[curr_col->atom_nb] = (char) 0; + inbuf += curr_col->atom_nb; + break; + case TFITS_ASCII_TYPE_D: + memset (field, ' ', curr_col->atom_nb); + sprintf (field, "%g", ((double *) data[i])[j]); + field[curr_col->atom_nb] = (char) 0; + break; + case TFITS_ASCII_TYPE_E: + case TFITS_ASCII_TYPE_F: + memset (field, ' ', curr_col->atom_nb); + sprintf (field, "%f", ((float *) data[i])[j]); + field[curr_col->atom_nb] = (char) 0; + break; + case TFITS_ASCII_TYPE_I: + memset (field, ' ', curr_col->atom_nb); + sprintf (field, "%d", ((int *) data[i])[j]); + field[curr_col->atom_nb] = (char) 0; + break; + default: + break; + } + memcpy (r, field, curr_col->atom_nb); + r += (curr_col->atom_nb); + } + } + else if (t->tab_t == QFITS_BINTABLE) + { + /* BINARY table */ + for (j = 0; j < t->nr; j++) + { + memcpy (r, inbuf, field_size); + inbuf += field_size; + r += field_size; + } - /* Byte swapping needed if on a little-endian machine */ + /* Byte swapping needed if on a little-endian machine */ #ifndef WORDS_BIGENDIAN - r = array[i] ; - for (j=0 ; jnr * curr_col->atom_nb ; j++) { - swap_bytes(r, curr_col->atom_size); - r += curr_col->atom_size ; - } + r = array[i]; + for (j = 0; j < t->nr * curr_col->atom_nb; j++) + { + swap_bytes (r, curr_col->atom_size); + r += curr_col->atom_size; + } #endif - } else return -1 ; - curr_col++ ; + } + else + return -1; + curr_col++; } - /* Write to the outfile */ - writt_char = 0 ; - for (i=0 ; inr ; i++) { - curr_col = t->col ; - for (j=0 ; jnc ; j++) { - field_size = qfits_table_get_field_size(t->tab_t, curr_col) ; - r = array[j] + field_size * i ; - line = (char *)calloc (field_size+1, sizeof (char)) ; - memcpy(line, r, field_size) ; - line[field_size] = (char)0 ; - fwrite(line, 1, field_size, outfile) ; - writt_char += field_size ; - curr_col++ ; - free(line) ; - } + /* Write to the outfile */ + writt_char = 0; + for (i = 0; i < t->nr; i++) + { + curr_col = t->col; + for (j = 0; j < t->nc; j++) + { + field_size = qfits_table_get_field_size (t->tab_t, curr_col); + r = array[j] + field_size * i; + line = (char *) calloc (field_size + 1, sizeof (char)); + memcpy (line, r, field_size); + line[field_size] = (char) 0; + fwrite (line, 1, field_size, outfile); + writt_char += field_size; + curr_col++; + free (line); + } } - /* Complete with blanks to FITS_BLOCK_SIZE characters */ - if (writt_char % FITS_BLOCK_SIZE) { - nb_blanks = FITS_BLOCK_SIZE - (writt_char%FITS_BLOCK_SIZE) ; - for (i=1 ; i<=nb_blanks ; i++) fwrite(" ", 1, 1, outfile) ; + /* Complete with blanks to FITS_BLOCK_SIZE characters */ + if (writt_char % FITS_BLOCK_SIZE) + { + nb_blanks = FITS_BLOCK_SIZE - (writt_char % FITS_BLOCK_SIZE); + for (i = 1; i <= nb_blanks; i++) + fwrite (" ", 1, 1, outfile); } - /* Free and return */ - for(i=0 ; inc ; i++) { - if (array[i] != NULL) free(array[i]) ; + /* Free and return */ + for (i = 0; i < t->nc; i++) + { + if (array[i] != NULL) + free (array[i]); } - free(array) ; - return 0 ; + free (array); + return 0; } /*----------------------------------------------------------------------------*/ @@ -2384,22 +2725,22 @@ static int qfits_table_append_data( @return the size */ /*----------------------------------------------------------------------------*/ -static int qfits_table_get_field_size( - int type, - qfits_col * col) +static int +qfits_table_get_field_size (int type, qfits_col * col) { - int field_size ; + int field_size; - switch (type) { - case QFITS_BINTABLE: - field_size = col->atom_nb * col->atom_size ; - break ; - case QFITS_ASCIITABLE: - field_size = col->atom_nb ; - break ; - default: - qfits_warning("unrecognized table type") ; - field_size = -1 ; - } - return field_size ; + switch (type) + { + case QFITS_BINTABLE: + field_size = col->atom_nb * col->atom_size; + break; + case QFITS_ASCIITABLE: + field_size = col->atom_nb; + break; + default: + qfits_warning ("unrecognized table type"); + field_size = -1; + } + return field_size; } diff --git a/qfits/src/tfits.h b/qfits/src/tfits.h index d5daba4..0d8d80c 100644 --- a/qfits/src/tfits.h +++ b/qfits/src/tfits.h @@ -19,7 +19,8 @@ #define TFITS_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*----------------------------------------------------------------------------- @@ -30,7 +31,7 @@ extern "C" { #include #include #include - + #include "fits_h.h" #include "static_sz.h" @@ -53,27 +54,28 @@ extern "C" { /*----------------------------------------------------------------------------*/ /** @brief Column data type - */ + */ /*----------------------------------------------------------------------------*/ -typedef enum _TFITS_DATA_TYPE_ { - TFITS_ASCII_TYPE_A, - TFITS_ASCII_TYPE_D, - TFITS_ASCII_TYPE_E, - TFITS_ASCII_TYPE_F, - TFITS_ASCII_TYPE_I, - TFITS_BIN_TYPE_A, - TFITS_BIN_TYPE_B, - TFITS_BIN_TYPE_C, - TFITS_BIN_TYPE_D, - TFITS_BIN_TYPE_E, - TFITS_BIN_TYPE_I, - TFITS_BIN_TYPE_J, - TFITS_BIN_TYPE_L, - TFITS_BIN_TYPE_M, - TFITS_BIN_TYPE_P, - TFITS_BIN_TYPE_X, - TFITS_BIN_TYPE_UNKNOWN -} tfits_type ; + typedef enum _TFITS_DATA_TYPE_ + { + TFITS_ASCII_TYPE_A, + TFITS_ASCII_TYPE_D, + TFITS_ASCII_TYPE_E, + TFITS_ASCII_TYPE_F, + TFITS_ASCII_TYPE_I, + TFITS_BIN_TYPE_A, + TFITS_BIN_TYPE_B, + TFITS_BIN_TYPE_C, + TFITS_BIN_TYPE_D, + TFITS_BIN_TYPE_E, + TFITS_BIN_TYPE_I, + TFITS_BIN_TYPE_J, + TFITS_BIN_TYPE_L, + TFITS_BIN_TYPE_M, + TFITS_BIN_TYPE_P, + TFITS_BIN_TYPE_X, + TFITS_BIN_TYPE_UNKNOWN + } tfits_type; /*----------------------------------------------------------------------------*/ /** @@ -87,8 +89,8 @@ typedef enum _TFITS_DATA_TYPE_ { generate a FITS table. */ /*----------------------------------------------------------------------------*/ -typedef struct qfits_col -{ + typedef struct qfits_col + { /** Number of atoms in one field. In ASCII tables, it is the number of characters in the field as defined @@ -97,13 +99,13 @@ typedef struct qfits_col it is the number of characters. A field with two complex object will have atom_nb = 4. */ - int atom_nb ; + int atom_nb; /** Number of decimals in a ASCII field. This value is always 0 for BIN tables */ - int atom_dec_nb ; + int atom_dec_nb; /** Size of one element in bytes. In ASCII tables, atom_size is the size @@ -124,44 +126,44 @@ typedef struct qfits_col conversion of the field is atom_size. In BIN tables, the size in bytes of a field is always atom_nb*atom_size. */ - int atom_size ; - + int atom_size; + /** Type of data in the column as specified in TFORM keyword In ASCII tables : TFITS_ASCII_TYPE_* with *=A, I, F, E or D In BIN tables : TFITS_BIN_TYPE_* with *=L, X, B, I, J, A, E, D, C, M or P */ - tfits_type atom_type ; + tfits_type atom_type; /** Label of the column */ - char tlabel[FITSVALSZ] ; + char tlabel[FITSVALSZ]; /** Unit of the data */ - char tunit[FITSVALSZ] ; - + char tunit[FITSVALSZ]; + /** Null value */ - char nullval[FITSVALSZ] ; + char nullval[FITSVALSZ]; /** Display format */ - char tdisp[FITSVALSZ] ; - + char tdisp[FITSVALSZ]; + /** zero and scale are used when the quantity in the field does not represent a true physical quantity. Basically, thez should be used when they are present: physical_value = zero + scale * field_value They are read from TZERO and TSCAL in the header */ - int zero_present ; - float zero ; - int scale_present ; - float scale ; + int zero_present; + float zero; + int scale_present; + float scale; /** Offset between the beg. of the table and the beg. of the column. */ - int off_beg ; - + int off_beg; + /** Flag to know if the column is readable. An empty col is not readable */ - int readable ; -} qfits_col ; + int readable; + } qfits_col; /*----------------------------------------------------------------------------*/ @@ -195,26 +197,26 @@ typedef struct qfits_col @endcode */ /*----------------------------------------------------------------------------*/ -typedef struct qfits_table -{ + typedef struct qfits_table + { /** Name of the file the table comes from or it is intended to end to */ - char filename[FILENAMESZ] ; + char filename[FILENAMESZ]; /** Table type. Possible values: QFITS_INVALIDTABLE, QFITS_BINTABLE, QFITS_ASCIITABLE */ - int tab_t ; + int tab_t; /** Width in bytes of the table */ - int tab_w ; + int tab_w; /** Number of columns */ - int nc ; + int nc; /** Number of raws */ - int nr ; + int nr; /** Array of qfits_col objects */ - qfits_col * col ; -} qfits_table ; + qfits_col *col; + } qfits_table; /*----------------------------------------------------------------------------- Function prototypes @@ -231,7 +233,7 @@ typedef struct qfits_table FITS table. */ /*----------------------------------------------------------------------------*/ -int qfits_is_table(char * filename, int xtnum) ; + int qfits_is_table (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -239,7 +241,7 @@ int qfits_is_table(char * filename, int xtnum) ; @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_prim_header_default(void) ; + qfits_header *qfits_table_prim_header_default (void); /*----------------------------------------------------------------------------*/ /** @@ -247,7 +249,7 @@ qfits_header * qfits_table_prim_header_default(void) ; @return the header object */ /*----------------------------------------------------------------------------*/ -qfits_header * qfits_table_ext_header_default(qfits_table *) ; + qfits_header *qfits_table_ext_header_default (qfits_table *); /*----------------------------------------------------------------------------*/ /** @@ -262,12 +264,9 @@ qfits_header * qfits_table_ext_header_default(qfits_table *) ; qfits_table_close() */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_new( - char * filename, - int table_type, - int table_width, - int nb_cols, - int nb_raws) ; + qfits_table *qfits_table_new (char *filename, + int table_type, + int table_width, int nb_cols, int nb_raws); /*----------------------------------------------------------------------------*/ /** @@ -293,21 +292,18 @@ qfits_table * qfits_table_new( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_col_fill( - qfits_col * qc, - int atom_nb, - int atom_dec_nb, - int atom_size, - tfits_type atom_type, - char * label, - char * unit, - char * nullval, - char * disp, - int zero_present, - float zero, - int scale_present, - float scale, - int offset_beg) ; + int qfits_col_fill (qfits_col * qc, + int atom_nb, + int atom_dec_nb, + int atom_size, + tfits_type atom_type, + char *label, + char *unit, + char *nullval, + char *disp, + int zero_present, + float zero, + int scale_present, float scale, int offset_beg); /*----------------------------------------------------------------------------*/ /** @@ -320,9 +316,7 @@ int qfits_col_fill( newly allocated qfits_table structure. */ /*----------------------------------------------------------------------------*/ -qfits_table * qfits_table_open( - char * filename, - int xtnum) ; + qfits_table *qfits_table_open (char *filename, int xtnum); /*----------------------------------------------------------------------------*/ /** @@ -332,7 +326,7 @@ qfits_table * qfits_table_open( Frees all memory associated to a qfits_table structure. */ /*----------------------------------------------------------------------------*/ -void qfits_table_close(qfits_table * t) ; + void qfits_table_close (qfits_table * t); /*----------------------------------------------------------------------------*/ /** @@ -363,10 +357,8 @@ void qfits_table_close(qfits_table * t) ; the pixel loader here. */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column( - qfits_table * th, - int colnum, - int * selection) ; + unsigned char *qfits_query_column (qfits_table * th, + int colnum, int *selection); /*----------------------------------------------------------------------------*/ /** @@ -380,11 +372,9 @@ unsigned char * qfits_query_column( Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -unsigned char * qfits_query_column_seq( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows) ; + unsigned char *qfits_query_column_seq (qfits_table * th, + int colnum, + int start_ind, int nb_rows); /*----------------------------------------------------------------------------*/ /** @@ -393,7 +383,7 @@ unsigned char * qfits_query_column_seq( @return the width (-1 in error case) */ /*----------------------------------------------------------------------------*/ -int qfits_compute_table_width(qfits_table * th) ; + int qfits_compute_table_width (qfits_table * th); /*----------------------------------------------------------------------------*/ /** @@ -420,11 +410,9 @@ int qfits_compute_table_width(qfits_table * th) ; the pixel loader here. */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_data( - qfits_table * th, - int colnum, - int * selection, - void * null_value) ; + void *qfits_query_column_data (qfits_table * th, + int colnum, + int *selection, void *null_value); /*----------------------------------------------------------------------------*/ /** @@ -439,12 +427,10 @@ void * qfits_query_column_data( of rows. Spares the overhead of the selection object allocation */ /*----------------------------------------------------------------------------*/ -void * qfits_query_column_seq_data( - qfits_table * th, - int colnum, - int start_ind, - int nb_rows, - void * null_value) ; + void *qfits_query_column_seq_data (qfits_table * th, + int colnum, + int start_ind, + int nb_rows, void *null_value); /*----------------------------------------------------------------------------*/ /** @@ -457,12 +443,9 @@ void * qfits_query_column_seq_data( @return array with 1 for NULLs and 0 for non-NULLs */ /*----------------------------------------------------------------------------*/ -int * qfits_query_column_nulls( - qfits_table * th, - int colnum, - int * selection, - int * nb_vals, - int * nb_nulls) ; + int *qfits_query_column_nulls (qfits_table * th, + int colnum, + int *selection, int *nb_vals, int *nb_nulls); /*----------------------------------------------------------------------------*/ /** @@ -473,10 +456,8 @@ int * qfits_query_column_nulls( @return -1 in error case, 0 otherwise */ /*----------------------------------------------------------------------------*/ -int qfits_save_table_hdrdump( - void ** array, - qfits_table * table, - qfits_header * fh) ; + int qfits_save_table_hdrdump (void **array, + qfits_table * table, qfits_header * fh); /*----------------------------------------------------------------------------*/ /** @@ -494,10 +475,8 @@ int qfits_save_table_hdrdump( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension( - FILE * outfile, - qfits_table * t, - void ** data) ; + int qfits_table_append_xtension (FILE * outfile, + qfits_table * t, void **data); /*----------------------------------------------------------------------------*/ /** @@ -515,11 +494,9 @@ int qfits_table_append_xtension( Notice that no main header is produced, only the extension part. */ /*----------------------------------------------------------------------------*/ -int qfits_table_append_xtension_hdr( - FILE * outfile, - qfits_table * t, - void ** data, - qfits_header * hdr) ; + int qfits_table_append_xtension_hdr (FILE * outfile, + qfits_table * t, + void **data, qfits_header * hdr); /*----------------------------------------------------------------------------*/ /** @@ -531,11 +508,9 @@ int qfits_table_append_xtension_hdr( @return the string to write */ /*----------------------------------------------------------------------------*/ -char * qfits_table_field_to_string( - qfits_table * table, - int col_id, - int row_id, - int use_zero_scale) ; + char *qfits_table_field_to_string (qfits_table * table, + int col_id, + int row_id, int use_zero_scale); /* */ #ifdef __cplusplus diff --git a/qfits/src/version.c b/qfits/src/version.c index ca1c120..5e4ea4a 100644 --- a/qfits/src/version.c +++ b/qfits/src/version.c @@ -1,9 +1,9 @@ /* Library version */ -static const char * -_qfits_version = "5.4.0" ; +static const char *_qfits_version = "5.4.0"; -const char * qfits_version(void) +const char * +qfits_version (void) { - return _qfits_version ; + return _qfits_version; } diff --git a/qfits/src/xmemory.c b/qfits/src/xmemory.c index b55da21..98cd979 100644 --- a/qfits/src/xmemory.c +++ b/qfits/src/xmemory.c @@ -119,10 +119,10 @@ -----------------------------------------------------------------------------*/ /** Initialization flag */ -static int xmemory_initialized=0 ; +static int xmemory_initialized = 0; /** Path to temporary directory */ -static char xmemory_tmpdirname[TMPDIRNAMESZ] = "." ; +static char xmemory_tmpdirname[TMPDIRNAMESZ] = "."; /*----------------------------------------------------------------------------*/ /** @@ -133,77 +133,79 @@ static char xmemory_tmpdirname[TMPDIRNAMESZ] = "." ; It is strictly internal to this source file. */ /*----------------------------------------------------------------------------*/ -static struct { +static struct +{ /** Number of active cells */ - int ncells ; + int ncells; /** Total allocated memory in bytes */ - size_t alloc_total ; + size_t alloc_total; /** Total allocated RAM in bytes */ - size_t alloc_ram ; + size_t alloc_ram; /** Total allocated VM in bytes */ - size_t alloc_swap ; + size_t alloc_swap; /** Peak allocation ever seen for diagnostics */ - size_t alloc_max ; + size_t alloc_max; /** Peak number of pointers ever seen for diagnostics */ - int max_cells ; + int max_cells; /** Current number of swap files */ - int nswapfiles ; + int nswapfiles; /** Registration counter for swap files */ - int file_reg ; + int file_reg; /** Current number of memory-mapped files */ - int n_mm_files ; + int n_mm_files; /** Current number of mappings derived from files */ - int n_mm_mappings ; + int n_mm_mappings; #ifdef __linux__ /** Page size in bytes (Linux only) */ - int pagesize ; + int pagesize; /** Value found for RLIMIT_DATA (Linux only) */ - int rlimit_data ; + int rlimit_data; #endif -} xmemory_table ; +} xmemory_table; /** Various infos about the pointers */ /** List of pointers (outside of cells for efficiency reason) */ -static void * xmemory_p_val[XMEMORY_MAXPTRS] ; +static void *xmemory_p_val[XMEMORY_MAXPTRS]; /** Pointed size in bytes */ -static size_t xmemory_p_size[XMEMORY_MAXPTRS] ; +static size_t xmemory_p_size[XMEMORY_MAXPTRS]; #if (XMEMORY_DEBUG>=1) /** Name of the source file where the alloc was requested */ -static char xmemory_p_filename[XMEMORY_MAXPTRS][SRCFILENAMESZ] ; +static char xmemory_p_filename[XMEMORY_MAXPTRS][SRCFILENAMESZ]; /** Line number where the alloc was requested */ -static int xmemory_p_lineno[XMEMORY_MAXPTRS] ; +static int xmemory_p_lineno[XMEMORY_MAXPTRS]; #endif /** Memory type: RAM, swap, or mapped file */ -static char xmemory_p_memtype[XMEMORY_MAXPTRS] ; +static char xmemory_p_memtype[XMEMORY_MAXPTRS]; /* Swap memory only */ /** Swap file ID */ -static int xmemory_p_swapfileid[XMEMORY_MAXPTRS] ; +static int xmemory_p_swapfileid[XMEMORY_MAXPTRS]; /** Swap file descriptor */ -static int xmemory_p_swapfd[XMEMORY_MAXPTRS] ; +static int xmemory_p_swapfd[XMEMORY_MAXPTRS]; /* Mapped files only */ /** Name of mapped file */ -static char xmemory_p_mm_filename[XMEMORY_MAXPTRS][MAPFILENAMESZ] ; +static char xmemory_p_mm_filename[XMEMORY_MAXPTRS][MAPFILENAMESZ]; /** Hash of mapped file name for quick search */ -static unsigned xmemory_p_mm_hash[XMEMORY_MAXPTRS] ; +static unsigned xmemory_p_mm_hash[XMEMORY_MAXPTRS]; /** Reference counter for this pointer */ -static int xmemory_p_mm_refcount[XMEMORY_MAXPTRS] ; +static int xmemory_p_mm_refcount[XMEMORY_MAXPTRS]; /*----------------------------------------------------------------------------- Private function prototypes -----------------------------------------------------------------------------*/ -static unsigned xmemory_hash(char *) ; -static void xmemory_init(void) ; -static void xmemory_cleanup(void); -static int xmemory_addcell(void*, size_t, const char*, int, char, int, int, char*) ; -static int xmemory_remcell(int) ; -static void xmemory_dumpcell(int, FILE*) ; -static char * xmemory_tmpfilename(int) ; -static char * strdup_(const char * str) ; -void xmemory_status_(const char *, int) ; +static unsigned xmemory_hash (char *); +static void xmemory_init (void); +static void xmemory_cleanup (void); +static int xmemory_addcell (void *, size_t, const char *, int, char, int, int, + char *); +static int xmemory_remcell (int); +static void xmemory_dumpcell (int, FILE *); +static char *xmemory_tmpfilename (int); +static char *strdup_ (const char *str); +void xmemory_status_ (const char *, int); /*----------------------------------------------------------------------------- Function codes @@ -221,22 +223,24 @@ void xmemory_status_(const char *, int) ; key itself in last resort. */ /*----------------------------------------------------------------------------*/ -static unsigned xmemory_hash(char * key) +static unsigned +xmemory_hash (char *key) { - int len ; - unsigned hash ; - int i ; + int len; + unsigned hash; + int i; - len = strlen(key); - for (hash=0, i=0 ; i>6) ; + len = strlen (key); + for (hash = 0, i = 0; i < len; i++) + { + hash += (unsigned) key[i]; + hash += (hash << 10); + hash ^= (hash >> 6); } - hash += (hash <<3); - hash ^= (hash >>11); - hash += (hash <<15); - return hash ; + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + return hash; } /*----------------------------------------------------------------------------*/ @@ -251,45 +255,42 @@ static unsigned xmemory_hash(char * key) maximum. */ /*----------------------------------------------------------------------------*/ -static void xmemory_init(void) +static void +xmemory_init (void) { - struct rlimit rlim ; + struct rlimit rlim; - xmem_debug( - fprintf(stderr, - "xmem: initializing main table size=%d ptrs (%ld bytes)\n", - XMEMORY_MAXPTRS, - (long)sizeof(xmemory_table)); + xmem_debug (fprintf (stderr, + "xmem: initializing main table size=%d ptrs (%ld bytes)\n", + XMEMORY_MAXPTRS, (long) sizeof (xmemory_table)); ); - /* Initialize memory table */ - memset(&xmemory_table, 0, sizeof(xmemory_table)); + /* Initialize memory table */ + memset (&xmemory_table, 0, sizeof (xmemory_table)); - /* Install cleanup routine at exit */ - atexit(xmemory_cleanup); - - /* Increase number of descriptors to maximum */ - getrlimit(RLIMIT_NOFILE, &rlim) ; - xmem_debug( - fprintf(stderr, "xmem: increasing from %ld to %ld file handles\n", - (long)rlim.rlim_cur, - (long)rlim.rlim_max); + /* Install cleanup routine at exit */ + atexit (xmemory_cleanup); + + /* Increase number of descriptors to maximum */ + getrlimit (RLIMIT_NOFILE, &rlim); + xmem_debug (fprintf + (stderr, "xmem: increasing from %ld to %ld file handles\n", + (long) rlim.rlim_cur, (long) rlim.rlim_max); ); - rlim.rlim_cur = rlim.rlim_max ; - setrlimit(RLIMIT_NOFILE, &rlim) ; + rlim.rlim_cur = rlim.rlim_max; + setrlimit (RLIMIT_NOFILE, &rlim); #ifdef __linux__ - /* Get RLIMIT_DATA on Linux */ - getrlimit(RLIMIT_DATA, &rlim); - xmemory_table.rlimit_data = rlim.rlim_cur ; - xmem_debug( - fprintf(stderr, "xmem: got RLIMIT_DATA=%d\n", - xmemory_table.rlimit_data); + /* Get RLIMIT_DATA on Linux */ + getrlimit (RLIMIT_DATA, &rlim); + xmemory_table.rlimit_data = rlim.rlim_cur; + xmem_debug (fprintf (stderr, "xmem: got RLIMIT_DATA=%d\n", + xmemory_table.rlimit_data); ); - /* Get page size on Linux */ - xmemory_table.pagesize = getpagesize(); + /* Get page size on Linux */ + xmemory_table.pagesize = getpagesize (); #endif - return ; + return; } /*----------------------------------------------------------------------------*/ @@ -300,29 +301,30 @@ static void xmemory_init(void) This routine will delete all swap files from the temporary area. */ /*----------------------------------------------------------------------------*/ -static void xmemory_cleanup(void) +static void +xmemory_cleanup (void) { - int reg ; + int reg; - if (xmemory_table.file_reg>0) { - xmem_debug( - fprintf(stderr, "xmem: cleaning up swap files... "); - ); - /* - * Call remove() on all possible VM files. If the file exists, it - * is effectively removed. It it does not, ignore the error. - * This is not the cleanest way of doing it, but this function is - * meant to be called also in cases of emergency (e.g. segfault), - * so it should not rely on a correct memory table. - */ - for (reg=0 ; reg 0) + { + xmem_debug (fprintf (stderr, "xmem: cleaning up swap files... "); + ); + /* + * Call remove() on all possible VM files. If the file exists, it + * is effectively removed. It it does not, ignore the error. + * This is not the cleanest way of doing it, but this function is + * meant to be called also in cases of emergency (e.g. segfault), + * so it should not rely on a correct memory table. + */ + for (reg = 0; reg < xmemory_table.file_reg; reg++) + { + remove (xmemory_tmpfilename (reg + 1)); + } + xmem_debug (fprintf (stderr, "xmem: done cleaning swap files\n"); + ); } - return ; + return; } /*----------------------------------------------------------------------------*/ @@ -344,66 +346,72 @@ static void xmemory_cleanup(void) the passed values are correct! */ /*----------------------------------------------------------------------------*/ -static int xmemory_addcell( - void * pointer, - size_t size, - const char * filename, - int lineno, - char memtype, - int swapfileid, - int swapfd, - char * mm_filename) +static int +xmemory_addcell (void *pointer, + size_t size, + const char *filename, + int lineno, + char memtype, int swapfileid, int swapfd, char *mm_filename) { - int pos, ii ; - - /* Check there is still some space left */ - if (xmemory_table.ncells >= XMEMORY_MAXPTRS) { - fprintf(stderr, "fatal xmemory error: reached max pointers (%d)\n", - XMEMORY_MAXPTRS); - exit(-1); - } - /* Find an available slot */ - pos = PTR_HASH(pointer); - for (ii = 0 ; ii= XMEMORY_MAXPTRS) + { + fprintf (stderr, "fatal xmemory error: reached max pointers (%d)\n", + XMEMORY_MAXPTRS); + exit (-1); + } + /* Find an available slot */ + pos = PTR_HASH (pointer); + for (ii = 0; ii < XMEMORY_MAXPTRS; ii++) + { + if (++pos == XMEMORY_MAXPTRS) + pos = 0; + if (xmemory_p_val[pos] == NULL) + break; + } + xmem_debug (fprintf (stderr, "xmem: freecell found at pos %d\n", pos); + ); + + /* Store information */ + xmemory_p_val[pos] = pointer; + xmemory_p_size[pos] = size; + + /* Filename and line number */ #if (XMEMORY_DEBUG>=1) - - if (filename) { - strncpy(xmemory_p_filename[pos], filename, SRCFILENAMESZ-1) ; - } else { - xmemory_p_filename[pos][0] = (char)0 ; + + if (filename) + { + strncpy (xmemory_p_filename[pos], filename, SRCFILENAMESZ - 1); } - xmemory_p_lineno[pos] = lineno ; + else + { + xmemory_p_filename[pos][0] = (char) 0; + } + xmemory_p_lineno[pos] = lineno; #endif - - xmemory_p_memtype[pos] = memtype ; - xmemory_p_swapfileid[pos] = swapfileid ; - xmemory_p_swapfd[pos] = swapfd ; - - if (mm_filename!=NULL) { - strncpy(xmemory_p_mm_filename[pos], mm_filename,MAPFILENAMESZ); - xmemory_p_mm_hash[pos] = xmemory_hash(mm_filename); - xmemory_p_mm_refcount[pos] = 1 ; - } else { - xmemory_p_mm_filename[pos][0] = 0 ; - xmemory_p_mm_hash[pos] = 0 ; - xmemory_p_mm_refcount[pos] = 0 ; + + xmemory_p_memtype[pos] = memtype; + xmemory_p_swapfileid[pos] = swapfileid; + xmemory_p_swapfd[pos] = swapfd; + + if (mm_filename != NULL) + { + strncpy (xmemory_p_mm_filename[pos], mm_filename, MAPFILENAMESZ); + xmemory_p_mm_hash[pos] = xmemory_hash (mm_filename); + xmemory_p_mm_refcount[pos] = 1; } - xmemory_table.ncells ++ ; - if (xmemory_table.ncells > xmemory_table.max_cells) - xmemory_table.max_cells = xmemory_table.ncells ; - return pos ; + else + { + xmemory_p_mm_filename[pos][0] = 0; + xmemory_p_mm_hash[pos] = 0; + xmemory_p_mm_refcount[pos] = 0; + } + xmemory_table.ncells++; + if (xmemory_table.ncells > xmemory_table.max_cells) + xmemory_table.max_cells = xmemory_table.ncells; + return pos; } /*----------------------------------------------------------------------------*/ @@ -417,16 +425,17 @@ static int xmemory_addcell( the passed values are correct! */ /*----------------------------------------------------------------------------*/ -static int xmemory_remcell(int pos) +static int +xmemory_remcell (int pos) { - xmem_debug( - fprintf(stderr, "xmem: removing cell from pos %d (cached pos)\n", pos); + xmem_debug (fprintf + (stderr, "xmem: removing cell from pos %d (cached pos)\n", pos); ); - /* Set pointer to NULL */ - xmemory_p_val[pos] = NULL ; - /* Decrement number of allocated pointers */ - xmemory_table.ncells -- ; - return 0 ; + /* Set pointer to NULL */ + xmemory_p_val[pos] = NULL; + /* Decrement number of allocated pointers */ + xmemory_table.ncells--; + return 0; } /*----------------------------------------------------------------------------*/ @@ -443,48 +452,51 @@ static int xmemory_remcell(int pos) immediately. */ /*----------------------------------------------------------------------------*/ -static void xmemory_dumpcell(int pos, FILE * out) +static void +xmemory_dumpcell (int pos, FILE * out) { - if (pos<0 || pos>=XMEMORY_MAXPTRS) return ; - if (xmemory_p_val[pos]==NULL) return ; + if (pos < 0 || pos >= XMEMORY_MAXPTRS) + return; + if (xmemory_p_val[pos] == NULL) + return; - if (xmemory_p_memtype[pos] == MEMTYPE_MMAP) { + if (xmemory_p_memtype[pos] == MEMTYPE_MMAP) + { #if (XMEMORY_DEBUG>=1) - fprintf(out, - "M(%p) - %s (%d) maps [%s] for %ld bytes", - xmemory_p_val[pos], - xmemory_p_filename[pos], - xmemory_p_lineno[pos], - xmemory_p_mm_filename[pos], - (long)xmemory_p_size[pos]); + fprintf (out, + "M(%p) - %s (%d) maps [%s] for %ld bytes", + xmemory_p_val[pos], + xmemory_p_filename[pos], + xmemory_p_lineno[pos], + xmemory_p_mm_filename[pos], (long) xmemory_p_size[pos]); #else - fprintf(out, - "M(%p) maps [%s] for %ld bytes", - xmemory_p_val[pos], - xmemory_p_mm_filename[pos], - (long)xmemory_p_size[pos]); -#endif - } else { -#if (XMEMORY_DEBUG>=1) - fprintf(out, "%c(%p) - %s (%d) for %ld bytes", - xmemory_p_memtype[pos], - xmemory_p_val[pos], - xmemory_p_filename[pos], - xmemory_p_lineno[pos], - (long)xmemory_p_size[pos]); -#else - fprintf(out, "%c(%p) for %ld bytes", - xmemory_p_memtype[pos], - xmemory_p_val[pos], - (long)xmemory_p_size[pos]); + fprintf (out, + "M(%p) maps [%s] for %ld bytes", + xmemory_p_val[pos], + xmemory_p_mm_filename[pos], (long) xmemory_p_size[pos]); #endif } - if (xmemory_p_memtype[pos]==MEMTYPE_SWAP) { - fprintf(out, " swf[%s][%d]", - xmemory_tmpfilename(xmemory_p_swapfileid[pos]), - xmemory_p_swapfd[pos]); + else + { +#if (XMEMORY_DEBUG>=1) + fprintf (out, "%c(%p) - %s (%d) for %ld bytes", + xmemory_p_memtype[pos], + xmemory_p_val[pos], + xmemory_p_filename[pos], + xmemory_p_lineno[pos], (long) xmemory_p_size[pos]); +#else + fprintf (out, "%c(%p) for %ld bytes", + xmemory_p_memtype[pos], + xmemory_p_val[pos], (long) xmemory_p_size[pos]); +#endif } - fprintf(out, "\n"); + if (xmemory_p_memtype[pos] == MEMTYPE_SWAP) + { + fprintf (out, " swf[%s][%d]", + xmemory_tmpfilename (xmemory_p_swapfileid[pos]), + xmemory_p_swapfd[pos]); + } + fprintf (out, "\n"); } /*----------------------------------------------------------------------------*/ @@ -498,13 +510,14 @@ static void xmemory_dumpcell(int pos, FILE * out) returns a pointer to it. */ /*----------------------------------------------------------------------------*/ -static char * xmemory_tmpfilename(int reg) +static char * +xmemory_tmpfilename (int reg) { - static char xmem_tmpfilename[TMPFILENAMESZ] ; - /* Create file name using tmp directory as a base */ - sprintf(xmem_tmpfilename, "%s/vmswap_%05ld_%05x", xmemory_tmpdirname, - (long)getpid(), reg) ; - return xmem_tmpfilename ; + static char xmem_tmpfilename[TMPFILENAMESZ]; + /* Create file name using tmp directory as a base */ + sprintf (xmem_tmpfilename, "%s/vmswap_%05ld_%05x", xmemory_tmpdirname, + (long) getpid (), reg); + return xmem_tmpfilename; } /*----------------------------------------------------------------------------*/ @@ -523,167 +536,175 @@ static char * xmemory_tmpfilename(int reg) @endcode */ /*----------------------------------------------------------------------------*/ -void * xmemory_malloc(size_t size, const char * filename, int lineno) +void * +xmemory_malloc (size_t size, const char *filename, int lineno) { - void * ptr ; - char * fname ; - int swapfileid ; - int swapfd ; - char wbuf[MEMPAGESZ] ; - int nbufs ; - int memtype ; - int i ; - int pos ; + void *ptr; + char *fname; + int swapfileid; + int swapfd; + char wbuf[MEMPAGESZ]; + int nbufs; + int memtype; + int i; + int pos; #ifdef __linux__ - int p ; + int p; #endif - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if (XMEMORY_MODE == 0) return malloc(size); - else if (XMEMORY_MODE == 1) { - ptr = malloc(size); - if (ptr == NULL) exit(1) ; - else return ptr ; - } - - /* Initialize table if needed */ - if (xmemory_initialized==0) { - xmemory_init() ; - xmemory_initialized++ ; + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if (XMEMORY_MODE == 0) + return malloc (size); + else if (XMEMORY_MODE == 1) + { + ptr = malloc (size); + if (ptr == NULL) + exit (1); + else + return ptr; } - /* Protect the call */ - if (size==0) { - xmem_debug( - fprintf(stderr, "xmem: malloc called with 0 size - %s (%d)\n", - filename, lineno); - ); - return NULL ; + /* Initialize table if needed */ + if (xmemory_initialized == 0) + { + xmemory_init (); + xmemory_initialized++; } - /* Try to allocate in memory */ + /* Protect the call */ + if (size == 0) + { + xmem_debug (fprintf + (stderr, "xmem: malloc called with 0 size - %s (%d)\n", + filename, lineno); + ); + return NULL; + } + + /* Try to allocate in memory */ #ifdef __linux__ - /* Linux does not honor the RLIMIT_DATA limit. - * The only way to limit the amount of memory taken by - * a process is to set RLIMIT_AS, which unfortunately also - * limits down the maximal amount of memory addressable with - * mmap() calls, making on-the-fly swap space creation useless - * in this module. To avoid this, the RLIMIT_DATA value - * is honored here with this test. - */ - ptr = NULL ; - if (xmemory_table.rlimit_data<1) { - /* No limit set on RLIMIT_DATA: proceed with malloc */ - ptr = malloc(size); - } else if (xmemory_table.alloc_total+size <= - (size_t)xmemory_table.rlimit_data) { - /* Next allocation will still be within limits: proceed */ - ptr = malloc(size); + /* Linux does not honor the RLIMIT_DATA limit. + * The only way to limit the amount of memory taken by + * a process is to set RLIMIT_AS, which unfortunately also + * limits down the maximal amount of memory addressable with + * mmap() calls, making on-the-fly swap space creation useless + * in this module. To avoid this, the RLIMIT_DATA value + * is honored here with this test. + */ + ptr = NULL; + if (xmemory_table.rlimit_data < 1) + { + /* No limit set on RLIMIT_DATA: proceed with malloc */ + ptr = malloc (size); + } + else if (xmemory_table.alloc_total + size <= + (size_t) xmemory_table.rlimit_data) + { + /* Next allocation will still be within limits: proceed */ + ptr = malloc (size); } #else - ptr = malloc(size); + ptr = malloc (size); #endif - if (ptr==NULL) { - /* No more RAM available: try to allocate private swap */ - xmem_debug( - fprintf(stderr, "xmem: hit a NULL pointer -- swapping\n"); - ); + if (ptr == NULL) + { + /* No more RAM available: try to allocate private swap */ + xmem_debug (fprintf (stderr, "xmem: hit a NULL pointer -- swapping\n"); + ); - /* Create swap file with rights: rw-rw-rw- */ - swapfileid = ++ xmemory_table.file_reg ; - fname = xmemory_tmpfilename(swapfileid); - swapfd = open(fname, O_RDWR | O_CREAT); - if (swapfd==-1) { - fprintf(stderr, "xmem: fatal error: cannot create swap file\n"); - exit(-1); - } - fchmod(swapfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); + /* Create swap file with rights: rw-rw-rw- */ + swapfileid = ++xmemory_table.file_reg; + fname = xmemory_tmpfilename (swapfileid); + swapfd = open (fname, O_RDWR | O_CREAT); + if (swapfd == -1) + { + fprintf (stderr, "xmem: fatal error: cannot create swap file\n"); + exit (-1); + } + fchmod (swapfd, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - /* Compute number of passes to insert buffer */ - nbufs = size / MEMPAGESZ ; - if (size % MEMPAGESZ != 0) nbufs ++ ; + /* Compute number of passes to insert buffer */ + nbufs = size / MEMPAGESZ; + if (size % MEMPAGESZ != 0) + nbufs++; - /* Dump empty buffers into file */ - memset(wbuf, 0, MEMPAGESZ); - for (i=0 ; i xmemory_table.alloc_max) - xmemory_table.alloc_max = xmemory_table.alloc_total ; + /* Add cell into general table */ + pos = xmemory_addcell (ptr, + size, + filename, lineno, memtype, swapfileid, swapfd, NULL); + /* Adjust size */ + xmemory_table.alloc_total += size; + /* Remember biggest allocated block */ + if (xmemory_table.alloc_total > xmemory_table.alloc_max) + xmemory_table.alloc_max = xmemory_table.alloc_total; - /* Insert memory stamp */ - return (void*)ptr ; + /* Insert memory stamp */ + return (void *) ptr; } /*----------------------------------------------------------------------------*/ @@ -703,20 +724,25 @@ void * xmemory_malloc(size_t size, const char * filename, int lineno) @endcode */ /*----------------------------------------------------------------------------*/ -void * xmemory_calloc(size_t nmemb, size_t size, const char * filename, int lineno) +void * +xmemory_calloc (size_t nmemb, size_t size, const char *filename, int lineno) { - void * ptr ; + void *ptr; - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if (XMEMORY_MODE == 0) return calloc(nmemb, size) ; - else if (XMEMORY_MODE == 1) { - ptr = calloc(nmemb, size) ; - if (ptr == NULL) exit(1) ; - else return ptr ; + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if (XMEMORY_MODE == 0) + return calloc (nmemb, size); + else if (XMEMORY_MODE == 1) + { + ptr = calloc (nmemb, size); + if (ptr == NULL) + exit (1); + else + return ptr; } - - ptr = xmemory_malloc(nmemb * size, filename, lineno) ; - return memset(ptr, 0, nmemb * size) ; + + ptr = xmemory_malloc (nmemb * size, filename, lineno); + return memset (ptr, 0, nmemb * size); } /*----------------------------------------------------------------------------*/ @@ -741,191 +767,208 @@ void * xmemory_calloc(size_t nmemb, size_t size, const char * filename, int line The returned pointer ptr must be deallocated with xmemory_fdealloc(ptr) */ /*----------------------------------------------------------------------------*/ -char * xmemory_falloc( - char * name, - size_t offs, - size_t * size, - const char * srcname, - int srclin) +char * +xmemory_falloc (char *name, + size_t offs, size_t *size, const char *srcname, int srclin) { - unsigned mm_hash ; - char * ptr ; - struct stat sta ; - int fd ; - int nptrs ; - int i ; + unsigned mm_hash; + char *ptr; + struct stat sta; + int fd; + int nptrs; + int i; - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) { + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) + { - if (size!=NULL) *size = 0 ; + if (size != NULL) + *size = 0; - /* Check file's existence and compute its size */ - if (stat(name, &sta)==-1) { - xmem_debug( - fprintf(stderr, "xmem: cannot stat file %s - %s (%d)\n", - name, srcname, srclin); - ); - if (XMEMORY_MODE == 0) return NULL ; - else exit(1) ; - } - /* Check offset request does not go past end of file */ - if (offs>=(size_t)sta.st_size) { - xmem_debug( - fprintf(stderr, - "xmem: falloc offsets larger than file size"); - ); - if (XMEMORY_MODE == 0) return NULL ; - else exit(1) ; - } + /* Check file's existence and compute its size */ + if (stat (name, &sta) == -1) + { + xmem_debug (fprintf + (stderr, "xmem: cannot stat file %s - %s (%d)\n", name, + srcname, srclin); + ); + if (XMEMORY_MODE == 0) + return NULL; + else + exit (1); + } + /* Check offset request does not go past end of file */ + if (offs >= (size_t) sta.st_size) + { + xmem_debug (fprintf (stderr, + "xmem: falloc offsets larger than file size"); + ); + if (XMEMORY_MODE == 0) + return NULL; + else + exit (1); + } - /* Open file */ - if ((fd=open(name, O_RDONLY))==-1) { - xmem_debug( - fprintf(stderr, "xmem: cannot open file %s - %s (%d)\n", - name, srcname, srclin); - ); - if (XMEMORY_MODE == 0) return NULL ; - else exit(1) ; - } + /* Open file */ + if ((fd = open (name, O_RDONLY)) == -1) + { + xmem_debug (fprintf + (stderr, "xmem: cannot open file %s - %s (%d)\n", name, + srcname, srclin); + ); + if (XMEMORY_MODE == 0) + return NULL; + else + exit (1); + } - /* Memory-map input file */ - ptr = (char*)mmap(0, sta.st_size, - PROT_READ | PROT_WRITE, MAP_PRIVATE,fd,0); - - /* Close file */ - close(fd); - if (ptr == (char*)-1 || ptr==NULL) { - xmem_debug( - perror("mmap"); - fprintf(stderr, "xmem: falloc cannot mmap file %s", name); - ); - if (XMEMORY_MODE == 0) return NULL ; - else exit(1) ; - } + /* Memory-map input file */ + ptr = (char *) mmap (0, sta.st_size, + PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - xmem_debug( - fprintf(stderr, - "xmem: falloc mmap succeeded for [%s] - %s (%d)\n", - name, srcname, srclin); - ); + /* Close file */ + close (fd); + if (ptr == (char *) -1 || ptr == NULL) + { + xmem_debug (perror ("mmap"); + fprintf (stderr, "xmem: falloc cannot mmap file %s", + name); + ); + if (XMEMORY_MODE == 0) + return NULL; + else + exit (1); + } - if (size!=NULL) (*size) = sta.st_size ; - - return ptr + offs ; + xmem_debug (fprintf (stderr, + "xmem: falloc mmap succeeded for [%s] - %s (%d)\n", + name, srcname, srclin); + ); + + if (size != NULL) + (*size) = sta.st_size; + + return ptr + offs; } - /* Protect the call */ - if (size!=NULL) *size = 0 ; + /* Protect the call */ + if (size != NULL) + *size = 0; - /* Initialize table if needed */ - if (xmemory_initialized==0) { - xmemory_init() ; - xmemory_initialized++ ; + /* Initialize table if needed */ + if (xmemory_initialized == 0) + { + xmemory_init (); + xmemory_initialized++; } - if (xmemory_table.ncells>0) { - /* Check if file has already been mapped */ - /* Compute hash for this name */ - mm_hash = xmemory_hash(name); - /* Loop over all memory cells */ - nptrs=0 ; - for (i=0 ; i= xmemory_p_size[i]) { - xmem_debug( - fprintf(stderr, - "xmem: falloc offset larger than file size"); - ); - return NULL ; - } - /* Increase reference counter */ - xmemory_p_mm_refcount[i] ++ ; - xmem_debug( - fprintf(stderr, - "xmem: incref on %s (%d mappings)\n", - name, - xmemory_p_mm_refcount[i]); - ); - /* Increase number of mappings */ - xmemory_table.n_mm_mappings ++ ; - /* Build up return pointer */ - ptr = (char*)xmemory_p_val[i] + offs ; - /* Available size is filesize minus offset */ - if (size!=NULL) { - *size = xmemory_p_size[i] - offs ; - } - /* Return constructed pointer as void * */ - return (void*)ptr ; - } - } - if (nptrs>=xmemory_table.ncells) break ; - } + if (xmemory_table.ncells > 0) + { + /* Check if file has already been mapped */ + /* Compute hash for this name */ + mm_hash = xmemory_hash (name); + /* Loop over all memory cells */ + nptrs = 0; + for (i = 0; i < XMEMORY_MAXPTRS; i++) + { + if (xmemory_p_val[i] != NULL) + nptrs++; + if ((xmemory_p_val[i] != NULL) && + (xmemory_p_mm_filename[i] != NULL) && + (xmemory_p_mm_hash[i] == mm_hash)) + { + if (!strncmp (xmemory_p_mm_filename[i], name, MAPFILENAMESZ)) + { + /* File already mapped */ + /* Check offset consistency wrt file size */ + if (offs >= xmemory_p_size[i]) + { + xmem_debug (fprintf (stderr, + "xmem: falloc offset larger than file size"); + ); + return NULL; + } + /* Increase reference counter */ + xmemory_p_mm_refcount[i]++; + xmem_debug (fprintf (stderr, + "xmem: incref on %s (%d mappings)\n", + name, xmemory_p_mm_refcount[i]); + ); + /* Increase number of mappings */ + xmemory_table.n_mm_mappings++; + /* Build up return pointer */ + ptr = (char *) xmemory_p_val[i] + offs; + /* Available size is filesize minus offset */ + if (size != NULL) + { + *size = xmemory_p_size[i] - offs; + } + /* Return constructed pointer as void * */ + return (void *) ptr; + } + } + if (nptrs >= xmemory_table.ncells) + break; + } } - /* First mapping attempt for this file */ - /* Check file's existence and compute its size */ - if (stat(name, &sta)==-1) { - xmem_debug( - fprintf(stderr, "xmem: cannot stat file %s - %s (%d)\n", - name, srcname, srclin); - ); - return NULL ; + /* First mapping attempt for this file */ + /* Check file's existence and compute its size */ + if (stat (name, &sta) == -1) + { + xmem_debug (fprintf (stderr, "xmem: cannot stat file %s - %s (%d)\n", + name, srcname, srclin); + ); + return NULL; } - /* Check offset request does not go past end of file */ - if (offs>=(size_t)sta.st_size) { - xmem_debug( - fprintf(stderr, - "xmem: falloc offsets larger than file size"); - ); - return NULL ; + /* Check offset request does not go past end of file */ + if (offs >= (size_t) sta.st_size) + { + xmem_debug (fprintf (stderr, + "xmem: falloc offsets larger than file size"); + ); + return NULL; } - /* Open file */ - if ((fd=open(name, O_RDONLY))==-1) { - xmem_debug( - fprintf(stderr, "xmem: cannot open file %s - %s (%d)\n", - name, srcname, srclin); - ); - return NULL ; + /* Open file */ + if ((fd = open (name, O_RDONLY)) == -1) + { + xmem_debug (fprintf (stderr, "xmem: cannot open file %s - %s (%d)\n", + name, srcname, srclin); + ); + return NULL; } - /* Memory-map input file */ - ptr = (char*)mmap(0, sta.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,fd,0); - - /* Close file */ - close(fd); - if (ptr == (char*)-1 || ptr==NULL) { - xmem_debug( - perror("mmap"); - fprintf(stderr, "xmem: falloc cannot mmap file %s", name); - ); - return NULL ; + /* Memory-map input file */ + ptr = + (char *) mmap (0, sta.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, + 0); + + /* Close file */ + close (fd); + if (ptr == (char *) -1 || ptr == NULL) + { + xmem_debug (perror ("mmap"); + fprintf (stderr, "xmem: falloc cannot mmap file %s", name); + ); + return NULL; } - xmemory_table.n_mm_files ++ ; - xmemory_table.n_mm_mappings ++ ; - xmem_debug( - fprintf(stderr, - "xmem: falloc mmap succeeded for [%s] - %s (%d)\n", - name, srcname, srclin); + xmemory_table.n_mm_files++; + xmemory_table.n_mm_mappings++; + xmem_debug (fprintf (stderr, + "xmem: falloc mmap succeeded for [%s] - %s (%d)\n", + name, srcname, srclin); ); - /* Add cell into general table */ - (void) xmemory_addcell((void*)ptr, sta.st_size, srcname, srclin, - MEMTYPE_MMAP, -1, -1, name) ; + /* Add cell into general table */ + (void) xmemory_addcell ((void *) ptr, sta.st_size, srcname, srclin, + MEMTYPE_MMAP, -1, -1, name); - if (size!=NULL) (*size) = sta.st_size ; - - return ptr + offs ; + if (size != NULL) + (*size) = sta.st_size; + + return ptr + offs; } /*----------------------------------------------------------------------------*/ @@ -939,143 +982,147 @@ char * xmemory_falloc( @return void */ /*----------------------------------------------------------------------------*/ -void xmemory_fdealloc( - void * ptr, - size_t offs, - size_t size, - const char * filename, - int lineno) +void +xmemory_fdealloc (void *ptr, + size_t offs, size_t size, const char *filename, int lineno) { - int i ; - int pos ; - char * swapname ; - int nptrs ; - int ii; + int i; + int pos; + char *swapname; + int nptrs; + int ii; - /* Do nothing for a NULL pointer */ - if (ptr==NULL) { - /* Output a warning */ - fprintf(stderr, "xmem: free requested on NULL pointer -- %s (%d)\n", - filename, lineno); - return ; - } - - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) { - munmap((char*)(ptr)-offs, size) ; - return ; - } - - /* Locate pointer in main table */ - nptrs = 0 ; - pos = -1 ; - i = PTR_HASH(ptr); - for (ii=0 ; ii= (char*)ptr)) { - pos = i ; - break ; - } - } - if (nptrs>=xmemory_table.ncells) break ; - } - if (pos==-1) { - fprintf(stderr, - "xmem: %s (%d) free requested on unallocated pointer (%p)\n", - filename, lineno, ptr); - /* Pointer sent to system's free() function, maybe it should not? */ - free(ptr); - return ; + /* Do nothing for a NULL pointer */ + if (ptr == NULL) + { + /* Output a warning */ + fprintf (stderr, "xmem: free requested on NULL pointer -- %s (%d)\n", + filename, lineno); + return; } - /* Deallocate pointer */ - switch (xmemory_p_memtype[pos]) { - case MEMTYPE_RAM: - /* --- RAM pointer */ - /* Free normal memory pointer */ - free(ptr); - xmemory_table.alloc_ram -= xmemory_p_size[pos] ; - break ; - case MEMTYPE_SWAP: - /* --- SWAP pointer */ - swapname = xmemory_tmpfilename(xmemory_p_swapfileid[pos]); - xmem_debug( - fprintf(stderr, "xmem: deallocating swap file [%s]\n", - swapname); - ); - /* Munmap file */ - if (munmap(ptr, xmemory_p_size[pos])!=0) { - xmem_debug( perror("munmap"); ); - } - /* Close swap file */ - if (close(xmemory_p_swapfd[pos])==-1) { - xmem_debug( perror("close"); ); - } - /* Remove swap file */ - if (remove(swapname)!=0) { - xmem_debug( perror("remove"); ); - } - xmemory_table.alloc_swap -= xmemory_p_size[pos] ; - xmemory_table.nswapfiles -- ; - break ; - case MEMTYPE_MMAP: - /* --- MEMORY-MAPPED pointer */ - /* Decrease reference count */ - xmemory_p_mm_refcount[pos] -- ; - /* Decrease total number of mappings */ - xmemory_table.n_mm_mappings -- ; - /* Non-null ref count means the file stays mapped */ - if (xmemory_p_mm_refcount[pos]>0) { - xmem_debug( - fprintf(stderr, "xmem: decref on %s (%d mappings)\n", - xmemory_p_mm_filename[pos], - xmemory_p_mm_refcount[pos]); - ); - return ; - } - /* Ref count reached zero: unmap the file */ - xmem_debug( - fprintf(stderr, - "xmem: unmapping file %s\n", - xmemory_p_mm_filename[pos]); - ); - munmap((char*)xmemory_p_val[pos], - xmemory_p_size[pos]); - /* Decrease total number of mapped files */ - xmemory_table.n_mm_files -- ; - break ; - default: - xmem_debug( - fprintf(stderr, "xmem: unknown memory cell type???"); - ); - break ; + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) + { + munmap ((char *) (ptr) - offs, size); + return; } - if (xmemory_p_memtype[pos]!=MEMTYPE_MMAP) { - /* Adjust allocated totals */ - xmemory_table.alloc_total -= xmemory_p_size[pos] ; - - /* Print out message in debug mode */ - xmem_debug( - fprintf(stderr, "xmem: free(%p) %ld bytes in %s (%d)\n", - ptr, - (long)xmemory_p_size[pos], - filename, - lineno); - ); + /* Locate pointer in main table */ + nptrs = 0; + pos = -1; + i = PTR_HASH (ptr); + for (ii = 0; ii < XMEMORY_MAXPTRS; ii++) + { + if (++i == XMEMORY_MAXPTRS) + i = 0; + if (xmemory_p_val[i] == NULL) + continue; + nptrs++; + if (xmemory_p_val[i] == ptr) + { + pos = i; + break; + } + if (xmemory_p_memtype[i] == MEMTYPE_MMAP) + { + if (((char *) xmemory_p_val[i] <= (char *) ptr) && + (((char *) xmemory_p_val[i] + + xmemory_p_size[i]) >= (char *) ptr)) + { + pos = i; + break; + } + } + if (nptrs >= xmemory_table.ncells) + break; } - /* Remove cell from main table */ - xmemory_remcell(pos) ; - return ; + if (pos == -1) + { + fprintf (stderr, + "xmem: %s (%d) free requested on unallocated pointer (%p)\n", + filename, lineno, ptr); + /* Pointer sent to system's free() function, maybe it should not? */ + free (ptr); + return; + } + + /* Deallocate pointer */ + switch (xmemory_p_memtype[pos]) + { + case MEMTYPE_RAM: + /* --- RAM pointer */ + /* Free normal memory pointer */ + free (ptr); + xmemory_table.alloc_ram -= xmemory_p_size[pos]; + break; + case MEMTYPE_SWAP: + /* --- SWAP pointer */ + swapname = xmemory_tmpfilename (xmemory_p_swapfileid[pos]); + xmem_debug (fprintf (stderr, "xmem: deallocating swap file [%s]\n", + swapname); + ); + /* Munmap file */ + if (munmap (ptr, xmemory_p_size[pos]) != 0) + { + xmem_debug (perror ("munmap");); + } + /* Close swap file */ + if (close (xmemory_p_swapfd[pos]) == -1) + { + xmem_debug (perror ("close");); + } + /* Remove swap file */ + if (remove (swapname) != 0) + { + xmem_debug (perror ("remove");); + } + xmemory_table.alloc_swap -= xmemory_p_size[pos]; + xmemory_table.nswapfiles--; + break; + case MEMTYPE_MMAP: + /* --- MEMORY-MAPPED pointer */ + /* Decrease reference count */ + xmemory_p_mm_refcount[pos]--; + /* Decrease total number of mappings */ + xmemory_table.n_mm_mappings--; + /* Non-null ref count means the file stays mapped */ + if (xmemory_p_mm_refcount[pos] > 0) + { + xmem_debug (fprintf (stderr, "xmem: decref on %s (%d mappings)\n", + xmemory_p_mm_filename[pos], + xmemory_p_mm_refcount[pos]); + ); + return; + } + /* Ref count reached zero: unmap the file */ + xmem_debug (fprintf (stderr, + "xmem: unmapping file %s\n", + xmemory_p_mm_filename[pos]); + ); + munmap ((char *) xmemory_p_val[pos], xmemory_p_size[pos]); + /* Decrease total number of mapped files */ + xmemory_table.n_mm_files--; + break; + default: + xmem_debug (fprintf (stderr, "xmem: unknown memory cell type???"); + ); + break; + } + + if (xmemory_p_memtype[pos] != MEMTYPE_MMAP) + { + /* Adjust allocated totals */ + xmemory_table.alloc_total -= xmemory_p_size[pos]; + + /* Print out message in debug mode */ + xmem_debug (fprintf (stderr, "xmem: free(%p) %ld bytes in %s (%d)\n", + ptr, (long) xmemory_p_size[pos], filename, lineno); + ); + } + /* Remove cell from main table */ + xmemory_remcell (pos); + return; } /*----------------------------------------------------------------------------*/ @@ -1091,138 +1138,146 @@ void xmemory_fdealloc( table. */ /*----------------------------------------------------------------------------*/ -void xmemory_free(void * ptr, const char * filename, int lineno) +void +xmemory_free (void *ptr, const char *filename, int lineno) { - int i ; - int pos ; - char * swapname ; - int nptrs ; - int ii; + int i; + int pos; + char *swapname; + int nptrs; + int ii; - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) { - free(ptr); - return ; - } - - /* Do nothing for a NULL pointer */ - if (ptr==NULL) { - /* Output a warning */ - fprintf(stderr, "xmem: free requested on NULL pointer -- %s (%d)\n", - filename, lineno); - return ; + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if ((XMEMORY_MODE == 0) || (XMEMORY_MODE == 1)) + { + free (ptr); + return; } - /* Locate pointer in main table */ - nptrs = 0 ; - pos = -1 ; - i = PTR_HASH(ptr); - for (ii=0 ; ii= (char*)ptr)) { - pos = i ; - break ; - } - } - if (nptrs>=xmemory_table.ncells) break ; - } - if (pos==-1) { - fprintf(stderr, - "xmem: %s (%d) free requested on unallocated pointer (%p)\n", - filename, lineno, ptr); - /* Pointer sent to system's free() function, maybe it should not? */ - free(ptr); - return ; + /* Do nothing for a NULL pointer */ + if (ptr == NULL) + { + /* Output a warning */ + fprintf (stderr, "xmem: free requested on NULL pointer -- %s (%d)\n", + filename, lineno); + return; } - /* Deallocate pointer */ - switch (xmemory_p_memtype[pos]) { - case MEMTYPE_RAM: - /* --- RAM pointer */ - /* Free normal memory pointer */ - free(ptr); - xmemory_table.alloc_ram -= xmemory_p_size[pos] ; - break ; - case MEMTYPE_SWAP: - /* --- SWAP pointer */ - swapname = xmemory_tmpfilename(xmemory_p_swapfileid[pos]); - xmem_debug( - fprintf(stderr, "xmem: deallocating swap file [%s]\n", - swapname); - ); - /* Munmap file */ - if (munmap(ptr, xmemory_p_size[pos])!=0) { - xmem_debug( perror("munmap"); ); - } - /* Close swap file */ - if (close(xmemory_p_swapfd[pos])==-1) { - xmem_debug( perror("close"); ); - } - /* Remove swap file */ - if (remove(swapname)!=0) { - xmem_debug( perror("remove"); ); - } - xmemory_table.alloc_swap -= xmemory_p_size[pos] ; - xmemory_table.nswapfiles -- ; - break ; - case MEMTYPE_MMAP: - /* --- MEMORY-MAPPED pointer */ - /* Decrease reference count */ - xmemory_p_mm_refcount[pos] -- ; - /* Decrease total number of mappings */ - xmemory_table.n_mm_mappings -- ; - /* Non-null ref count means the file stays mapped */ - if (xmemory_p_mm_refcount[pos]>0) { - xmem_debug( - fprintf(stderr, "xmem: decref on %s (%d mappings)\n", - xmemory_p_mm_filename[pos], - xmemory_p_mm_refcount[pos]); - ); - return ; - } - /* Ref count reached zero: unmap the file */ - xmem_debug( - fprintf(stderr, - "xmem: unmapping file %s\n", - xmemory_p_mm_filename[pos]); - ); - munmap((char*)xmemory_p_val[pos], - xmemory_p_size[pos]); - /* Decrease total number of mapped files */ - xmemory_table.n_mm_files -- ; - break ; - default: - xmem_debug( - fprintf(stderr, "xmem: unknown memory cell type???"); - ); - break ; + /* Locate pointer in main table */ + nptrs = 0; + pos = -1; + i = PTR_HASH (ptr); + for (ii = 0; ii < XMEMORY_MAXPTRS; ii++) + { + if (++i == XMEMORY_MAXPTRS) + i = 0; + if (xmemory_p_val[i] == NULL) + continue; + nptrs++; + if (xmemory_p_val[i] == ptr) + { + pos = i; + break; + } + if (xmemory_p_memtype[i] == MEMTYPE_MMAP) + { + if (((char *) xmemory_p_val[i] <= (char *) ptr) && + (((char *) xmemory_p_val[i] + + xmemory_p_size[i]) >= (char *) ptr)) + { + pos = i; + break; + } + } + if (nptrs >= xmemory_table.ncells) + break; + } + if (pos == -1) + { + fprintf (stderr, + "xmem: %s (%d) free requested on unallocated pointer (%p)\n", + filename, lineno, ptr); + /* Pointer sent to system's free() function, maybe it should not? */ + free (ptr); + return; } - if (xmemory_p_memtype[pos]!=MEMTYPE_MMAP) { - /* Adjust allocated totals */ - xmemory_table.alloc_total -= xmemory_p_size[pos] ; - - /* Print out message in debug mode */ - xmem_debug( - fprintf(stderr, "xmem: free(%p) %ld bytes in %s (%d)\n", - ptr, - (long)xmemory_p_size[pos], - filename, - lineno); - ); + /* Deallocate pointer */ + switch (xmemory_p_memtype[pos]) + { + case MEMTYPE_RAM: + /* --- RAM pointer */ + /* Free normal memory pointer */ + free (ptr); + xmemory_table.alloc_ram -= xmemory_p_size[pos]; + break; + case MEMTYPE_SWAP: + /* --- SWAP pointer */ + swapname = xmemory_tmpfilename (xmemory_p_swapfileid[pos]); + xmem_debug (fprintf (stderr, "xmem: deallocating swap file [%s]\n", + swapname); + ); + /* Munmap file */ + if (munmap (ptr, xmemory_p_size[pos]) != 0) + { + xmem_debug (perror ("munmap");); + } + /* Close swap file */ + if (close (xmemory_p_swapfd[pos]) == -1) + { + xmem_debug (perror ("close");); + } + /* Remove swap file */ + if (remove (swapname) != 0) + { + xmem_debug (perror ("remove");); + } + xmemory_table.alloc_swap -= xmemory_p_size[pos]; + xmemory_table.nswapfiles--; + break; + case MEMTYPE_MMAP: + /* --- MEMORY-MAPPED pointer */ + /* Decrease reference count */ + xmemory_p_mm_refcount[pos]--; + /* Decrease total number of mappings */ + xmemory_table.n_mm_mappings--; + /* Non-null ref count means the file stays mapped */ + if (xmemory_p_mm_refcount[pos] > 0) + { + xmem_debug (fprintf (stderr, "xmem: decref on %s (%d mappings)\n", + xmemory_p_mm_filename[pos], + xmemory_p_mm_refcount[pos]); + ); + return; + } + /* Ref count reached zero: unmap the file */ + xmem_debug (fprintf (stderr, + "xmem: unmapping file %s\n", + xmemory_p_mm_filename[pos]); + ); + munmap ((char *) xmemory_p_val[pos], xmemory_p_size[pos]); + /* Decrease total number of mapped files */ + xmemory_table.n_mm_files--; + break; + default: + xmem_debug (fprintf (stderr, "xmem: unknown memory cell type???"); + ); + break; } - /* Remove cell from main table */ - xmemory_remcell(pos) ; - return ; + + if (xmemory_p_memtype[pos] != MEMTYPE_MMAP) + { + /* Adjust allocated totals */ + xmemory_table.alloc_total -= xmemory_p_size[pos]; + + /* Print out message in debug mode */ + xmem_debug (fprintf (stderr, "xmem: free(%p) %ld bytes in %s (%d)\n", + ptr, (long) xmemory_p_size[pos], filename, lineno); + ); + } + /* Remove cell from main table */ + xmemory_remcell (pos); + return; } /*----------------------------------------------------------------------------*/ @@ -1242,55 +1297,65 @@ void xmemory_free(void * ptr, const char * filename, int lineno) @endcode */ /*----------------------------------------------------------------------------*/ -void * xmemory_realloc(void * ptr, size_t size, const char * filename, int lineno) +void * +xmemory_realloc (void *ptr, size_t size, const char *filename, int lineno) { - void * ptr2 ; - size_t small_sz ; - size_t ptr_sz ; - int pos = -1 ; - int i ; - - /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ - if (XMEMORY_MODE == 0) return realloc(ptr, size) ; - else if (XMEMORY_MODE == 1) { - ptr2 = realloc(ptr, size) ; - if (ptr2 == NULL) exit(1) ; - else return ptr2 ; + void *ptr2; + size_t small_sz; + size_t ptr_sz; + int pos = -1; + int i; + + /* If XMEMORY_MODE is 0 or 1, do not use the xmemory model */ + if (XMEMORY_MODE == 0) + return realloc (ptr, size); + else if (XMEMORY_MODE == 1) + { + ptr2 = realloc (ptr, size); + if (ptr2 == NULL) + exit (1); + else + return ptr2; } - if (ptr == NULL) return xmemory_malloc(size, filename, lineno) ; + if (ptr == NULL) + return xmemory_malloc (size, filename, lineno); - /* Get the pointer size */ - for (i=0 ; i=1) - fprintf(stderr, "#----- memory diagnostics called from %s (%d) --------\n", - filename, - lineno); + fprintf (stderr, "#----- memory diagnostics called from %s (%d) --------\n", + filename, lineno); - fprintf(stderr, - "#- Peak memory usage\n" - "ALL_maxalloc_kb %ld\n" - "ALL_maxpointers %d\n", - (long)(xmemory_table.alloc_max/1024), - xmemory_table.max_cells); - fprintf(stderr, - "#- Local implementation\n" - "TAB_ptrs %d\n" - "TAB_size %ld bytes\n", - XMEMORY_MAXPTRS, - (long)sizeof(xmemory_table)); + fprintf (stderr, + "#- Peak memory usage\n" + "ALL_maxalloc_kb %ld\n" + "ALL_maxpointers %d\n", + (long) (xmemory_table.alloc_max / 1024), xmemory_table.max_cells); + fprintf (stderr, + "#- Local implementation\n" + "TAB_ptrs %d\n" + "TAB_size %ld bytes\n", + XMEMORY_MAXPTRS, (long) sizeof (xmemory_table)); #ifdef __linux__ - fprintf(stderr, - "#- Linux specific\n" - "LINUX_pagesize %d bytes\n" - "LINUX_RLIMIT_DATA %d kb\n", - xmemory_table.pagesize, - xmemory_table.rlimit_data); + fprintf (stderr, + "#- Linux specific\n" + "LINUX_pagesize %d bytes\n" + "LINUX_RLIMIT_DATA %d kb\n", + xmemory_table.pagesize, xmemory_table.rlimit_data); #endif #endif - if (xmemory_table.ncells<1) return ; - fprintf(stderr, "#----- memory status called from %s (%d) --------\n", - filename, - lineno); + if (xmemory_table.ncells < 1) + return; + fprintf (stderr, "#----- memory status called from %s (%d) --------\n", + filename, lineno); - fprintf(stderr, - "#- ALL status\n" - "ALL_npointers %d\n" - "ALL_size %ld\n" - "ALL_maxalloc_kb %ld\n" - "ALL_maxpointers %d\n", - xmemory_table.ncells, - (long)xmemory_table.alloc_total, - (long)(xmemory_table.alloc_max/1024), - xmemory_table.max_cells); + fprintf (stderr, + "#- ALL status\n" + "ALL_npointers %d\n" + "ALL_size %ld\n" + "ALL_maxalloc_kb %ld\n" + "ALL_maxpointers %d\n", + xmemory_table.ncells, + (long) xmemory_table.alloc_total, + (long) (xmemory_table.alloc_max / 1024), xmemory_table.max_cells); - if (xmemory_table.alloc_ram > 0) { - fprintf(stderr, - "#- RAM status\n" - "RAM_alloc %ld\n", - (long)xmemory_table.alloc_ram); + if (xmemory_table.alloc_ram > 0) + { + fprintf (stderr, + "#- RAM status\n" + "RAM_alloc %ld\n", (long) xmemory_table.alloc_ram); } - if (xmemory_table.alloc_swap > 0) { - fprintf(stderr, - "#- SWP status\n" - "SWP_alloc %ld\n" - "SWP_files %d\n", - (long)xmemory_table.alloc_swap, - xmemory_table.nswapfiles); + if (xmemory_table.alloc_swap > 0) + { + fprintf (stderr, + "#- SWP status\n" + "SWP_alloc %ld\n" + "SWP_files %d\n", + (long) xmemory_table.alloc_swap, xmemory_table.nswapfiles); } - if (xmemory_table.n_mm_files>0) { - fprintf(stderr, - "#- MAP status\n" - "MAP_files %d\n" - "MAP_mappings %d\n", - xmemory_table.n_mm_files, - xmemory_table.n_mm_mappings); + if (xmemory_table.n_mm_files > 0) + { + fprintf (stderr, + "#- MAP status\n" + "MAP_files %d\n" + "MAP_mappings %d\n", + xmemory_table.n_mm_files, xmemory_table.n_mm_mappings); } - fprintf(stderr, "#- pointer details\n"); - for (i=0 ; i #include #include - + #define MAXSTRSZ 512 #define MACROS_FILE "config.make" @@ -42,812 +42,986 @@ /* Global variable */ -struct { - /* List of supported OS's */ - enum { - os_linux, - os_cygwin, - os_hp08, - os_hp09, - os_hp10, - os_hp11, - os_solaris, - os_irix, - os_aix, - os_dec, - os_bsd, - os_darwin - } local_os ; - char sysname[MAXSTRSZ]; - char release[MAXSTRSZ]; - char machine[MAXSTRSZ]; +struct +{ + /* List of supported OS's */ + enum + { + os_linux, + os_cygwin, + os_hp08, + os_hp09, + os_hp10, + os_hp11, + os_solaris, + os_irix, + os_aix, + os_dec, + os_bsd, + os_darwin + } local_os; + char sysname[MAXSTRSZ]; + char release[MAXSTRSZ]; + char machine[MAXSTRSZ]; - /* Bits per byte on this machine */ - int bits_per_byte ; - /* Big endian machine? */ - int big_endian ; + /* Bits per byte on this machine */ + int bits_per_byte; + /* Big endian machine? */ + int big_endian; - /* x86 architecture? */ - int arch_x86 ; - /* CPU type: 386, 486, 586, 686 */ - int cpu_x86 ; + /* x86 architecture? */ + int arch_x86; + /* CPU type: 386, 486, 586, 686 */ + int cpu_x86; - /* Compile with debug options on */ - int debug_compile ; - /* Compile with warnings on */ - int lint_compile ; - /* Compiler type: native cc or gcc */ - int compiler ; - /* Compile with threads */ - int with_threads ; - /* Number of detected CPUs */ - int ncpus ; - /* Compile static library */ - int lib_static ; - /* Compile dynamic library */ - int lib_dynamic ; - /* Prefix for installation */ - char * prefix ; - /* With or without xmemory handling */ - int xmemory_mode ; - /* Maximum number of pointers allowed */ - int max_ptrs ; -} config ; + /* Compile with debug options on */ + int debug_compile; + /* Compile with warnings on */ + int lint_compile; + /* Compiler type: native cc or gcc */ + int compiler; + /* Compile with threads */ + int with_threads; + /* Number of detected CPUs */ + int ncpus; + /* Compile static library */ + int lib_static; + /* Compile dynamic library */ + int lib_dynamic; + /* Prefix for installation */ + char *prefix; + /* With or without xmemory handling */ + int xmemory_mode; + /* Maximum number of pointers allowed */ + int max_ptrs; +} config; /* * Check that all files present in the list of names are * somewhere present in the list of paths. */ -int config_findfiles(paths, names) - char ** paths ; - char ** names ; +int +config_findfiles (paths, names) + char **paths; + char **names; { - struct stat sta ; - char spath[MAXSTRSZ]; - int i, j ; - int nfound ; + struct stat sta; + char spath[MAXSTRSZ]; + int i, j; + int nfound; - /* Loop on all paths */ - i=0 ; - while (paths[i]) { - printf("searching in %s...\n", paths[i]); - /* Loop on all file names */ - j=0 ; - nfound=0 ; - while (names[j]) { - sprintf(spath, "%s/%s", paths[i], names[j]); - if (stat(spath, &sta)==0) { - nfound++ ; - } - j++ ; - } - if (j==nfound) { - printf("found in %s\n", paths[i]); - return i ; - } - i++ ; + /* Loop on all paths */ + i = 0; + while (paths[i]) + { + printf ("searching in %s...\n", paths[i]); + /* Loop on all file names */ + j = 0; + nfound = 0; + while (names[j]) + { + sprintf (spath, "%s/%s", paths[i], names[j]); + if (stat (spath, &sta) == 0) + { + nfound++; + } + j++; + } + if (j == nfound) + { + printf ("found in %s\n", paths[i]); + return i; + } + i++; } - return -1 ; + return -1; } /* Guess local OS name and potentially release number */ -void detect_config() +void +detect_config () { - struct utsname name ; - int i ; - unsigned char c ; - int x ; + struct utsname name; + int i; + unsigned char c; + int x; - if (config.sysname[0]==0) { - printf("detecting local OS............. "); - /* Get machine OS type and release number */ - if (uname(&name)==-1) { - printf("error calling uname\n"); - exit(-1); - } - printf("%s %s", name.sysname, name.release); - strcpy(config.sysname, name.sysname); - strcpy(config.release, name.release); - strcpy(config.machine, name.machine); - } else { - printf("forcing config for OS.......... %s %s", - config.sysname, - config.release); + if (config.sysname[0] == 0) + { + printf ("detecting local OS............. "); + /* Get machine OS type and release number */ + if (uname (&name) == -1) + { + printf ("error calling uname\n"); + exit (-1); + } + printf ("%s %s", name.sysname, name.release); + strcpy (config.sysname, name.sysname); + strcpy (config.release, name.release); + strcpy (config.machine, name.machine); + } + else + { + printf ("forcing config for OS.......... %s %s", + config.sysname, config.release); } - /* Lowercase everything */ - i=0 ; - while(config.sysname[i]!=0) { - config.sysname[i] = tolower(config.sysname[i]); - i++ ; - } - i=0 ; - while(config.release[i]!=0) { - config.release[i] = tolower(config.release[i]); - i++ ; - } - - /* Switch on OS type and release number */ - if (strstr(config.sysname, "linux")!=NULL) { - config.local_os = os_linux ; - printf(" - linux\n"); - } else if (strstr(config.sysname, "cygwin")!=NULL) { - config.local_os = os_cygwin ; - printf(" - cygwin\n"); - } else if (strstr(config.sysname, "hp")!=NULL) { - if (strstr(config.release, "8.")!=NULL) { - config.local_os = os_hp08 ; - printf(" - hpux_08\n"); - } else if (strstr(config.release, "9.")!=NULL) { - config.local_os = os_hp09 ; - printf(" - hpux_09\n"); - } else if (strstr(config.release, "10.")!=NULL) { - config.local_os = os_hp10 ; - printf(" - hpux_10\n"); - } else if (strstr(config.release, "11.")!=NULL) { - config.local_os = os_hp11 ; - printf(" - hpux_11\n"); - } - } else if ((strstr(config.sysname, "sun")!=NULL) || - (strstr(config.sysname, "solaris")!=NULL)) { - config.local_os = os_solaris ; - printf(" - solaris\n"); - } else if (strstr(config.sysname, "irix")!=NULL) { - config.local_os = os_irix ; - printf(" - irix\n"); - } else if (strstr(config.sysname, "aix")!=NULL) { - config.local_os = os_aix ; - printf(" - aix\n"); - } else if (strstr(config.sysname, "osf")!=NULL) { - config.local_os = os_dec ; - printf(" - osf/1\n"); - } else if (strstr(config.sysname, "bsd")!=NULL) { - config.local_os = os_bsd ; - printf(" - %s\n", config.sysname); - } else if (strstr(config.sysname, "darwin")!=NULL) { - config.local_os = os_darwin ; - printf(" - %s\n", config.sysname); - } else { - printf("cannot identify your OS\n"); - printf("Use the option --os=NAME to force an OS type\n"); - exit(-1); - } - - /* Find out about x86 architectures */ - if ((config.machine[0]=='i') && (strstr(config.machine, "86")!=NULL)) { - config.arch_x86 = 1 ; - config.cpu_x86 = atoi(config.machine+1) ; - printf("detected x86 architecture...... %d\n", config.cpu_x86); - } else { - config.arch_x86 = 0 ; - printf("detected x86 architecture...... no\n"); + /* Lowercase everything */ + i = 0; + while (config.sysname[i] != 0) + { + config.sysname[i] = tolower (config.sysname[i]); + i++; } - - /* Compute number of bits in a byte on this machine */ - printf("computing bits per byte........ "); - c=1 ; i=0 ; - while (c) { - c<<=1 ; i++ ; - } - config.bits_per_byte = i ; - printf("%d\n", i); - - /* Detect endian-ness */ - printf("detecting byte-order........... "); - x = 1 ; - x = (*(char*)&x) ; - - config.big_endian = !x ; - if (config.big_endian) { - printf("big endian (motorola)\n"); - } else { - printf("little endian (intel)\n"); + i = 0; + while (config.release[i] != 0) + { + config.release[i] = tolower (config.release[i]); + i++; } - return ; + /* Switch on OS type and release number */ + if (strstr (config.sysname, "linux") != NULL) + { + config.local_os = os_linux; + printf (" - linux\n"); + } + else if (strstr (config.sysname, "cygwin") != NULL) + { + config.local_os = os_cygwin; + printf (" - cygwin\n"); + } + else if (strstr (config.sysname, "hp") != NULL) + { + if (strstr (config.release, "8.") != NULL) + { + config.local_os = os_hp08; + printf (" - hpux_08\n"); + } + else if (strstr (config.release, "9.") != NULL) + { + config.local_os = os_hp09; + printf (" - hpux_09\n"); + } + else if (strstr (config.release, "10.") != NULL) + { + config.local_os = os_hp10; + printf (" - hpux_10\n"); + } + else if (strstr (config.release, "11.") != NULL) + { + config.local_os = os_hp11; + printf (" - hpux_11\n"); + } + } + else if ((strstr (config.sysname, "sun") != NULL) || + (strstr (config.sysname, "solaris") != NULL)) + { + config.local_os = os_solaris; + printf (" - solaris\n"); + } + else if (strstr (config.sysname, "irix") != NULL) + { + config.local_os = os_irix; + printf (" - irix\n"); + } + else if (strstr (config.sysname, "aix") != NULL) + { + config.local_os = os_aix; + printf (" - aix\n"); + } + else if (strstr (config.sysname, "osf") != NULL) + { + config.local_os = os_dec; + printf (" - osf/1\n"); + } + else if (strstr (config.sysname, "bsd") != NULL) + { + config.local_os = os_bsd; + printf (" - %s\n", config.sysname); + } + else if (strstr (config.sysname, "darwin") != NULL) + { + config.local_os = os_darwin; + printf (" - %s\n", config.sysname); + } + else + { + printf ("cannot identify your OS\n"); + printf ("Use the option --os=NAME to force an OS type\n"); + exit (-1); + } + + /* Find out about x86 architectures */ + if ((config.machine[0] == 'i') && (strstr (config.machine, "86") != NULL)) + { + config.arch_x86 = 1; + config.cpu_x86 = atoi (config.machine + 1); + printf ("detected x86 architecture...... %d\n", config.cpu_x86); + } + else + { + config.arch_x86 = 0; + printf ("detected x86 architecture...... no\n"); + } + + /* Compute number of bits in a byte on this machine */ + printf ("computing bits per byte........ "); + c = 1; + i = 0; + while (c) + { + c <<= 1; + i++; + } + config.bits_per_byte = i; + printf ("%d\n", i); + + /* Detect endian-ness */ + printf ("detecting byte-order........... "); + x = 1; + x = (*(char *) &x); + + config.big_endian = !x; + if (config.big_endian) + { + printf ("big endian (motorola)\n"); + } + else + { + printf ("little endian (intel)\n"); + } + + return; } /* Locate a program in the user's PATH */ -int locate_program(pname) - char * pname ; +int +locate_program (pname) + char *pname; { - int i, j, lg; - char * path ; - char buf[MAXSTRSZ]; + int i, j, lg; + char *path; + char buf[MAXSTRSZ]; - path = getenv("PATH"); - if (path!=NULL) { - for (i=0; path[i]; ) { - for (j=i ; (path[j]) && (path[j]!=':') ; j++); - lg = j - i; - strncpy(buf, path + i, lg); - if (lg == 0) buf[lg++] = '.'; - buf[lg++] = '/'; - strcpy(buf + lg, pname); - if (access(buf, X_OK) == 0) { - printf("using [%s]\n", buf); - return 1 ; - } - buf[0] = 0; - i = j; - if (path[i] == ':') i++ ; - } - } else { - /* No PATH variable: abort with an error */ - return -1 ; + path = getenv ("PATH"); + if (path != NULL) + { + for (i = 0; path[i];) + { + for (j = i; (path[j]) && (path[j] != ':'); j++); + lg = j - i; + strncpy (buf, path + i, lg); + if (lg == 0) + buf[lg++] = '.'; + buf[lg++] = '/'; + strcpy (buf + lg, pname); + if (access (buf, X_OK) == 0) + { + printf ("using [%s]\n", buf); + return 1; + } + buf[0] = 0; + i = j; + if (path[i] == ':') + i++; + } } - /* If the buffer is still empty, the command was not found */ - if (buf[0] == 0) return 0 ; - /* Otherwise Ok */ - return 1 ; -} - -void make_config_make() -{ - FILE * sysc ; - - /* Set compiler name */ - printf("looking for a C compiler....... "); - if (getenv("PATH")==NULL) { - printf("error: undefined PATH variable, cannot locate a compiler\n"); - exit(-1) ; - } - - /* Open output sysconf */ - if ((sysc=fopen(MACROS_FILE, "w"))==NULL) { - printf("cannot create %s: aborting compilation", MACROS_FILE); - exit(-1) ; - } - - /* In automatic mode, find out which compiler to use */ - if (config.compiler==COMPILER_AUTO) { - switch (config.local_os) { - - /* Use gcc under Linux, BSD, Cygwin & IRIX */ - case os_linux: - case os_cygwin: - case os_bsd: - case os_irix: - config.compiler = COMPILER_GCC ; - break ; - - /* All others use 'cc' (default) */ - default: - config.compiler = COMPILER_CC ; - break ; - } - } - - - /* Make sure the compiler can be found */ - if (config.compiler==COMPILER_CC) { - if (locate_program("cc")!=1) { - printf("cannot locate cc\n"); - /* Try out with gcc */ - config.compiler = COMPILER_GCC ; - } - } - if (config.compiler==COMPILER_GCC) { - if (locate_program("gcc")!=1) { - printf("cannot locate gcc\n"); - exit(-1); - } - } - - - /* Set compiler name and cflags */ - switch (config.compiler) { - - case COMPILER_CC: - fprintf(sysc, "CC = cc\n"); - fprintf(sysc, "CFLAGS = "); - if (config.xmemory_mode == 0) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - fprintf(sysc, "-DXMEMORY_MAXPTRS=1 ") ; - } else if (config.xmemory_mode == 1) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - fprintf(sysc, "-DXMEMORY_MAXPTRS=1 ") ; - } else if (config.xmemory_mode == 2) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - if (config.max_ptrs > 0) { - fprintf(sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs) ; - } - } else if (config.xmemory_mode < 0) { - if (config.max_ptrs > 0) { - fprintf(sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs) ; - } - } - switch (config.local_os) { - case os_hp08: - case os_hp09: - case os_hp10: - if (config.with_threads) { - printf("threads not supported on this platform\n"); - } - if (config.debug_compile) { - fprintf(sysc, "-Ae -g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-Ae -O\n"); - } - fprintf(sysc, "RELOC = +z\n"); - fprintf(sysc, "SHARED = -b\n"); - break ; - - case os_hp11: - if (config.with_threads) { - fprintf(sysc, "-lpthread "); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O\n"); - } - fprintf(sysc, "RELOC = +z\n"); - fprintf(sysc, "SHARED = -b\n"); - break ; - - case os_solaris: - if (config.with_threads) { - fprintf(sysc, "-mt -lpthread "); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-xO5\n"); - } - fprintf(sysc, "RELOC = -G\n"); - fprintf(sysc, "SHARED = -G\n"); - break ; - - case os_dec: - if (config.with_threads) { - printf("threads not supported on this platform\n"); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O\n"); - } - fprintf(sysc, "RELOC =\n"); - fprintf(sysc, "SHARED = -shared -expect_unresolved \"*\"\n"); - break ; - - case os_irix: - case os_aix: - case os_bsd: - if (config.with_threads) { - printf("threads not supported on this platform\n"); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O\n"); - } - fprintf(sysc, "RELOC =\n"); - fprintf(sysc, "SHARED =\n"); - break ; - - case os_cygwin: - case os_linux: - /* cc with Linux? Why not! */ - if (config.with_threads) { - fprintf(sysc, "-pthread "); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O3\n"); - } - fprintf(sysc, "RELOC = -fpic\n"); - fprintf(sysc, "SHARED = -shared\n"); - break ; - - case os_darwin: - /* Darwin uses gcc but calls it 'cc' */ - if (config.with_threads) { - fprintf(sysc, "-pthread "); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O3\n"); - } - fprintf(sysc, "RELOC = -fPIC\n"); - fprintf(sysc, "SHARED = -shared\n"); - break ; - - - default: - printf("error: unsupported OS\n"); - exit(-1) ; - } - break ; - - case COMPILER_GCC: - fprintf(sysc, "CC = gcc\n"); - fprintf(sysc, "CFLAGS = "); - - if (config.xmemory_mode == 0) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - fprintf(sysc, "-DXMEMORY_MAXPTRS=1 ") ; - } else if (config.xmemory_mode == 1) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - fprintf(sysc, "-DXMEMORY_MAXPTRS=1 ") ; - } else if (config.xmemory_mode == 2) { - fprintf(sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode) ; - if (config.max_ptrs > 0) { - fprintf(sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs) ; - } - } else if (config.xmemory_mode < 0) { - if (config.max_ptrs > 0) { - fprintf(sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs) ; - } - } - if (config.with_threads) { - fprintf(sysc, "-pthread "); - } - if (config.lint_compile) { - fprintf(sysc, " -Wall -pedantic "); - } - if (config.debug_compile) { - fprintf(sysc, "-g%s\n", XMEMDEBUG); - } else { - fprintf(sysc, "-O3\n"); - } - if (config.local_os != os_cygwin) - fprintf(sysc, "RELOC = -fPIC -DPIC\n"); - fprintf(sysc, "SHARED = -shared\n"); - break ; - - default: - printf("error in compiler option switch: aborting\n"); - exit(-1); - break ; - } - - if (config.debug_compile) { - printf(" in debug mode\n"); - } else { - printf(" all optimizations on\n"); - } - if (config.lint_compile) { - printf(" with all warnings\n"); - } - - /* LFLAGS */ - fprintf(sysc, "LFLAGS = "); - switch (config.local_os) { - - case os_hp10: - case os_hp11: - fprintf(sysc, "-Wl,+vnocompatwarnings "); - break ; - - default: - break ; - } - fprintf(sysc, "\n"); - - /* DYNSUF */ - switch (config.local_os) { - case os_hp08: - case os_hp09: - case os_hp10: - case os_hp11: - fprintf(sysc, "DYNSUF = sl\n"); - break ; - - case os_darwin: - fprintf(sysc, "DYNSUF = so\n"); - break ; - - - default: - fprintf(sysc, "DYNSUF = so\n"); - break ; - } - - /* STRIP */ - if (config.debug_compile) { - fprintf(sysc, "STRIP = true\n"); - } else { - fprintf(sysc, "STRIP = strip\n"); - } - - /* TARGETS */ - fprintf(sysc, "TARGETS ="); - printf("static library................. %s\n", - config.lib_static ? "yes" : "no"); - if (config.lib_static) { - fprintf(sysc, " static"); - } - printf("shared library................. %s\n", - config.lib_dynamic ? "yes" : "no"); - if (config.lib_dynamic) { - fprintf(sysc, " dynamic"); - } - fprintf(sysc,"\n"); - - /* PREFIX */ - fprintf(sysc, "prefix = %s\n", - config.prefix ? config.prefix : PREFIX_DEFAULT); - printf("setting installation prefix as: [%s]\n", - config.prefix ? config.prefix : PREFIX_DEFAULT); - - fclose(sysc); + else + { + /* No PATH variable: abort with an error */ + return -1; + } + /* If the buffer is still empty, the command was not found */ + if (buf[0] == 0) + return 0; + /* Otherwise Ok */ + return 1; } - -void make_config_h() +void +make_config_make () { - FILE * out ; + FILE *sysc; - out = fopen(HEADER_FILE, "w"); - if (out==NULL) { - printf("cannot create header file %s: aborting", HEADER_FILE); - return ; - } - - fprintf(out, - "/* This file automatically generated */\n" - "#ifndef _CONFIG_H_\n" - "#define _CONFIG_H_\n" - "\n" - "%s", - config.big_endian ? "#define WORDS_BIGENDIAN 1\n" : "\n" - ); - - /* Generate SIZEOF macros for basic types */ - printf("detecting basic size types\n"); - fprintf(out, - "#define SIZEOF_CHAR %d\n" - "#define SIZEOF_SHORT %d\n" - "#define SIZEOF_INT %d\n" - "#define SIZEOF_LONG %d\n" - "#define SIZEOF_FLOAT %d\n" - "#define SIZEOF_DOUBLE %d\n", - sizeof(char), - sizeof(short), - sizeof(int), - sizeof(long), - sizeof(float), - sizeof(double)); - printf( - "sizeof(char)................... %d\n" - "sizeof(short).................. %d\n" - "sizeof(int).................... %d\n" - "sizeof(long)................... %d\n" - "sizeof(float).................. %d\n" - "sizeof(double)................. %d\n", - sizeof(char), - sizeof(short), - sizeof(int), - sizeof(long), - sizeof(float), - sizeof(double)); - - /* Do not output CHAR_BIT on AIX */ - if (config.local_os!=os_aix) { - fprintf(out, - "\n" - "#ifndef CHAR_BIT\n" - "#define CHAR_BIT\t%d\n" - "#endif\n" - "\n\n", - config.bits_per_byte); - } - - if (config.arch_x86) { - fprintf(out, "#define CPU_X86 %d\n", config.cpu_x86); + /* Set compiler name */ + printf ("looking for a C compiler....... "); + if (getenv ("PATH") == NULL) + { + printf ("error: undefined PATH variable, cannot locate a compiler\n"); + exit (-1); } - if (config.with_threads) { - fprintf(out, "#define HAS_PTHREADS 1\n"); + /* Open output sysconf */ + if ((sysc = fopen (MACROS_FILE, "w")) == NULL) + { + printf ("cannot create %s: aborting compilation", MACROS_FILE); + exit (-1); + } + + /* In automatic mode, find out which compiler to use */ + if (config.compiler == COMPILER_AUTO) + { + switch (config.local_os) + { + + /* Use gcc under Linux, BSD, Cygwin & IRIX */ + case os_linux: + case os_cygwin: + case os_bsd: + case os_irix: + config.compiler = COMPILER_GCC; + break; + + /* All others use 'cc' (default) */ + default: + config.compiler = COMPILER_CC; + break; } - fprintf(out, "\n"); - switch (config.local_os) { - case os_hp08: - case os_hp09: - case os_hp10: - case os_hp11: - fprintf(out, "#define OS_HPUX 1\n"); - break ; + } - case os_linux: - fprintf(out, "#define OS_LINUX 1\n"); - break ; - case os_cygwin: - fprintf(out, "#define OS_CYGWIN 1\n"); - break ; - - case os_irix: - fprintf(out, "#define OS_IRIX 1\n"); - break ; - - case os_aix: - fprintf(out, "#define OS_AIX 1\n"); - break ; - - case os_dec: - fprintf(out, "#define OS_DEC 1\n"); - break ; - - case os_solaris: - fprintf(out, "#define OS_SOLARIS 1\n"); - break ; - - case os_bsd: - fprintf(out, "#define OS_BSD 1\n"); - break ; - - case os_darwin: - fprintf(out, "#define OS_DARWIN 1\n"); - break ; - - default: - fprintf(out, "#define OS_UNKNOWN 1\n"); - break ; + /* Make sure the compiler can be found */ + if (config.compiler == COMPILER_CC) + { + if (locate_program ("cc") != 1) + { + printf ("cannot locate cc\n"); + /* Try out with gcc */ + config.compiler = COMPILER_GCC; } - fprintf(out, "#endif\n"); - fclose(out); - printf("done\n"); - return ; + } + if (config.compiler == COMPILER_GCC) + { + if (locate_program ("gcc") != 1) + { + printf ("cannot locate gcc\n"); + exit (-1); + } + } + + /* Set compiler name and cflags */ + switch (config.compiler) + { + + case COMPILER_CC: + fprintf (sysc, "CC = cc\n"); + fprintf (sysc, "CFLAGS = "); + if (config.xmemory_mode == 0) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + fprintf (sysc, "-DXMEMORY_MAXPTRS=1 "); + } + else if (config.xmemory_mode == 1) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + fprintf (sysc, "-DXMEMORY_MAXPTRS=1 "); + } + else if (config.xmemory_mode == 2) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + if (config.max_ptrs > 0) + { + fprintf (sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs); + } + } + else if (config.xmemory_mode < 0) + { + if (config.max_ptrs > 0) + { + fprintf (sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs); + } + } + switch (config.local_os) + { + case os_hp08: + case os_hp09: + case os_hp10: + if (config.with_threads) + { + printf ("threads not supported on this platform\n"); + } + if (config.debug_compile) + { + fprintf (sysc, "-Ae -g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-Ae -O\n"); + } + fprintf (sysc, "RELOC = +z\n"); + fprintf (sysc, "SHARED = -b\n"); + break; + + case os_hp11: + if (config.with_threads) + { + fprintf (sysc, "-lpthread "); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O\n"); + } + fprintf (sysc, "RELOC = +z\n"); + fprintf (sysc, "SHARED = -b\n"); + break; + + case os_solaris: + if (config.with_threads) + { + fprintf (sysc, "-mt -lpthread "); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-xO5\n"); + } + fprintf (sysc, "RELOC = -G\n"); + fprintf (sysc, "SHARED = -G\n"); + break; + + case os_dec: + if (config.with_threads) + { + printf ("threads not supported on this platform\n"); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O\n"); + } + fprintf (sysc, "RELOC =\n"); + fprintf (sysc, "SHARED = -shared -expect_unresolved \"*\"\n"); + break; + + case os_irix: + case os_aix: + case os_bsd: + if (config.with_threads) + { + printf ("threads not supported on this platform\n"); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O\n"); + } + fprintf (sysc, "RELOC =\n"); + fprintf (sysc, "SHARED =\n"); + break; + + case os_cygwin: + case os_linux: + /* cc with Linux? Why not! */ + if (config.with_threads) + { + fprintf (sysc, "-pthread "); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O3\n"); + } + fprintf (sysc, "RELOC = -fpic\n"); + fprintf (sysc, "SHARED = -shared\n"); + break; + + case os_darwin: + /* Darwin uses gcc but calls it 'cc' */ + if (config.with_threads) + { + fprintf (sysc, "-pthread "); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O3\n"); + } + fprintf (sysc, "RELOC = -fPIC\n"); + fprintf (sysc, "SHARED = -shared\n"); + break; + + + default: + printf ("error: unsupported OS\n"); + exit (-1); + } + break; + + case COMPILER_GCC: + fprintf (sysc, "CC = gcc\n"); + fprintf (sysc, "CFLAGS = "); + + if (config.xmemory_mode == 0) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + fprintf (sysc, "-DXMEMORY_MAXPTRS=1 "); + } + else if (config.xmemory_mode == 1) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + fprintf (sysc, "-DXMEMORY_MAXPTRS=1 "); + } + else if (config.xmemory_mode == 2) + { + fprintf (sysc, "-DXMEMORY_MODE=%d ", config.xmemory_mode); + if (config.max_ptrs > 0) + { + fprintf (sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs); + } + } + else if (config.xmemory_mode < 0) + { + if (config.max_ptrs > 0) + { + fprintf (sysc, "-DXMEMORY_MAXPTRS=%d ", config.max_ptrs); + } + } + if (config.with_threads) + { + fprintf (sysc, "-pthread "); + } + if (config.lint_compile) + { + fprintf (sysc, " -Wall -pedantic "); + } + if (config.debug_compile) + { + fprintf (sysc, "-g%s\n", XMEMDEBUG); + } + else + { + fprintf (sysc, "-O3\n"); + } + if (config.local_os != os_cygwin) + fprintf (sysc, "RELOC = -fPIC -DPIC\n"); + fprintf (sysc, "SHARED = -shared\n"); + break; + + default: + printf ("error in compiler option switch: aborting\n"); + exit (-1); + break; + } + + if (config.debug_compile) + { + printf (" in debug mode\n"); + } + else + { + printf (" all optimizations on\n"); + } + if (config.lint_compile) + { + printf (" with all warnings\n"); + } + + /* LFLAGS */ + fprintf (sysc, "LFLAGS = "); + switch (config.local_os) + { + + case os_hp10: + case os_hp11: + fprintf (sysc, "-Wl,+vnocompatwarnings "); + break; + + default: + break; + } + fprintf (sysc, "\n"); + + /* DYNSUF */ + switch (config.local_os) + { + case os_hp08: + case os_hp09: + case os_hp10: + case os_hp11: + fprintf (sysc, "DYNSUF = sl\n"); + break; + + case os_darwin: + fprintf (sysc, "DYNSUF = so\n"); + break; + + + default: + fprintf (sysc, "DYNSUF = so\n"); + break; + } + + /* STRIP */ + if (config.debug_compile) + { + fprintf (sysc, "STRIP = true\n"); + } + else + { + fprintf (sysc, "STRIP = strip\n"); + } + + /* TARGETS */ + fprintf (sysc, "TARGETS ="); + printf ("static library................. %s\n", + config.lib_static ? "yes" : "no"); + if (config.lib_static) + { + fprintf (sysc, " static"); + } + printf ("shared library................. %s\n", + config.lib_dynamic ? "yes" : "no"); + if (config.lib_dynamic) + { + fprintf (sysc, " dynamic"); + } + fprintf (sysc, "\n"); + + /* PREFIX */ + fprintf (sysc, "prefix = %s\n", + config.prefix ? config.prefix : PREFIX_DEFAULT); + printf ("setting installation prefix as: [%s]\n", + config.prefix ? config.prefix : PREFIX_DEFAULT); + + fclose (sysc); } -void help() + +void +make_config_h () { - printf( - "\n\n" - "***** qfits configure help\n" - "Use: configure [options]\n" - "\n" - "options are:\n" - "\t--debug Compile modules in debug mode\n" - "\t--help Get this help\n" - "\n" - "\t--with-cc Force compilation with local cc\n" - "\t--with-gcc Force compilation with gcc\n" - "\n" - "\t--enable-static Compile static library (default)\n" - "\t--enable-shared Compile shared library\n" - "\t--disable-shared Do not compile shared library (default)\n" - "\t--disable-static Do not compile static library\n" - "\n" - "\t--xmemory-mode=M M=0 switches off the internal xmemory\n" - "\t handling, M=1 exits the program\n" - "\t whenever a memory allocation fails,\n" - "\t M=2 switches on the internal xmemory\n" - "\t handling.\n" - "\n" - "\t--max-ptrs=MAXPTRS Set MAXPTRS as the maximum number of\n" - "\t pointers allowed\n" - "\n" - "\t--prefix=PATH Install in PATH (must be absolute)\n" - "\t--mt Compile with multithreading support\n" - "\n" - "options specific to compilation with gcc (for developers):\n" - "\t--lint Compile with -Wall\n" - "\n" - "If your platform is not or incorrectly recognized, you\n" - "can force a given configuration with this option:\n" - "\n" - "\t--os=NAME Where NAME is one of the following:\n" - "\n" - "\tlinux - Linux systems (any processor type)\n" - "\tcygwin - Cygwin (UNIX environment for Windows)\n" - "\thp08 - HPUX version 8.x\n" - "\thp09 - HPUX version 9.x\n" - "\thp10 - HPUX version 10.x\n" - "\thp11 - HPUX version 11.x\n" - "\tirix - SGI IRIX64\n" - "\taix - IBM AIX (any version)\n" - "\tdec - Dec OSF/1 or Tru64 Unix\n" - "\tsolaris - Sun Solaris >=2.5\n" - "\tbsd - BSD compatible Unix\n" - "\tdarwin - Darwin (BSD compatible on Mac)\n" - "\n" - ); + FILE *out; + + out = fopen (HEADER_FILE, "w"); + if (out == NULL) + { + printf ("cannot create header file %s: aborting", HEADER_FILE); + return; + } + + fprintf (out, + "/* This file automatically generated */\n" + "#ifndef _CONFIG_H_\n" + "#define _CONFIG_H_\n" + "\n" + "%s", config.big_endian ? "#define WORDS_BIGENDIAN 1\n" : "\n"); + + /* Generate SIZEOF macros for basic types */ + printf ("detecting basic size types\n"); + fprintf (out, + "#define SIZEOF_CHAR %d\n" + "#define SIZEOF_SHORT %d\n" + "#define SIZEOF_INT %d\n" + "#define SIZEOF_LONG %d\n" + "#define SIZEOF_FLOAT %d\n" + "#define SIZEOF_DOUBLE %d\n", + sizeof (char), + sizeof (short), + sizeof (int), sizeof (long), sizeof (float), sizeof (double)); + printf ("sizeof(char)................... %d\n" + "sizeof(short).................. %d\n" + "sizeof(int).................... %d\n" + "sizeof(long)................... %d\n" + "sizeof(float).................. %d\n" + "sizeof(double)................. %d\n", + sizeof (char), + sizeof (short), + sizeof (int), sizeof (long), sizeof (float), sizeof (double)); + + /* Do not output CHAR_BIT on AIX */ + if (config.local_os != os_aix) + { + fprintf (out, + "\n" + "#ifndef CHAR_BIT\n" + "#define CHAR_BIT\t%d\n" + "#endif\n" "\n\n", config.bits_per_byte); + } + + if (config.arch_x86) + { + fprintf (out, "#define CPU_X86 %d\n", config.cpu_x86); + } + + if (config.with_threads) + { + fprintf (out, "#define HAS_PTHREADS 1\n"); + } + fprintf (out, "\n"); + switch (config.local_os) + { + case os_hp08: + case os_hp09: + case os_hp10: + case os_hp11: + fprintf (out, "#define OS_HPUX 1\n"); + break; + + case os_linux: + fprintf (out, "#define OS_LINUX 1\n"); + break; + + case os_cygwin: + fprintf (out, "#define OS_CYGWIN 1\n"); + break; + + case os_irix: + fprintf (out, "#define OS_IRIX 1\n"); + break; + + case os_aix: + fprintf (out, "#define OS_AIX 1\n"); + break; + + case os_dec: + fprintf (out, "#define OS_DEC 1\n"); + break; + + case os_solaris: + fprintf (out, "#define OS_SOLARIS 1\n"); + break; + + case os_bsd: + fprintf (out, "#define OS_BSD 1\n"); + break; + + case os_darwin: + fprintf (out, "#define OS_DARWIN 1\n"); + break; + + default: + fprintf (out, "#define OS_UNKNOWN 1\n"); + break; + } + fprintf (out, "#endif\n"); + fclose (out); + printf ("done\n"); + return; + } -int main(argc, argv) - int argc ; - char * argv[]; +void +help () { - char sysname[MAXSTRSZ]; - char * sval ; - int i ; - - config.debug_compile = 0; - config.lint_compile = 0 ; - config.compiler = COMPILER_AUTO ; - config.with_threads = 0 ; - config.lib_static = 1 ; - config.lib_dynamic = 0 ; - config.prefix = NULL ; - config.max_ptrs = -1 ; - config.xmemory_mode = -1 ; - - memset(config.sysname, 0, MAXSTRSZ); - memset(config.release, 0, MAXSTRSZ); - memset(config.machine, 0, MAXSTRSZ); - - for (i=0 ; i= 2.5"); - } else if (!strcmp(sysname, "bsd")) { - strcpy(config.sysname, "BSD compatible"); - } else if (!strcmp(sysname, "darwin")) { - strcpy(config.sysname, "Darwin"); - } else { - printf("unsupported OS: %s\n", sysname); - return -1 ; - } - } - } - - detect_config(); - make_config_make(); - make_config_h(); - return 0 ; + printf ("\n\n" + "***** qfits configure help\n" + "Use: configure [options]\n" + "\n" + "options are:\n" + "\t--debug Compile modules in debug mode\n" + "\t--help Get this help\n" + "\n" + "\t--with-cc Force compilation with local cc\n" + "\t--with-gcc Force compilation with gcc\n" + "\n" + "\t--enable-static Compile static library (default)\n" + "\t--enable-shared Compile shared library\n" + "\t--disable-shared Do not compile shared library (default)\n" + "\t--disable-static Do not compile static library\n" + "\n" + "\t--xmemory-mode=M M=0 switches off the internal xmemory\n" + "\t handling, M=1 exits the program\n" + "\t whenever a memory allocation fails,\n" + "\t M=2 switches on the internal xmemory\n" + "\t handling.\n" + "\n" + "\t--max-ptrs=MAXPTRS Set MAXPTRS as the maximum number of\n" + "\t pointers allowed\n" + "\n" + "\t--prefix=PATH Install in PATH (must be absolute)\n" + "\t--mt Compile with multithreading support\n" + "\n" + "options specific to compilation with gcc (for developers):\n" + "\t--lint Compile with -Wall\n" + "\n" + "If your platform is not or incorrectly recognized, you\n" + "can force a given configuration with this option:\n" + "\n" + "\t--os=NAME Where NAME is one of the following:\n" + "\n" + "\tlinux - Linux systems (any processor type)\n" + "\tcygwin - Cygwin (UNIX environment for Windows)\n" + "\thp08 - HPUX version 8.x\n" + "\thp09 - HPUX version 9.x\n" + "\thp10 - HPUX version 10.x\n" + "\thp11 - HPUX version 11.x\n" + "\tirix - SGI IRIX64\n" + "\taix - IBM AIX (any version)\n" + "\tdec - Dec OSF/1 or Tru64 Unix\n" + "\tsolaris - Sun Solaris >=2.5\n" + "\tbsd - BSD compatible Unix\n" + "\tdarwin - Darwin (BSD compatible on Mac)\n" "\n"); } +int +main (argc, argv) + int argc; + char *argv[]; +{ + char sysname[MAXSTRSZ]; + char *sval; + int i; + config.debug_compile = 0; + config.lint_compile = 0; + config.compiler = COMPILER_AUTO; + config.with_threads = 0; + config.lib_static = 1; + config.lib_dynamic = 0; + config.prefix = NULL; + config.max_ptrs = -1; + config.xmemory_mode = -1; + memset (config.sysname, 0, MAXSTRSZ); + memset (config.release, 0, MAXSTRSZ); + memset (config.machine, 0, MAXSTRSZ); + + for (i = 0; i < argc; i++) + { + if (!strcmp (argv[i], "--help")) + { + help (); + return 1; + } + else if (!strcmp (argv[i], "--debug")) + { + config.debug_compile = 1; + } + else if (!strcmp (argv[i], "--with-cc")) + { + config.compiler = COMPILER_CC; + } + else if (!strcmp (argv[i], "--with-gcc")) + { + config.compiler = COMPILER_GCC; + } + else if (!strcmp (argv[i], "--lint")) + { + config.lint_compile = 1; + } + else if (!strcmp (argv[i], "--mt")) + { + config.with_threads = 1; + } + else if (!strcmp (argv[i], "--enable-shared")) + { + config.lib_dynamic = 1; + } + else if (!strcmp (argv[i], "--disable-shared")) + { + config.lib_dynamic = 0; + } + else if (!strcmp (argv[i], "--enable-static")) + { + config.lib_static = 1; + } + else if (!strcmp (argv[i], "--disable-static")) + { + config.lib_static = 0; + } + else if (!strncmp (argv[i], "--prefix=", 9)) + { + config.prefix = strchr (argv[i], '=') + 1; + } + else if (!strncmp (argv[i], "--max-ptrs=", 11)) + { + sval = strchr (argv[i], '=') + 1; + config.max_ptrs = (int) atoi (sval); + } + else if (!strncmp (argv[i], "--xmemory-mode=", 15)) + { + sval = strchr (argv[i], '=') + 1; + config.xmemory_mode = (int) atoi (sval); + } + else if (!strncmp (argv[i], "--os=", 5)) + { + strcpy (sysname, argv[i] + 5); + if (!strcmp (sysname, "linux")) + { + strcpy (config.sysname, "Linux"); + } + else if (!strcmp (sysname, "cygwin")) + { + strcpy (config.sysname, "CYGWIN"); + } + else if (!strcmp (sysname, "hp08")) + { + strcpy (config.sysname, "HPUX"); + strcpy (config.release, "8.x"); + } + else if (!strcmp (sysname, "hp09")) + { + strcpy (config.sysname, "HPUX"); + strcpy (config.release, "9.x"); + } + else if (!strcmp (sysname, "hp10")) + { + strcpy (config.sysname, "HPUX"); + strcpy (config.release, "10.x"); + } + else if (!strcmp (sysname, "hp11")) + { + strcpy (config.sysname, "HPUX"); + strcpy (config.release, "11.x"); + } + else if (!strcmp (sysname, "irix")) + { + strcpy (config.sysname, "IRIX64"); + } + else if (!strcmp (sysname, "aix")) + { + strcpy (config.sysname, "AIX"); + } + else if (!strcmp (sysname, "dec")) + { + strcpy (config.sysname, "Dec OSF/1 or Tru64"); + } + else if (!strcmp (sysname, "solaris")) + { + strcpy (config.sysname, "Solaris"); + strcpy (config.release, ">= 2.5"); + } + else if (!strcmp (sysname, "bsd")) + { + strcpy (config.sysname, "BSD compatible"); + } + else if (!strcmp (sysname, "darwin")) + { + strcpy (config.sysname, "Darwin"); + } + else + { + printf ("unsupported OS: %s\n", sysname); + return -1; + } + } + } + + detect_config (); + make_config_make (); + make_config_h (); + return 0; +}