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_strfunc.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 
00003 /*-----------------------------------------------------------------*/
00004 /*! Find if needle is in haystack,
00005     ignoring case and ignoring any characters in ignore. */
00006 
00007 char * ig_strstr( char *haystack , char *needle , char *ignore )
00008 {
00009    char *hs, *ne , *cp ;
00010    int ii, jj ;
00011 
00012    if( haystack == NULL || haystack[0] == '\0' ||
00013        needle   == NULL || needle[0]   == '\0'   ) return NULL ;
00014 
00015    /* make uppercase copy of haystack */
00016 
00017    hs = strdup(haystack) ; jj = strlen(hs) ;
00018    for( ii=0 ; ii < jj ; ii++ ) hs[ii] = toupper(hs[ii]) ;
00019 
00020    /* replace all ignore characters in hs with a period */
00021 
00022    if( ignore != NULL && ignore[0] != '\0' ){
00023      for( ii=0 ; ii < jj ; ii++ ){
00024        if( strchr(ignore,hs[ii]) != NULL ) hs[ii] = '.' ;
00025      }
00026    }
00027 
00028    /* make uppercase copy of needle */
00029 
00030    ne = strdup(needle) ; jj = strlen(ne) ;
00031    for( ii=0 ; ii < jj ; ii++ ) ne[ii] = toupper(ne[ii]) ;
00032 
00033    /* replace all ignore characters in ne with a period */
00034 
00035    if( ignore != NULL && ignore[0] != '\0' ){
00036      for( ii=0 ; ii < jj ; ii++ ){
00037        if( strchr(ignore,ne[ii]) != NULL ) ne[ii] = '.' ;
00038      }
00039    }
00040 
00041    /* now find if mangled needle is in the mangled haystack */
00042 
00043    cp = strstr( hs , ne ) ;
00044 
00045    /* if it is, then find corresponding location in original haystack */
00046 
00047    if( cp != NULL ){
00048      jj = cp-hs ;            /* pointer arithmetic */
00049      cp = haystack + jj ;    /* ditto */
00050    }
00051 
00052    /* we're outta here */
00053 
00054    free(ne); free(hs); return cp;
00055 }
00056 
00057 /*--------------------------------------------------------------------------
00058   Free up an array of previously malloc()-ed strings
00059 ----------------------------------------------------------------------------*/
00060 
00061 void freeup_strings( int n , char **sar )
00062 {
00063    int ii ;
00064    if( sar == NULL ) return ;
00065    for( ii=0 ; ii < n ; ii++ )
00066       if( sar[ii] != NULL ) free(sar[ii]) ;
00067    free(sar) ;
00068    return ;
00069 }
00070 
00071 /*--------------------------------------------------------------------------
00072   Break a string into a set of sub-strings.
00073   Return value is number of sub-strings (< 0 is an error);
00074   (*stok)[i] is the i-th string.
00075 ----------------------------------------------------------------------------*/
00076 
00077 #define DQUOT  '"'   /* double quote character */
00078 #define SQUOT  '\''  /* single quote character */
00079 #define NUL    '\0'  /* ASCII NUL character    */
00080 
00081 int breakup_string( char *sin , char ***stok )
00082 {
00083    int n_tok , quote , ll ;
00084    char **s_tok , *cpt , *sss , qch=NUL ;
00085 
00086    if( stok == NULL || sin == NULL || sin[0] == NUL ) return -1 ;
00087 
00088    n_tok = 0 ;
00089    s_tok = NULL ;
00090 
00091    cpt = sin ;
00092 
00093    while( *cpt != '\0' ){  /* loop until we use up the input string */
00094 
00095       /* skip whitespace */
00096 
00097       while( isspace(*cpt) ) cpt++ ;
00098       if( *cpt == NUL ) break ;        /* reached end */
00099 
00100       /* if starts with a quote, note that factoid */
00101 
00102       if( *cpt == SQUOT || *cpt == DQUOT ){
00103          quote = 1 ; qch = *cpt ; cpt++ ;   /* qch = quote character */
00104          if( *cpt == NUL ) break ;
00105       } else {
00106         quote = 0 ;
00107       }
00108 
00109       /* scan until end of sub-string (next quote, or next nonblank) */
00110 
00111       sss = cpt ;    /* start of sub-string */
00112       if( quote ){
00113          while( *cpt != NUL && *cpt != qch    ) cpt++ ;  /* scan to next quote */
00114       } else {
00115          while( *cpt != NUL && !isspace(*cpt) ) cpt++ ;  /* to next non-blank */
00116       }
00117 
00118       /* cpt now points to character after end of sub-string */
00119 
00120       ll = cpt - sss ;  /* number of characters in sub-string (may be zero) */
00121 
00122       /* make a new entry in the string table */
00123 
00124       s_tok = (char **) realloc( s_tok , sizeof(char *) * (n_tok+1) ) ;
00125       s_tok[n_tok] = (char *) malloc(ll+4) ;
00126       if( ll > 0 ) memcpy( s_tok[n_tok] , sss , ll ) ;
00127       s_tok[n_tok][ll] = NUL ;
00128       n_tok++ ;
00129 
00130       /* skip the character after the end of sub-string */
00131 
00132       if( *cpt == NUL ) break ;  /* end of the line, baby [14 Apr 2005] */
00133       cpt++ ;
00134    } /* end of loop over input string */
00135 
00136    *stok = s_tok ; return n_tok ;
00137 }
 

Powered by Plone

This site conforms to the following standards: