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

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

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

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