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  

l_mcw_glob.h File Reference

#include <string.h>
#include "mcw_malloc.h"

Go to the source code of this file.


Data Structures

struct  glob_t

Defines

#define MCW_free_wildcards(a, b)   MCW_free_expand(a,b)
#define GLOB_APPEND   0x001
#define GLOB_DOOFFS   0x002
#define GLOB_ERR   0x004
#define GLOB_MAGCHAR   0x008
#define GLOB_MARK   0x010
#define GLOB_NOCHECK   0x020
#define GLOB_NOSORT   0x040
#define GLOB_QUOTE   0x080
#define GLOB_NOMAGIC   0x100
#define GLOB_ALTNOT   0x200
#define GLOB_NOSPACE   (-1)
#define GLOB_ABEND   (-2)

Functions

int rglob_set_sort_dir (int dir)
void MCW_file_expand (int nin, char **fin, int *nout, char ***fout)
void MCW_free_expand (int gnum, char **gout)
void MCW_warn_expand (int www)
void MCW_wildcards (char *fnam, int *nout, char ***fout)
int glob (const char *, int, int(*)(char *, int), glob_t *)
void globfree (glob_t *)

Define Documentation

#define GLOB_ABEND   (-2)
 

Definition at line 74 of file l_mcw_glob.h.

#define GLOB_ALTNOT   0x200
 

Definition at line 71 of file l_mcw_glob.h.

#define GLOB_APPEND   0x001
 

Definition at line 61 of file l_mcw_glob.h.

#define GLOB_DOOFFS   0x002
 

Definition at line 62 of file l_mcw_glob.h.

#define GLOB_ERR   0x004
 

Definition at line 63 of file l_mcw_glob.h.

#define GLOB_MAGCHAR   0x008
 

Definition at line 64 of file l_mcw_glob.h.

#define GLOB_MARK   0x010
 

Definition at line 65 of file l_mcw_glob.h.

#define GLOB_NOCHECK   0x020
 

Definition at line 66 of file l_mcw_glob.h.

#define GLOB_NOMAGIC   0x100
 

Definition at line 69 of file l_mcw_glob.h.

#define GLOB_NOSORT   0x040
 

Definition at line 67 of file l_mcw_glob.h.

#define GLOB_NOSPACE   (-1)
 

Definition at line 73 of file l_mcw_glob.h.

#define GLOB_QUOTE   0x080
 

Definition at line 68 of file l_mcw_glob.h.

#define MCW_free_wildcards a,
b       MCW_free_expand(a,b)
 

Definition at line 50 of file l_mcw_glob.h.


Function Documentation

int glob const char *   ,
int   ,
int(*   )(char *, int),
glob_t  
 

Definition at line 295 of file mcw_glob.c.

References __P, ALTNOT, c, CHAR, Char, compare(), EOS, flags, glob_t::gl_errfunc, glob_t::gl_flags, glob_t::gl_matchc, glob_t::gl_offs, glob_t::gl_pathc, glob_t::gl_pathv, glob1(), globextend(), LBRACKET, M_ALL, M_ALTNOT, M_END, M_NOT, m_not, M_ONE, M_PROTECT, M_RNG, M_SET, MAXPATHLEN, no_match, NOT, pglob, QUESTION, QUOTE, RANGE, RBRACKET, STAR, and Strchr().

Referenced by MCW_file_expand().

00296 {
00297     int     err, oldpathc;
00298     Char *bufnext, *bufend, *compilebuf, m_not;
00299     const unsigned char *compilepat, *patnext;
00300     int     c, nnot;
00301     Char patbuf[MAXPATHLEN + 1], *qpatnext;
00302     int     no_match;
00303 
00304     patnext = (unsigned char *) pattern;
00305     if (!(flags & GLOB_APPEND)) {
00306         pglob->gl_pathc = 0;
00307         pglob->gl_pathv = NULL;
00308         if (!(flags & GLOB_DOOFFS))
00309             pglob->gl_offs = 0;
00310     }
00311     pglob->gl_flags = flags & ~GLOB_MAGCHAR;
00312     pglob->gl_errfunc = errfunc;
00313     oldpathc = pglob->gl_pathc;
00314     pglob->gl_matchc = 0;
00315 
00316     if (pglob->gl_flags & GLOB_ALTNOT) {
00317         nnot = ALTNOT;
00318         m_not = M_ALTNOT;
00319     }
00320     else {
00321         nnot = NOT;
00322         m_not = M_NOT;
00323     }
00324 
00325     bufnext = patbuf;
00326     bufend = bufnext + MAXPATHLEN;
00327     compilebuf = bufnext;
00328     compilepat = patnext;
00329 
00330     no_match = *patnext == nnot;
00331     if (no_match)
00332         patnext++;
00333 
00334     if (flags & GLOB_QUOTE) {
00335         /* Protect the quoted characters */
00336         while (bufnext < bufend && (c = *patnext++) != EOS)
00337             if (c == QUOTE) {
00338                 if ((c = *patnext++) == EOS) {
00339                     c = QUOTE;
00340                     --patnext;
00341                 }
00342                 *bufnext++ = (Char) (c | M_PROTECT);
00343             }
00344             else
00345                 *bufnext++ = (Char) c;
00346     }
00347     else
00348         while (bufnext < bufend && (c = *patnext++) != EOS)
00349             *bufnext++ = (Char) c;
00350     *bufnext = EOS;
00351 
00352     bufnext = patbuf;
00353     qpatnext = patbuf;
00354     /* we don't need to check for buffer overflow any more */
00355     while ((c = *qpatnext++) != EOS) {
00356         switch (c) {
00357         case LBRACKET:
00358             c = *qpatnext;
00359             if (c == nnot)
00360                 ++qpatnext;
00361             if (*qpatnext == EOS ||
00362                 Strchr(qpatnext + 1, RBRACKET) == NULL) {
00363                 *bufnext++ = LBRACKET;
00364                 if (c == nnot)
00365                     --qpatnext;
00366                 break;
00367             }
00368             pglob->gl_flags |= GLOB_MAGCHAR;
00369             *bufnext++ = M_SET;
00370             if (c == nnot)
00371                 *bufnext++ = m_not;
00372             c = *qpatnext++;
00373             do {
00374                 *bufnext++ = CHAR(c);
00375                 if (*qpatnext == RANGE &&
00376                     (c = qpatnext[1]) != RBRACKET) {
00377                     *bufnext++ = M_RNG;
00378                     *bufnext++ = CHAR(c);
00379                     qpatnext += 2;
00380                 }
00381             } while ((c = *qpatnext++) != RBRACKET);
00382             *bufnext++ = M_END;
00383             break;
00384         case QUESTION:
00385             pglob->gl_flags |= GLOB_MAGCHAR;
00386             *bufnext++ = M_ONE;
00387             break;
00388         case STAR:
00389             pglob->gl_flags |= GLOB_MAGCHAR;
00390             /* collapse adjacent stars to one, to avoid
00391              * exponential behavior
00392              */
00393             if (bufnext == patbuf || bufnext[-1] != M_ALL)
00394                 *bufnext++ = M_ALL;
00395             break;
00396         default:
00397             *bufnext++ = CHAR(c);
00398             break;
00399         }
00400     }
00401     *bufnext = EOS;
00402 #ifdef DEBUG
00403     qprintf(patbuf);
00404 #endif
00405 
00406     if ((err = glob1(patbuf, pglob, no_match)) != 0)
00407         return (err);
00408 
00409     /*
00410      * If there was no match we are going to append the pattern
00411      * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
00412      * and the pattern did not contain any magic characters
00413      * GLOB_NOMAGIC is there just for compatibility with csh.
00414      */
00415     if (pglob->gl_pathc == oldpathc &&
00416         ((flags & GLOB_NOCHECK) ||
00417          ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
00418         if (!(flags & GLOB_QUOTE)) {
00419             Char *dp = compilebuf;
00420             const unsigned char *sp = compilepat;
00421 
00422             while ((*dp++ = *sp++) != '\0')
00423                 continue;
00424         }
00425         else {
00426             /*
00427              * copy pattern, interpreting quotes; this is slightly different
00428              * than the interpretation of quotes above -- which should prevail?
00429              */
00430             while (*compilepat != EOS) {
00431                 if (*compilepat == QUOTE) {
00432                     if (*++compilepat == EOS)
00433                         --compilepat;
00434                 }
00435                 *compilebuf++ = (unsigned char) *compilepat++;
00436             }
00437             *compilebuf = EOS;
00438         }
00439         return (globextend(patbuf, pglob));
00440     }
00441     else if (!(flags & GLOB_NOSORT))
00442         qsort((char *) (pglob->gl_pathv + pglob->gl_offs + oldpathc),
00443               pglob->gl_pathc - oldpathc, sizeof(char *),
00444               (int (*) __P((const void *, const void *))) compare);
00445     return (0);
00446 }

void globfree glob_t  
 

Definition at line 719 of file mcw_glob.c.

References glob_t::gl_offs, glob_t::gl_pathc, glob_t::gl_pathv, i, pglob, ptr_t, and xfree.

Referenced by MCW_file_expand().

00720 {
00721     register int i;
00722     register char **pp;
00723 
00724     if (pglob->gl_pathv != NULL) {
00725         pp = pglob->gl_pathv + pglob->gl_offs;
00726         for (i = pglob->gl_pathc; i--; ++pp)
00727             if (*pp)
00728                 xfree((ptr_t) *pp), *pp = NULL;
00729         xfree((ptr_t) pglob->gl_pathv), pglob->gl_pathv = NULL;
00730     }
00731 }

void MCW_file_expand int    nin,
char **    fin,
int *    nout,
char ***    fout
 

Routines that allows filename wildcarding to be handled inside to3d. The advantage: limitations of shell command line lengths.

  • 29 July 1996: Incorporated "glob" functions from tcsh-6.05, rather than rely on system supplying a library.
  • 30 July 1996: Extended routine to allow for 3D: type prefixes.
  • 10 Feb 2000: and for 3A: prefixes. --------------------------------------------------------------------------

Definition at line 751 of file mcw_glob.c.

References fout, glob_t::gl_pathc, glob_t::gl_pathv, glob(), globfree(), ilen, malloc, and realloc.

Referenced by AFNI_read_inputs(), main(), MCW_wildcards(), NIH_glob(), read_ge_files(), T3D_read_images(), THD_get_all_filenames(), THD_get_wildcard_filenames(), and THD_init_session().

00752 {
00753    glob_t gl ;
00754    int    ii , gnum, gold , ilen ;
00755    char ** gout ;
00756    char *  fn ;
00757    char prefix[4] , fpre[128] , fname[256] ;
00758    int  b1,b2,b3,b4,b5 , ib,ig , lpre ;
00759 
00760    if( nin <= 0 ){ *nout = 0 ; return ; }
00761 
00762    gnum = 0 ;
00763    gout = NULL ;
00764 
00765    for( ii=0 ; ii < nin ; ii++ ){
00766       fn = fin[ii] ;
00767 
00768       ig = 0 ;
00769 
00770       /** look for 3D: prefix **/
00771 
00772       if( strlen(fn) > 9 && fn[0] == '3' && fn[1] == 'D' ){
00773          ib = 0 ;
00774          prefix[ib++] = '3' ;
00775          prefix[ib++] = 'D' ;
00776          if( fn[2] == ':' ){ prefix[ib++] = '\0' ; }
00777          else              { prefix[ib++] = fn[2] ; prefix[ib++] = '\0' ; }
00778 
00779          ig = sscanf( fn+ib , "%d:%d:%d:%d:%d:%s" ,     /* must scan all */
00780                       &b1,&b2,&b3,&b4,&b5 , fname ) ;   /* six items OK  */
00781 
00782          /** if have all 6 3D: items, then make a 3D: prefix for output **/
00783 
00784          if( ig == 6 ){
00785             sprintf(fpre , "%s:%d:%d:%d:%d:%d:" , prefix,b1,b2,b3,b4,b5) ;
00786             lpre = strlen(fpre) ;
00787          } else {
00788             ig = 0 ;
00789          }
00790       }
00791 
00792       if( strlen(fn) > 9 && fn[0] == '3' && fn[1] == 'A' && fn[3] == ':' ){
00793          ib = 0 ;
00794          prefix[ib++] = '3' ;
00795          prefix[ib++] = 'A' ;
00796          prefix[ib++] = fn[2] ;
00797          prefix[ib++] = '\0' ;
00798 
00799          ig = sscanf( fn+ib , "%d:%d:%d:%s" ,  /* must scan all */
00800                       &b1,&b2,&b3, fname ) ;   /* four items OK */
00801 
00802          /** if have all 4 3A: items, then make a 3A: prefix for output **/
00803 
00804          if( ig == 4 ){
00805             sprintf(fpre , "%s:%d:%d:%d:" , prefix,b1,b2,b3) ;
00806             lpre = strlen(fpre) ;
00807          } else {
00808             ig = 0 ;
00809          }
00810       }
00811 
00812       if( ig > 0 ) (void) glob( fname , 0 , NULL ,  &gl ) ;  /* 3D: was OK */
00813       else         (void) glob( fn    , 0 , NULL ,  &gl ) ;  /*     not OK */
00814 
00815       /** put each matched string into the output array **/
00816 
00817       if( gl.gl_pathc > 0 ){
00818 
00819          /** make space for output now **/
00820          gold  = gnum ;
00821          gnum += gl.gl_pathc ;
00822          if( gout == NULL ) gout = (char **) malloc (      sizeof(char *)*gnum);
00823          else               gout = (char **) realloc(gout, sizeof(char *)*gnum);
00824 
00825          for( ib=0 ; ib < gl.gl_pathc ; ib++ ){
00826             ilen = strlen( gl.gl_pathv[ib] ) + 1 ;  /* length of this name */
00827             if( ig > 0 ) ilen += lpre ;             /* plus 3D: prefix?    */
00828 
00829             gout[ib+gold] = (char *) malloc( sizeof(char) * ilen ) ; /* output! */
00830 
00831             if( ig > 0 ){
00832                strcpy( gout[ib+gold] , fpre ) ;             /* 3D: prefix */
00833                strcat( gout[ib+gold] , gl.gl_pathv[ib] ) ;  /* then name  */
00834             }
00835             else {
00836                strcpy( gout[ib+gold] , gl.gl_pathv[ib] ) ;  /* just name */
00837             }
00838          }
00839 
00840       } else if( ig == 6 && strcmp(fname,"ALLZERO") == 0 ){ /* 06 Mar 2001 */
00841 
00842          gold = gnum ; gnum++ ;
00843          if( gout == NULL ) gout = (char **) malloc (      sizeof(char *)*gnum);
00844          else               gout = (char **) realloc(gout, sizeof(char *)*gnum);
00845 
00846          ilen = lpre + strlen(fname) + 1 ;
00847          gout[gold] = (char *) malloc( sizeof(char) * ilen ) ; /* output! */
00848          strcpy( gout[gold] , fpre ) ;
00849          strcat( gout[gold] , fname ) ;
00850 
00851       } else {  /* 30 Apr 2001 */
00852 
00853          if( warn )  /* 13 Jul 2001 - print only if told to do so */
00854            fprintf(stderr,"** Can't find file %s\n", (ig>0) ? fname : fn ) ;
00855       }
00856 
00857       globfree( &gl ) ;
00858    }
00859 
00860    *nout = gnum ; *fout = gout ; return ;
00861 }

void MCW_free_expand int    gnum,
char **    gout
 

make space for output now *

Definition at line 908 of file mcw_glob.c.

References free.

Referenced by AFNI_read_inputs(), main(), NIH_glob_free(), read_ge_files(), T3D_read_images(), THD_get_all_filenames(), THD_get_wildcard_filenames(), and THD_init_session().

00909 {
00910    int ii ;
00911 
00912    if( gout == NULL || gnum == 0 ) return ;
00913 
00914    for( ii=0 ; ii < gnum ; ii++ ) free( gout[ii] ) ;
00915    free( gout ) ;
00916    return ;
00917 }

void MCW_warn_expand int    www
 

Definition at line 740 of file mcw_glob.c.

References warn.

Referenced by AFNI_read_inputs(), main(), T3D_read_images(), THD_get_all_filenames(), and THD_get_wildcard_filenames().

00740 { warn = www; return; }  /* 13 Jul 2001 */

void MCW_wildcards char *    fnam,
int *    nout,
char ***    fout
 

Simpler interface to MCW_file_expand().

  • fnam = string of form "*.zork fred*.* ?a?b"; e.g., 1 or more wildcards
  • nout = pointer to output count
  • fout = pointer to output list of strings.
Sample usage: int nfile ; char **flist ; MCW_wildcards( "*.jpg *.JPG" , &nfile , &flist ) ; ... do something with flist[0]..flist[nfile-1] if nfile > 0 ... MCW_free_wildcards( nfile , flist ) ; -------------------------------------------------------------------------

Definition at line 877 of file mcw_glob.c.

References fout, free, MCW_file_expand(), and realloc.

Referenced by AFNI_find_jpegs(), and THD_init_session().

00878 {
00879    char **fin=NULL, *fcop ;
00880    int ii , nin , lf , ls ;
00881 
00882    if( fnam == NULL || *fnam == '\0' ){ *nout = 0 ; return ; }
00883    fcop = strdup(fnam) ; lf = strlen(fcop) ;
00884    ls = 1 ;
00885    for( nin=ii=0 ; ii < lf ; ii++ ){
00886      if( isspace(fcop[ii]) ){   /* This is a blank, so next */
00887        ls = 1 ;                 /*  non-blank is a new word. */
00888        fcop[ii] = '\0' ;        /* Set this char to NUL.      */
00889 
00890      } else {                   /* Not a blank. */
00891 
00892        if( ls ){                /* If last was a blank, is new name. */
00893          fin = (char **) realloc( fin , sizeof(char *)*(nin+1) ) ;
00894          fin[nin++] = fcop+ii ;
00895        }
00896        ls = 0 ;
00897      }
00898    }
00899 
00900    if( nin == 0 ){ *nout = 0 ; free(fcop) ; return ; }
00901 
00902    MCW_file_expand( nin , fin , nout , fout ) ;
00903    free(fin) ; free(fcop) ; return ;
00904 }

int rglob_set_sort_dir int    dir
 

set the direction of the sort (either small to large, or the reverse)

Definition at line 743 of file l_mcw_glob.c.

References g_sort_dir.

Referenced by init_options().

00744 {
00745    if ( dir == 1 )       g_sort_dir =  1;
00746    else if ( dir == -1 ) g_sort_dir = -1;
00747    else                  return 1;          /* else, ignore and signal error */
00748                                                                                 
00749    return 0;
00750 }
 

Powered by Plone

This site conforms to the following standards: