version 1.1, 2003/03/26 04:45:37 |
version 1.5, 2005/07/03 08:27:38 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/util/oxreplace.c,v 1.4 2005/04/14 07:11:58 takayama Exp $ */ |
/* cf. fb/src/misc/nan-tfb2.c */ |
/* cf. fb/src/misc/nan-tfb2.c */ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
#include <time.h> |
#include <time.h> |
|
int ReplaceLine=0; |
|
|
char *readAsString(FILE *fp) { |
char *readAsString(FILE *fp) { |
static char *s = NULL; |
static char *s = NULL; |
static int size= 102400; |
static int size= 102400; |
Line 38 matches(char *word,char *s,int j) { |
|
Line 42 matches(char *word,char *s,int j) { |
|
} |
} |
return 1; |
return 1; |
} |
} |
|
|
|
isHexc(int c) { |
|
if ((c >= '0') && (c<='9')) return 1; |
|
if ((c >= 'A') && (c<='F')) return 1; |
|
if ((c >= 'a') && (c<='f')) return 1; |
|
return 0; |
|
} |
|
char *hex2str(char hstr[]) { |
|
int i,n,c,k; |
|
char *s; |
|
char ts[3]; |
|
/* printf("%s\n",hstr); */ |
|
n = strlen(hstr); |
|
s = (char *) malloc(n/2+2); |
|
if (s == NULL) {fprintf(stderr,"Memory exhausted.\n"); exit(10);} |
|
k = 0; i = 0; |
|
s[k] = 0; |
|
while (i<n) { |
|
while ((!isHexc(hstr[i])) && (i<n)) i++; |
|
if (isHexc(hstr[i]) && (i<n)) ts[0] = hstr[i]; else ts[0] = 0; |
|
i++; |
|
while ((!isHexc(hstr[i])) && (i<n)) i++; |
|
if (isHexc(hstr[i]) && (i<n)) ts[1] = hstr[i]; else ts[1] = 0; |
|
i++; ts[2] = 0; |
|
if (ts[0] != 0) { |
|
sscanf(ts,"%xd",&c); |
|
s[k] = c; k++; s[k] = 0; |
|
} |
|
} |
|
if (strcmp(s,"") == 0) return NULL; |
|
else return s; |
|
} |
|
|
main(int argc, char *argv[]) { |
main(int argc, char *argv[]) { |
int i; |
int i; |
char *old = NULL; |
char *old = NULL; |
Line 48 main(int argc, char *argv[]) { |
|
Line 85 main(int argc, char *argv[]) { |
|
old = argv[i+1]; i++; |
old = argv[i+1]; i++; |
} else if (strcmp(argv[i],"--new") == 0) { |
} else if (strcmp(argv[i],"--new") == 0) { |
new = argv[i+1]; i++; |
new = argv[i+1]; i++; |
} else if (strcmp(argv[i],"--f") == 0) { |
} else if (strcmp(argv[i],"--oldx") == 0) { |
|
old = hex2str(argv[i+1]); i++; |
|
} else if (strcmp(argv[i],"--newx") == 0) { |
|
new = hex2str(argv[i+1]); i++; |
|
} else if (strcmp(argv[i],"--replaceLine") == 0) { |
|
ReplaceLine = 1; |
|
} else if (strcmp(argv[i],"--f") == 0) { |
fprintf(stderr,"--f option (rule file) has not yet been implemented.\n"); |
fprintf(stderr,"--f option (rule file) has not yet been implemented.\n"); |
exit(10); |
exit(10); |
}else { |
}else { |
Line 61 main(int argc, char *argv[]) { |
|
Line 104 main(int argc, char *argv[]) { |
|
} |
} |
} |
} |
} |
} |
|
exit(0); |
} |
} |
|
|
#define DEBUG 1 |
#define DEBUG 1 |
Line 71 replaceOneWord(char *fname,char *old, char *new) { |
|
Line 115 replaceOneWord(char *fname,char *old, char *new) { |
|
char *s; |
char *s; |
char *fnameBackup; |
char *fnameBackup; |
char *comm; |
char *comm; |
int i; |
int i,j,mm; |
#ifdef DEBUG |
#ifdef DEBUG |
fprintf(stderr,"fname=%s, old=%s, new=%s\n",fname,old,new); |
fprintf(stderr,"fname=%s, old=%s, new=%s, ReplaceLine=%d\n",fname,old,new,ReplaceLine); |
#endif |
#endif |
fp = fopen(fname,"r"); |
fp = fopen(fname,"r"); |
if (fp == NULL) { |
if (fp == NULL) { |
Line 94 replaceOneWord(char *fname,char *old, char *new) { |
|
Line 138 replaceOneWord(char *fname,char *old, char *new) { |
|
fpOrig = fopen(fnameBackup,"r"); |
fpOrig = fopen(fnameBackup,"r"); |
fp = fopen(fname,"w"); |
fp = fopen(fname,"w"); |
s = readAsString(fpOrig); |
s = readAsString(fpOrig); |
for (i=0; i<strlen(s); i++) { |
if (ReplaceLine) { |
if (!matches(old,s+i,strlen(old))) { |
for (i=0; i<strlen(s); i++) { |
fputc(s[i],fp); |
/* Look for \n */ |
}else{ |
mm = 0; |
fprintf(fp,"%s",new); |
for (j = i; j<strlen(s); j++) { |
i += strlen(old)-1; |
if (matches(old,s+j,strlen(old))) mm = 1; |
|
if (s[j] == '\n') { break;} |
|
} |
|
if (mm) { |
|
i = j; |
|
fprintf(fp,"%s\n",new); |
|
}else{ |
|
for ( ; i < j; i++) { |
|
putc(s[i],fp); |
|
} |
|
putc('\n',fp); |
|
} |
} |
} |
|
}else{ |
|
for (i=0; i<strlen(s); i++) { |
|
if (!matches(old,s+i,strlen(old))) { |
|
fputc(s[i],fp); |
|
}else{ |
|
fprintf(fp,"%s",new); |
|
i += strlen(old)-1; |
|
} |
|
} |
} |
} |
fclose(fp); fclose(fpOrig); |
fclose(fp); fclose(fpOrig); |
free(comm); free(fnameBackup); |
free(comm); free(fnameBackup); |
Line 109 replaceOneWord(char *fname,char *old, char *new) { |
|
Line 173 replaceOneWord(char *fname,char *old, char *new) { |
|
|
|
|
|
usage() { |
usage() { |
fprintf(stderr,"oxreplace [--old oword --new nword --f rule_file_name] \n"); |
fprintf(stderr,"oxreplace [--old oword --new nword --f rule_file_name --replaceLine] \n"); |
fprintf(stderr," [file1 file2 ... ] \n"); |
fprintf(stderr," [file1 file2 ... ] \n"); |
|
fprintf(stderr," Use --oldx or --newx to give a word in hexadecimal codes\n"); |
} |
} |
|
|