Annotation of OpenXM/src/util/oxreplace.c, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$ */
! 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: }
! 41: main(int argc, char *argv[]) {
! 42: int i;
! 43: char *old = NULL;
! 44: char *new = NULL;
! 45: if (argc < 2) {usage(); exit(0); }
! 46: for (i=1; i<argc; i++) {
! 47: if (strcmp(argv[i],"--old") == 0) {
! 48: old = argv[i+1]; i++;
! 49: } else if (strcmp(argv[i],"--new") == 0) {
! 50: new = argv[i+1]; i++;
! 51: } else if (strcmp(argv[i],"--f") == 0) {
! 52: fprintf(stderr,"--f option (rule file) has not yet been implemented.\n");
! 53: exit(10);
! 54: }else {
! 55: if ((old != NULL) && (new != NULL)) {
! 56: replaceOneWord(argv[i],old,new);
! 57: }else{
! 58: fprintf(stderr,"--old and --new option or --f option are not given.\n");
! 59: usage();
! 60: exit(10);
! 61: }
! 62: }
! 63: }
! 64: }
! 65:
! 66: #define DEBUG 1
! 67:
! 68: replaceOneWord(char *fname,char *old, char *new) {
! 69: FILE *fp;
! 70: FILE *fpOrig;
! 71: char *s;
! 72: char *fnameBackup;
! 73: char *comm;
! 74: int i;
! 75: #ifdef DEBUG
! 76: fprintf(stderr,"fname=%s, old=%s, new=%s\n",fname,old,new);
! 77: #endif
! 78: fp = fopen(fname,"r");
! 79: if (fp == NULL) {
! 80: fprintf(stderr,"File %s is not found.\n",fname);
! 81: return -1;
! 82: }
! 83: fclose(fp);
! 84:
! 85: fnameBackup = (char *)malloc(strlen(fname)+200);
! 86: comm = (char *)malloc(strlen(fname)+300);
! 87: if (fnameBackup == NULL) {
! 88: fprintf(stderr,"No more memory.\n"); exit(10);
! 89: }
! 90: sprintf(fnameBackup,"%s.%ld.oxreplace-tmp.old",fname,(long) time(NULL));
! 91: sprintf(comm,"cp %s %s",fname,fnameBackup);
! 92: system(comm);
! 93:
! 94: fpOrig = fopen(fnameBackup,"r");
! 95: fp = fopen(fname,"w");
! 96: s = readAsString(fpOrig);
! 97: for (i=0; i<strlen(s); i++) {
! 98: if (!matches(old,s+i,strlen(old))) {
! 99: fputc(s[i],fp);
! 100: }else{
! 101: fprintf(fp,"%s",new);
! 102: i += strlen(old)-1;
! 103: }
! 104: }
! 105: fclose(fp); fclose(fpOrig);
! 106: free(comm); free(fnameBackup);
! 107: return 0;
! 108: }
! 109:
! 110:
! 111: usage() {
! 112: fprintf(stderr,"oxreplace [--old oword --new nword --f rule_file_name] \n");
! 113: fprintf(stderr," [file1 file2 ... ] \n");
! 114: }
! 115:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>