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  

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

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 72 of file mcw_glob.h.

#define GLOB_ALTNOT   0x200
 

Definition at line 69 of file mcw_glob.h.

#define GLOB_APPEND   0x001
 

Definition at line 59 of file mcw_glob.h.

#define GLOB_DOOFFS   0x002
 

Definition at line 60 of file mcw_glob.h.

#define GLOB_ERR   0x004
 

Definition at line 61 of file mcw_glob.h.

#define GLOB_MAGCHAR   0x008
 

Definition at line 62 of file mcw_glob.h.

#define GLOB_MARK   0x010
 

Definition at line 63 of file mcw_glob.h.

#define GLOB_NOCHECK   0x020
 

Definition at line 64 of file mcw_glob.h.

#define GLOB_NOMAGIC   0x100
 

Definition at line 67 of file mcw_glob.h.

#define GLOB_NOSORT   0x040
 

Definition at line 65 of file mcw_glob.h.

#define GLOB_NOSPACE   (-1)
 

Definition at line 71 of file mcw_glob.h.

#define GLOB_QUOTE   0x080
 

Definition at line 66 of file mcw_glob.h.

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

Definition at line 48 of file mcw_glob.h.

Referenced by AFNI_find_jpegs().


Function Documentation

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

Definition at line 298 of file l_mcw_glob.c.

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

void globfree glob_t  
 

Definition at line 722 of file l_mcw_glob.c.

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

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 765 of file l_mcw_glob.c.

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

void MCW_free_expand int    gnum,
char **    gout
 

make space for output now *

Definition at line 922 of file l_mcw_glob.c.

00923 {
00924    int ii ;
00925 
00926    if( gout == NULL ) return ;
00927 
00928    for( ii=0 ; ii < gnum ; ii++ ) free( gout[ii] ) ;
00929    free( gout ) ;
00930    return ;
00931 }

void MCW_warn_expand int    www
 

Definition at line 754 of file l_mcw_glob.c.

00754 { 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 891 of file l_mcw_glob.c.

00892 {
00893    char **fin=NULL, *fcop ;
00894    int ii , nin , lf , ls ;
00895 
00896    if( fnam == NULL || *fnam == '\0' ){ *nout = 0 ; return ; }
00897    fcop = strdup(fnam) ; lf = strlen(fcop) ;
00898    ls = 1 ;
00899    for( nin=ii=0 ; ii < lf ; ii++ ){
00900      if( isspace(fcop[ii]) ){   /* This is a blank, so next */
00901        ls = 1 ;                 /*  non-blank is a new word. */
00902        fcop[ii] = '\0' ;        /* Set this char to NUL.      */
00903 
00904      } else {                   /* Not a blank. */
00905 
00906        if( ls ){                /* If last was a blank, is new name. */
00907          fin = (char **) realloc( fin , sizeof(char *)*(nin+1) ) ;
00908          fin[nin++] = fcop+ii ;
00909        }
00910        ls = 0 ;
00911      }
00912    }
00913 
00914    if( nin == 0 ){ *nout = 0 ; free(fcop) ; return ; }
00915 
00916    MCW_file_expand( nin , fin , nout , fout ) ;
00917    free(fin) ; free(fcop) ; return ;
00918 }
 

Powered by Plone

This site conforms to the following standards: