Annotation of OpenXM/src/util/oxreplace.c, Revision 1.6
1.6 ! takayama 1: /* $OpenXM: OpenXM/src/util/oxreplace.c,v 1.5 2005/07/03 08:27:38 ohara Exp $ */
1.1 takayama 2: /* cf. fb/src/misc/nan-tfb2.c */
3: #include <stdio.h>
1.5 ohara 4: #include <stdlib.h>
5: #include <string.h>
1.1 takayama 6: #include <time.h>
1.4 takayama 7: int ReplaceLine=0;
8:
1.1 takayama 9: char *readAsString(FILE *fp) {
10: static char *s = NULL;
11: static int size= 102400;
12: int p = 0;
13: char *s2;
14: int c;
15: if (s == NULL) {
16: s = (char *) malloc(size);
17: if (s == NULL) {
18: fprintf(stderr,"no more memory.\n"); exit(10);
19: }
20: }
21: s[0] = 0;
22: while ((c=fgetc(fp)) != EOF) {
23: s[p++] = c; s[p] = 0;
24: if (p >= size-2) {
25: s2 = s;
26: size = 2*size;
27: s = (char *) malloc(size);
28: if (s == NULL) {
29: fprintf(stderr,"no more memory.\n"); exit(10);
30: }
31: strcpy(s,s2);
32: free(s2);
33: }
34: }
35: return s;
36: }
37:
38: matches(char *word,char *s,int j) {
39: int i;
40: for (i=0; i<j; i++) {
41: if (word[i] != s[i]) return 0;
42: }
43: return 1;
44: }
1.3 takayama 45:
46: isHexc(int c) {
47: if ((c >= '0') && (c<='9')) return 1;
48: if ((c >= 'A') && (c<='F')) return 1;
49: if ((c >= 'a') && (c<='f')) return 1;
50: return 0;
51: }
52: char *hex2str(char hstr[]) {
53: int i,n,c,k;
54: char *s;
55: char ts[3];
56: /* printf("%s\n",hstr); */
57: n = strlen(hstr);
58: s = (char *) malloc(n/2+2);
59: if (s == NULL) {fprintf(stderr,"Memory exhausted.\n"); exit(10);}
60: k = 0; i = 0;
61: s[k] = 0;
62: while (i<n) {
63: while ((!isHexc(hstr[i])) && (i<n)) i++;
64: if (isHexc(hstr[i]) && (i<n)) ts[0] = hstr[i]; else ts[0] = 0;
65: i++;
66: while ((!isHexc(hstr[i])) && (i<n)) i++;
67: if (isHexc(hstr[i]) && (i<n)) ts[1] = hstr[i]; else ts[1] = 0;
68: i++; ts[2] = 0;
69: if (ts[0] != 0) {
70: sscanf(ts,"%xd",&c);
71: s[k] = c; k++; s[k] = 0;
72: }
73: }
74: if (strcmp(s,"") == 0) return NULL;
75: else return s;
76: }
77:
1.1 takayama 78: main(int argc, char *argv[]) {
79: int i;
80: char *old = NULL;
81: char *new = NULL;
82: if (argc < 2) {usage(); exit(0); }
83: for (i=1; i<argc; i++) {
84: if (strcmp(argv[i],"--old") == 0) {
85: old = argv[i+1]; i++;
86: } else if (strcmp(argv[i],"--new") == 0) {
87: new = argv[i+1]; i++;
1.3 takayama 88: } else if (strcmp(argv[i],"--oldx") == 0) {
89: old = hex2str(argv[i+1]); i++;
90: } else if (strcmp(argv[i],"--newx") == 0) {
91: new = hex2str(argv[i+1]); i++;
1.4 takayama 92: } else if (strcmp(argv[i],"--replaceLine") == 0) {
93: ReplaceLine = 1;
94: } else if (strcmp(argv[i],"--f") == 0) {
1.1 takayama 95: fprintf(stderr,"--f option (rule file) has not yet been implemented.\n");
96: exit(10);
97: }else {
98: if ((old != NULL) && (new != NULL)) {
99: replaceOneWord(argv[i],old,new);
100: }else{
101: fprintf(stderr,"--old and --new option or --f option are not given.\n");
102: usage();
103: exit(10);
104: }
105: }
106: }
1.2 takayama 107: exit(0);
1.1 takayama 108: }
109:
110: #define DEBUG 1
111:
112: replaceOneWord(char *fname,char *old, char *new) {
113: FILE *fp;
114: FILE *fpOrig;
115: char *s;
116: char *fnameBackup;
117: char *comm;
1.4 takayama 118: int i,j,mm;
1.6 ! takayama 119: int ns,nold;
1.1 takayama 120: #ifdef DEBUG
1.4 takayama 121: fprintf(stderr,"fname=%s, old=%s, new=%s, ReplaceLine=%d\n",fname,old,new,ReplaceLine);
1.1 takayama 122: #endif
123: fp = fopen(fname,"r");
124: if (fp == NULL) {
125: fprintf(stderr,"File %s is not found.\n",fname);
126: return -1;
127: }
128: fclose(fp);
129:
130: fnameBackup = (char *)malloc(strlen(fname)+200);
131: comm = (char *)malloc(strlen(fname)+300);
132: if (fnameBackup == NULL) {
133: fprintf(stderr,"No more memory.\n"); exit(10);
134: }
135: sprintf(fnameBackup,"%s.%ld.oxreplace-tmp.old",fname,(long) time(NULL));
136: sprintf(comm,"cp %s %s",fname,fnameBackup);
137: system(comm);
138:
139: fpOrig = fopen(fnameBackup,"r");
140: fp = fopen(fname,"w");
141: s = readAsString(fpOrig);
1.6 ! takayama 142: ns = strlen(s); nold=strlen(old);
1.4 takayama 143: if (ReplaceLine) {
1.6 ! takayama 144: for (i=0; i<ns; i++) {
1.4 takayama 145: /* Look for \n */
146: mm = 0;
1.6 ! takayama 147: for (j = i; j<ns; j++) {
! 148: if (matches(old,s+j,nold)) mm = 1;
1.4 takayama 149: if (s[j] == '\n') { break;}
150: }
151: if (mm) {
152: i = j;
153: fprintf(fp,"%s\n",new);
154: }else{
155: for ( ; i < j; i++) {
156: putc(s[i],fp);
157: }
158: putc('\n',fp);
159: }
1.1 takayama 160: }
1.4 takayama 161: }else{
1.6 ! takayama 162: for (i=0; i<ns; i++) {
! 163: if (!matches(old,s+i,nold)) {
1.4 takayama 164: fputc(s[i],fp);
165: }else{
166: fprintf(fp,"%s",new);
167: i += strlen(old)-1;
168: }
169: }
1.1 takayama 170: }
171: fclose(fp); fclose(fpOrig);
172: free(comm); free(fnameBackup);
173: return 0;
174: }
175:
176:
177: usage() {
1.4 takayama 178: fprintf(stderr,"oxreplace [--old oword --new nword --f rule_file_name --replaceLine] \n");
1.1 takayama 179: fprintf(stderr," [file1 file2 ... ] \n");
1.4 takayama 180: fprintf(stderr," Use --oldx or --newx to give a word in hexadecimal codes\n");
1.1 takayama 181: }
182:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>