Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
thd_intlist.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #include "mrilib.h"
00008 
00009 static int allow_negative = 0 ;
00010 
00011 
00012 
00013 void MCW_intlist_allow_negative( int iii )   
00014 {
00015    allow_negative = iii ; return ;
00016 }
00017 
00018 
00019 
00020 #define ISEND(c) ( (c)==']' || (c)=='}' || (c)=='\0' )
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 int * MCW_get_intlist( int nvals , char *str )
00047 {
00048    int *subv = NULL ;
00049    int ii , ipos , nout , slen ;
00050    int ibot,itop,istep , nused ;
00051    char *cpt ;
00052 
00053    
00054 
00055    if( nvals < 1 ) return NULL ;
00056 
00057    
00058 
00059    if( str == NULL || str[0] == '\0' ) return NULL ;
00060 
00061    
00062 
00063    subv    = (int *) malloc( sizeof(int) * 2 ) ;
00064    subv[0] = nout = 0 ;
00065 
00066    ipos = 0 ;
00067    if( str[ipos] == '[' || str[ipos] == '{' ) ipos++ ;
00068 
00069    
00070 
00071    slen = strlen(str) ;
00072    while( ipos < slen && !ISEND(str[ipos]) ){
00073 
00074       while( isspace(str[ipos]) ) ipos++ ;   
00075       if( ISEND(str[ipos]) ) break ;         
00076 
00077 
00078 
00079       if( str[ipos] == '$' ){  
00080          ibot = nvals-1 ; ipos++ ;
00081       } else {                 
00082          ibot = strtol( str+ipos , &cpt , 10 ) ;
00083          if( ibot < 0 && !allow_negative ){
00084            fprintf(stderr,"** ERROR: sub-brick index %d is out of range 0..%d\n",
00085                    ibot,nvals-1) ;
00086            free(subv) ; return NULL ;
00087          }
00088          if( ibot >= nvals ){
00089            fprintf(stderr,"** ERROR: sub-brick index %d is out of range 0..%d\n",
00090                    ibot,nvals-1) ;
00091            free(subv) ; return NULL ;
00092          }
00093          nused = (cpt-(str+ipos)) ;
00094          if( ibot == 0 && nused == 0 ){
00095            fprintf(stderr,"** ERROR: sub-brick syntax error '%s'\n",str+ipos) ;
00096            free(subv) ; return NULL ;
00097          }
00098          ipos += nused ;
00099       }
00100 
00101       while( isspace(str[ipos]) ) ipos++ ;   
00102 
00103 
00104 
00105       if( str[ipos] == ',' || ISEND(str[ipos]) ){
00106          nout++ ;
00107          subv = (int *) realloc( (char *)subv , sizeof(int) * (nout+1) ) ;
00108          subv[0]    = nout ;
00109          subv[nout] = ibot ;
00110          if( ISEND(str[ipos]) ) break ; 
00111          ipos++ ; continue ;            
00112       }
00113 
00114 
00115 
00116       if( str[ipos] == '-' ){
00117          ipos++ ;
00118       } else if( str[ipos] == '.' && str[ipos+1] == '.' ){
00119          ipos++ ; ipos++ ;
00120       } else {
00121          fprintf(stderr,"** ERROR: sub-brick selector syntax is bad: '%s'\n",
00122                  str+ipos) ;
00123          free(subv) ; return NULL ;
00124       }
00125 
00126 
00127 
00128       if( str[ipos] == '$' ){  
00129          itop = nvals-1 ; ipos++ ;
00130       } else {                 
00131          itop = strtol( str+ipos , &cpt , 10 ) ;
00132          if( itop < 0 && !allow_negative ){
00133            fprintf(stderr,"** ERROR: sub-brick index %d is out of range 0..%d\n",
00134                    itop,nvals-1) ;
00135            free(subv) ; return NULL ;
00136          }
00137          if( itop >= nvals ){
00138            fprintf(stderr,"** ERROR: sub-brick index %d is out of range 0..%d\n",
00139                    itop,nvals-1) ;
00140            free(subv) ; return NULL ;
00141          }
00142          nused = (cpt-(str+ipos)) ;
00143          if( itop == 0 && nused == 0 ){
00144            fprintf(stderr,"** ERROR: sub-brick syntax error '%s'\n",str+ipos) ;
00145            free(subv) ; return NULL ;
00146          }
00147          ipos += nused ;
00148       }
00149 
00150 
00151 
00152       istep = (ibot <= itop) ? 1 : -1 ;
00153 
00154       while( isspace(str[ipos]) ) ipos++ ;                  
00155 
00156 
00157 
00158       if( str[ipos] == '(' ){  
00159          ipos++ ;
00160          istep = strtol( str+ipos , &cpt , 10 ) ;
00161          if( istep == 0 ){
00162            fprintf(stderr,"** ERROR: sub-brick loop step is 0!\n") ;
00163            free(subv) ; return NULL ;
00164          }
00165          nused = (cpt-(str+ipos)) ;
00166          ipos += nused ;
00167          if( str[ipos] == ')' ) ipos++ ;
00168          if( (ibot-itop)*istep > 0 ){
00169            fprintf(stderr,"** WARNING: sub-brick count '%d..%d(%d)' means nothing!\n",
00170                    ibot,itop,istep ) ;
00171          }
00172       }
00173 
00174 
00175 
00176       for( ii=ibot ; (ii-itop)*istep <= 0 ; ii += istep ){
00177          nout++ ;
00178          subv = (int *) realloc( (char *)subv , sizeof(int) * (nout+1) ) ;
00179          subv[0]    = nout ;
00180          subv[nout] = ii ;
00181       }
00182 
00183 
00184 
00185       while( isspace(str[ipos]) ) ipos++ ;                  
00186       if( str[ipos] == ',' ) ipos++ ;                       
00187 
00188    }  
00189 
00190    if( subv[0] == 0 ){ free(subv); subv = NULL; }
00191    return subv ;
00192 }