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