cobjects.pas 52 KB

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