diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index d19b7524..e25c5313 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -26,3 +26,10 @@ if(NIFTI_BUILD_APPLICATIONS) install_nifti_target(${NIFTI_PACKAGE_PREFIX}afni_xml_tool) install_nifti_target(${NIFTI_PACKAGE_PREFIX}cifti_tool) endif() + +if(NIFTI_BUILD_TESTING) + add_executable(${NIFTI_PACKAGE_PREFIX}test_axml_recur_find_xml test_axml_recur_find_xml.c) + target_link_libraries(${NIFTI_PACKAGE_PREFIX}test_axml_recur_find_xml PRIVATE ${NIFTI_CIFTILIB_NAME}) + add_test(NAME ${NIFTI_PACKAGE_PREFIX}axml_recur_find_xml + COMMAND $) +endif() diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 39da4433..b1de9de1 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -570,7 +570,7 @@ afni_xml_t * axml_recur_find_xml(int(*func)(afni_xml_t *, int), afni_xml_t * ax, /* search deeper */ if( ax->nchild > 0 && ax->xchild ) for( ind=0; ind < ax->nchild; ind++ ) { - rv = axml_recur_find_xml(func, ax, depth+1, max_depth); + rv = axml_recur_find_xml(func, ax->xchild[ind], depth+1, max_depth); if( rv ) return rv; } diff --git a/cifti/afni_xml.h b/cifti/afni_xml.h index b48758af..ecc4ab3e 100644 --- a/cifti/afni_xml.h +++ b/cifti/afni_xml.h @@ -88,6 +88,8 @@ int axml_free_xlist(afni_xml_list * axlist); char * axml_attr_value(afni_xml_t * ax, const char * name); int axml_recur(int(*func)(FILE*,afni_xml_t*,int), afni_xml_t * ax); +afni_xml_t * axml_recur_find_xml(int (*func)(afni_xml_t *, int), afni_xml_t * ax, + int depth, int max_depth); /* control API */ diff --git a/cifti/test_axml_recur_find_xml.c b/cifti/test_axml_recur_find_xml.c new file mode 100644 index 00000000..0f0bae78 --- /dev/null +++ b/cifti/test_axml_recur_find_xml.c @@ -0,0 +1,55 @@ +#include +#include + +#include "afni_xml.h" + +static int is_target(afni_xml_t * ax, int depth) +{ + (void)depth; + return ax->name && strcmp(ax->name, "target") == 0; +} + +static int check(const char * label, afni_xml_t * got, afni_xml_t * want) +{ + if( got == want ) return 0; + fprintf(stderr, "FAIL %s: got %p, want %p\n", label, (void *)got, (void *)want); + return 1; +} + +int main(void) +{ + char root_name[] = "root", mid_name[] = "mid"; + char other_name[] = "other", target_name[] = "target"; + afni_xml_t root, mid, other, target; + afni_xml_t * root_kids[2]; + afni_xml_t * mid_kids[1]; + int nfail = 0; + + memset(&root, 0, sizeof(root)); + memset(&mid, 0, sizeof(mid)); + memset(&other, 0, sizeof(other)); + memset(&target, 0, sizeof(target)); + + root.name = root_name; + other.name = other_name; + mid.name = mid_name; + target.name = target_name; + + root_kids[0] = &other; + root_kids[1] = ∣ + root.nchild = 2; + root.xchild = root_kids; + + mid_kids[0] = ⌖ + mid.nchild = 1; + mid.xchild = mid_kids; + + nfail += check("depth limit reaches target", axml_recur_find_xml(is_target, &root, 0, 2), &target); + nfail += check("depth limit stops short", axml_recur_find_xml(is_target, &root, 0, 1), NULL); + nfail += check("unlimited depth", axml_recur_find_xml(is_target, &root, 0, 0), &target); + nfail += check("leaf without match", axml_recur_find_xml(is_target, &other, 0, 0), NULL); + + if( nfail ) return 1; + printf("axml_recur_find_xml: all checks passed\n"); + return 0; +} diff --git a/fsliolib/fslio.c b/fsliolib/fslio.c index 39a9c3ea..a0d50221 100644 --- a/fsliolib/fslio.c +++ b/fsliolib/fslio.c @@ -28,6 +28,15 @@ #include "fslio.h" #include "assert.h" +/* Internal: no header declares these and nothing outside this file calls them. */ +static int FslIsValidFileType(int filetype); +static int FslGetFileType2(const FSLIO *fslio, int quiet); +static int FslFileType(const char *fname); +static int FslGetReadFileType(const FSLIO *fslio); +static void FslInit4Write(FSLIO* fslio, const char* filename, int ft); +static int fsl_fileexists(const char* fname); +static int check_for_multiple_filenames(const char* filename); + static int FslIgnoreMFQ=0; static int FslOverrideOutputType=-1; @@ -56,7 +65,7 @@ const char* FslFileTypeString(int filetype) } -int FslIsValidFileType(int filetype) +static int FslIsValidFileType(int filetype) { if ( (filetype!=FSL_TYPE_ANALYZE) && (filetype!=FSL_TYPE_ANALYZE_GZ) && (filetype!=FSL_TYPE_NIFTI) && (filetype!=FSL_TYPE_NIFTI_GZ) && @@ -85,7 +94,7 @@ int FslBaseFileType(int filetype) } -int FslGetFileType2(const FSLIO *fslio, int quiet) +static int FslGetFileType2(const FSLIO *fslio, int quiet) { if (fslio==NULL) FSLIOERR("FslGetFileType: Null pointer passed for FSLIO"); if ( (fslio->file_mode==FSL_TYPE_MINC) || (fslio->file_mode==FSL_TYPE_MINC_GZ) ) { @@ -188,7 +197,7 @@ int FslGetEnvOutputType(void) } -int FslFileType(const char* fname) +static int FslFileType(const char* fname) { /* return type is FSL_TYPE_* or -1 to indicate undetermined */ /* use name as first priority but if that is ambiguous then resolve using environment */ @@ -222,7 +231,7 @@ int FslFileType(const char* fname) /************************************************************ * FslGetReadFileType ************************************************************/ -/*! \fn int FslGetReadFileType(const FSLIO *fslio) +/*! \fn static int FslGetReadFileType(const FSLIO *fslio) \brief return the best estimate of the true file type This function is used to return the best estimate of the true file type once @@ -387,7 +396,7 @@ void FslSetInit(FSLIO* fslio) -void FslInit4Write(FSLIO* fslio, const char* filename, int ft) +static void FslInit4Write(FSLIO* fslio, const char* filename, int ft) { /* ft determines filetype if ft>=0*/ int imgtype; @@ -500,7 +509,7 @@ void FslCloneHeader(FSLIO *dest, const FSLIO *src) } -int fsl_fileexists(const char* fname) +static int fsl_fileexists(const char* fname) { znzFile fp; fp = znzopen( fname , "rb" , 1 ) ; @@ -557,7 +566,7 @@ int FslCheckForMultipleFileNames(const char* filename) -int check_for_multiple_filenames(const char* filename) +static int check_for_multiple_filenames(const char* filename) { char *basename, *tmpname; char *otype; @@ -1340,8 +1349,9 @@ void FslGetAuxFile(FSLIO *fslio,char *aux_file) { if (fslio==NULL) FSLIOERR("FslGetAuxFile: Null pointer passed for FSLIO"); if (fslio->niftiptr!=NULL) { - strncpy(aux_file,fslio->niftiptr->aux_file, 24); - aux_file[24-1] = '\0'; + /* the caller's buffer must hold as many bytes as nifti_image aux_file */ + strncpy(aux_file,fslio->niftiptr->aux_file,sizeof(fslio->niftiptr->aux_file)-1); + aux_file[sizeof(fslio->niftiptr->aux_file)-1] = '\0'; } if (fslio->mincptr!=NULL) { fprintf(stderr,"Warning:: Minc is not yet supported\n"); @@ -1362,37 +1372,6 @@ void FslSetAuxFile(FSLIO *fslio,const char *aux_file) } -void FslSetVoxUnits(FSLIO *fslio, const char *units) -{ - int unitcode=0; - if (fslio==NULL) FSLIOERR("FslSetVoxUnits: Null pointer passed for FSLIO"); - if (fslio->niftiptr!=NULL) { - if (strcmp(units,nifti_units_string(NIFTI_UNITS_METER))==0) { - unitcode = NIFTI_UNITS_METER; - } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MM))==0) { - unitcode = NIFTI_UNITS_MM; - } else if (strcmp(units,nifti_units_string(NIFTI_UNITS_MICRON))==0) { - unitcode = NIFTI_UNITS_MICRON; - } - fslio->niftiptr->xyz_units = unitcode; - } - if (fslio->mincptr!=NULL) { - fprintf(stderr,"Warning:: Minc is not yet supported\n"); - } -} - - -void FslGetVoxUnits(FSLIO *fslio, char *units) -{ - if (fslio==NULL) FSLIOERR("FslGetVoxUnits: Null pointer passed for FSLIO"); - if (fslio->niftiptr!=NULL) { - strcpy(units,nifti_units_string(fslio->niftiptr->xyz_units)); - } - if (fslio->mincptr!=NULL) { - fprintf(stderr,"Warning:: Minc is not yet supported\n"); - } -} - void FslSetTimeUnits(FSLIO *fslio, const char *units) { int unitcode=0; diff --git a/fsliolib/fslio.h b/fsliolib/fslio.h index d979fd48..0e642b59 100644 --- a/fsliolib/fslio.h +++ b/fsliolib/fslio.h @@ -180,6 +180,8 @@ FSL_API void FslWriteHeader(FSLIO *fslio); FSL_API int FslFileExists(const char *filename); FSL_API char *FslMakeBaseName(const char *fname); +FSL_API void FslGetHdrImgNames(const char* filename, const FSLIO* fslio, + char** hdrname, char** imgname); FSL_API int FslCheckForMultipleFileNames(const char* filename); FSL_API int FslGetEnvOutputType(void); @@ -233,6 +235,7 @@ FSL_API void FslGetTimeUnits(FSLIO *fslio, char *units); FSL_API void FslSetDataType(FSLIO *fslio, short t); FSL_API size_t FslGetDataType(FSLIO *fslio, short *t); FSL_API int FslGetIntensityScaling(FSLIO *fslio, float *slope, float *intercept); +FSL_API void FslSetIntensityScaling(FSLIO *fslio, float slope, float intercept); FSL_API void FslSetIntent(FSLIO *fslio, short intent_code, float p1, float p2, float p3); FSL_API short FslGetIntent(FSLIO *fslio, short *intent_code, float *p1, float *p2, float *p3); diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 7ec9c086..9a998ee5 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -474,6 +474,9 @@ static const nifti_type_ele nifti_type_list[] = { { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, }; +/* also defined in libniftiio; both export it */ +int nifti_fileexists(const char* fname); + /*---------------------------------------------------------------------------*/ /* prototypes for internal functions - not part of exported library */ @@ -7867,7 +7870,7 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { +static int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); @@ -7899,7 +7902,7 @@ int doPigz2(nifti_image *nim, struct nifti_2_header nhdr, const nifti_brick_list return 0; } -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" ); diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 9811e715..35bff9c4 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -413,6 +413,9 @@ static const nifti_type_ele nifti_type_list[] = { { 2304, 4, 0, "NIFTI_TYPE_RGBA32" }, }; +/* also defined in libnifti2; both export it */ +int nifti_fileexists(const char* fname); + /*---------------------------------------------------------------------------*/ /* prototypes for internal functions - not part of exported library */ @@ -5776,39 +5779,7 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, #ifdef PIGZ #ifdef HAVE_ZLIB -int doPigz2(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { - FILE *pigzPipe; - char command[768]; - strcpy(command, "pigz" ); - strcat(command, " -n -f > \""); - strcat(command, nim->fname); - strcat(command, "\""); - #ifdef _MSC_VER - if (( pigzPipe = _popen(command, "w")) == NULL) - return -1; - #else - if (( pigzPipe = popen(command, "w")) == NULL) - return -1; - #endif - znzFile fp; - fp = (znzFile) calloc(1,sizeof(struct znzptr)); - fp->zfptr = NULL; - fp->withz = 0; - fp->nzfptr = pigzPipe; - fwrite(&nhdr, sizeof(nhdr), 1, pigzPipe); - if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - nifti_write_extensions(fp,nim); - nifti_write_all_data(fp,nim,NBL); - #ifdef _MSC_VER - _pclose(pigzPipe); - #else - pclose(pigzPipe); - #endif - free(fp); - return 0; -} - -int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { +static int doPigz(nifti_image *nim, struct nifti_1_header nhdr, const nifti_brick_list * NBL) { FILE *pigzPipe; char command[768]; strcpy(command, "pigz" );