home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / TREPLACE.CMM < prev    next >
Text File  |  1995-10-02  |  3KB  |  98 lines

  1. // TReplace.cmm - Text replace.  Replace text in a file.
  2. // ver.1
  3.  
  4.  
  5. Instructions()
  6. {
  7.    puts("TReplace.cmm: Text replace. Replace text in a file.\a")
  8.    puts(``)
  9.    puts(`USAGE: CEnvi TReplace <filespec> <oldtext> <newtext>`)
  10.    puts(``)
  11.    puts(`WHERE: filespec - File specification for any text-mode file`)
  12.    puts(`       oldtext  - Any text to find in lines of filespec`)
  13.    puts(`       newtext  - New text to replace found old text`)
  14.    puts(``)
  15.    puts(`NOTE: This search is case-sensitive and slow, with very few options,`)
  16.    puts(`      But the Cmm code is very simple.`)
  17.    puts(``)
  18.    puts(`EXAMPLE: CEnvi TReplace c:\autoexec.bat F:\UTL\CENVID F:\UTL\CENVIW`)
  19.    puts(``)
  20.    exit(EXIT_FAILURE);
  21. }
  22.  
  23. #define TEMP_FILE_NAME  "TREPLACE.TMP"
  24.  
  25. main(argc,argv)
  26. {
  27.    if ( 4 != argc )
  28.       Instructions();
  29.  
  30.    // Get input parameters
  31.    FileSpec = argv[1];
  32.    OldText = argv[2];
  33.    NewText = argv[3];
  34.  
  35.    // Open old source file
  36.    SrcFP = fopen(FileSpec,"rt");
  37.    if ( !SrcFP ) {
  38.       printf("Unable to open source file \"%s\" for reading.\n\a",FileSpec);
  39.       exit(EXIT_FAILURE);
  40.    }
  41.  
  42.    // open temporary file in same directory as source file
  43.    FileNameParts = SplitFileName(FileSpec);
  44.    sprintf(TemporaryFileSpec,"%s%s",FileNameParts.dir,TEMP_FILE_NAME);
  45.    DstFP = fopen(TemporaryFileSpec,"wt");
  46.    if ( !DstFP ) {
  47.       printf("Unable to open temporary file \"%s\" for reading.\n\a",TemporaryFileSpec);
  48.       fclose(SrcFP);
  49.       exit(EXIT_FAILURE);
  50.    }
  51.  
  52.    // replace all text in source
  53.    TextReplaced = WriteWithReplace(SrcFP,DstFP,OldText,NewText);
  54.  
  55.    // not reading or writing any more, so close files
  56.    SrcError = ferror(SrcFP) & fclose(SrcFP);
  57.    DstError = ferror(DstFP) & fclose(DstFP);
  58.  
  59.    // if error in either file then say so
  60.    if ( SrcError ) {
  61.       printf("Error reading source file \"%s\".\a\n",FileSpec);
  62.    } else if ( TextReplaced ) {
  63.       // text was replaced so remove old file and replace with new one
  64.       if ( DstError )
  65.          printf("Error writing temporary file \"%s\".\a\n",TemporaryFileSpec);
  66.       else if ( remove(FileSpec) )
  67.          printf("Unable to remove old file \"%s\" for replacing.\n\a",FileSpec);
  68.       else if ( rename(TemporaryFileSpec,FileSpec) )
  69.          printf("Unable to replace old file with new file. Old file \"%s\"\n"
  70.                 "may be lost!\a\n",FileSpec);
  71.       else
  72.          printf("Replace %d \"%s\" with \"%s\".\n",TextReplaced,OldText,NewText);
  73.    } else {
  74.       puts("No text replaced.");
  75.    }
  76.  
  77.    // whatever else may have happened, delete temporary file if there
  78.    remove(TemporaryFileSpec);
  79. }
  80.  
  81. WriteWithReplace(src,dst,find,replace)
  82. {
  83.    lCount = 0;
  84.    FindLen = strlen(find);
  85.    ReplaceLen = strlen(replace);
  86.    while( lFound = lLine = fgets(src) ) {
  87.       while ( lFound = strstr(lFound,find) ) {
  88.          lCount++;
  89.          strcpy(lFound+ReplaceLen,lFound+FindLen);
  90.          memcpy(lFound,replace,ReplaceLen);
  91.          lFound += ReplaceLen;
  92.       }
  93.       fputs(lLine,dst);
  94.    }
  95.    return lCount;
  96. }
  97.  
  98.