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

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

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

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