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  

cs_addto_args.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006    
00007 #include "cs.h"
00008 #include "string.h"
00009 
00010 #define BLEN 4096
00011 
00012 /*------------------ 18 Nov 1999: utility routines --------------------*/
00013 
00014 static void tokenize_string( char * sin , int * ntok , char *** stok )
00015 {
00016    int     n_tok , ii ;
00017    char ** s_tok , *cpt , *sss ;
00018 
00019    if( stok == NULL ) return ;
00020    if( ntok == NULL || sin == NULL || sin[0] == '\0' ){ *stok = NULL; return; }
00021 
00022    n_tok = 0 ;
00023    s_tok = (char **) malloc( sizeof(char *) ) ;
00024 
00025    /* break input into tokens, copy them in the new arg list */
00026 
00027    cpt = strtok( sin , " \t\n\r\f\v" ) ;
00028    if( cpt == NULL ){ free(s_tok); *stok = NULL; return; }     /* do nothing */
00029 
00030    while( cpt != NULL ){
00031       ii  = strlen(cpt) ;
00032       sss = (char *) malloc( sizeof(char) * (ii+1) ) ;
00033       strcpy(sss,cpt) ;
00034       n_tok++ ;
00035       s_tok = (char **) realloc( s_tok , sizeof(char *) * n_tok ) ;
00036       s_tok[n_tok-1] = sss ;
00037 
00038       cpt = strtok( NULL , " \t\n\r\f\v" ) ;
00039    }
00040 
00041    *ntok = n_tok ; *stok = s_tok ; return ;
00042 }
00043 
00044 static void duplicate_string_list( int nin , char ** sin , char *** sout )
00045 {
00046    int ii , ll ;
00047    char ** s_out = NULL ;
00048 
00049    if( sout == NULL ) return ;
00050    if( nin < 1 || sin == NULL ){ *sout = NULL ; return ; }
00051 
00052    s_out = (char **) malloc( sizeof(char *) * nin ) ;
00053    for( ii=0 ; ii < nin ; ii++ ){
00054       ll = strlen(sin[ii]) ;
00055       s_out[ii] = (char *) malloc( sizeof(char) * (ll+1) );
00056       strcpy( s_out[ii] , sin[ii] ) ;
00057    }
00058 
00059    *sout = s_out ; return ;
00060 }
00061 
00062 static void free_string_list( int nin , char ** sin )
00063 {
00064    int ii ;
00065    if( sin == NULL ) return ;
00066    for( ii=0 ; ii < nin ; ii++ ) if( sin[ii] != NULL ) free(sin[ii]) ;
00067    free(sin) ; return ;
00068 }
00069 
00070 static void appendto_string_list( int *nfirst , char *** sfirst ,
00071                                   int nsecond , char ** ssecond  )
00072 {
00073    int     nf=*nfirst , ii , ll ;
00074    char ** sf ;
00075 
00076    if( nsecond < 1 || ssecond == NULL ) return ;  /* nothing to do */
00077 
00078    if( *sfirst == NULL || nf == 0 )
00079       sf = (char **) malloc( sizeof(char *) * nsecond ) ;
00080    else
00081       sf = (char **) realloc( *sfirst , sizeof(char *)*(nf+nsecond) ) ;
00082 
00083    for( ii=0 ; ii < nsecond ; ii++ ){
00084       ll = strlen(ssecond[ii]) ;
00085       sf[nf+ii] = (char *) malloc( sizeof(char) * (ll+1) ) ;
00086       strcpy( sf[nf+ii] , ssecond[ii] ) ;
00087    }
00088 
00089    *nfirst = nf+nsecond ;
00090    *sfirst = sf         ; return ;
00091 }
00092 
00093 /*----------------------------------------------------------------------------
00094    18 Nov 1999: Take the input string (sin) and put its pieces at the
00095                 front of the arg list, just after argv[0].
00096                 If *new_argv is returned as NULL, then there are no new args.
00097 ------------------------------------------------------------------------------*/
00098 
00099 void prepend_string_to_args( char * sin ,
00100                              int argc , char * argv[] ,
00101                              int * new_argc , char *** new_argv )
00102 {
00103    int     n_argc , ii , ntok=0    ;
00104    char ** n_argv ,   ** stok=NULL ;
00105    char * cpt , * sss ;
00106 
00107    if( new_argc == NULL || new_argv == NULL ) return ;              /* error */
00108 
00109    if( sin == NULL || sin[0] == '\0' ){ *new_argv = NULL; return; } /* do nothing */
00110 
00111    /*-- if no inputs after argv[0], prepend and append are identical --*/
00112 
00113    if( argc < 2 ){
00114       append_string_to_args( sin , argc , argv , new_argc , new_argv ) ;
00115       return ;
00116    }
00117 
00118    /*-- OK, must do it my way --*/
00119 
00120    tokenize_string( sin , &ntok , &stok ) ;
00121    if( stok == NULL || ntok < 1 ){ *new_argv = NULL; return; }      /* do nothing */
00122 
00123    /* copy first input arg to output */
00124 
00125    duplicate_string_list( 1 , argv , &n_argv ) ;
00126    n_argc = 1 ;
00127 
00128    /* append token list to output */
00129 
00130    appendto_string_list( &n_argc , &n_argv , ntok , stok ) ;
00131    free_string_list( ntok , stok ) ;
00132 
00133    /* append rest of input args to output */
00134 
00135    appendto_string_list( &n_argc , &n_argv , argc-1 , argv+1 ) ;
00136 
00137    *new_argc = n_argc ;  /* the results! */
00138    *new_argv = n_argv ;
00139    return ;
00140 }
00141 
00142 /*----------------------------------------------------------------------------
00143    18 Nov 1999: Take the input string (sin) and append its pieces to
00144                 the existing command line arguments.
00145                 If *new_argv is returned as NULL, then there are no new args.
00146 ------------------------------------------------------------------------------*/
00147 
00148 void append_string_to_args( char * sin ,
00149                             int argc , char * argv[] ,
00150                             int * new_argc , char *** new_argv )
00151 {
00152    int     n_argc , ii , ntok=0    ;
00153    char ** n_argv ,   ** stok=NULL ;
00154    char * cpt , * sss ;
00155 
00156    if( new_argc == NULL || new_argv == NULL ) return ;              /* error */
00157 
00158    if( sin == NULL || sin[0] == '\0' ){ *new_argv = NULL; return; } /* do nothing */
00159 
00160    tokenize_string( sin , &ntok , &stok ) ;
00161    if( stok == NULL || ntok < 1 ){ *new_argv = NULL; return; }      /* do nothing */
00162 
00163    /* copy input args to output */
00164 
00165    if( argc > 0 ){
00166       duplicate_string_list( argc , argv , &n_argv ) ;
00167       n_argc = argc ;
00168    } else {                                                    /* shouldn't happen */
00169       n_argv = NULL ;
00170       n_argc = 0 ;
00171    }
00172 
00173    /* append token list to output */
00174 
00175    appendto_string_list( &n_argc , &n_argv , ntok , stok ) ;
00176    free_string_list( ntok , stok ) ;
00177 
00178    *new_argc = n_argc ;  /* the results! */
00179    *new_argv = n_argv ;
00180    return ;
00181 }
00182 
00183 /*-------------------------------------------------------------------------------
00184    Copy stdin to the command line arguments if the last argument on the
00185    command line is '-@'.
00186    18 Nov 1999: modified to put the actual args mangling in the routine above.
00187 ---------------------------------------------------------------------------------*/
00188 
00189 void addto_args( int argc , char * argv[] , int * new_argc , char *** new_argv )
00190 {
00191    int  ii , nsin , nall ;
00192    char lbuf[4096] ;
00193    char * sin , * cpt ;
00194 
00195    /*-- sanity checks --*/
00196 
00197    if( new_argc == NULL || new_argv == NULL ) return ;
00198 
00199    if( strcmp(argv[argc-1],"-@") != 0 ){ *new_argv = NULL; return; } /* do nothing */
00200 
00201    /* suck the standard input in */
00202 
00203    nall = BLEN ;
00204    sin  = (char *) malloc( sizeof(char) * nall ) ;  /* will hold stdin */
00205    nsin = 0 ;
00206 
00207    do{
00208       cpt = fgets( lbuf , BLEN , stdin ) ; /* read line */
00209       if( cpt == NULL ) break ;            /* end of file */
00210       ii = strlen(lbuf) ;
00211       if( ii+nsin >= nall-4 ){             /* make more sin space */
00212          nall += BLEN ;
00213          sin   = (char *) realloc( sin , sizeof(char) * nall ) ;
00214       }
00215       strcat(sin,lbuf) ; nsin = strlen(sin) ;  /* add to sin array */
00216    } while(1) ;
00217 
00218    if( nsin == 0 ){ *new_argv = NULL; free(sin); return; }  /* nothing was read */
00219 
00220    append_string_to_args( sin , argc-1 , argv , new_argc , new_argv ) ;  /* real work */
00221 
00222    free(sin) ; return ;
00223 }
 

Powered by Plone

This site conforms to the following standards: