Doxygen Source Code Documentation
strblast.c File Reference
#include "mrilib.h"
Go to the source code of this file.
Functions | |
void | help_n_exit (void) |
int | suck_file (char *fname, char **fbuf) |
int | main (int argc, char *argv[]) |
Function Documentation
|
Definition at line 190 of file strblast.c. Referenced by main().
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 } |
|
\** File : SUMA.c
Input paramters :
Definition at line 18 of file strblast.c. References argc, free, help_n_exit(), machdep(), MCW_file_expand(), MCW_warn_expand(), and suck_file().
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; /* for -unescape option 24 Mar 2005 [rickr] */ 00025 int use_newstr = 0; 00026 char newchar = 'x'; 00027 00028 int nfname=0 ; /* for globbing */ 00029 char **fname=NULL ; 00030 00031 /* help? */ 00032 00033 if( argc < 3 ) help_n_exit(); 00034 00035 /* Check for arguments. If we don't get an exact match, continue, */ 00036 /* allowing the user to start a target string with '-'. */ 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 /* load the target */ 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 ){ /* let's leave argv alone, so dup the string */ 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'; /* and terminate */ 00092 00093 /* now for the ol' switcheroo... */ 00094 targ = tnew; ntarg = jj; 00095 } 00096 00097 /* if the replacement string is too long, truncate it */ 00098 if( ntarg < njstr ){ 00099 if( use_newstr ){ jstr[ntarg] = '\0' ; njstr = ntarg; } 00100 else { jstr[0] = newchar ; njstr = 1; } 00101 } 00102 00103 /* get input filenames */ 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 /* loop over files */ 00114 00115 for( ff=0 ; ff < nfname ; ff++ ){ 00116 00117 /* read it all into memory */ 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 /* scan for start character */ 00128 00129 for( nfind=ii=0 ; ii < nbuf-ntarg ; ii++ ){ 00130 00131 if( fbuf[ii] == targ[0] ){ /* if find it, check rest of string */ 00132 00133 for( jj=1; jj < ntarg && fbuf[ii+jj]==targ[jj] ; jj++ ) ; /* nada */ 00134 00135 if( jj == ntarg ){ /* found it */ 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 } /* end of loop over files */ 00162 00163 exit(0) ; 00164 } |
|
Definition at line 168 of file strblast.c. References close(), fd, free, malloc, read(), and THD_filesize().
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 } |