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

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

1.5     ! ohara       1: /* $OpenXM: OpenXM/src/util/oxreplace.c,v 1.4 2005/04/14 07:11:58 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;
                      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.1       takayama  119: #ifdef DEBUG
1.4       takayama  120:   fprintf(stderr,"fname=%s, old=%s, new=%s, ReplaceLine=%d\n",fname,old,new,ReplaceLine);
1.1       takayama  121: #endif
                    122:   fp = fopen(fname,"r");
                    123:   if (fp == NULL) {
                    124:        fprintf(stderr,"File %s is not found.\n",fname);
                    125:        return -1;
                    126:   }
                    127:   fclose(fp);
                    128:
                    129:   fnameBackup = (char *)malloc(strlen(fname)+200);
                    130:   comm = (char *)malloc(strlen(fname)+300);
                    131:   if (fnameBackup == NULL) {
                    132:        fprintf(stderr,"No more memory.\n"); exit(10);
                    133:   }
                    134:   sprintf(fnameBackup,"%s.%ld.oxreplace-tmp.old",fname,(long) time(NULL));
                    135:   sprintf(comm,"cp %s %s",fname,fnameBackup);
                    136:   system(comm);
                    137:
                    138:   fpOrig = fopen(fnameBackup,"r");
                    139:   fp = fopen(fname,"w");
                    140:   s = readAsString(fpOrig);
1.4       takayama  141:   if (ReplaceLine) {
                    142:     for (i=0; i<strlen(s); i++) {
                    143:       /* Look for \n */
                    144:       mm = 0;
                    145:          for (j = i; j<strlen(s); j++) {
                    146:         if (matches(old,s+j,strlen(old))) mm = 1;
                    147:                if (s[j] == '\n') { break;}
                    148:       }
                    149:       if (mm) {
                    150:                i = j;
                    151:                fprintf(fp,"%s\n",new);
                    152:          }else{
                    153:                for ( ; i < j; i++) {
                    154:                  putc(s[i],fp);
                    155:                }
                    156:                putc('\n',fp);
                    157:          }
1.1       takayama  158:        }
1.4       takayama  159:   }else{
                    160:     for (i=0; i<strlen(s); i++) {
                    161:       if (!matches(old,s+i,strlen(old))) {
                    162:         fputc(s[i],fp);
                    163:       }else{
                    164:         fprintf(fp,"%s",new);
                    165:         i += strlen(old)-1;
                    166:       }
                    167:     }
1.1       takayama  168:   }
                    169:   fclose(fp); fclose(fpOrig);
                    170:   free(comm); free(fnameBackup);
                    171:   return 0;
                    172: }
                    173:
                    174:
                    175: usage() {
1.4       takayama  176:   fprintf(stderr,"oxreplace [--old oword --new nword --f rule_file_name --replaceLine] \n");
1.1       takayama  177:   fprintf(stderr,"          [file1 file2 ... ] \n");
1.4       takayama  178:   fprintf(stderr,"    Use --oldx or --newx to give a word in hexadecimal codes\n");
1.1       takayama  179: }
                    180:

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