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

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

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

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