Pastebin

Paste #2419: readfile.lpr

< previous paste - next paste>

Pasted by tdn

Download View as text

program readfile;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp,
  windows
  { you can add units after this };

type

  { readfile }

  rfile = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ rfile }

procedure rfile.DoRun;
var
  ErrorMsg, FileName: String;
  Fin : File;
  NumRead, PrevNumRead : Word;
  Buf : Array of Byte;
  BufSize, NumReadOps, StartTime, EndTime, ExecTime, Size:  Int64;
  Speed : Double;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h','help,bufsize:,in:');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h','help') then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

  if HasOption('bufsize:') then begin
    WriteLn('Hello');
    BufSize := StrToInt(GetOptionValue('bufsize'));
    WriteLn('--------bufsize=', BufSize);
    Terminate;
    Exit;
  end;

  if HasOption('in:') then begin
    FileName := GetOptionValue('in');
    WriteLn('--------FileName=', FileName);
    Terminate;
    Exit;
  end;

  If Paramcount < 1 then WriteHelp;
  // Initialize variables
  NumRead  := 0;
  //FileName := ParamStr(1);
  BufSize  := 2048;
  SetLength(Buf, BufSize);
  Bufsize  := Length(Buf);
  WriteLn();
  WriteLn(BufSize);
  NumReadOps:=0; // Number of read operations

  If FileExists (FileName) then
    begin
      System.Assign (Fin, FileName);
      Reset (Fin,1);
      WriteLn('Reading "',FileName,'"');
      WriteLn('FileSize: ',FileSize(Fin), ' bytes');
      WriteLn('Bufsize:', bufsize);

      StartTime := Windows.GetTickCount();
      Repeat
        PrevNumRead := NumRead;
        BlockRead (Fin,buf[0],BufSize,NumRead);
        inc(NumReadOps);
        //If NumReadOps mod 100000 = 0 then
        //  writeln(Total, ' : ', NumReadOps * BufSize);
      Until (NumRead=0);
      close(Fin);
      EndTime := Windows.GetTickCount();
      // Add 1 in order to avoid divbyzero on very short exectime
      ExecTime:= EndTime - StartTime + 1;

      WriteLn();
      //WriteLn('Numread=',NumRead);
      //WriteLn('PrevNumRead=',PrevNumRead);
      //writeln(NumReadOps, ' : ', NumReadOps * BufSize);

      Size  := (NumReadOps-2) * BufSize + PrevNumRead;
      If Size < 0 then Size := 0;
      Speed := Size / ExecTime;
      WriteLn ('Read ',Size,
              ' bytes (', FloatToStrF(Size/1024/1024, ffFixed,1,1),
              ' mb) from file ',FileName,
              ' in ', ExecTime, ' ms :');
      Speed := Size / (ExecTime/1000);
      WriteLn(FloatToStrF(speed,ffFixed,1,2),                  ' b/s');
      WriteLn(FloatToStrF(speed/1024,ffFixed,1,2),            ' kb/s');
      WriteLn(FloatToStrF(speed/1024/1024,ffFixed,1,2),       ' mb/s');
      WriteLn(FloatToStrF(speed/1024/1024/1024,ffFixed,1,2),  ' gb/s');
    end
  else
  begin
    Writeln ('File "'+FileName+'" NOT found');
  end;
  //Write('Enter to quit');
  //ReadLn;

  // stop program loop
  Terminate;
end;

constructor rfile.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor rfile.Destroy;
begin
  inherited Destroy;
end;

procedure rfile.WriteHelp;
begin
  { add your help code here }
//  writeln('Usage: ',ExeName,' -h');
  WriteLn('Usage: '+ExeName+' --in=<filename> [--bufsize=N]');
  Halt;
end;


var
  Application: rfile;

{$R *.res}


begin
  Application:=rfile.Create(nil);
  Application.Title:='rfile';
  Application.Run;
  Application.Free;
end.

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.