Commit 6e6cb61f authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

micropetHeaderRead() and micropetExists() finalized and tested

parent d3bfce59
......@@ -21,9 +21,15 @@ int micropetHeaderRead(
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
if(verbose>0) {printf("%s(%s, ...)\n", __func__, hdrfile); fflush(stdout);}
if(verbose>0) {printf("%s('%s', ...)\n", __func__, hdrfile); fflush(stdout);}
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
/* Empty file name or shorter than 'x.hdr' means not a microPET header file */
if(hdrfile==NULL || strnlen(hdrfile, 6)<5) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
return(TPCERROR_INVALID_FILENAME);
}
/* Read header */
IFT ift, *local_hdr;
iftInit(&ift);
......@@ -68,8 +74,9 @@ int micropetHeaderRead(
/******************************************************************************/
/******************************************************************************/
/** @brief Verify if specified file name is a microPET file.
@details Other image formats may have similar names, thus image header contents are verified here.
/** @brief Verify if specified file name refers to an existing microPET file.
@details Other image formats may have similar names (*.img and *img.hdr), thus image header
contents are verified here. Pixel data (*.img) is only verified to exist.
@sa niftiExists, anaExists, micropetHeaderRead
@return Returns 0 if it is not microPET, otherwise the major version of microPET format.
@todo Finalize and test.
......@@ -91,7 +98,7 @@ int micropetExists(
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
if(verbose>0) {printf("%s(%s, ...)\n", __func__, filename); fflush(stdout);}
if(verbose>0) {printf("%s('%s', ...)\n", __func__, filename); fflush(stdout);}
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
/* Initiate output */
......@@ -100,7 +107,7 @@ int micropetExists(
if(header!=NULL) iftFree(header);
/* Empty file name means not a microPET file */
if(strnlen(filename, 2)<1) {
if(filename==NULL || strnlen(filename, 2)<1) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
return(0);
}
......@@ -110,8 +117,8 @@ int micropetExists(
/* If there are extensions, what is the last extension? */
/* Based on that, try to make image and header file names */
char *last_extension=filenameGetExtension(filename);
if(verbose>1) printf(" last extension := '%s'\n", last_extension);
if(last_extension==NULL) { // Base name was given
if(verbose>1) {printf(" last extension := '%s'\n", last_extension); fflush(stdout);}
if(last_extension==NULL) { // Base name without any extension was given
strcpy(local_imgfile, filename); strlcat(local_imgfile, ".img", FILENAME_MAX);
strcpy(local_hdrfile, local_imgfile); strlcat(local_hdrfile, ".hdr", FILENAME_MAX);
} else if(strcasecmp(last_extension, ".IMG")==0) { // img file name was given
......@@ -120,13 +127,14 @@ int micropetExists(
} else if(strcasecmp(last_extension, ".HDR")==0) { // header file name was given
strcpy(local_imgfile, filename); filenameRmExtension(local_imgfile);
strcpy(local_hdrfile, filename);
} else {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
return(0);
} else { // Base name, including extra extensions, was given
strcpy(local_imgfile, filename); strlcat(local_imgfile, ".img", FILENAME_MAX);
strcpy(local_hdrfile, local_imgfile); strlcat(local_hdrfile, ".hdr", FILENAME_MAX);
}
if(verbose>1) {
printf(" tentative image file name := '%s'\n", local_imgfile);
printf(" tentative header file name := '%s'\n", local_hdrfile);
fflush(stdout);
}
/* Check that files do exist */
......@@ -147,14 +155,29 @@ int micropetExists(
}
/* Read header to check that this indeed is a microPET file */
if(micropetHeaderRead(local_hdrfile, header, status)!=TPCERROR_OK) return(0);
IFT ift, *local_hdr;
iftInit(&ift);
if(header==NULL) local_hdr=&ift; else local_hdr=header;
if(micropetHeaderRead(local_hdrfile, local_hdr, status)!=TPCERROR_OK) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
return(0);
}
/* Read file version from the header */
double version=0.0;
if(iftGetDoubleValue(local_hdr, "version", 0, &version)<0) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_HEADER);
iftFree(&ift); return(0);
}
iftFree(&ift);
if(verbose>1) {printf(" version := %g\n", version); fflush(stdout);}
/* Set output */
if(hdrfile!=NULL) strlcpy(hdrfile, local_hdrfile, FILENAME_MAX);
if(imgfile!=NULL) strlcpy(imgfile, local_imgfile, FILENAME_MAX);
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return(1);
return((int)version);
}
/*****************************************************************************/
......
......@@ -22,17 +22,17 @@ int test_micropetHeaderRead(
IFT header; iftInit(&header);
if(verbose>1) printf("\n testing with empty input.\n");
ret=micropetHeaderRead(NULL, NULL, NULL);
ret=micropetHeaderRead(NULL, NULL, status);
if(verbose>1) printf("did not segfault, that is good.\n");
if(ret==TPCERROR_OK) return(1);
fname[0]=(char)0;
ret=micropetHeaderRead(fname, NULL, NULL);
ret=micropetHeaderRead(fname, NULL, status);
if(ret==TPCERROR_OK) return(2);
if(verbose>1) printf("\n testing with non-existing file.\n");
strcpy(fname, "nofile.img.hdr");
ret=micropetHeaderRead(fname, &header, NULL);
ret=micropetHeaderRead(fname, &header, status);
if(ret==TPCERROR_OK) return(3);
if(verbose>1) printf("\n testing with real header file.\n");
......@@ -43,6 +43,11 @@ int test_micropetHeaderRead(
iftFree(&header);
if(verbose>1) printf("\n testing without giving pointer to header data.\n");
strcpy(fname, "P108731.ct.img.hdr");
ret=micropetHeaderRead(fname, NULL, status);
if(ret!=TPCERROR_OK) return(21);
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
}
......@@ -60,8 +65,67 @@ int test_micropetExists(
printf("\n=====================================\n");
}
if(verbose>1) printf("did not segfault, that is good.\n");
if(verbose>1) printf("\n testing with empty input.\n");
if(micropetExists(NULL, NULL, NULL, NULL, status)!=0) {
if(verbose>1) printf(" failed, but at least did not segfault.\n");
return(1);
}
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
if(micropetExists("", NULL, NULL, NULL, status)!=0) {
if(verbose>1) printf(" failed, but at least did not segfault.\n");
return(2);
}
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
if(micropetExists("./", NULL, NULL, NULL, status)!=0) {
if(verbose>1) printf(" failed, but at least did not segfault.\n");
return(3);
}
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
char fname[256];
if(verbose>1) printf("\n testing with non-existing files.\n");
strcpy(fname, "nofile.img.hdr");
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
if(micropetExists(fname, NULL, NULL, NULL, status)!=0) return(11);
strcpy(fname, "nofile.img");
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
if(micropetExists(fname, NULL, NULL, NULL, status)!=0) return(12);
strcpy(fname, "nofile");
if(micropetExists(fname, NULL, NULL, NULL, status)!=0) return(13);
if(verbose>1) {printf(" ok\n"); fflush(stdout);}
if(verbose>1) printf("\n creating dummy data file, since function is testing just that one exists.\n");
if(!fileExist("P108731.ct.img")) {
FILE *fp=fopen("P108731.ct.img", "w");
fprintf(fp, "dummy file.\n");
fclose(fp);
}
if(verbose>1) printf("\n testing with existing file.\n");
strcpy(fname, "P108731.ct.img.hdr");
if(micropetExists(fname, NULL, NULL, NULL, status)!=2) return(21);
strcpy(fname, "P108731.ct.img");
if(micropetExists(fname, NULL, NULL, NULL, status)!=2) return(22);
strcpy(fname, "P108731.ct");
if(micropetExists(fname, NULL, NULL, NULL, status)!=2) return(23);
if(verbose>1) printf("\n storing file names.\n");
char imgname[256], hdrname[256];
strcpy(fname, "P108731.ct");
if(micropetExists(fname, hdrname, imgname, NULL, status)!=2) return(31);
if(strcmp(hdrname, "P108731.ct.img.hdr")!=0) return(32);
if(strcmp(imgname, "P108731.ct.img")!=0) return(33);
if(verbose>1) printf("\n storing header.\n");
IFT header; iftInit(&header);
strcpy(fname, "P108731.ct.img");
if(micropetExists(fname, NULL, NULL, &header, status)!=2) {iftFree(&header); return(41);}
if(iftFindKey(&header, "number_of_dimensions", 0)<0) {
iftFree(&header); return(42);
}
iftFree(&header);
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment