/*---------------------------------------------------------------------------- File name : replacekey.c Author : N. Devillard Created on : July 14th, 1998 Description : Search & Replace operations in FITS headers ---------------------------------------------------------------------------*/ /* $Id: replacekey.c,v 1.1 2001/12/14 09:15:19 ndevilla Exp $ $Author: ndevilla $ $Date: 2001/12/14 09:15:19 $ $Revision: 1.1 $ */ /*---------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include /*---------------------------------------------------------------------------- Defines ---------------------------------------------------------------------------*/ #define NM_SIZ 512 #define DEFAULT_PLACEHOLDER "COMMENT PLACEHOLDER" /*---------------------------------------------------------------------------- Private functions and module variables ---------------------------------------------------------------------------*/ static char prog_desc[] = "replace keyholder in a FITS header" ; static void usage(char *) ; static int replace_placeholder(char *, char *, char*) ; static int get_FITS_header_size(char *) ; /*---------------------------------------------------------------------------- main() ---------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { char name_in[NM_SIZ] ; char placeholder[NM_SIZ] ; char card[NM_SIZ] ; if (argc<3) usage(argv[0]) ; strncpy(name_in, argv[1], NM_SIZ) ; strncpy(card, argv[2], NM_SIZ) ; if (argc==4) { strncpy(placeholder, argv[3], NM_SIZ) ; } else { strncpy(placeholder, DEFAULT_PLACEHOLDER, NM_SIZ); } /* * Sanity tests */ if (strlen(card)>80) { fprintf(stderr, "requested card is too long: %d chars\n",strlen(card)); return -1; } if (replace_placeholder(name_in, card, placeholder) != 0) { fprintf(stderr, "error during placeholder replacement: aborting\n") ; } return 0 ; } /* * This function only gives the usage for the program */ static void usage(char *pname) { printf("%s : %s\n", pname, prog_desc) ; printf("use : %s [placeholder]\n", pname) ; printf("\n\n") ; exit(0) ; } /*--------------------------------------------------------------------------- Function : replace_placeholder() In : name of the FITS file to modify, card to insert, name of the placeholder Out : int 0 if Ok, anything else otherwise Job : replaces a placeholder by a given FITS card Notice : modifies the input FITS file Does not check the validity of the given FITS card ---------------------------------------------------------------------------*/ static int replace_placeholder( char * name_in, char * card, char * placeholder) { int fd ; char * buf ; char * where ; int hs ; /* * mmap the FITS header of the input file */ hs = get_FITS_header_size(name_in) ; if (hs < 1) { fprintf(stderr, "error getting FITS header size for %s\n", name_in); return -1 ; } fd = open(name_in, O_RDWR) ; if (fd == -1) { fprintf(stderr, "cannot open %s: aborting\n", name_in) ; return -1 ; } buf = (char*)mmap(0, hs, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0) ; if (buf == (char*)-1) { perror("mmap") ; fprintf(stderr, "cannot mmap file: %s\n", name_in) ; close(fd) ; return -1 ; } /* * Apply search and replace for the input keyword lists */ where = buf ; do { where = strstr(where, placeholder); if (where == NULL) { fprintf(stderr, "not found in input: [%s]\n", placeholder) ; close(fd); munmap(buf,hs); return -1 ; } } while ((where-buf)%80) ; /* Replace current placeholder by blanks */ memset(where, ' ', 80); /* Copy card into placeholder */ memcpy(where, card, strlen(card)); close(fd) ; munmap(buf, hs) ; return 0 ; } /*--------------------------------------------------------------------------- Function : get_FITS_header_size() In : FITS file name Out : unsigned long Job : compute the size (in bytes) of a FITS header Notice : should always be a multiple of 2880. This implementation assumes only that 80 characters are found per line. ---------------------------------------------------------------------------*/ #define FITS_BLSZ 2880 static int get_FITS_header_size(char * name) { 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 ; } count = 0 ; while (!found) { if (fread(line, 1, 80, in)!=80) { break ; } count ++ ; if (!strncmp(line, "END ", 4)) { found = 1 ; } } 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 ; } return hs ; } #undef FITS_BLSZ /*------------------------------ end of file ------------------------------*/