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 File Reference

#include "thd_compress.h"
#include "Amalloc.h"

Go to the source code of this file.


Defines

#define NFOPMAX   16

Functions

int COMPRESS_is_file (char *pathname)
int COMPRESS_has_suffix (char *fname, int mode)
int COMPRESS_filecode (char *fname)
void putin_fop_table (FILE *fp, int ppp)
int COMPRESS_fclose (FILE *fp)
char * COMPRESS_filename (char *fname)
char * COMPRESS_add_suffix (char *fname, int mm)
FILE * COMPRESS_fopen_read (char *fname)
FILE * COMPRESS_fopen_write (char *fname, int mm)
int COMPRESS_unlink (char *fname)

Variables

int fop_init = 0
int fop_fileno [NFOPMAX]
int fop_popend [NFOPMAX]

Define Documentation

#define NFOPMAX   16
 

add the suffixes to the name, and check again *

Definition at line 78 of file thd_compress.c.

Referenced by COMPRESS_fclose(), and putin_fop_table().


Function Documentation

char* COMPRESS_add_suffix char *    fname,
int    mm
 

Definition at line 165 of file thd_compress.c.

References AFMALL, COMPRESS_has_suffix(), and COMPRESS_LASTCODE.

Referenced by RENAME_main().

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 }

int COMPRESS_fclose FILE *    fp
 

Definition at line 113 of file thd_compress.c.

References fop_fileno, fop_init, fop_popend, NFOPMAX, and pclose.

Referenced by adwarp_refashion_dataset(), AFNI_refashion_dataset(), main(), THD_load_datablock(), and THD_write_datablock().

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 }

int COMPRESS_filecode char *    fname
 

Definition at line 46 of file thd_compress.c.

References AFMALL, COMPRESS_has_suffix(), COMPRESS_is_file(), COMPRESS_LASTCODE, COMPRESS_NOFILE, COMPRESS_NONE, and free.

Referenced by COMPRESS_filename(), COMPRESS_fopen_read(), main(), RENAME_main(), THD_datablock_from_atr(), THD_force_malloc_type(), and THD_rename_dataset_files().

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 }

char* COMPRESS_filename char *    fname
 

Definition at line 138 of file thd_compress.c.

References AFMALL, COMPRESS_filecode(), COMPRESS_has_suffix(), COMPRESS_NOFILE, and COMPRESS_NONE.

Referenced by COMPRESS_unlink(), main(), and THD_rename_dataset_files().

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 }

FILE* COMPRESS_fopen_read char *    fname
 

Definition at line 189 of file thd_compress.c.

References AFMALL, COMPRESS_filecode(), COMPRESS_has_suffix(), COMPRESS_NOFILE, COMPRESS_NONE, free, popen, and putin_fop_table().

Referenced by THD_load_datablock().

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 }

FILE* COMPRESS_fopen_write char *    fname,
int    mm
 

Definition at line 234 of file thd_compress.c.

References AFMALL, COMPRESS_has_suffix(), free, popen, and putin_fop_table().

Referenced by adwarp_refashion_dataset(), AFNI_refashion_dataset(), main(), and THD_write_datablock().

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 }

int COMPRESS_has_suffix char *    fname,
int    mode
 

Definition at line 28 of file thd_compress.c.

References COMPRESS_LASTCODE.

Referenced by COMPRESS_add_suffix(), COMPRESS_filecode(), COMPRESS_filename(), COMPRESS_fopen_read(), and COMPRESS_fopen_write().

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 }

int COMPRESS_is_file char *    pathname
 

Definition at line 13 of file thd_compress.c.

Referenced by COMPRESS_filecode().

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 }

int COMPRESS_unlink char *    fname
 

Definition at line 273 of file thd_compress.c.

References COMPRESS_filename(), free, and unlink.

Referenced by adwarp_refashion_dataset(), AFNI_refashion_dataset(), THD_delete_3dim_dataset(), THD_open_3dcalc(), and THD_write_datablock().

00274 {
00275    char * fff = COMPRESS_filename(fname) ;
00276    int     ii = -1 ;
00277    if( fff != NULL ){ ii=unlink(fff); free(fff); }
00278    return ii ;
00279 }

void putin_fop_table FILE *    fp,
int    ppp
[static]
 

Definition at line 83 of file thd_compress.c.

References fop_fileno, fop_init, fop_popend, and NFOPMAX.

Referenced by COMPRESS_fopen_read(), and COMPRESS_fopen_write().

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 }

Variable Documentation

int fop_fileno[NFOPMAX] [static]
 

Definition at line 80 of file thd_compress.c.

Referenced by COMPRESS_fclose(), and putin_fop_table().

int fop_init = 0 [static]
 

Definition at line 79 of file thd_compress.c.

Referenced by COMPRESS_fclose(), and putin_fop_table().

int fop_popend[NFOPMAX] [static]
 

Definition at line 81 of file thd_compress.c.

Referenced by COMPRESS_fclose(), and putin_fop_table().

 

Powered by Plone

This site conforms to the following standards: