cobjects.pas 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This module provides some basic objects
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$E+,N+,D+,F+}
  20. {$endif}
  21. {$I-}
  22. {$R-}{ necessary for crc calculation }
  23. unit cobjects;
  24. interface
  25. uses
  26. strings
  27. {$ifndef linux}
  28. ,dos
  29. {$else}
  30. ,linux
  31. {$endif}
  32. ;
  33. const hasharraysize = 253; {The size of a hasharray should be a prime
  34. number for better spreading of nodes in
  35. the array!! (DM)}
  36. type
  37. pstring = ^string;
  38. {$ifdef TP}
  39. { redeclare dword only in case of emergency, some small things
  40. of the compiler won't work then correctly (FK)
  41. }
  42. dword = longint;
  43. {$endif TP}
  44. pfileposinfo = ^tfileposinfo;
  45. tfileposinfo = record
  46. line : longint;
  47. column : word;
  48. fileindex : word;
  49. end;
  50. { some help data types }
  51. pstringitem = ^tstringitem;
  52. tstringitem = record
  53. data : pstring;
  54. next : pstringitem;
  55. fileinfo : tfileposinfo; { pointer to tinputfile }
  56. end;
  57. plinkedlist_item = ^tlinkedlist_item;
  58. tlinkedlist_item = object
  59. next,previous : plinkedlist_item;
  60. { does nothing }
  61. constructor init;
  62. destructor done;virtual;
  63. function getcopy:plinkedlist_item;virtual;
  64. end;
  65. pstring_item = ^tstring_item;
  66. tstring_item = object(tlinkedlist_item)
  67. str : pstring;
  68. constructor init(const s : string);
  69. destructor done;virtual;
  70. end;
  71. { this implements a double linked list }
  72. plinkedlist = ^tlinkedlist;
  73. tlinkedlist = object
  74. first,last : plinkedlist_item;
  75. constructor init;
  76. destructor done;
  77. { disposes the items of the list }
  78. procedure clear;
  79. { concats a new item at the end }
  80. procedure concat(p : plinkedlist_item);
  81. { inserts a new item at the begin }
  82. procedure insert(p : plinkedlist_item);
  83. { inserts another list at the begin and make this list empty }
  84. procedure insertlist(p : plinkedlist);
  85. { concats another list at the end and make this list empty }
  86. procedure concatlist(p : plinkedlist);
  87. procedure concatlistcopy(p : plinkedlist);
  88. { removes p from the list (p isn't disposed) }
  89. { it's not tested if p is in the list ! }
  90. procedure remove(p : plinkedlist_item);
  91. { is the linkedlist empty ? }
  92. function empty:boolean;
  93. end;
  94. { String Queue}
  95. PStringQueue=^TStringQueue;
  96. TStringQueue=object
  97. first,last : PStringItem;
  98. constructor Init;
  99. destructor Done;
  100. function Empty:boolean;
  101. function Get:string;
  102. procedure Insert(const s:string);
  103. procedure Concat(const s:string);
  104. procedure Clear;
  105. end;
  106. { string container }
  107. pstringcontainer = ^tstringcontainer;
  108. tstringcontainer = object
  109. root,
  110. last : pstringitem;
  111. doubles : boolean; { if this is set to true, doubles are allowed }
  112. constructor init;
  113. constructor init_no_double;
  114. destructor done;
  115. { true when the container is empty }
  116. function empty:boolean;
  117. { inserts a string }
  118. procedure insert(const s : string);
  119. procedure insert_with_tokeninfo(const s : string;const file_info : tfileposinfo);
  120. { gets a string }
  121. function get : string;
  122. function get_with_tokeninfo(var file_info : tfileposinfo) : string;
  123. { true if string is in the container }
  124. function find(const s:string):boolean;
  125. { deletes all strings }
  126. procedure clear;
  127. end;
  128. Pnamedindexobject=^Tnamedindexobject;
  129. Tnamedindexobject=object
  130. indexnr : longint;
  131. _name : Pstring;
  132. next,
  133. left,right : Pnamedindexobject;
  134. speedvalue : longint;
  135. constructor init;
  136. constructor initname(const n:string);
  137. destructor done;virtual;
  138. procedure setname(const n:string);
  139. function name:string;
  140. end;
  141. Pdictionaryhasharray=^Tdictionaryhasharray;
  142. Tdictionaryhasharray=array[0..hasharraysize-1] of Pnamedindexobject;
  143. Tnamedindexcallback = procedure(p:Pnamedindexobject);
  144. Pdictionary=^Tdictionary;
  145. Tdictionary=object
  146. noclear : boolean;
  147. replace_existing : boolean;
  148. constructor init;
  149. destructor done;virtual;
  150. procedure usehash;
  151. procedure clear;
  152. function empty:boolean;
  153. procedure foreach(proc2call:Tnamedindexcallback);
  154. function insert(obj:Pnamedindexobject):Pnamedindexobject;
  155. function rename(const olds,news : string):Pnamedindexobject;
  156. function search(const s:string):Pnamedindexobject;
  157. function speedsearch(const s:string;speedvalue:longint):Pnamedindexobject;
  158. private
  159. root : Pnamedindexobject;
  160. hasharray : Pdictionaryhasharray;
  161. procedure cleartree(obj:Pnamedindexobject);
  162. function insertnode(newnode:Pnamedindexobject;var currnode:Pnamedindexobject):Pnamedindexobject;
  163. procedure inserttree(currtree,currroot:Pnamedindexobject);
  164. end;
  165. pdynamicarray = ^tdynamicarray;
  166. tdynamicarray = object
  167. posn,
  168. count,
  169. limit,
  170. elemlen,
  171. growcount : longint;
  172. data : pchar;
  173. constructor init(Aelemlen,Agrow:longint);
  174. destructor done;
  175. function size:longint;
  176. function usedsize:longint;
  177. procedure grow;
  178. procedure align(i:longint);
  179. procedure seek(i:longint);
  180. procedure write(var d;len:longint);
  181. procedure read(var d;len:longint);
  182. procedure writepos(pos:longint;var d;len:longint);
  183. procedure readpos(pos:longint;var d;len:longint);
  184. end;
  185. tindexobjectarray=array[1..16000] of Pnamedindexobject;
  186. Pnamedindexobjectarray=^tindexobjectarray;
  187. pindexarray=^tindexarray;
  188. tindexarray=object
  189. first : Pnamedindexobject;
  190. count : longint;
  191. constructor init(Agrowsize:longint);
  192. destructor done;
  193. procedure clear;
  194. procedure foreach(proc2call : Tnamedindexcallback);
  195. procedure deleteindex(p:Pnamedindexobject);
  196. procedure delete(p:Pnamedindexobject);
  197. procedure insert(p:Pnamedindexobject);
  198. function search(nr:longint):Pnamedindexobject;
  199. private
  200. growsize,
  201. size : longint;
  202. data : Pnamedindexobjectarray;
  203. procedure grow(gsize:longint);
  204. end;
  205. {$ifdef BUFFEREDFILE}
  206. { this is implemented to allow buffered binary I/O }
  207. pbufferedfile = ^tbufferedfile;
  208. tbufferedfile = object
  209. f : file;
  210. buf : pchar;
  211. bufsize,buflast,bufpos : longint;
  212. { 0 closed, 1 input, 2 output }
  213. iomode : byte;
  214. { true, if the compile should change the endian of the output }
  215. change_endian : boolean;
  216. { calcules a crc for the file, }
  217. { but it's assumed, that there no seek while do_crc is true }
  218. do_crc : boolean;
  219. crc : longint;
  220. { temporary closing feature }
  221. tempclosed : boolean;
  222. tempmode : byte;
  223. temppos : longint;
  224. { inits a buffer with the size bufsize which is assigned to }
  225. { the file filename }
  226. constructor init(const filename : string;_bufsize : longint);
  227. { closes the file, if needed, and releases the memory }
  228. destructor done;virtual;
  229. { opens the file for input, other accesses are rejected }
  230. function reset:boolean;
  231. { opens the file for output, other accesses are rejected }
  232. procedure rewrite;
  233. { reads or writes the buffer from or to disk }
  234. procedure flush;
  235. { writes a string to the file }
  236. { the string is written without a length byte }
  237. procedure write_string(const s : string);
  238. { writes a zero terminated string }
  239. procedure write_pchar(p : pchar);
  240. { write specific data types, takes care of }
  241. { byte order }
  242. procedure write_byte(b : byte);
  243. procedure write_word(w : word);
  244. procedure write_long(l : longint);
  245. procedure write_double(d : double);
  246. { writes any data }
  247. procedure write_data(var data;count : longint);
  248. { reads any data }
  249. procedure read_data(var data;bytes : longint;var count : longint);
  250. { closes the file and releases the buffer }
  251. procedure close;
  252. { temporary closing }
  253. procedure tempclose;
  254. procedure tempreopen;
  255. { goto the given position }
  256. procedure seek(l : longint);
  257. { installes an user defined buffer }
  258. { and releases the old one, but be }
  259. { careful, if the old buffer contains }
  260. { data, this data is lost }
  261. procedure setbuf(p : pchar;s : longint);
  262. { reads the file time stamp of the file, }
  263. { the file must be opened }
  264. function getftime : longint;
  265. { returns filesize }
  266. function getsize : longint;
  267. { returns the path }
  268. function getpath : string;
  269. { resets the crc }
  270. procedure clear_crc;
  271. { returns the crc }
  272. function getcrc : longint;
  273. end;
  274. {$endif BUFFEREDFILE}
  275. { releases the string p and assignes nil to p }
  276. { if p=nil then freemem isn't called }
  277. procedure stringdispose(var p : pstring);
  278. { idem for ansistrings }
  279. procedure ansistringdispose(var p : pchar;length : longint);
  280. { allocates mem for a copy of s, copies s to this mem and returns }
  281. { a pointer to this mem }
  282. function stringdup(const s : string) : pstring;
  283. { allocates memory for s and copies s as zero terminated string
  284. to that mem and returns a pointer to that mem }
  285. function strpnew(const s : string) : pchar;
  286. { makes a char lowercase, with spanish, french and german char set }
  287. function lowercase(c : char) : char;
  288. { makes zero terminated string to a pascal string }
  289. { the data in p is modified and p is returned }
  290. function pchar2pstring(p : pchar) : pstring;
  291. { ambivalent to pchar2pstring }
  292. function pstring2pchar(p : pstring) : pchar;
  293. implementation
  294. {$ifndef TP}
  295. function getspeedvalue(const s : string) : longint;
  296. var
  297. p1,p2:^byte;
  298. i : longint;
  299. begin
  300. p1:=@s;
  301. longint(p2):=longint(p1)+p1^+1;
  302. inc(longint(p1));
  303. i:=0;
  304. while p1<>p2 do
  305. begin
  306. inc(i,p1^);
  307. inc(longint(p1));
  308. end;
  309. getspeedvalue:=i;
  310. end;
  311. {$else}
  312. function getspeedvalue(const s : string) : longint;
  313. type
  314. ptrrec=record
  315. ofs,seg:word;
  316. end;
  317. var
  318. l,w : longint;
  319. p1,p2 : ^byte;
  320. begin
  321. p1:=@s;
  322. ptrrec(p2).seg:=ptrrec(p1).seg;
  323. ptrrec(p2).ofs:=ptrrec(p1).ofs+p1^+1;
  324. inc(p1);
  325. l:=0;
  326. while p1<>p2 do
  327. begin
  328. l:=l+p1^;
  329. inc(p1);
  330. end;
  331. getspeedvalue:=l;
  332. end;
  333. {$endif}
  334. function pchar2pstring(p : pchar) : pstring;
  335. var
  336. w,i : longint;
  337. begin
  338. w:=strlen(p);
  339. for i:=w-1 downto 0 do
  340. p[i+1]:=p[i];
  341. p[0]:=chr(w);
  342. pchar2pstring:=pstring(p);
  343. end;
  344. function pstring2pchar(p : pstring) : pchar;
  345. var
  346. w,i : longint;
  347. begin
  348. w:=length(p^);
  349. for i:=1 to w do
  350. p^[i-1]:=p^[i];
  351. p^[w]:=#0;
  352. pstring2pchar:=pchar(p);
  353. end;
  354. function lowercase(c : char) : char;
  355. begin
  356. case c of
  357. #65..#90 : c := chr(ord (c) + 32);
  358. #154 : c:=#129; { german }
  359. #142 : c:=#132; { german }
  360. #153 : c:=#148; { german }
  361. #144 : c:=#130; { french }
  362. #128 : c:=#135; { french }
  363. #143 : c:=#134; { swedish/norge (?) }
  364. #165 : c:=#164; { spanish }
  365. #228 : c:=#229; { greek }
  366. #226 : c:=#231; { greek }
  367. #232 : c:=#227; { greek }
  368. end;
  369. lowercase := c;
  370. end;
  371. function strpnew(const s : string) : pchar;
  372. var
  373. p : pchar;
  374. begin
  375. getmem(p,length(s)+1);
  376. strpcopy(p,s);
  377. strpnew:=p;
  378. end;
  379. procedure stringdispose(var p : pstring);
  380. begin
  381. if assigned(p) then
  382. freemem(p,length(p^)+1);
  383. p:=nil;
  384. end;
  385. procedure ansistringdispose(var p : pchar;length : longint);
  386. begin
  387. if assigned(p) then
  388. freemem(p,length+1);
  389. p:=nil;
  390. end;
  391. function stringdup(const s : string) : pstring;
  392. var
  393. p : pstring;
  394. begin
  395. getmem(p,length(s)+1);
  396. p^:=s;
  397. stringdup:=p;
  398. end;
  399. {****************************************************************************
  400. TStringQueue
  401. ****************************************************************************}
  402. constructor TStringQueue.Init;
  403. begin
  404. first:=nil;
  405. end;
  406. function TStringQueue.Empty:boolean;
  407. begin
  408. Empty:=(first=nil);
  409. end;
  410. function TStringQueue.Get:string;
  411. var
  412. newnode : pstringitem;
  413. begin
  414. if first=nil then
  415. begin
  416. Get:='';
  417. exit;
  418. end;
  419. Get:=first^.data^;
  420. stringdispose(first^.data);
  421. newnode:=first;
  422. first:=first^.next;
  423. dispose(newnode);
  424. end;
  425. procedure TStringQueue.Insert(const s:string);
  426. var
  427. newnode : pstringitem;
  428. begin
  429. new(newnode);
  430. newnode^.next:=first;
  431. newnode^.data:=stringdup(s);
  432. first:=newnode;
  433. if last=nil then
  434. last:=newnode;
  435. end;
  436. procedure TStringQueue.Concat(const s:string);
  437. var
  438. newnode : pstringitem;
  439. begin
  440. new(newnode);
  441. newnode^.next:=nil;
  442. newnode^.data:=stringdup(s);
  443. if first=nil then
  444. first:=newnode
  445. else
  446. last^.next:=newnode;
  447. last:=newnode;
  448. end;
  449. procedure TStringQueue.Clear;
  450. var
  451. newnode : pstringitem;
  452. begin
  453. while (first<>nil) do
  454. begin
  455. newnode:=first;
  456. stringdispose(first^.data);
  457. first:=first^.next;
  458. dispose(newnode);
  459. end;
  460. end;
  461. destructor TStringQueue.Done;
  462. begin
  463. Clear;
  464. end;
  465. {****************************************************************************
  466. TSTRINGCONTAINER
  467. ****************************************************************************}
  468. constructor tstringcontainer.init;
  469. begin
  470. root:=nil;
  471. last:=nil;
  472. doubles:=true;
  473. end;
  474. constructor tstringcontainer.init_no_double;
  475. begin
  476. root:=nil;
  477. last:=nil;
  478. doubles:=false;
  479. end;
  480. destructor tstringcontainer.done;
  481. begin
  482. clear;
  483. end;
  484. function tstringcontainer.empty:boolean;
  485. begin
  486. empty:=(root=nil);
  487. end;
  488. procedure tstringcontainer.insert(const s : string);
  489. var
  490. newnode : pstringitem;
  491. begin
  492. if not(doubles) then
  493. begin
  494. newnode:=root;
  495. while assigned(newnode) do
  496. begin
  497. if newnode^.data^=s then exit;
  498. newnode:=newnode^.next;
  499. end;
  500. end;
  501. new(newnode);
  502. newnode^.next:=nil;
  503. newnode^.data:=stringdup(s);
  504. if root=nil then root:=newnode
  505. else last^.next:=newnode;
  506. last:=newnode;
  507. end;
  508. procedure tstringcontainer.insert_with_tokeninfo(const s : string; const file_info : tfileposinfo);
  509. var
  510. newnode : pstringitem;
  511. begin
  512. if not(doubles) then
  513. begin
  514. newnode:=root;
  515. while assigned(newnode) do
  516. begin
  517. if newnode^.data^=s then exit;
  518. newnode:=newnode^.next;
  519. end;
  520. end;
  521. new(newnode);
  522. newnode^.next:=nil;
  523. newnode^.data:=stringdup(s);
  524. newnode^.fileinfo:=file_info;
  525. if root=nil then root:=newnode
  526. else last^.next:=newnode;
  527. last:=newnode;
  528. end;
  529. procedure tstringcontainer.clear;
  530. var
  531. newnode : pstringitem;
  532. begin
  533. newnode:=root;
  534. while assigned(newnode) do
  535. begin
  536. stringdispose(newnode^.data);
  537. root:=newnode^.next;
  538. dispose(newnode);
  539. newnode:=root;
  540. end;
  541. last:=nil;
  542. root:=nil;
  543. end;
  544. function tstringcontainer.get : string;
  545. var
  546. newnode : pstringitem;
  547. begin
  548. if root=nil then
  549. get:=''
  550. else
  551. begin
  552. get:=root^.data^;
  553. newnode:=root;
  554. root:=root^.next;
  555. stringdispose(newnode^.data);
  556. dispose(newnode);
  557. end;
  558. end;
  559. function tstringcontainer.get_with_tokeninfo(var file_info : tfileposinfo) : string;
  560. var
  561. newnode : pstringitem;
  562. begin
  563. if root=nil then
  564. begin
  565. get_with_tokeninfo:='';
  566. file_info.fileindex:=0;
  567. file_info.line:=0;
  568. file_info.column:=0;
  569. end
  570. else
  571. begin
  572. get_with_tokeninfo:=root^.data^;
  573. newnode:=root;
  574. root:=root^.next;
  575. stringdispose(newnode^.data);
  576. file_info:=newnode^.fileinfo;
  577. dispose(newnode);
  578. end;
  579. end;
  580. function tstringcontainer.find(const s:string):boolean;
  581. var
  582. newnode : pstringitem;
  583. begin
  584. find:=false;
  585. newnode:=root;
  586. while assigned(newnode) do
  587. begin
  588. if newnode^.data^=s then
  589. begin
  590. find:=true;
  591. exit;
  592. end;
  593. newnode:=newnode^.next;
  594. end;
  595. end;
  596. {****************************************************************************
  597. TLINKEDLIST_ITEM
  598. ****************************************************************************}
  599. constructor tlinkedlist_item.init;
  600. begin
  601. previous:=nil;
  602. next:=nil;
  603. end;
  604. destructor tlinkedlist_item.done;
  605. begin
  606. end;
  607. function tlinkedlist_item.getcopy:plinkedlist_item;
  608. var
  609. l : longint;
  610. p : plinkedlist_item;
  611. begin
  612. l:=sizeof(self);
  613. getmem(p,l);
  614. move(self,p^,l);
  615. getcopy:=p;
  616. end;
  617. {****************************************************************************
  618. TSTRING_ITEM
  619. ****************************************************************************}
  620. constructor tstring_item.init(const s : string);
  621. begin
  622. str:=stringdup(s);
  623. end;
  624. destructor tstring_item.done;
  625. begin
  626. stringdispose(str);
  627. inherited done;
  628. end;
  629. {****************************************************************************
  630. TLINKEDLIST
  631. ****************************************************************************}
  632. constructor tlinkedlist.init;
  633. begin
  634. first:=nil;
  635. last:=nil;
  636. end;
  637. destructor tlinkedlist.done;
  638. begin
  639. clear;
  640. end;
  641. procedure tlinkedlist.clear;
  642. var
  643. newnode : plinkedlist_item;
  644. begin
  645. newnode:=first;
  646. while assigned(newnode) do
  647. begin
  648. first:=newnode^.next;
  649. dispose(newnode,done);
  650. newnode:=first;
  651. end;
  652. end;
  653. procedure tlinkedlist.insertlist(p : plinkedlist);
  654. begin
  655. { empty list ? }
  656. if not(assigned(p^.first)) then
  657. exit;
  658. p^.last^.next:=first;
  659. { we have a double linked list }
  660. if assigned(first) then
  661. first^.previous:=p^.last;
  662. first:=p^.first;
  663. if not(assigned(last)) then
  664. last:=p^.last;
  665. { p becomes empty }
  666. p^.first:=nil;
  667. p^.last:=nil;
  668. end;
  669. procedure tlinkedlist.concat(p : plinkedlist_item);
  670. begin
  671. if not(assigned(first)) then
  672. begin
  673. first:=p;
  674. p^.previous:=nil;
  675. p^.next:=nil;
  676. end
  677. else
  678. begin
  679. last^.next:=p;
  680. p^.previous:=last;
  681. p^.next:=nil;
  682. end;
  683. last:=p;
  684. end;
  685. procedure tlinkedlist.insert(p : plinkedlist_item);
  686. begin
  687. if not(assigned(first)) then
  688. begin
  689. last:=p;
  690. p^.previous:=nil;
  691. p^.next:=nil;
  692. end
  693. else
  694. begin
  695. first^.previous:=p;
  696. p^.previous:=nil;
  697. p^.next:=first;
  698. end;
  699. first:=p;
  700. end;
  701. procedure tlinkedlist.remove(p : plinkedlist_item);
  702. begin
  703. if not(assigned(p)) then
  704. exit;
  705. if (first=p) and (last=p) then
  706. begin
  707. first:=nil;
  708. last:=nil;
  709. end
  710. else if first=p then
  711. begin
  712. first:=p^.next;
  713. if assigned(first) then
  714. first^.previous:=nil;
  715. end
  716. else if last=p then
  717. begin
  718. last:=last^.previous;
  719. if assigned(last) then
  720. last^.next:=nil;
  721. end
  722. else
  723. begin
  724. p^.previous^.next:=p^.next;
  725. p^.next^.previous:=p^.previous;
  726. end;
  727. p^.next:=nil;
  728. p^.previous:=nil;
  729. end;
  730. procedure tlinkedlist.concatlist(p : plinkedlist);
  731. begin
  732. if not(assigned(p^.first)) then
  733. exit;
  734. if not(assigned(first)) then
  735. first:=p^.first
  736. else
  737. begin
  738. last^.next:=p^.first;
  739. p^.first^.previous:=last;
  740. end;
  741. last:=p^.last;
  742. { make p empty }
  743. p^.last:=nil;
  744. p^.first:=nil;
  745. end;
  746. procedure tlinkedlist.concatlistcopy(p : plinkedlist);
  747. var
  748. newnode,newnode2 : plinkedlist_item;
  749. begin
  750. newnode:=p^.first;
  751. while assigned(newnode) do
  752. begin
  753. newnode2:=newnode^.getcopy;
  754. if assigned(newnode2) then
  755. begin
  756. if not(assigned(first)) then
  757. begin
  758. first:=newnode2;
  759. newnode2^.previous:=nil;
  760. newnode2^.next:=nil;
  761. end
  762. else
  763. begin
  764. last^.next:=newnode2;
  765. newnode2^.previous:=last;
  766. newnode2^.next:=nil;
  767. end;
  768. last:=newnode2;
  769. end;
  770. newnode:=newnode^.next;
  771. end;
  772. end;
  773. function tlinkedlist.empty:boolean;
  774. begin
  775. empty:=(first=nil);
  776. end;
  777. {****************************************************************************
  778. Tnamedindexobject
  779. ****************************************************************************}
  780. constructor Tnamedindexobject.init;
  781. begin
  782. { index }
  783. indexnr:=-1;
  784. next:=nil;
  785. { dictionary }
  786. left:=nil;
  787. right:=nil;
  788. _name:=nil;
  789. speedvalue:=-1;
  790. end;
  791. constructor Tnamedindexobject.initname(const n:string);
  792. begin
  793. { index }
  794. indexnr:=-1;
  795. next:=nil;
  796. { dictionary }
  797. left:=nil;
  798. right:=nil;
  799. speedvalue:=-1;
  800. _name:=stringdup(n);
  801. end;
  802. destructor Tnamedindexobject.done;
  803. begin
  804. stringdispose(_name);
  805. end;
  806. procedure Tnamedindexobject.setname(const n:string);
  807. begin
  808. if speedvalue=-1 then
  809. begin
  810. if assigned(_name) then
  811. stringdispose(_name);
  812. _name:=stringdup(n);
  813. end;
  814. end;
  815. function Tnamedindexobject.name:string;
  816. begin
  817. if assigned(_name) then
  818. name:=_name^
  819. else
  820. name:='';
  821. end;
  822. {****************************************************************************
  823. TDICTIONARY
  824. ****************************************************************************}
  825. constructor Tdictionary.init;
  826. begin
  827. root:=nil;
  828. hasharray:=nil;
  829. noclear:=false;
  830. replace_existing:=false;
  831. end;
  832. procedure Tdictionary.usehash;
  833. begin
  834. if not(assigned(root)) and
  835. not(assigned(hasharray)) then
  836. begin
  837. new(hasharray);
  838. fillchar(hasharray^,sizeof(hasharray^),0);
  839. end;
  840. end;
  841. destructor Tdictionary.done;
  842. begin
  843. if not noclear then
  844. clear;
  845. if assigned(hasharray) then
  846. dispose(hasharray);
  847. end;
  848. procedure Tdictionary.cleartree(obj:Pnamedindexobject);
  849. begin
  850. if assigned(obj^.left) then
  851. cleartree(obj^.left);
  852. if assigned(obj^.right) then
  853. cleartree(obj^.right);
  854. dispose(obj,done);
  855. obj:=nil;
  856. end;
  857. procedure Tdictionary.clear;
  858. var
  859. w : longint;
  860. begin
  861. if assigned(root) then
  862. cleartree(root);
  863. if assigned(hasharray) then
  864. for w:=0 to hasharraysize-1 do
  865. if assigned(hasharray^[w]) then
  866. cleartree(hasharray^[w]);
  867. end;
  868. function Tdictionary.empty:boolean;
  869. var
  870. w : longint;
  871. begin
  872. if assigned(hasharray) then
  873. begin
  874. empty:=false;
  875. for w:=0 to hasharraysize-1 do
  876. if assigned(hasharray^[w]) then
  877. exit;
  878. empty:=true;
  879. end
  880. else
  881. empty:=(root=nil);
  882. end;
  883. procedure Tdictionary.foreach(proc2call:Tnamedindexcallback);
  884. procedure a(p:Pnamedindexobject);
  885. begin
  886. proc2call(p);
  887. if assigned(p^.left) then
  888. a(p^.left);
  889. if assigned(p^.right) then
  890. a(p^.right);
  891. end;
  892. var
  893. i : longint;
  894. begin
  895. if assigned(hasharray) then
  896. begin
  897. for i:=0 to hasharraysize-1 do
  898. if assigned(hasharray^[i]) then
  899. a(hasharray^[i]);
  900. end
  901. else
  902. if assigned(root) then
  903. a(root);
  904. end;
  905. function Tdictionary.insert(obj:Pnamedindexobject):Pnamedindexobject;
  906. begin
  907. obj^.speedvalue:=getspeedvalue(obj^._name^);
  908. if assigned(hasharray) then
  909. insert:=insertnode(obj,hasharray^[obj^.speedvalue mod hasharraysize])
  910. else
  911. insert:=insertnode(obj,root);
  912. end;
  913. function tdictionary.insertnode(newnode:Pnamedindexobject;var currnode:Pnamedindexobject):Pnamedindexobject;
  914. var
  915. s1,s2:^string;
  916. begin
  917. if currnode=nil then
  918. begin
  919. currnode:=newnode;
  920. insertnode:=currnode;
  921. end
  922. { first check speedvalue, to allow a fast insert }
  923. else
  924. if currnode^.speedvalue>newnode^.speedvalue then
  925. insertnode:=insertnode(newnode,currnode^.right)
  926. else
  927. if currnode^.speedvalue<newnode^.speedvalue then
  928. insertnode:=insertnode(newnode,currnode^.left)
  929. else
  930. begin
  931. new(s1);
  932. new(s2);
  933. s1^:=currnode^._name^;
  934. s2^:=newnode^._name^;
  935. if s1^>s2^ then
  936. begin
  937. dispose(s2);
  938. dispose(s1);
  939. insertnode:=insertnode(newnode,currnode^.right);
  940. end
  941. else
  942. if s1^<s2^ then
  943. begin
  944. dispose(s2);
  945. dispose(s1);
  946. insertnode:=insertnode(newnode,currnode^.left);
  947. end
  948. else
  949. begin
  950. dispose(s2);
  951. dispose(s1);
  952. if replace_existing and
  953. assigned(currnode) then
  954. begin
  955. newnode^.left:=currnode^.left;
  956. newnode^.right:=currnode^.right;
  957. currnode:=newnode;
  958. insertnode:=newnode;
  959. end
  960. else
  961. insertnode:=currnode;
  962. end;
  963. end;
  964. end;
  965. procedure tdictionary.inserttree(currtree,currroot:Pnamedindexobject);
  966. begin
  967. if assigned(currtree) then
  968. begin
  969. inserttree(currtree^.left,currroot);
  970. inserttree(currtree^.right,currroot);
  971. insertnode(currtree,currroot);
  972. end;
  973. end;
  974. function tdictionary.rename(const olds,news : string):Pnamedindexobject;
  975. var
  976. spdval : longint;
  977. lasthp,
  978. hp,hp2,hp3 : Pnamedindexobject;
  979. begin
  980. spdval:=getspeedvalue(olds);
  981. if assigned(hasharray) then
  982. hp:=hasharray^[spdval mod hasharraysize]
  983. else
  984. hp:=root;
  985. lasthp:=nil;
  986. while assigned(hp) do
  987. begin
  988. if spdval>hp^.speedvalue then
  989. begin
  990. lasthp:=hp;
  991. hp:=hp^.left
  992. end
  993. else
  994. if spdval<hp^.speedvalue then
  995. begin
  996. lasthp:=hp;
  997. hp:=hp^.right
  998. end
  999. else
  1000. begin
  1001. if (hp^.name=olds) then
  1002. begin
  1003. { get in hp2 the replacer for the root or hasharr }
  1004. hp2:=hp^.left;
  1005. hp3:=hp^.right;
  1006. if not assigned(hp2) then
  1007. begin
  1008. hp2:=hp^.right;
  1009. hp3:=hp^.left;
  1010. end;
  1011. { remove entry from the tree }
  1012. if assigned(lasthp) then
  1013. begin
  1014. if lasthp^.left=hp then
  1015. lasthp^.left:=hp2
  1016. else
  1017. lasthp^.right:=hp2;
  1018. end
  1019. else
  1020. begin
  1021. if assigned(hasharray) then
  1022. hasharray^[spdval mod hasharraysize]:=hp2
  1023. else
  1024. root:=hp2;
  1025. end;
  1026. { reinsert the hp3 in the tree from hp2 }
  1027. inserttree(hp3,hp2);
  1028. { reinsert }
  1029. stringdispose(hp^._name);
  1030. hp^._name:=stringdup(news);
  1031. hp^.speedvalue:=getspeedvalue(news);
  1032. if assigned(hasharray) then
  1033. rename:=insertnode(hp,hasharray^[hp^.speedvalue mod hasharraysize])
  1034. else
  1035. rename:=insertnode(hp,root);
  1036. exit;
  1037. end
  1038. else
  1039. if olds>hp^.name then
  1040. begin
  1041. lasthp:=hp;
  1042. hp:=hp^.left
  1043. end
  1044. else
  1045. begin
  1046. lasthp:=hp;
  1047. hp:=hp^.right;
  1048. end;
  1049. end;
  1050. end;
  1051. end;
  1052. function Tdictionary.search(const s:string):Pnamedindexobject;
  1053. begin
  1054. search:=speedsearch(s,getspeedvalue(s));
  1055. end;
  1056. function Tdictionary.speedsearch(const s:string;speedvalue:longint):Pnamedindexobject;
  1057. var
  1058. newnode:Pnamedindexobject;
  1059. begin
  1060. if assigned(hasharray) then
  1061. newnode:=hasharray^[speedvalue mod hasharraysize]
  1062. else
  1063. newnode:=root;
  1064. while assigned(newnode) do
  1065. begin
  1066. if speedvalue>newnode^.speedvalue then
  1067. newnode:=newnode^.left
  1068. else
  1069. if speedvalue<newnode^.speedvalue then
  1070. newnode:=newnode^.right
  1071. else
  1072. begin
  1073. if (newnode^._name^=s) then
  1074. begin
  1075. speedsearch:=newnode;
  1076. exit;
  1077. end
  1078. else
  1079. if s>newnode^._name^ then
  1080. newnode:=newnode^.left
  1081. else
  1082. newnode:=newnode^.right;
  1083. end;
  1084. end;
  1085. speedsearch:=nil;
  1086. end;
  1087. {****************************************************************************
  1088. tdynamicarray
  1089. ****************************************************************************}
  1090. constructor tdynamicarray.init(Aelemlen,Agrow:longint);
  1091. begin
  1092. posn:=0;
  1093. count:=0;
  1094. limit:=0;
  1095. data:=nil;
  1096. elemlen:=Aelemlen;
  1097. growcount:=Agrow;
  1098. grow;
  1099. end;
  1100. function tdynamicarray.size:longint;
  1101. begin
  1102. size:=limit*elemlen;
  1103. end;
  1104. function tdynamicarray.usedsize:longint;
  1105. begin
  1106. usedsize:=count*elemlen;
  1107. end;
  1108. procedure tdynamicarray.grow;
  1109. var
  1110. osize : longint;
  1111. odata : pchar;
  1112. begin
  1113. osize:=size;
  1114. odata:=data;
  1115. inc(limit,growcount);
  1116. getmem(data,size);
  1117. if assigned(odata) then
  1118. begin
  1119. move(odata^,data^,osize);
  1120. freemem(odata,osize);
  1121. end;
  1122. fillchar(data[osize],growcount*elemlen,0);
  1123. end;
  1124. procedure tdynamicarray.align(i:longint);
  1125. var
  1126. j : longint;
  1127. begin
  1128. j:=(posn*elemlen mod i);
  1129. if j<>0 then
  1130. begin
  1131. j:=i-j;
  1132. while limit<(posn+j) do
  1133. grow;
  1134. inc(posn,j);
  1135. if (posn>count) then
  1136. count:=posn;
  1137. end;
  1138. end;
  1139. procedure tdynamicarray.seek(i:longint);
  1140. begin
  1141. while limit<i do
  1142. grow;
  1143. posn:=i;
  1144. if (posn>count) then
  1145. count:=posn;
  1146. end;
  1147. procedure tdynamicarray.write(var d;len:longint);
  1148. begin
  1149. while limit<(posn+len) do
  1150. grow;
  1151. move(d,data[posn*elemlen],len*elemlen);
  1152. inc(posn,len);
  1153. if (posn>count) then
  1154. count:=posn;
  1155. end;
  1156. procedure tdynamicarray.read(var d;len:longint);
  1157. begin
  1158. move(data[posn*elemlen],d,len*elemlen);
  1159. inc(posn,len);
  1160. if (posn>count) then
  1161. count:=posn;
  1162. end;
  1163. procedure tdynamicarray.writepos(pos:longint;var d;len:longint);
  1164. begin
  1165. while limit<(pos+len) do
  1166. grow;
  1167. move(d,data[pos*elemlen],len*elemlen);
  1168. posn:=pos+len;
  1169. if (posn>count) then
  1170. count:=posn;
  1171. end;
  1172. procedure tdynamicarray.readpos(pos:longint;var d;len:longint);
  1173. begin
  1174. while limit<(pos+len) do
  1175. grow;
  1176. move(data[pos*elemlen],d,len*elemlen);
  1177. posn:=pos+len;
  1178. if (posn>count) then
  1179. count:=posn;
  1180. end;
  1181. destructor tdynamicarray.done;
  1182. begin
  1183. if assigned(data) then
  1184. freemem(data,size);
  1185. end;
  1186. {****************************************************************************
  1187. tindexarray
  1188. ****************************************************************************}
  1189. constructor tindexarray.init(Agrowsize:longint);
  1190. begin
  1191. growsize:=Agrowsize;
  1192. size:=0;
  1193. count:=0;
  1194. data:=nil;
  1195. first:=nil;
  1196. end;
  1197. destructor tindexarray.done;
  1198. begin
  1199. if assigned(data) then
  1200. begin
  1201. clear;
  1202. freemem(data,size*4);
  1203. data:=nil;
  1204. end;
  1205. end;
  1206. function tindexarray.search(nr:longint):Pnamedindexobject;
  1207. begin
  1208. if nr<=count then
  1209. search:=data^[nr]
  1210. else
  1211. search:=nil;
  1212. end;
  1213. procedure tindexarray.clear;
  1214. var
  1215. i : longint;
  1216. begin
  1217. for i:=1 to count do
  1218. if assigned(data^[i]) then
  1219. begin
  1220. dispose(data^[i],done);
  1221. data^[i]:=nil;
  1222. end;
  1223. count:=0;
  1224. end;
  1225. procedure tindexarray.foreach(proc2call : Tnamedindexcallback);
  1226. var
  1227. i : longint;
  1228. begin
  1229. for i:=1 to count do
  1230. if assigned(data^[i]) then
  1231. proc2call(data^[i]);
  1232. end;
  1233. procedure tindexarray.grow(gsize:longint);
  1234. var
  1235. osize : longint;
  1236. odata : Pnamedindexobjectarray;
  1237. begin
  1238. osize:=size;
  1239. odata:=data;
  1240. inc(size,gsize);
  1241. getmem(data,size*4);
  1242. if assigned(odata) then
  1243. begin
  1244. move(odata^,data^,osize*4);
  1245. freemem(odata,osize*4);
  1246. end;
  1247. fillchar(data^[osize+1],gsize*4,0);
  1248. end;
  1249. procedure tindexarray.deleteindex(p:Pnamedindexobject);
  1250. var
  1251. i : longint;
  1252. begin
  1253. { update linked list }
  1254. i:=p^.indexnr;
  1255. while (i>0) do
  1256. begin
  1257. dec(i);
  1258. if (i>0) and assigned(data^[i]) then
  1259. begin
  1260. data^[i]^.next:=data^[p^.indexnr]^.next;
  1261. break;
  1262. end;
  1263. end;
  1264. if i=0 then
  1265. first:=p;
  1266. { remove entry }
  1267. p^.next:=nil;
  1268. data^[p^.indexnr]:=nil;
  1269. end;
  1270. procedure tindexarray.delete(p:Pnamedindexobject);
  1271. begin
  1272. deleteindex(p);
  1273. dispose(p,done);
  1274. end;
  1275. procedure tindexarray.insert(p:Pnamedindexobject);
  1276. var
  1277. i : longint;
  1278. begin
  1279. if p^.indexnr=-1 then
  1280. begin
  1281. inc(count);
  1282. p^.indexnr:=count;
  1283. end;
  1284. if p^.indexnr>count then
  1285. count:=p^.indexnr;
  1286. if count>size then
  1287. grow(((count div growsize)+1)*growsize);
  1288. data^[p^.indexnr]:=p;
  1289. { update linked list backward }
  1290. i:=p^.indexnr;
  1291. while (i>0) do
  1292. begin
  1293. dec(i);
  1294. if (i>0) and assigned(data^[i]) then
  1295. begin
  1296. data^[i]^.next:=p;
  1297. break;
  1298. end;
  1299. end;
  1300. if i=0 then
  1301. first:=p;
  1302. { update linked list forward }
  1303. i:=p^.indexnr;
  1304. while (i<=count) do
  1305. begin
  1306. inc(i);
  1307. if (i<=count) and assigned(data^[i]) then
  1308. begin
  1309. p^.next:=data^[i];
  1310. exit;
  1311. end;
  1312. end;
  1313. if i>count then
  1314. p^.next:=nil;
  1315. end;
  1316. {$ifdef BUFFEREDFILE}
  1317. {****************************************************************************
  1318. TBUFFEREDFILE
  1319. ****************************************************************************}
  1320. Const
  1321. crcseed = $ffffffff;
  1322. crctable : array[0..255] of longint = (
  1323. $00000000,$77073096,$ee0e612c,$990951ba,$076dc419,$706af48f,
  1324. $e963a535,$9e6495a3,$0edb8832,$79dcb8a4,$e0d5e91e,$97d2d988,
  1325. $09b64c2b,$7eb17cbd,$e7b82d07,$90bf1d91,$1db71064,$6ab020f2,
  1326. $f3b97148,$84be41de,$1adad47d,$6ddde4eb,$f4d4b551,$83d385c7,
  1327. $136c9856,$646ba8c0,$fd62f97a,$8a65c9ec,$14015c4f,$63066cd9,
  1328. $fa0f3d63,$8d080df5,$3b6e20c8,$4c69105e,$d56041e4,$a2677172,
  1329. $3c03e4d1,$4b04d447,$d20d85fd,$a50ab56b,$35b5a8fa,$42b2986c,
  1330. $dbbbc9d6,$acbcf940,$32d86ce3,$45df5c75,$dcd60dcf,$abd13d59,
  1331. $26d930ac,$51de003a,$c8d75180,$bfd06116,$21b4f4b5,$56b3c423,
  1332. $cfba9599,$b8bda50f,$2802b89e,$5f058808,$c60cd9b2,$b10be924,
  1333. $2f6f7c87,$58684c11,$c1611dab,$b6662d3d,$76dc4190,$01db7106,
  1334. $98d220bc,$efd5102a,$71b18589,$06b6b51f,$9fbfe4a5,$e8b8d433,
  1335. $7807c9a2,$0f00f934,$9609a88e,$e10e9818,$7f6a0dbb,$086d3d2d,
  1336. $91646c97,$e6635c01,$6b6b51f4,$1c6c6162,$856530d8,$f262004e,
  1337. $6c0695ed,$1b01a57b,$8208f4c1,$f50fc457,$65b0d9c6,$12b7e950,
  1338. $8bbeb8ea,$fcb9887c,$62dd1ddf,$15da2d49,$8cd37cf3,$fbd44c65,
  1339. $4db26158,$3ab551ce,$a3bc0074,$d4bb30e2,$4adfa541,$3dd895d7,
  1340. $a4d1c46d,$d3d6f4fb,$4369e96a,$346ed9fc,$ad678846,$da60b8d0,
  1341. $44042d73,$33031de5,$aa0a4c5f,$dd0d7cc9,$5005713c,$270241aa,
  1342. $be0b1010,$c90c2086,$5768b525,$206f85b3,$b966d409,$ce61e49f,
  1343. $5edef90e,$29d9c998,$b0d09822,$c7d7a8b4,$59b33d17,$2eb40d81,
  1344. $b7bd5c3b,$c0ba6cad,$edb88320,$9abfb3b6,$03b6e20c,$74b1d29a,
  1345. $ead54739,$9dd277af,$04db2615,$73dc1683,$e3630b12,$94643b84,
  1346. $0d6d6a3e,$7a6a5aa8,$e40ecf0b,$9309ff9d,$0a00ae27,$7d079eb1,
  1347. $f00f9344,$8708a3d2,$1e01f268,$6906c2fe,$f762575d,$806567cb,
  1348. $196c3671,$6e6b06e7,$fed41b76,$89d32be0,$10da7a5a,$67dd4acc,
  1349. $f9b9df6f,$8ebeeff9,$17b7be43,$60b08ed5,$d6d6a3e8,$a1d1937e,
  1350. $38d8c2c4,$4fdff252,$d1bb67f1,$a6bc5767,$3fb506dd,$48b2364b,
  1351. $d80d2bda,$af0a1b4c,$36034af6,$41047a60,$df60efc3,$a867df55,
  1352. $316e8eef,$4669be79,$cb61b38c,$bc66831a,$256fd2a0,$5268e236,
  1353. $cc0c7795,$bb0b4703,$220216b9,$5505262f,$c5ba3bbe,$b2bd0b28,
  1354. $2bb45a92,$5cb36a04,$c2d7ffa7,$b5d0cf31,$2cd99e8b,$5bdeae1d,
  1355. $9b64c2b0,$ec63f226,$756aa39c,$026d930a,$9c0906a9,$eb0e363f,
  1356. $72076785,$05005713,$95bf4a82,$e2b87a14,$7bb12bae,$0cb61b38,
  1357. $92d28e9b,$e5d5be0d,$7cdcefb7,$0bdbdf21,$86d3d2d4,$f1d4e242,
  1358. $68ddb3f8,$1fda836e,$81be16cd,$f6b9265b,$6fb077e1,$18b74777,
  1359. $88085ae6,$ff0f6a70,$66063bca,$11010b5c,$8f659eff,$f862ae69,
  1360. $616bffd3,$166ccf45,$a00ae278,$d70dd2ee,$4e048354,$3903b3c2,
  1361. $a7672661,$d06016f7,$4969474d,$3e6e77db,$aed16a4a,$d9d65adc,
  1362. $40df0b66,$37d83bf0,$a9bcae53,$debb9ec5,$47b2cf7f,$30b5ffe9,
  1363. $bdbdf21c,$cabac28a,$53b39330,$24b4a3a6,$bad03605,$cdd70693,
  1364. $54de5729,$23d967bf,$b3667a2e,$c4614ab8,$5d681b02,$2a6f2b94,
  1365. $b40bbe37,$c30c8ea1,$5a05df1b,$2d02ef8d);
  1366. constructor tbufferedfile.init(const filename : string;_bufsize : longint);
  1367. begin
  1368. assign(f,filename);
  1369. bufsize:=_bufsize;
  1370. bufpos:=0;
  1371. buflast:=0;
  1372. do_crc:=false;
  1373. iomode:=0;
  1374. tempclosed:=false;
  1375. change_endian:=false;
  1376. clear_crc;
  1377. end;
  1378. destructor tbufferedfile.done;
  1379. begin
  1380. close;
  1381. end;
  1382. procedure tbufferedfile.clear_crc;
  1383. begin
  1384. crc:=crcseed;
  1385. end;
  1386. procedure tbufferedfile.setbuf(p : pchar;s : longint);
  1387. begin
  1388. flush;
  1389. freemem(buf,bufsize);
  1390. bufsize:=s;
  1391. buf:=p;
  1392. end;
  1393. function tbufferedfile.reset:boolean;
  1394. var
  1395. ofm : byte;
  1396. begin
  1397. ofm:=filemode;
  1398. iomode:=1;
  1399. getmem(buf,bufsize);
  1400. filemode:=0;
  1401. {$I-}
  1402. system.reset(f,1);
  1403. {$I+}
  1404. reset:=(ioresult=0);
  1405. filemode:=ofm;
  1406. end;
  1407. procedure tbufferedfile.rewrite;
  1408. begin
  1409. iomode:=2;
  1410. getmem(buf,bufsize);
  1411. system.rewrite(f,1);
  1412. end;
  1413. procedure tbufferedfile.flush;
  1414. var
  1415. {$ifdef FPC}
  1416. count : longint;
  1417. {$else}
  1418. count : integer;
  1419. {$endif}
  1420. begin
  1421. if iomode=2 then
  1422. begin
  1423. if bufpos=0 then
  1424. exit;
  1425. blockwrite(f,buf^,bufpos)
  1426. end
  1427. else if iomode=1 then
  1428. if buflast=bufpos then
  1429. begin
  1430. blockread(f,buf^,bufsize,count);
  1431. buflast:=count;
  1432. end;
  1433. bufpos:=0;
  1434. end;
  1435. function tbufferedfile.getftime : longint;
  1436. var
  1437. l : longint;
  1438. {$ifdef linux}
  1439. Info : Stat;
  1440. {$endif}
  1441. begin
  1442. {$ifndef linux}
  1443. { this only works if the file is open !! }
  1444. dos.getftime(f,l);
  1445. {$else}
  1446. Fstat(f,Info);
  1447. l:=info.mtime;
  1448. {$endif}
  1449. getftime:=l;
  1450. end;
  1451. function tbufferedfile.getsize : longint;
  1452. begin
  1453. getsize:=filesize(f);
  1454. end;
  1455. procedure tbufferedfile.seek(l : longint);
  1456. begin
  1457. if iomode=2 then
  1458. begin
  1459. flush;
  1460. system.seek(f,l);
  1461. end
  1462. else if iomode=1 then
  1463. begin
  1464. { forces a reload }
  1465. bufpos:=buflast;
  1466. system.seek(f,l);
  1467. flush;
  1468. end;
  1469. end;
  1470. type
  1471. {$ifdef tp}
  1472. bytearray1 = array [1..65535] of byte;
  1473. {$else}
  1474. bytearray1 = array [1..10000000] of byte;
  1475. {$endif}
  1476. procedure tbufferedfile.read_data(var data;bytes : longint;var count : longint);
  1477. var
  1478. p : pchar;
  1479. c,i : longint;
  1480. begin
  1481. p:=pchar(@data);
  1482. count:=0;
  1483. while bytes-count>0 do
  1484. begin
  1485. if bytes-count>buflast-bufpos then
  1486. begin
  1487. move((buf+bufpos)^,(p+count)^,buflast-bufpos);
  1488. inc(count,buflast-bufpos);
  1489. bufpos:=buflast;
  1490. flush;
  1491. { can't we read anything ? }
  1492. if bufpos=buflast then
  1493. break;
  1494. end
  1495. else
  1496. begin
  1497. move((buf+bufpos)^,(p+count)^,bytes-count);
  1498. inc(bufpos,bytes-count);
  1499. count:=bytes;
  1500. break;
  1501. end;
  1502. end;
  1503. if do_crc then
  1504. begin
  1505. c:=crc;
  1506. for i:=1 to bytes do
  1507. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1508. crc:=c;
  1509. end;
  1510. end;
  1511. procedure tbufferedfile.write_data(var data;count : longint);
  1512. var
  1513. c,i : longint;
  1514. begin
  1515. if bufpos+count>bufsize then
  1516. flush;
  1517. move(data,(buf+bufpos)^,count);
  1518. inc(bufpos,count);
  1519. if do_crc then
  1520. begin
  1521. c:=crc;
  1522. for i:=1 to count do
  1523. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1524. crc:=c;
  1525. end;
  1526. end;
  1527. function tbufferedfile.getcrc : longint;
  1528. begin
  1529. getcrc:=crc xor crcseed;
  1530. end;
  1531. procedure tbufferedfile.write_string(const s : string);
  1532. begin
  1533. if bufpos+length(s)>bufsize then
  1534. flush;
  1535. { why is there not CRC here ??? }
  1536. move(s[1],(buf+bufpos)^,length(s));
  1537. inc(bufpos,length(s));
  1538. { should be
  1539. write_data(s[1],length(s)); }
  1540. end;
  1541. procedure tbufferedfile.write_pchar(p : pchar);
  1542. var
  1543. l : longint;
  1544. begin
  1545. l:=strlen(p);
  1546. if l>=bufsize then
  1547. runerror(222);
  1548. { why is there not CRC here ???}
  1549. if bufpos+l>bufsize then
  1550. flush;
  1551. move(p^,(buf+bufpos)^,l);
  1552. inc(bufpos,l);
  1553. { should be
  1554. write_data(p^,l); }
  1555. end;
  1556. procedure tbufferedfile.write_byte(b : byte);
  1557. begin
  1558. write_data(b,sizeof(byte));
  1559. end;
  1560. procedure tbufferedfile.write_long(l : longint);
  1561. var
  1562. w1,w2 : word;
  1563. begin
  1564. if change_endian then
  1565. begin
  1566. w1:=l and $ffff;
  1567. w2:=l shr 16;
  1568. l:=swap(w2)+(longint(swap(w1)) shl 16);
  1569. write_data(l,sizeof(longint))
  1570. end
  1571. else
  1572. write_data(l,sizeof(longint))
  1573. end;
  1574. procedure tbufferedfile.write_word(w : word);
  1575. begin
  1576. if change_endian then
  1577. begin
  1578. w:=swap(w);
  1579. write_data(w,sizeof(word))
  1580. end
  1581. else
  1582. write_data(w,sizeof(word));
  1583. end;
  1584. procedure tbufferedfile.write_double(d : double);
  1585. begin
  1586. write_data(d,sizeof(double));
  1587. end;
  1588. function tbufferedfile.getpath : string;
  1589. begin
  1590. {$ifdef dummy}
  1591. getpath:=strpas(filerec(f).name);
  1592. {$endif}
  1593. getpath:='';
  1594. end;
  1595. procedure tbufferedfile.close;
  1596. begin
  1597. if iomode<>0 then
  1598. begin
  1599. flush;
  1600. system.close(f);
  1601. freemem(buf,bufsize);
  1602. buf:=nil;
  1603. iomode:=0;
  1604. end;
  1605. end;
  1606. procedure tbufferedfile.tempclose;
  1607. begin
  1608. if iomode<>0 then
  1609. begin
  1610. temppos:=system.filepos(f);
  1611. tempmode:=iomode;
  1612. tempclosed:=true;
  1613. system.close(f);
  1614. iomode:=0;
  1615. end
  1616. else
  1617. tempclosed:=false;
  1618. end;
  1619. procedure tbufferedfile.tempreopen;
  1620. var
  1621. ofm : byte;
  1622. begin
  1623. if tempclosed then
  1624. begin
  1625. case tempmode of
  1626. 1 : begin
  1627. ofm:=filemode;
  1628. iomode:=1;
  1629. filemode:=0;
  1630. system.reset(f,1);
  1631. filemode:=ofm;
  1632. end;
  1633. 2 : begin
  1634. iomode:=2;
  1635. system.rewrite(f,1);
  1636. end;
  1637. end;
  1638. system.seek(f,temppos);
  1639. tempclosed:=false;
  1640. end;
  1641. end;
  1642. {$endif BUFFEREDFILE}
  1643. end.
  1644. {
  1645. $Log$
  1646. Revision 1.28 1999-05-05 10:05:48 florian
  1647. * a delphi compiled compiler recompiles ppc
  1648. Revision 1.27 1999/05/05 09:19:03 florian
  1649. * more fixes to get it with delphi running
  1650. Revision 1.26 1999/04/21 09:43:31 peter
  1651. * storenumber works
  1652. * fixed some typos in double_checksum
  1653. + incompatible types type1 and type2 message (with storenumber)
  1654. Revision 1.25 1999/04/15 10:01:44 peter
  1655. * small update for storenumber
  1656. Revision 1.24 1999/04/14 09:14:47 peter
  1657. * first things to store the symbol/def number in the ppu
  1658. Revision 1.23 1999/04/08 20:59:39 florian
  1659. * fixed problem with default properties which are a class
  1660. * case bug (from the mailing list with -O2) fixed, the
  1661. distance of the case labels can be greater than the positive
  1662. range of a longint => it is now a dword for fpc
  1663. Revision 1.22 1999/03/31 13:55:10 peter
  1664. * assembler inlining working for ag386bin
  1665. Revision 1.21 1999/03/19 16:35:29 pierre
  1666. * Tnamedindexobject done also removed left and right
  1667. Revision 1.20 1999/03/18 20:30:45 peter
  1668. + .a writer
  1669. Revision 1.19 1999/03/01 13:32:00 pierre
  1670. * external used before implemented problem fixed
  1671. Revision 1.18 1999/02/24 00:59:13 peter
  1672. * small updates for ag386bin
  1673. Revision 1.17 1999/01/19 11:00:33 daniel
  1674. + Tdictionary object: Tsymtable will become object(TTdictionary) in the
  1675. future
  1676. + Tnamed_item object: Tsym will become object(Tnamed_item) in the future
  1677. Revision 1.16 1998/11/04 10:11:37 peter
  1678. * ansistring fixes
  1679. Revision 1.15 1998/10/19 18:04:40 peter
  1680. + tstringcontainer.init_no_doubles
  1681. Revision 1.14 1998/09/18 16:03:37 florian
  1682. * some changes to compile with Delphi
  1683. Revision 1.13 1998/08/12 19:28:16 peter
  1684. * better libc support
  1685. Revision 1.12 1998/07/14 14:46:47 peter
  1686. * released NEWINPUT
  1687. Revision 1.11 1998/07/07 11:19:54 peter
  1688. + NEWINPUT for a better inputfile and scanner object
  1689. Revision 1.10 1998/07/01 15:26:59 peter
  1690. * better bufferfile.reset error handling
  1691. Revision 1.9 1998/06/03 23:40:37 peter
  1692. + unlimited file support, release tempclose
  1693. Revision 1.8 1998/05/20 09:42:33 pierre
  1694. + UseTokenInfo now default
  1695. * unit in interface uses and implementation uses gives error now
  1696. * only one error for unknown symbol (uses lastsymknown boolean)
  1697. the problem came from the label code !
  1698. + first inlined procedures and function work
  1699. (warning there might be allowed cases were the result is still wrong !!)
  1700. * UseBrower updated gives a global list of all position of all used symbols
  1701. with switch -gb
  1702. Revision 1.7 1998/05/06 18:36:53 peter
  1703. * tai_section extended with code,data,bss sections and enumerated type
  1704. * ident 'compiled by FPC' moved to pmodules
  1705. * small fix for smartlink
  1706. Revision 1.6 1998/05/06 08:38:37 pierre
  1707. * better position info with UseTokenInfo
  1708. UseTokenInfo greatly simplified
  1709. + added check for changed tree after first time firstpass
  1710. (if we could remove all the cases were it happen
  1711. we could skip all firstpass if firstpasscount > 1)
  1712. Only with ExtDebug
  1713. Revision 1.5 1998/04/30 15:59:40 pierre
  1714. * GDB works again better :
  1715. correct type info in one pass
  1716. + UseTokenInfo for better source position
  1717. * fixed one remaining bug in scanner for line counts
  1718. * several little fixes
  1719. Revision 1.4 1998/04/29 10:33:50 pierre
  1720. + added some code for ansistring (not complete nor working yet)
  1721. * corrected operator overloading
  1722. * corrected nasm output
  1723. + started inline procedures
  1724. + added starstarn : use ** for exponentiation (^ gave problems)
  1725. + started UseTokenInfo cond to get accurate positions
  1726. Revision 1.3 1998/04/27 23:10:28 peter
  1727. + new scanner
  1728. * $makelib -> if smartlink
  1729. * small filename fixes pmodule.setfilename
  1730. * moved import from files.pas -> import.pas
  1731. Revision 1.2 1998/04/07 11:09:04 peter
  1732. + filemode is set correct in tbufferedfile.reset
  1733. }