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

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "mcw_malloc.h"

Go to the source code of this file.


Defines

#define COMPRESS_NOFILE   -666
#define COMPRESS_NONE   -1
#define COMPRESS_GZIP   0
#define COMPRESS_BZIP2   1
#define COMPRESS_COMPRESS   2
#define COMPRESS_BRIKCOMP   3
#define COMPRESS_LASTCODE   3
#define NUM_COMPRESS_elist   3

Functions

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

Variables

char * COMPRESS_suffix [] = { ".gz" , ".bz2" , ".Z", ".briz" }
int COMPRESS_suffix_len [] = { 3 , 4 , 2 , 5}
char * COMPRESS_program []
int COMPRESS_program_ok [] = { 1 , 1 , 1 , 0 }
char * COMPRESS_unprogram []
char * COMPRESS_enviro [] = { "GZIP" , "BZIP2" , "COMPRESS" , "BRIKCOMP" }
char * COMPRESS_elist [] = { "GZIP" , "BZIP2" , "COMPRESS" }

Define Documentation

#define COMPRESS_BRIKCOMP   3
 

Definition at line 31 of file thd_compress.h.

#define COMPRESS_BZIP2   1
 

Definition at line 26 of file thd_compress.h.

#define COMPRESS_COMPRESS   2
 

Definition at line 27 of file thd_compress.h.

#define COMPRESS_GZIP   0
 

Definition at line 25 of file thd_compress.h.

Referenced by THD_write_datablock().

#define COMPRESS_LASTCODE   3
 

Definition at line 33 of file thd_compress.h.

Referenced by COMP_main(), COMPRESS_add_suffix(), COMPRESS_filecode(), COMPRESS_has_suffix(), MAIN_workprocess(), THD_enviro_write_compression(), and THD_set_write_compression().

#define COMPRESS_NOFILE   -666
 

Definition at line 23 of file thd_compress.h.

Referenced by COMPRESS_filecode(), COMPRESS_filename(), COMPRESS_fopen_read(), main(), RENAME_main(), THD_datablock_from_atr(), THD_get_write_compression(), THD_rename_dataset_files(), and THD_write_datablock().

#define COMPRESS_NONE   -1
 

Definition at line 24 of file thd_compress.h.

Referenced by COMP_main(), COMPRESS_filecode(), COMPRESS_filename(), COMPRESS_fopen_read(), ENV_compressor(), RT_tell_afni_one(), RT_worker(), THD_enviro_write_compression(), THD_set_write_compression(), and THD_write_datablock().

#define NUM_COMPRESS_elist   3
 

Definition at line 52 of file thd_compress.h.

Referenced by ENV_compressor(), and ENV_init().


Function Documentation

char* COMPRESS_add_suffix char *    fname,
int    mode
 

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 }

Variable Documentation

char* COMPRESS_elist[] = { "GZIP" , "BZIP2" , "COMPRESS" } [static]
 

Definition at line 53 of file thd_compress.h.

char* COMPRESS_enviro[] = { "GZIP" , "BZIP2" , "COMPRESS" , "BRIKCOMP" } [static]
 

Definition at line 50 of file thd_compress.h.

char* COMPRESS_program[] [static]
 

Initial value:

 { "gzip -1c > %s"  ,
                                        "bzip2 -1c > %s" ,
                                        "compress > %s"  ,
                                        "cat > %s"}

Definition at line 38 of file thd_compress.h.

int COMPRESS_program_ok[] = { 1 , 1 , 1 , 0 } [static]
 

Definition at line 43 of file thd_compress.h.

char* COMPRESS_suffix[] = { ".gz" , ".bz2" , ".Z", ".briz" } [static]
 

Definition at line 35 of file thd_compress.h.

int COMPRESS_suffix_len[] = { 3 , 4 , 2 , 5} [static]
 

Definition at line 36 of file thd_compress.h.

char* COMPRESS_unprogram[] [static]
 

Initial value:

 { "gzip -dc %s"  ,
                                        "bzip2 -dc %s" ,
                                        "uncompress -c %s",
                                        "brikcomp -c %s" }

Definition at line 45 of file thd_compress.h.

 

Powered by Plone

This site conforms to the following standards: