Delphi & Pascal (česká wiki)
Přejít na: navigace, hledání
{ DEALER.PAS                Copyright (c) TrSek alias Zdeno Sekerak }
{ Zobrazenie/spracovanie odmien pre dealerov.                       }
{                                                                   }
{ Datum:19.06.1995                            http://www.trsek.com  }
 
function ddealer(nazo,subor:string;var meno,priezvisko:string;vyber:boolean;hlada,povod:integer):integer;
var err:integer;
   i,ir,x,poc:integer;
    ch:char;
    index:array[1..vcisla] of integer;
    dealer:tdealer;
begin
  if hlada=-1 then begin
     ddealer:=0;
     meno:=nothing(SizeOf(meno));
     priezvisko:=nothing(SizeOf(Priezvisko));
     exit;
     end;
 
  assign(fde,subor);
  {$I-}
  reset(fde);
  err:=ioresult;
  if err<>0 then begin
     rewrite(fde);
     err:=ioresult;
     end;
  {$I+}
  if err<>0 then hlaska('Chyba z pisu na disk. Pracovně disk chraneně proti z pisu.',0);
 
                                      { kto hlada ten najde !!! - databazovy sleng }
  for i:=1 to filesize(fde) do begin
      seek(fde,i-1);
      read(fde,dealer);
      if not(dealer.del) then
         if hlada=dealer.cis_deal then begin
            priezvisko:=dealer.priezv;
            ddealer:=dealer.cis_deal;
            close(fde);exit;
           end;
      end;
 
  farba(pomest,fomest);
  okno(4,2,48,23,' '+nazo+' ÄÄÄÄÄ Priezvisko ÄÄÄÄÄ',
                 'ESC-Opusti ENTER-věber Pohyb çˇpkami',pomest);
 
  farba(pnmest,fnmest);ir:=1;
  for i:=1 to vcisla do index[i]:=0;
  for i:=1 to filesize(fde) do begin
      seek(fde,i-1);
      read(fde,dealer);
      if not(dealer.del) then begin
         index[i]:=i;
         if ir<19 then begin
            gotoxy(2,ir);write(dealer.cis_deal:3,'-',dealer.priezv,'    ');
            ir:=ir+1;
            end;
         end;
      end;
 
  poc:=i;
  if vyber then i:=ir-1
           else i:=ir;
  if i<1 then i:=1;
 
  ch:=#1;x:=1;ir:=0;err:=1;
  seek(fde,index[1]-1);read(fde,dealer);
  i:=1;ir:=0;dec(povod);   {.sd}
  if povod>0 then err:=2;
 
  repeat
   KurzorZap(false);
   if err in [1,2] then begin if err in [1] then err:=0; end
                   else ch:=readkey;
 
   if (ch=#0) or (err=2) then begin
      farba(pnmest,fnmest);
      gotoxy(2,i);write(dealer.cis_deal:3,'-',dealer.priezv);
      if err=2 then begin
                    if povod>0  then begin ch:=#80;dec(povod);end;
                    if povod<=0 then err:=0;
                    end
               else ch:=readkey;
      case ch of
        #83:begin                               { DEL }
            dealer.del:=not(dealer.del);
            if dealer.del then hlaska(' Ozna>eně na vymazanie. ',70)
                          else hlaska(' Vymazanie zruçen,. ',70);
            seek(fde,index[i+ir]-1);write(fde,dealer);
            end;
        #72:begin x:=x-1;                       { sipka hore }
            if x<1 then begin
               x:=1;i:=i-1;if i<1 then begin gotoxy(1,1);insline;i:=1;ir:=ir-1;end;
               if ir<0 then begin ir:=0;x:=1;delline;end;
               end;
            end;
        #80:begin x:=x+1;if x>1 then            { sipka dole }
               if dealer.priezv=nothing(sizeof(dealer.priezv)-1) then begin
                       hlaska(' Ňalej nem"§eç najprv vyplĺ priezvisko',70);
                       x:=x-1;
                       end
                      else begin
                       x:=1;i:=i+1;
                       if i>19 then begin gotoxy(1,1);delline;i:=19;ir:=ir+1;end;
                       if index[i+ir]=0 then begin
                          index[i+ir]:=filesize(fde)+1;
                          dealer.cis_deal:=filesize(fde)+1;
                          dealer.priezv:=nothing(sizeof(dealer.priezv));
                         { dealer.meno:=nothing(sizeof(dealer.meno));}
                          dealer.del:=false;
                          end;
                        end;
                       end;
        else begin
             farba(pvmest,fvmest);
             dealer.priezv:=tread(6,i,sizeof(dealer.priezv)-1,dealer.priezv,'',#0,ch);
             end;
        end;
      ch:=#0;
     end;
 
     if (index[i+ir]<=filesize(fde)) and (index[i+ir]<>0) then begin
        seek(fde,index[i+ir]-1);read(fde,dealer);
        end
        else begin
         if dealer.priezv<>nothing(sizeof(dealer.priezv)-1) then
            begin
            dealer.cis_deal:=filesize(fde)+1;
            dealer.del:=false;
            dealer.priezv:=nothing(sizeof(dealer.priezv)-1);
            {dealer.meno:=nothing(sizeof(dealer.meno)-1);}
           end
           else index[i+ir]:=filesize(fde)+1;
         end;
 
     farba(pnmest,fnmest);
     gotoxy(2,i);write(dealer.cis_deal:3,'-'{,dealer.meno,' '},dealer.priezv);
     if dealer.del then write(' DEL')
                   else write('    ');
     if x=1 then {begin farba(pvmest,fvmest);gotoxy(6,i);write(dealer.meno);gotoxy(6,i);end
            else} begin farba(pvmest,fvmest);gotoxy(6{+sizeof(dealer.meno)},i);write(dealer.priezv);
                 gotoxy(7{+sizeof(dealer.meno)},i);end;
 
     if (not(vyber) and (ch in ['A'..'z',#32,#13])) or (vyber and (i+ir>poc) and (ch in ['A'..'z',#32,#13])) then begin
        if x=1 then {dealer.meno:=tread(6,i,sizeof(dealer.meno)-1,dealer.meno,#13,ch)
               else }dealer.priezv:=tread(6{+sizeof(dealer.meno)},i,sizeof(dealer.priezv)-1,dealer.priezv,'',#13,ch);
        err:=2;ch:=#80;
 
        if x=1 then {begin farba(pnmest,fvmest);gotoxy(6,i);write(dealer.meno);gotoxy(6,i);end
               else }begin farba(pnmest,fvmest);gotoxy(6{+sizeof(dealer.meno)},i);write(dealer.priezv);end;
               end;
 
     if not(dealer.priezv=nothing(sizeof(dealer.priezv)-1)) then begin
        seek(fde,index[i+ir]-1);write(fde,dealer);
        end;
     {meno:=dealer.meno;}priezvisko:=dealer.priezv;
   until ((ch=#27) or ((ch=#13)) );              { vyber and ch=#13 }
   close(fde);
   ddealer:=dealer.cis_deal;
 end;