home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / VPatch / Source / GenPat / GenPat2.dpr next >
Text File  |  2003-12-27  |  7KB  |  227 lines

  1. program GenPat2;
  2.  
  3. {
  4.   VPatch 2 - Patch Generator
  5.   ===============================
  6.  
  7.   (c) 2001-2003 Van de Sande Productions
  8.  
  9.   This is the main program unit for the commandline version. It implements
  10.   commandline options (like /b=) and displays help if no options are given.
  11.  
  12.   What's new
  13.   ----------
  14.   2.1   20031219    Koen            Added error checking, handling, shouldn't
  15.                                     crash when invalid arguments, returns
  16.                                     exit codes now.
  17.   2.0   20030811    Koen            Initial documentation
  18. }
  19.  
  20. {$APPTYPE CONSOLE}
  21. uses
  22.   PatchGenerator in 'PatchGenerator.pas',
  23.   VDSP_CRC in 'VDSP_CRC.pas',
  24.   Sysutils,
  25.   TreeCode in 'TreeCode.pas';
  26.  
  27. type
  28.   TEventHandler = class
  29.     procedure PrintDebug(S: String);
  30.   end;
  31.  
  32.     procedure TEventhandler.PrintDebug(S: String);
  33.     begin
  34.       WriteLn(S);
  35.     end;
  36.  
  37. {$DEFINE READCONFIG}          //try to read genpat.ini?
  38. {.$DEFINE AUTOWAIT}           //have /wait command line switch on by default?
  39.                               //useful when debugging
  40.  
  41. var
  42.   Config: TextFile;
  43.   T1,T2: TDateTime;
  44.   d, i: Integer;
  45.   S,Key: String;
  46.   SourceFile, TargetFile, PatchFile: String;
  47.   ShowDebug, ShowHelp: Boolean;
  48.   PG: TPatchGenerator;
  49.   EV: TEventHandler;
  50.  
  51. begin
  52.   EV:=TEventHandler.Create;
  53.   PG:=TPatchGenerator.Create;
  54.   PG.StartBlockSize:=64;
  55.  
  56.   WriteLn('GenPat v2.1');
  57.   WriteLn('===========');
  58.   WriteLn;
  59.   WriteLn('(c) 2001-2003 Van de Sande Productions');
  60.   WriteLn('Website: http://www.tibed.net/vpatch');
  61.   WriteLn('E-mail:  koen@tibed.net');
  62.   WriteLn;
  63.   ShowDebug:=FindCmdLineSwitch('debug',['/'],True);
  64.   if ShowDebug then
  65.     DebugEvent:=EV.PrintDebug;
  66.  
  67. {$IFDEF READCONFIG}
  68.   if FileExists('genpat.ini') then begin
  69.     AssignFile(Config,'genpat.ini');
  70.     Reset(Config);
  71.     while not eof(Config) do begin
  72.       ReadLn(Config,S);
  73.       d:=Pos('=',S);
  74.       if not (d=0) then begin
  75.         Key:=LowerCase(Copy(S,1,d-1));
  76.         S:=Copy(S,d+1,Length(S));
  77.         if CompareStr(Key,'startblocksize')=0 then PG.StartBlockSize:=StrToInt(S);
  78.       end;
  79.     end;
  80.     CloseFile(Config);
  81.   end;
  82. {$ENDIF}
  83.   i:=0;
  84.   for d:=1 to ParamCount do begin
  85.     if CompareStr(LowerCase(Copy(ParamStr(d),1,3)),'/b=')=0 then begin
  86.       PG.StartBlockSize:=StrToInt(Copy(ParamStr(d),4,10));
  87.     end else begin
  88.       // not a parameter?
  89.       if not (ParamStr(d)[1] = '/') then begin
  90.         if i = 2 then begin
  91.           PatchFile:=ParamStr(d);
  92.           Inc(i);
  93.         end;
  94.         if i = 1 then begin
  95.           TargetFile:=ParamStr(d);
  96.           Inc(i);
  97.         end;
  98.         if i = 0 then begin
  99.           SourceFile:=ParamStr(d);
  100.           Inc(i);
  101.         end;
  102.       end;
  103.     end;
  104.   end;
  105.  
  106.   ShowHelp:=False;
  107.   if(CompareStr(PatchFile,'')=0) then ShowHelp:=True;
  108.   if SourceFile = '' then ShowHelp:=True;
  109.   if TargetFile = '' then ShowHelp:=True;
  110.  
  111.   if ShowHelp then begin
  112.     WriteLn('This program will take (sourcefile) as input and create a (patchfile).');
  113.     WriteLn('With this patchfile, you can convert a (sourcefile) into (targetfile).');
  114.     WriteLn;
  115.     WriteLn('Command line info:');
  116.     WriteLn('  GENPAT (sourcefile) (targetfile) (patchfile)');
  117.     WriteLn;
  118.     WriteLn('Command line options (you do not need them):');
  119.     WriteLn('/B=(BlockSize)             Set blocksize (def=64), multiple of 2');
  120.     WriteLn('/NOEQUALERROR              Exit code becomes 0 instead of 10 when');
  121.     WriteLn('                           two files with equal CRC are encountered');
  122.     WriteLn('                           (patch file will remain unchanged)');
  123.     WriteLn('/DEBUG                     Show runtime debug information');
  124.     WriteLn;
  125.     WriteLn('Note: filenames should never start with / character!');
  126.     WriteLn;
  127.     WriteLn('Possible exit codes:');
  128.     WriteLn('  0  Success');
  129.     WriteLn('  1  Arguments missing');
  130.     WriteLn('  2  Source file not found');
  131.     WriteLn('  3  Target file not found');
  132.     WriteLn('  4  Unknown error while reading existing patch file');
  133.     WriteLn('  5  Unknown error while generating patch');
  134.     WriteLn('  6  Unknown error while writing patch file to disk');
  135.     WriteLn('  10 CRC of source and target file are equal (impossible with /NOEQUALERROR)');
  136.     WriteLn('  11 Not enough memory for source file');
  137.     WriteLn('  12 Not enough memory for target file');
  138.     PG.Free;
  139.     ExitCode:=1;
  140.     Exit;
  141.   end;
  142.  
  143.   // stop if file error, result shown above
  144.   if not FileExists(SourceFile) then begin
  145.     WriteLn('Error: Source file not found');
  146.     PG.Free;
  147.     ExitCode:=2;
  148.     Exit;
  149.   end;
  150.   if not FileExists(TargetFile) then begin
  151.     WriteLn('Error: Target file not found');
  152.     PG.Free;
  153.     ExitCode:=3;
  154.     Exit;
  155.   end;
  156.  
  157.   if FileExists(PatchFile) then begin
  158.     WriteLn('Using existing file to include patches in: '+PatchFile);
  159.     try
  160.       PG.LoadFromFile(PatchFile);
  161.     except
  162.       on E: Exception do begin
  163.         WriteLn('Error: Reading existing patch file failed');
  164.         WriteLn('Error message: ', E.ClassName, ': ', E.Message);
  165.         PG.Free;
  166.         ExitCode:=4;
  167.         Exit;
  168.       end;
  169.     end;
  170.   end;
  171.  
  172.   WriteLn('Source (original) file: ', SourceFile);
  173.   WriteLn('Target (newer) file:    ', TargetFile);
  174.  
  175.   T1:=Now;
  176.  
  177.   // create patch file, with error handling
  178.   try
  179.     i:=PG.CreatePatch(SourceFile,TargetFile);
  180.   except
  181.     on E: Exception do begin
  182.       WriteLn('Error: Generating patch failed');
  183.       WriteLn('Error message: ', E.ClassName, ': ', E.Message);
  184.       PG.Free;
  185.       ExitCode:=5;
  186.       Exit;
  187.     end;
  188.   end;
  189.  
  190.   if(i < 0) then begin
  191.     if(i = -1) then begin
  192.       if not FindCmdLineSwitch('noequalerror',['/'],True) then
  193.         WriteLn('Error: CRC of source and target file are equal');
  194.     end;
  195.     if(i = -2) then WriteLn('Error: Not enough memory for source file');
  196.     if(i = -3) then WriteLn('Error: Not enough memory for target file');
  197.     ExitCode:=9 - i;
  198.  
  199.     if(i = -1) and (FindCmdLineSwitch('noequalerror',['/'],True)) then begin
  200.       WriteLn('Equal CRCs ignored (no patch will be written and exit code is 0)');
  201.       ExitCode:=0;
  202.     end;
  203.   end else begin
  204.     WriteLn('Patch body size: '+IntToStr(i));
  205.     try
  206.       PG.WriteToFile(PatchFile);
  207.     except
  208.       on E: Exception do begin
  209.         WriteLn('Error: Writing patch to file ' + PatchFile + ' failed');
  210.         WriteLn('Error message: ', E.ClassName, ': ', E.Message);
  211.         PG.Free;
  212.         ExitCode:=6;
  213.         Exit;
  214.       end;
  215.     end;
  216.  
  217.     T2:=Now;
  218.     Write('Time taken for generation: ');
  219.     WriteLn(FloatToStr((T2-T1)*24*60*60),'s');
  220.     WriteLn;
  221.  
  222.     ExitCode:=0;
  223.   end;
  224.  
  225.   PG.Free;
  226. end.
  227.