home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DPSX / TOOL-PAS.ZIP / BMREP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-05-21  |  3.6 KB  |  176 lines

  1.  
  2. (*
  3.  * Bmrep - multiple replacement of exact length strings in multiple binary files
  4.  *
  5.  * from gmrep s.h.smith, 8-apr-87 (21-may-91)
  6.  *
  7.  *)
  8.  
  9. uses mdosio,tools;
  10.  
  11. const
  12.    version = '2.0 (5-21-91)';
  13.  
  14. type
  15.    replacement_rec = record
  16.       old:     ^string;
  17.       rep:     ^string;
  18.    end;
  19.  
  20. const
  21.    max_replace = 100;
  22.  
  23. var
  24.    table:  array[1..max_replace] of replacement_rec;
  25.    reps:   integer;
  26.  
  27. const
  28.    nblocks = 150;
  29.  
  30.  
  31. procedure error(why: string);
  32. begin
  33.    writeln;
  34.    writeln('FATAL ERROR: ',why);
  35.    halt(1);
  36. end;
  37.  
  38. procedure load_replacement_list;
  39. var
  40.    p:       integer;
  41.    old,rep: string;
  42.    delim:   char;
  43.    line:    string;
  44.    infd:    text;
  45.  
  46. begin
  47.    if not dos_exists(paramstr(1)) then
  48.       error('missing replacement list file: '+paramstr(1));
  49.  
  50.    assign(infd,paramstr(1));
  51.    reset(infd);
  52.    reps := 0;
  53.  
  54.    while not eof(infd) do
  55.    begin
  56.       readln(infd,line);
  57.       line := line + ';';
  58.  
  59.       delim := line[1];
  60.       if delim <> ';' then
  61.       begin
  62.          inc(reps);
  63.          delete(line,1,1);
  64.  
  65.          p := pos(delim,line);
  66.          if p=0 then error('missing delimiter: '+line);
  67.          old := copy(line,1,p-1);
  68.          getmem(table[reps].old,length(old)+1);
  69.          table[reps].old^ := old;
  70.  
  71.          line := copy(line,p+1,255);
  72.          p := pos(delim,line);
  73.          if p=0 then error('missing delimiter: '+line);
  74.          rep := copy(line,1,p-1);
  75.          getmem(table[reps].rep,length(rep)+1);
  76.          table[reps].rep^ := rep;
  77.  
  78. {         writeln(' replace [',old,'] with [',rep,']'); }
  79.  
  80.          if length(old) <> length(rep) then
  81.             error('Old and new lengths must be the same ('+old+'/'+rep+')');
  82.       end;
  83.    end;
  84.  
  85.    close(infd);
  86.    writeln(reps,' replacement table entries');
  87.    writeln;
  88. end;
  89.  
  90.  
  91. procedure perform_replacement(name: string);
  92. type
  93.    buffer = array[1..maxint] of char;
  94.    bufptr = ^buffer;
  95. var
  96.    buf:     bufptr;
  97.    count:   word;
  98.    infd:    dos_handle;
  99.    outfd:   dos_handle;
  100.    i,j,p:   integer;
  101.    hits:    integer;
  102.    bak:     string;
  103.  
  104. begin
  105.    write(name:15);
  106.  
  107.    if not dos_exists(name) then
  108.       error('missing input file: ' + name);
  109.  
  110.    backup_file(name);
  111.  
  112.    bak := remove_ext(name)+'.bak';
  113.    infd := dos_open(bak,open_read);
  114.    outfd := dos_create(name);
  115.  
  116.    new(buf);
  117.    hits := 0;
  118.    count := dos_read(infd,buf^,sizeof(buf^));
  119.  
  120.    if count = sizeof(buf^) then
  121.       error('File too large!');
  122.  
  123.    for i := 1 to reps do
  124.    with table[i] do
  125.    begin
  126.       for j := 1 to count-length(old^) do
  127.       begin
  128.          p := 1;
  129.          repeat
  130.             inc(p);
  131.          until (p = length(old^)) or (buf^[j+p-1] <> old^[p]);
  132.  
  133.          if buf^[j+p-1] = old^[p] then
  134.          begin
  135.             inc(hits);
  136.             for p := 1 to length(old^) do
  137.                buf^[j+p-1] := rep^[p];
  138.          end;
  139.       end;
  140.    end;
  141.  
  142.    dos_write(outfd,buf^,count);
  143.  
  144.    dos_close(infd);
  145.    dos_close(outfd);
  146.    writeln('   ',hits,' replacements');
  147.  
  148.    dispose(buf);
  149. end;
  150.  
  151.  
  152. var
  153.    i,j: integer;
  154.  
  155. begin
  156.    if paramcount < 2 then
  157.    begin
  158.       writeln('BMREP ',version,' (C) 1991 Samuel H. Smith');
  159.       writeln('Usage:  BMREP replacement_list file_list');
  160.       writeln;
  161.       writeln('Replacement list file may contain any number lines with the format:');
  162.       writeln('<delimiter>old string<delimiter>new string<delimiter>');
  163.       halt;
  164.    end;
  165.  
  166.    load_replacement_list;
  167.  
  168.    for i := 2 to paramcount do
  169.    begin
  170.       getfiles(paramstr(i),filetable,filecount);
  171.       for j := 1 to filecount do
  172.          perform_replacement(filetable[j]^);
  173.    end;
  174. end.
  175.  
  176.