[BACK]Return to oxreplace.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / util

Annotation of OpenXM/src/util/oxreplace.c, Revision 1.4

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>