1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "mrilib.h"
8 #include "thd.h"
9 
10 /*=============================================================================*/
11 /*** Adapted from THD_get_many_timeseries() for *.1D files ***/
12 
13 /***  Read all *.csv and *.tsv files from a list of directories ***/
14 
THD_get_many_tcsv(THD_string_array * dlist)15 NI_ELARR * THD_get_many_tcsv( THD_string_array *dlist )
16 {
17    int id , ii , ndir ;
18    NI_ELARR *outar=NULL, *tmpar=NULL ;
19    char *epath , *eee ;
20    char  efake[] = "./" ;
21    THD_string_array *qlist ; /* 02 Feb 2002 */
22 
23 ENTRY("THD_get_many_tcsv") ;
24 
25    /*----- sanity check and initialize -----*/
26 
27                        epath = my_getenv( "AFNI_TSPATH" ) ;
28    if( epath == NULL ) epath = my_getenv( "AFNI_TS_PATH" ) ;
29    if( epath == NULL ) epath = efake ;
30 
31    ndir = (dlist != NULL) ? dlist->num : 0 ;
32 
33    if( ndir == 0 && epath == NULL ) RETURN( outar ) ;
34 
35    INIT_ELARR( outar ) ; /* output array */
36    INIT_SARR( qlist ) ;  /* list of directories */
37 
38    /*----- for each input directory, find all *.1D files -----*/
39 
40    for( id=0 ; id < ndir ; id++ ){
41 
42       if( THD_forbidden_directory(dlist->ar[id]) ) continue ;
43 
44       ADDTO_SARR(qlist,dlist->ar[id]) ; /* save this name for later use */
45 
46       tmpar = THD_get_all_tcsv( dlist->ar[id] ) ; /* read files from */
47       if( tmpar == NULL ) continue ;              /* this directory */
48 
49       for( ii=0 ; ii < tmpar->num ; ii++ )  /* move data to output array */
50         ADDTO_ELARR( outar , tmpar->elarr[ii] ) ;
51 
52       FREE_ELARR(tmpar) ;  /* don't need this no more */
53    }
54 
55    /*----- also do directories in environment path, if any -----*/
56 
57    if( epath != NULL ){
58       int epos = 0 , ll = strlen(epath) ;
59       char *elocal ;
60       char  ename[THD_MAX_NAME] ;
61 
62       /* copy path list into local memory */
63 
64       elocal = (char *) malloc( sizeof(char) * (ll+2) ) ;
65       if( elocal == NULL ){
66          fprintf(stderr,
67                 "\n*** THD_get_many_tcsv malloc failure - is memory full? ***\n");
68          EXIT(1) ;
69       }
70       strcpy( elocal , epath ) ; elocal[ll] = ' ' ; elocal[ll+1] = '\0' ;
71 
72       /* replace colons with blanks */
73 
74       for( ii=0 ; ii < ll ; ii++ )
75          if( elocal[ii] == ':' ) elocal[ii] = ' ' ;
76 
77       /* extract blank delimited strings,
78          use as directory names to get timeseries files */
79 
80       do{
81          ii = sscanf( elocal+epos , "%s%n" , ename , &id ) ;
82          if( ii < 1 ) break ;  /* no read --> end of work */
83          epos += id ;          /* epos = char after last one scanned */
84 
85          ii = strlen(ename) ;                         /* make sure name has */
86          if( ename[ii-1] != '/' ){                    /* a trailing '/' on it */
87             ename[ii]  = '/' ; ename[ii+1] = '\0' ;
88          }
89 
90          if( !THD_is_directory(ename) ) continue ;  /* 21 May 2002 - rcr */
91          if( THD_forbidden_directory(ename) ) continue ; /* 18 Sep 2020 */
92 
93          /* 02 Feb 2002: check if scanned this directory before */
94 
95          for( ii=0 ; ii < qlist->num ; ii++ )
96             if( THD_equiv_files(qlist->ar[ii],ename) ) break ;
97          if( ii < qlist->num ) continue ;  /* skip to end of do loop */
98          ADDTO_SARR(qlist,ename) ;
99 
100          tmpar = THD_get_all_tcsv( ename ) ; /* read all files from this directory */
101          if( tmpar != NULL ){
102            for( ii=0 ; ii < tmpar->num ; ii++ )   /* move data to output array */
103              ADDTO_ELARR( outar , tmpar->elarr[ii] ) ;
104 
105            FREE_ELARR(tmpar) ;                    /* don't need this no more */
106          }
107       } while( epos < ll ) ;  /* scan until 'epos' is after end of epath */
108 
109       free(elocal) ;
110    }
111 
112    if( ELARR_COUNT(outar) == 0 ) DESTROY_ELARR(outar) ;
113 
114    DESTROY_SARR(qlist) ;
115    RETURN( outar ) ;
116 }
117 
118 /*---------------------------------------------------*/
119 /*  Read all *.tsv and *.csv files from directory   */
120 /*---------------------------------------------------*/
121 
THD_get_all_tcsv(char * dname)122 NI_ELARR * THD_get_all_tcsv( char *dname )
123 {
124    int ir , ii ;
125    char *fname , * tname ;
126    float *far ;
127    char *cmd , *flist ;
128    NI_str_array *qsar=NULL ;
129    NI_element *outim , *flim=NULL ;
130    NI_ELARR   *outar ;
131 
132    unsigned long max_fsize ;  /* 20 Jul 2004: max 1D file size to load */
133 
134 ENTRY("THD_get_all_tcsv") ;
135 
136    max_fsize = (unsigned long) AFNI_numenv( "AFNI_MAX_1DSIZE" ) ;
137    if( max_fsize == 0 ) max_fsize = 123*1024 ;
138    if( max_fsize == 1 ) RETURN(NULL) ;
139 
140    /*----- sanity check and initialize -----*/
141 
142    if( dname == NULL || strlen(dname) == 0 ) RETURN(NULL) ;
143 
144    /*----- find all *.csv and *.tsv files -----*/
145 
146    ii  = strlen(dname) ;
147    cmd = (char *) malloc(sizeof(char)*3*(ii+64)) ;
148    sprintf( cmd , "find %s -maxdepth 4 -type f -name '*.[ctCT][sS][vV]'" , dname ) ;
149 
150    flist = THD_suck_pipe( cmd ) ;
151    free(cmd) ;
152    if( flist == NULL || strlen(flist) < 4 ){
153      if( flist != NULL ) free(flist) ;
154      RETURN(NULL) ;
155    }
156 
157    /* break output into discrete strings */
158 
159    qsar = NI_decode_string_list( flist , ";" ) ;
160    if( qsar == NULL || qsar->num == 0 ){    /* should never happen */
161      free(flist) ; RETURN(NULL) ;
162    }
163 
164    INIT_ELARR( outar ) ;
165 
166    /* try to read each file */
167 
168    for( ir=0 ; ir < qsar->num ; ir++ ){
169      fname = qsar->str[ir] ; if( fname == NULL ) continue ;
170 
171      /* too big? */
172      if( THD_filesize(fname) > max_fsize ) continue ;
173 
174      if( STRING_HAS_SUFFIX_CASE(fname,".csv") ){        /* .csv */
175        flim = THD_read_csv( fname ) ;
176      } else if( STRING_HAS_SUFFIX_CASE(fname,".tsv") ){ /* .tsv */
177        flim = THD_read_tsv( fname ) ;
178      } else {
179        flim = NULL ;
180      }
181 
182      /* if it worked, and it's OK, add it to the output */
183 
184      if( flim != NULL && NI_element_type(flim) == NI_ELEMENT_TYPE ){
185        if( flim->vec_num <= 0 || flim->vec_len <= 0 ){
186          NI_free_element(flim) ; flim = NULL ; /* nothing inside?! */
187        } else {
188          tname = THD_trailname(fname,1) ;
189          flim->filename = strdup(tname) ;
190          ADDTO_ELARR( outar , flim ) ;
191        }
192      } else {
193        NI_free_element(flim) ; flim = NULL ;   /* should not happen */
194      }
195    }
196 
197    /* return to the caller */
198 
199    NI_delete_str_array(qsar) ;
200    if( ELARR_COUNT(outar) == 0 ) DESTROY_ELARR(outar) ;
201    RETURN(outar) ;
202 }
203