Советы по Delphi



Работа с индексами Clipper'а - часть 2


/p>

Файл NtxAdd.pas

unit NtxAdd;

interface

uses
classes,SysUtils,NtxRO;

type
TNtxAdd=class(TNtxRO)protectedfunction Changed:boolean; override;function Add(var s:ShortString;var rn:integer;var nxt:integer):boolean;procedure NewRoot(s:ShortString;rn:integer;nxt:integer); virtual;function GetFreePtr(p:PBuf):Word;publicconstructor Create(nm:ShortString;ks:Word);constructor Open(nm:ShortString);procedure Insert(key:ShortString;rn:integer);end;
implementation

function
TNtxAdd.GetFreePtr(p:PBuf):Word;
var i,j:integer;
r:Word;fl:boolean;begin
r:=(max+2)*2;for i:=1 to max+1 dobegin fl:=True;for j:=1 to GetCount(p)+1 doif GetCount(PBuf(@(p^[j*2])))=r then fl:=False;if fl thenbeginResult:=r;exit;end;r:=r+isz;end;Result:=0;end;

function TNtxAdd.Add(var s:ShortString;var rn:integer;var nxt:integer):boolean;
var p:PBuf;
w,fr:Word;i:integer;tmp:integer;begin
with
tr dobeginp:=GetPage(h,(TTraceRec(Items[Count-1])).pg);if GetCount(p)thenbeginfr:=GetFreePtr(p);if fr=0 thenbeginSelf.Error:=True;Result:=True;exit;end;w:=GetCount(p)+1;p^[0]:=w and $FF; p^[1]:=(w and $FF00)shr 8;w:=(TTraceRec(Items[Count-1])).cn;for i:=GetCount(p)+1 downto w+1 dobeginp^[2*i] :=p^[2*i-2];p^[2*i+1]:=p^[2*i-1];end;p^[2*w] := fr and $FF;p^[2*w+1]:=(fr and $FF00)shr 8;for i:=0 to length(s)-1 do p^[fr+8+i]:=ord(s[i+1]);for i:=0 to 3 dobeginp^[fr+i]:=nxt mod $100;nxt:=nxt div $100;end;for i:=0 to 3 dobeginp^[fr+i+4]:=rn mod $100;rn:=rn div $100;end;FileSeek(h,(TTraceRec(Items[Count-1])).pg,0);FileWrite(h,p^,1024);Result:=True;endelsebeginfr:=GetCount(p)+1;fr:=GetCount(PBuf(@(p^[fr*2])));w:=(TTraceRec(Items[Count-1])).cn;for i:=GetCount(p)+1 downto w+1 dobeginp^[2*i] :=p^[2*i-2];p^[2*i+1]:=p^[2*i-1];end;p^[2*w] := fr and $FF;p^[2*w+1]:=(fr and $FF00)shr 8;for i:=0 to length(s)-1 do p^[fr+8+i]:=ord(s[i+1]);for i:=0 to 3 dobeginp^[fr+i+4]:=rn mod $100;rn:=rn div $100;end; tmp:=0;for i:=3 downto 0 do tmp:=$100*tmp+p^[fr+i];for i:=0 to 3 dobeginp^[fr+i]:=nxt mod $100;nxt:=nxt div $100;end;w:=hlf;p^[0]:=w and $FF; p^[1]:=(w and $FF00)shr 8;fr:=GetCount(PBuf(@(p^[(hlf+1)*2])));s:=''; rn:=0;for i:=0 to ksz-1 dobegins:=s+chr(p^[fr+8+i]);p^[fr+8+i]:=0;end;for i:=3 downto 0 dobeginrn:=$100*rn+p^[fr+i+4];p^[fr+i+4]:=0;end;nxt:=FileSeek(h,0,2);FileWrite(h,p^,1024);for i:=1 to hlf dobeginp^[2*i] :=p^[2*(i+hlf+1)];p^[2*i+1]:=p^[2*(i+hlf+1)+1];end;for i:=0 to 3 dobeginp^[fr+i]:=tmp mod $100;tmp:=tmp div $100;end;FileSeek(h,(TTraceRec(Items[Count-1])).pg,0);FileWrite(h,p^,1024);Result:=False;end;end;end;

procedure TNtxAdd.NewRoot(s:ShortString;rn:integer;nxt:integer);
var p:PBuf;
i,fr:integer;begin
p:=GetPage(h,0);for i:=0 to 1023 do p^[i]:=0;fr:=(max+2)*2;p^[0]:=1;p^[2]:=fr and $FF; p^[3]:=(fr and $FF00)shr 8;for i:=0 to length(s)-1 do p^[fr+8+i]:=ord(s[i+1]);for i:=0 to 3 dobeginp^[fr+i]:=nxt mod $100;nxt:=nxt div $100;end;for i:=0 to 3 dobeginp^[fr+i+4]:=rn mod $100;rn:=rn div $100;end;fr:=fr+isz;p^[4]:=fr and $FF; p^[5]:=(fr and $FF00)shr 8;nxt:=GetRoot;for i:=0 to 3 dobeginp^[fr+i]:=nxt mod $100;nxt:=nxt div $100;end;nxt:=FileSeek(h,0,2);FileWrite(h,p^,1024);FileSeek(h,4,0);FileWrite(h,nxt,sizeof(integer));end;

procedure TNtxAdd.Insert(key:ShortString;rn:integer);
var nxt:integer;
i:integer;begin nxt:=0;if DosFl then key:=WinToDos(key);if length(key)>ksz then key:=Copy(key,1,ksz);for i:=1 to ksz-length(key) do key:=key+' ';Clear;Load(GetRoot);Seek(key,False);while True dobeginif Add(key,rn,nxt) then break;if tr.Count=1 thenbeginNewRoot(key,rn,nxt);break;end;Pop;end;end;

constructor TNtxAdd.Create(nm:ShortString;ks:Word);
var p:PBuf;
i:integer;begin
Error:=False;DeleteFile(nm);h:=FileCreate(nm);if h>0 thenbeginp:=GetPage(h,0);for i:=0 to 1023 do p^[i]:=0;p^[14]:=ks and $FF; p^[15]:=(ks and $FF00)shr 8; ks:=ks+8;p^[12]:=ks and $FF; p^[13]:=(ks and $FF00)shr 8; i:=(1020-ks)div(2+ks); i:=i div 2;p^[20]:=i and $FF; p^[21]:=(i and $FF00)shr 8; i:=i*2; max:=i;p^[18]:=i and $FF; p^[19]:=(i and $FF00)shr 8; i:=1024;p^[4 ]:=i and $FF; p^[5 ]:=(i and $FF00)shr 8;FileWrite(h,p^,1024);for i:=0 to 1023 do p^[i]:=0; i:=(max+2)*2;p^[2 ]:=i and $FF; p^[3 ]:=(i and $FF00)shr 8;FileWrite(h,p^,1024);end else Error:=True;FileClose(h);FreeHandle(h);Open(nm);end;

constructor TNtxAdd.Open(nm:ShortString);
begin
Error:=False;h:=FileOpen(nm,fmOpenReadWrite or fmShareExclusive);if h>0 thenbeginFileSeek(h,12,0);FileRead(h,isz,2);FileSeek(h,14,0);FileRead(h,ksz,2);FileSeek(h,18,0);FileRead(h,max,2);FileSeek(h,20,0);FileRead(h,hlf,2);DosFl:=True;tr:=TList.Create;end else Error:=True;end;

function TNtxAdd.Changed:boolean;
begin
Result:=(csize=0);csize:=-1;end;

end.
<


Содержание  Назад  Вперед