1 /*
2   This file is part of "fitsverify" and was imported from:
3     http://heasarc.gsfc.nasa.gov/docs/software/ftools/fitsverify/
4  */
5 #include "fverify.h"
6 static HduName  **hduname;
7 static int total_err=1;  /* initialzed to 1 in case fail to open file */
8 static int total_warn=0;
9 
get_total_warn()10 int get_total_warn()
11 {
12     return (total_warn);
13 }
get_total_err()14 int get_total_err()
15 {
16     return (total_err);
17 }
18 
19 /* Get the total hdu number and allocate the memory for hdu array */
init_hduname()20 void init_hduname()
21 {
22     int i;
23     /* allocate memories for the hdu structure array  */
24     hduname = (HduName **)malloc(totalhdu*sizeof(HduName *));
25     for (i=0; i < totalhdu; i++) {
26 	hduname[i] = (HduName *)calloc(1, sizeof(HduName));
27 	hduname[i]->hdutype = -1;
28         hduname[i]->errnum = 0;
29         hduname[i]->wrnno = 0;
30         strcpy(hduname[i]->extname,"");
31         hduname[i]->extver = 0;
32     }
33     return;
34 }
35 /* set the hduname memeber hdutype, extname, extver */
set_hduname(int hdunum,int hdutype,char * extname,int extver)36 void set_hduname(  int hdunum,		/* hdu number */
37 		   int hdutype,		/* hdutype */
38 		   char* extname,	/* extension name */
39                    int  extver 		/* extension version */
40                 )
41 {
42     int i;
43     i = hdunum - 1;
44     hduname[i]->hdutype = hdutype;
45     if(extname!=NULL)
46         strcpy (hduname[i]->extname,extname);
47     else
48         strcpy(hduname[i]->extname,"");
49     hduname[i]->extver = extver;
50     return;
51 }
52 
53 
54 /* get the total errors and total warnings in this hdu */
set_hduerr(int hdunum)55 void set_hduerr(int hdunum	/* hdu number */
56                 )
57 {
58     int i;
59     i = hdunum - 1;
60     num_err_wrn(&(hduname[i]->errnum), &(hduname[i]->wrnno));
61     reset_err_wrn();   /* reset the error and warning counter */
62     return;
63 }
64 
65 /* set the basic information for hduname structure */
set_hdubasic(int hdunum,int hdutype)66 void set_hdubasic(int hdunum,int hdutype)
67 {
68    set_hduname(hdunum, hdutype, NULL, 0);
69    set_hduerr(hdunum);
70    return;
71 }
72 
73 /* test to see whether the two extension having the same name */
74 /* return 1: identical 0: different */
test_hduname(int hdunum1,int hdunum2)75 int test_hduname (int hdunum1,		/* index of first hdu */
76 		  int hdunum2		/* index of second hdu */
77                   )
78 {
79     HduName *p1;
80     HduName *p2;
81 
82     p1 = hduname[hdunum1-1];
83     p2 = hduname[hdunum2-1];
84     if(!strlen(p1->extname) || !strlen(p2->extname)) return 0;
85     if(!strcmp(p1->extname,p2->extname) && p1->hdutype == p2->hdutype
86        && p2->extver == p1->extver && hdunum1 != hdunum2){
87 	   return 1;
88     }
89     return 0;
90 }
91 
92 /* Added the error numbers */
total_errors(int * toterr,int * totwrn)93 void total_errors (int *toterr, int * totwrn)
94 {
95    int i = 0;
96    int ierr, iwrn;
97    *toterr = 0;
98    *totwrn = 0;
99 
100    if (totalhdu == 0) { /* this means the file couldn't be opened */
101        *toterr = 1;
102        return;
103    }
104 
105    for (i = 0; i < totalhdu; i++) {
106        *toterr += hduname[i]->errnum;
107        *totwrn += hduname[i]->wrnno;
108    }
109    /*check the end of file errors */
110    num_err_wrn(&ierr, &iwrn);
111    *toterr +=ierr;
112    *totwrn +=iwrn;
113    return;
114 }
115 
116 /* print the extname, exttype, extver, errnum and wrnno in a  table */
hdus_summary(FILE * out)117 void hdus_summary(FILE *out)
118 {
119    HduName *p;
120    int i;
121    int ierr, iwrn;
122    char temp[FLEN_VALUE];
123    char temp1[FLEN_VALUE];
124 
125    wrtsep(out,'+'," Error Summary  ",60);
126    wrtout(out," ");
127    sprintf(comm," HDU#  Name (version)       Type             Warnings  Errors");
128    wrtout(out,comm);
129 
130    sprintf(comm," 1                          Primary Array    %-4d      %-4d  ",
131 	   hduname[0]->wrnno,hduname[0]->errnum);
132    wrtout(out,comm);
133    for (i=2; i <= totalhdu; i++) {
134        p = hduname[i-1];
135        strcpy(temp,p->extname);
136        if(p->extver && p->extver!= -999) {
137            sprintf(temp1," (%-d)",p->extver);
138            strcat(temp,temp1);
139        }
140        switch(hduname[i-1]->hdutype){
141 	   case IMAGE_HDU:
142                sprintf(comm," %-5d %-20s Image Array      %-4d      %-4d  ",
143 	               i,temp, p->wrnno,p->errnum);
144                wrtout(out,comm);
145 	       break;
146 	   case ASCII_TBL:
147                sprintf(comm," %-5d %-20s ASCII Table      %-4d      %-4d  ",
148 	               i,temp, p->wrnno,p->errnum);
149                wrtout(out,comm);
150 	       break;
151 	   case BINARY_TBL:
152                sprintf(comm," %-5d %-20s Binary Table     %-4d      %-4d  ",
153 	               i,temp, p->wrnno,p->errnum);
154                wrtout(out,comm);
155 	       break;
156            default:
157                sprintf(comm," %-5d %-20s Unknown HDU      %-4d      %-4d  ",
158 	               i,temp, p->wrnno,p->errnum);
159                wrtout(out,comm);
160 	       break;
161       }
162    }
163    /* check the end of file */
164    num_err_wrn(&ierr, &iwrn);
165    if (iwrn || ierr) {
166      sprintf(comm," End-of-file %-30s  %-4d      %-4d  ", "", iwrn,ierr);
167      wrtout(out,comm);
168    }
169    wrtout(out," ");
170    return;
171 }
172 
173 
174 
destroy_hduname()175 void destroy_hduname()
176 {
177    int i;
178    for (i=0; i < totalhdu; i++) free(hduname[i]);
179    free(hduname);
180    return;
181 }
182 
183 /* Routine to test the extra bytes at the end of file */
test_end(fitsfile * infits,FILE * out)184    void  test_end(fitsfile *infits,
185 		  FILE *out)
186 
187 {
188    int status = 0;
189    LONGLONG headstart, datastart, dataend;
190    int hdutype;
191 
192    /* check whether there are any HDU left */
193    fits_movrel_hdu(infits,1, &hdutype, &status);
194    if (!status) {
195        wrtout(out,"< End-of-File >");
196        sprintf(errmes,
197     "There are extraneous HDU(s) beyond the end of last HDU.");
198        wrterr(out,errmes,2);
199        wrtout(out," ");
200        return;
201    }
202 
203    if (status != END_OF_FILE) {
204       wrtserr(out,"Bad HDU? ",&status,2);
205       return;
206    }
207 
208    status = 0;
209    fits_clear_errmsg();
210    if(ffghadll(infits, &headstart, &datastart, &dataend, &status))
211        wrtferr(out, "",&status,1);
212 
213    /* try to move to the last byte of this extension.  */
214    if (ffmbyt(infits, dataend - 1,0,&status))
215    {
216        sprintf(errmes,
217    "Error trying to read last byte of the file at byte %ld.", (long) dataend);
218        wrterr(out,errmes,2);
219        wrtout(out,"< End-of-File >");
220        wrtout(out," ");
221        return;
222    }
223 
224    /* try to move to what would be the first byte of the next extension.
225      If successfull, we have a problem... */
226 
227    ffmbyt(infits, dataend,0,&status);
228    if(status == 0) {
229        wrtout(out,"< End-of-File >");
230        sprintf(errmes,
231      "File has extra byte(s) after last HDU at byte %ld.", (long) dataend);
232        wrterr(out,errmes,2);
233        wrtout(out," ");
234    }
235 
236    return;
237 }
238 
239 
240 
241 /******************************************************************************
242 * Function
243 *      init_report
244 *
245 *
246 * DESCRIPTION:
247 *      Initialize the fverify report
248 *
249 *******************************************************************************/
init_report(FILE * out,char * rootnam)250 void init_report(FILE *out,              /* output file */
251                  char *rootnam          /* input file name */
252                  )
253 {
254     sprintf(comm,"\n%d Header-Data Units in this file.",totalhdu);
255     wrtout(out,comm);
256     wrtout(out," ");
257 
258     reset_err_wrn();
259     init_hduname();
260 }
261 
262 /******************************************************************************
263 * Function
264 *      close_report
265 *
266 *
267 * DESCRIPTION:
268 *      Close the fverify report
269 *
270 *******************************************************************************/
close_report(FILE * out)271 void close_report(FILE *out              /* output file */ )
272 {
273     int numerrs = 0;                    /* number of the errors         */
274     int numwrns = 0;                    /* number of the warnings       */
275 
276     /* print out a summary of all the hdus */
277     if(prstat)hdus_summary(out);
278     total_errors (&numerrs, &numwrns);
279 
280     total_warn = numwrns;
281     total_err  = numerrs;
282 
283     /* get the total number of errors and warnnings */
284     sprintf(comm,"**** Verification found %d warning(s) and %d error(s). ****",
285               numwrns, numerrs);
286     wrtout(out,comm);
287 
288     update_parfile(numerrs,numwrns);
289     /* destroy the hdu name */
290     destroy_hduname();
291     return ;
292 }
293 
294