Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
strblast.c
Go to the documentation of this file.00001 #include "mrilib.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 void help_n_exit( void );
00016 int suck_file( char *fname , char **fbuf ) ;
00017
00018 int main( int argc , char * argv[] )
00019 {
00020 int nbuf , ntarg , ii,jj, nfind , ff ;
00021 char *fbuf , *targ ;
00022 char *jstr=strdup("AFNI-rules-") ; int njstr=strlen(jstr) ;
00023
00024 int ac, unesc = 0;
00025 int use_newstr = 0;
00026 char newchar = 'x';
00027
00028 int nfname=0 ;
00029 char **fname=NULL ;
00030
00031
00032
00033 if( argc < 3 ) help_n_exit();
00034
00035
00036
00037 for( ac = 1; ac < argc && argv[ac][0] == '-'; ac++ ){
00038 if( strcmp(argv[ac], "-help") == 0 )
00039 help_n_exit();
00040
00041 if( strcmp(argv[ac], "-new_char") == 0 ){
00042 ac++;
00043 if( ac >= argc ){
00044 fprintf(stderr,"** -new_char option requires an argument\n");
00045 exit(1);
00046 }
00047
00048 newchar = argv[ac][0];
00049 }
00050
00051 if( strcmp(argv[ac], "-new_string") == 0 ){
00052 ac++;
00053 if( ac >= argc ){
00054 fprintf(stderr,"** -new_string option requires an argument\n");
00055 exit(1);
00056 }
00057
00058 jstr = strdup(argv[ac]);
00059 njstr = strlen(jstr);
00060 use_newstr = 1;
00061 }
00062
00063 if( strcmp(argv[ac], "-unescape") == 0 )
00064 unesc = 1;
00065 }
00066
00067 if( ac > argc-2 ){
00068 fprintf(stderr,"** missing target string or input files\n");
00069 fprintf(stderr," (please see 'strblast -help')\n");
00070 exit(1);
00071 }
00072
00073 machdep() ;
00074
00075
00076
00077 targ = argv[ac] ; ntarg = strlen(targ) ; ac++ ;
00078 if( ntarg < 1 ){
00079 fprintf(stderr,"** Can't enter an empty target string!\n") ;
00080 exit(1) ;
00081 }
00082 if( unesc ){
00083 char * tnew = strdup(targ);
00084 if( !tnew ){ fprintf(stderr,"** cannot dup targetstring?!\n"); exit(1); }
00085 for(ii=0, jj = 0; ii<ntarg; ii++, jj++){
00086 if(tnew[ii] == '\\' && tnew[ii+1] == 't'){tnew[jj] = '\t'; ii++;}
00087 else if(tnew[ii] == '\\' && tnew[ii+1] == 'n'){tnew[jj] = '\n'; ii++;}
00088 else if(tnew[ii] == '\\' && tnew[ii+1] == 'r'){tnew[jj] = '\r'; ii++;}
00089 else if(ii > jj) tnew[jj] = tnew[ii];
00090 }
00091 tnew[jj] = '\0';
00092
00093
00094 targ = tnew; ntarg = jj;
00095 }
00096
00097
00098 if( ntarg < njstr ){
00099 if( use_newstr ){ jstr[ntarg] = '\0' ; njstr = ntarg; }
00100 else { jstr[0] = newchar ; njstr = 1; }
00101 }
00102
00103
00104
00105 MCW_warn_expand(1) ;
00106 MCW_file_expand( argc-ac , argv+ac , &nfname , &fname ) ;
00107 MCW_warn_expand(0) ;
00108 if( nfname == 0 ){
00109 fprintf(stderr,"** No files found from command line!\n") ;
00110 exit(1) ;
00111 }
00112
00113
00114
00115 for( ff=0 ; ff < nfname ; ff++ ){
00116
00117
00118
00119 fbuf = NULL ;
00120 nbuf = suck_file( fname[ff] , &fbuf ) ;
00121 if( nbuf < ntarg || fbuf == NULL ){
00122 fprintf(stderr,"** Can't read input file %s\n",fname[ff]) ;
00123 if( fbuf != NULL ) free(fbuf) ;
00124 continue ;
00125 }
00126
00127
00128
00129 for( nfind=ii=0 ; ii < nbuf-ntarg ; ii++ ){
00130
00131 if( fbuf[ii] == targ[0] ){
00132
00133 for( jj=1; jj < ntarg && fbuf[ii+jj]==targ[jj] ; jj++ ) ;
00134
00135 if( jj == ntarg ){
00136 nfind++ ;
00137 for( jj=0 ; jj < njstr ; jj++ ) fbuf[ii+jj] = jstr[jj] ;
00138 for( ; jj < ntarg ; jj++ ) fbuf[ii+jj] = newchar ;
00139 }
00140 ii += ntarg-1 ;
00141 }
00142 }
00143
00144 if( nfind > 0 ){
00145 FILE *fp ;
00146 fprintf(stderr,"++ Found %d copies of target %s in file %s\n",
00147 nfind,targ,fname[ff] ) ;
00148 fp = fopen( fname[ff] , "wb" ) ;
00149 if( fp == NULL ){
00150 fprintf(stderr,"** Can't open file %s for output!\n",fname[ff]) ;
00151 exit(1) ;
00152 }
00153 fwrite( fbuf , 1 , nbuf , fp ) ; fclose(fp) ;
00154 } else {
00155 fprintf(stderr,"++ Found no copies of target %s in file %s\n",
00156 targ , fname[ff] ) ;
00157 }
00158
00159 free(fbuf) ;
00160
00161 }
00162
00163 exit(0) ;
00164 }
00165
00166
00167
00168 int suck_file( char *fname , char **fbuf )
00169 {
00170 int len , fd , ii ;
00171 char * buf ;
00172
00173 if( fname == NULL || fname[0] == '\0' || fbuf == NULL ) return 0 ;
00174
00175 len = THD_filesize( fname ) ;
00176 if( len <= 0 ) return 0 ;
00177
00178 buf = (char *) malloc( sizeof(char) * (len+4) ) ;
00179 if( buf == NULL ) return 0 ;
00180
00181 fd = open( fname , O_RDONLY ) ;
00182 if( fd < 0 ) return 0 ;
00183
00184 ii = read( fd , buf , len ) ;
00185 close( fd ) ;
00186 if( ii <= 0 ){ free(buf) ; return 0; }
00187 *fbuf = buf ; return ii ;
00188 }
00189
00190 void help_n_exit( void )
00191 {
00192 printf("Usage: strblast [options] TARGETSTRING filename ...\n"
00193 "Finds exact copies of the target string in each of\n"
00194 "the input files, and replaces all characters with\n"
00195 "some junk string.\n"
00196 "\n"
00197 "options:\n"
00198 "\n"
00199 " -help : show this help\n"
00200 "\n"
00201 " -new_char CHAR : replace TARGETSTRING with CHAR (repeated)\n"
00202 "\n"
00203 " This option is used to specify what TARGETSTRING is\n"
00204 " replaced with. In this case, replace it with repeated\n"
00205 " copies of the character CHAR.\n"
00206 "\n"
00207 " -new_string STRING : replace TARGETSTRING with STRING\n"
00208 "\n"
00209 " This option is used to specify what TARGETSTRING is\n"
00210 " replaced with. In this case, replace it with the string\n"
00211 " STRING. If STRING is not long enough, then CHAR from the\n"
00212 " -new_char option will be used to complete the overwrite\n"
00213 " (or the character 'x', by default).\n"
00214 "\n"
00215 " -unescape : parse TARGETSTRING for escaped characters\n"
00216 " (includes '\\t', '\\n', '\\r')\n"
00217 "\n"
00218 " If this option is given, strblast will parse TARGETSTRING\n"
00219 " replacing any escaped characters with their encoded ASCII\n"
00220 " values.\n"
00221 "\n"
00222 "Examples:\n"
00223 " strings I.001 | more # see if Subject Name is present\n"
00224 " strblast 'Subject Name' I.*\n"
00225 "\n"
00226 " strblast -unescape \"END OF LINE\\n\" infile.txt\n"
00227 " strblast -new_char \" \" \"BAD STRING\" infile.txt\n"
00228 " strblast -new_string \"GOOD\" \"BAD STRING\" infile.txt\n"
00229 "\n"
00230 "Notes and Warnings:\n"
00231 " * strblast will modify the input files irreversibly!\n"
00232 " You might want to test if they are still usable.\n"
00233 " * strblast reads files into memory to operate on them.\n"
00234 " If the file is too big to fit in memory, strblast\n"
00235 " will fail.\n"
00236 " * strblast will do internal wildcard expansion, so\n"
00237 " if there are too many input files for your shell to\n"
00238 " handle, you can do something like\n"
00239 " strblast 'Subject Name' 'I.*'\n"
00240 " and strblast will expand the 'I.*' wildcard for you.\n"
00241 ) ;
00242 exit(0) ;
00243 }