Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

thd_compress.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006    
00007 #include "thd_compress.h"
00008 #include "Amalloc.h"
00009 
00010 /*** check if the file exists on disk
00011      -- returns 1 if it does, 0 if it does not ***/
00012 
00013 int COMPRESS_is_file( char * pathname )
00014 {
00015    static struct stat buf ;
00016    int ii ;
00017 
00018    if( pathname == NULL ) return 0 ;
00019    ii = stat( pathname , &buf ) ; if( ii != 0 ) return 0 ;
00020    ii = (buf.st_mode & S_IFREG) != 0 ; return ii ;
00021 }
00022 
00023 /*--------------------------------------------------------------*/
00024 
00025 /*** check if the filename has the apposite
00026      suffix for the compression mode given  ***/
00027 
00028 int COMPRESS_has_suffix( char * fname , int mode )
00029 {
00030    int ll ;
00031 
00032    if( mode < 0                 ) return 1 ;
00033    if( mode > COMPRESS_LASTCODE ) return 0 ;
00034 
00035    ll = strlen(fname) ;
00036    return ( ll > COMPRESS_suffix_len[mode] &&
00037             strcmp(COMPRESS_suffix[mode] ,
00038                    fname+(ll-COMPRESS_suffix_len[mode])) == 0 ) ;
00039 }
00040 
00041 /*--------------------------------------------------------------*/
00042 
00043 /*** return the compression code for the given filename;
00044      if the file doesn't exist, will return COMPRESS_NOFILE ***/
00045 
00046 int COMPRESS_filecode( char * fname )
00047 {
00048    int ii ;
00049    char * buf ;
00050 
00051    if( fname == NULL || fname[0] == '\0' ) return COMPRESS_NOFILE ;
00052 
00053    /** check the filename suffix **/
00054 
00055    for( ii=0 ; ii <= COMPRESS_LASTCODE ; ii++ ){
00056       if( COMPRESS_has_suffix(fname,ii) ){
00057          if( COMPRESS_is_file(fname) ) return ii ;
00058          else                          return COMPRESS_NOFILE ;
00059       }
00060    }
00061    if( COMPRESS_is_file(fname) ) return COMPRESS_NONE ;
00062 
00063    /** add the suffixes to the name, and check again **/
00064 
00065    buf = AFMALL(char, sizeof(char) * (strlen(fname)+16) ) ;
00066    for( ii=0 ; ii <= COMPRESS_LASTCODE ; ii++ ){
00067       strcpy(buf,fname) ; strcat(buf,COMPRESS_suffix[ii]) ;
00068       if( COMPRESS_is_file(buf) ){ free(buf) ; return ii ; }
00069    }
00070    free(buf) ; return COMPRESS_NOFILE ;
00071 }
00072 
00073 /*--------------------------------------------------------------*/
00074 
00075 /*** keep a table of which open files used fopen and which
00076      used popen -- because they must be closed differently ***/
00077 
00078 #define NFOPMAX 16
00079 static int fop_init = 0 ;
00080 static int fop_fileno[NFOPMAX] ;
00081 static int fop_popend[NFOPMAX] ;
00082 
00083 static void putin_fop_table( FILE * fp , int ppp )
00084 {
00085    int ii ;
00086 
00087    if( fp == NULL ) return ;  /* can't do much with nothing */
00088 
00089    if( ! fop_init ){                       /* initialize the table */
00090       for( ii=0 ; ii < NFOPMAX ; ii++ ){
00091          fop_fileno[ii] = -1 ;
00092          fop_popend[ii] =  0 ;
00093       }
00094       fop_init = 1 ;
00095    }
00096 
00097    for( ii=0 ; ii < NFOPMAX ; ii++ )       /* find an unused entry */
00098       if( fop_fileno[ii] < 0 ) break ;
00099 
00100    if( ii == NFOPMAX ){
00101       fprintf(stderr,"\n*** AFNI compressor table overflow!\n") ;
00102       return ;
00103    }
00104 
00105    fop_fileno[ii] = fileno(fp) ;   /* save the file number */
00106    fop_popend[ii] = ppp ;          /* save the popen code */
00107    return ;
00108 }
00109 
00110 /*** Use this to close a file,
00111      so that pclose or fclose can be called correctly ***/
00112 
00113 int COMPRESS_fclose( FILE * fp )
00114 {
00115    int fn , ii ;
00116 
00117    if( fp == NULL || ! fop_init ) return fclose(fp) ;
00118 
00119    fn = fileno(fp) ;
00120    for( ii=0 ; ii < NFOPMAX ; ii++ ){   /* find the file number */
00121       if( fop_fileno[ii] == fn ){       /* found it! */
00122          fop_fileno[ii] = -1 ;          /* empty this table entry */
00123          if( fop_popend[ii] ) return pclose(fp) ;
00124          else                 return fclose(fp) ;
00125       }
00126    }
00127 
00128    return fclose(fp) ;  /* couldn't find it, so use fclose */
00129 }
00130 
00131 /*--------------------------------------------------------------*/
00132 
00133 /*** return a malloc-ed filename string that has the
00134      correct compression suffix attached.
00135      If NULL is returned, the file doesn't exist.
00136      The return string should be free()-ed after it is used. ***/
00137 
00138 char * COMPRESS_filename( char * fname )
00139 {
00140    char * buf ;
00141    int ll , mm ;
00142 
00143    if( fname == NULL || fname[0] == '\0' ) return NULL ;
00144 
00145    mm  = COMPRESS_filecode( fname ) ;  /* find compression mode */
00146    if( mm == COMPRESS_NOFILE ) return NULL ;
00147 
00148    ll  = strlen(fname) ;
00149    buf = AFMALL(char, sizeof(char) * (ll+16) ) ;  /* worst case */
00150 
00151    if( mm == COMPRESS_NONE ){
00152       strcpy(buf,fname) ;
00153    } else {
00154       if( ! COMPRESS_has_suffix(fname,mm) ){
00155          strcpy(buf,fname) ; strcat(buf,COMPRESS_suffix[mm]) ;
00156       } else {
00157          strcpy(buf,fname) ;
00158       }
00159    }
00160    return buf ;
00161 }
00162 
00163 /*--- May 1998: a simple routine ---*/
00164 
00165 char * COMPRESS_add_suffix( char * fname , int mm )
00166 {
00167    char * buf ;
00168    int ll ;
00169 
00170    if( fname == NULL || fname[0] == '\0' ) return NULL ;
00171 
00172    ll  = strlen(fname) ;
00173    buf = AFMALL(char, sizeof(char) * (ll+16) ) ;
00174 
00175    strcpy(buf,fname) ;
00176    if( mm >= 0 && mm <= COMPRESS_LASTCODE &&
00177        ! COMPRESS_has_suffix(fname,mm)      ){
00178 
00179       strcat(buf,COMPRESS_suffix[mm]) ;
00180    }
00181 
00182    return buf ;
00183 }
00184 
00185 /*--------------------------------------------------------------*/
00186 
00187 /*** open a file for readin, possibly using compresson ***/
00188 
00189 FILE * COMPRESS_fopen_read( char * fname )
00190 {
00191    FILE * fp ;
00192    int mm ;
00193    char * buf , * cmd ;
00194 
00195    if( fname == NULL || fname[0] == '\0' ) return NULL ;
00196 
00197    mm = COMPRESS_filecode( fname ) ;  /* find compression mode */
00198 
00199    if( mm == COMPRESS_NOFILE ) return NULL ;  /* can't do nothin */
00200 
00201    if( mm == COMPRESS_NONE ){
00202       fp = fopen(fname,"r") ;   /* open it normally */
00203       putin_fop_table(fp,0) ;   /* save its open method */
00204       return fp ;
00205    }
00206 
00207 #if 1
00208    if( ! COMPRESS_has_suffix(fname,mm) ){
00209       buf = AFMALL(char, sizeof(char) * (strlen(fname)+16) ) ;
00210       strcpy(buf,fname) ; strcat(buf,COMPRESS_suffix[mm]) ;
00211    } else {
00212       buf = fname ;
00213    }
00214 #else
00215    buf = fname ;
00216 #endif
00217 
00218    cmd = AFMALL(char, sizeof(char) * (strlen(buf)+32) ) ;
00219    sprintf(cmd,COMPRESS_unprogram[mm],buf) ;
00220 
00221    fp = popen(cmd,"r") ;    /* open a pipe to read the file */
00222    putin_fop_table(fp,1) ;  /* save its open method */
00223 
00224    free(cmd) ; if( buf != fname ) free(buf) ;
00225    return fp ;
00226 }
00227 
00228 /*-------------------------------------------------------------
00229     open a file for writing, possibly using compresson;
00230      mm should be one of the COMPRESS_ codes at the top
00231      of file thd_compress.h
00232 ---------------------------------------------------------------*/
00233 
00234 FILE * COMPRESS_fopen_write( char * fname , int mm )
00235 {
00236    FILE * fp ;
00237    char * buf , * cmd ;
00238 
00239    if( fname == NULL || fname[0] == '\0' ) return NULL ;
00240 
00241    /* Don't compress if the compression program isn't marked as OK   */
00242    /* [For modes that can only be compressed offline, like BRIKCOMP] */
00243 
00244    if( mm < 0 || ! COMPRESS_program_ok[mm] ){
00245       fp = fopen(fname,"w") ;   /* open it normally */
00246       putin_fop_table(fp,0) ;   /* save its open method */
00247       return fp ;
00248    }
00249 
00250 #if 1
00251    if( ! COMPRESS_has_suffix(fname,mm) ){
00252       buf = AFMALL(char, sizeof(char) * (strlen(fname)+16) ) ;
00253       strcpy(buf,fname) ; strcat(buf,COMPRESS_suffix[mm]) ;
00254    } else {
00255       buf = fname ;
00256    }
00257 #else
00258    buf = fname ;
00259 #endif
00260 
00261    cmd = AFMALL(char,  sizeof(char) * (strlen(buf)+32) ) ;
00262    sprintf(cmd,COMPRESS_program[mm],buf) ;
00263 
00264    fp = popen(cmd,"w") ;    /* open a pipe to write the file */
00265    putin_fop_table(fp,1) ;  /* save its open method */
00266 
00267    free(cmd) ; if( buf != fname ) free(buf) ;
00268    return fp ;
00269 }
00270 
00271 /*----------------------------------------------------------*/
00272 
00273 int COMPRESS_unlink( char * fname )
00274 {
00275    char * fff = COMPRESS_filename(fname) ;
00276    int     ii = -1 ;
00277    if( fff != NULL ){ ii=unlink(fff); free(fff); }
00278    return ii ;
00279 }
 

Powered by Plone

This site conforms to the following standards: