2
0

cobjects.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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. pfileposinfo = ^tfileposinfo;
  39. tfileposinfo = record
  40. line : longint;
  41. column : word;
  42. fileindex : word;
  43. end;
  44. { some help data types }
  45. pstringitem = ^tstringitem;
  46. tstringitem = record
  47. data : pstring;
  48. next : pstringitem;
  49. fileinfo : tfileposinfo; { pointer to tinputfile }
  50. end;
  51. plinkedlist_item = ^tlinkedlist_item;
  52. tlinkedlist_item = object
  53. next,previous : plinkedlist_item;
  54. { does nothing }
  55. constructor init;
  56. destructor done;virtual;
  57. end;
  58. pstring_item = ^tstring_item;
  59. tstring_item = object(tlinkedlist_item)
  60. str : pstring;
  61. constructor init(const s : string);
  62. destructor done;virtual;
  63. end;
  64. { this implements a double linked list }
  65. plinkedlist = ^tlinkedlist;
  66. tlinkedlist = object
  67. first,last : plinkedlist_item;
  68. constructor init;
  69. destructor done;
  70. { disposes the items of the list }
  71. procedure clear;
  72. { concats a new item at the end }
  73. procedure concat(p : plinkedlist_item);
  74. { inserts a new item at the begin }
  75. procedure insert(p : plinkedlist_item);
  76. { inserts another list at the begin and make this list empty }
  77. procedure insertlist(p : plinkedlist);
  78. { concats another list at the end and make this list empty }
  79. procedure concatlist(p : plinkedlist);
  80. { removes p from the list (p isn't disposed) }
  81. { it's not tested if p is in the list ! }
  82. procedure remove(p : plinkedlist_item);
  83. { is the linkedlist empty ? }
  84. function empty:boolean;
  85. end;
  86. { String Queue}
  87. PStringQueue=^TStringQueue;
  88. TStringQueue=object
  89. first,last : PStringItem;
  90. constructor Init;
  91. destructor Done;
  92. function Empty:boolean;
  93. function Get:string;
  94. procedure Insert(const s:string);
  95. procedure Concat(const s:string);
  96. procedure Clear;
  97. end;
  98. { string container }
  99. pstringcontainer = ^tstringcontainer;
  100. tstringcontainer = object
  101. root,
  102. last : pstringitem;
  103. doubles : boolean; { if this is set to true, doubles are allowed }
  104. constructor init;
  105. constructor init_no_double;
  106. destructor done;
  107. { true when the container is empty }
  108. function empty:boolean;
  109. { inserts a string }
  110. procedure insert(const s : string);
  111. procedure insert_with_tokeninfo(const s : string;const file_info : tfileposinfo);
  112. { gets a string }
  113. function get : string;
  114. function get_with_tokeninfo(var file_info : tfileposinfo) : string;
  115. { true if string is in the container }
  116. function find(const s:string):boolean;
  117. { deletes all strings }
  118. procedure clear;
  119. end;
  120. Pnamed_object=^Tnamed_object;
  121. Pdictionary=^Tdictionary;
  122. Pdictionaryhasharray=^Tdictionaryhasharray;
  123. Tdictionaryhasharray=array[0..hasharraysize-1] of Pnamed_object;
  124. Tcallback = procedure(p:Pnamed_object);
  125. Tdictionary=object
  126. root:Pnamed_object;
  127. hasharray:Pdictionaryhasharray;
  128. replace_existing : boolean;
  129. constructor init(usehash:boolean);
  130. procedure clear;virtual;
  131. procedure foreach(proc2call:Tcallback);
  132. function insert(obj:Pnamed_object):Pnamed_object;virtual;
  133. function search(const s:string):Pnamed_object;
  134. function speedsearch(const s:string;
  135. speedvalue:longint):Pnamed_object;virtual;
  136. destructor done;virtual;
  137. end;
  138. Tnamed_object=object
  139. _name:Pstring;
  140. left,right:Pnamed_object;
  141. speedvalue:longint;
  142. owner:Pdictionary;
  143. constructor init(const n:string);
  144. destructor done;virtual;
  145. function name:string;
  146. end;
  147. {$ifdef BUFFEREDFILE}
  148. { this is implemented to allow buffered binary I/O }
  149. pbufferedfile = ^tbufferedfile;
  150. tbufferedfile = object
  151. f : file;
  152. buf : pchar;
  153. bufsize,buflast,bufpos : longint;
  154. { 0 closed, 1 input, 2 output }
  155. iomode : byte;
  156. { true, if the compile should change the endian of the output }
  157. change_endian : boolean;
  158. { calcules a crc for the file, }
  159. { but it's assumed, that there no seek while do_crc is true }
  160. do_crc : boolean;
  161. crc : longint;
  162. { temporary closing feature }
  163. tempclosed : boolean;
  164. tempmode : byte;
  165. temppos : longint;
  166. { inits a buffer with the size bufsize which is assigned to }
  167. { the file filename }
  168. constructor init(const filename : string;_bufsize : longint);
  169. { closes the file, if needed, and releases the memory }
  170. destructor done;virtual;
  171. { opens the file for input, other accesses are rejected }
  172. function reset:boolean;
  173. { opens the file for output, other accesses are rejected }
  174. procedure rewrite;
  175. { reads or writes the buffer from or to disk }
  176. procedure flush;
  177. { writes a string to the file }
  178. { the string is written without a length byte }
  179. procedure write_string(const s : string);
  180. { writes a zero terminated string }
  181. procedure write_pchar(p : pchar);
  182. { write specific data types, takes care of }
  183. { byte order }
  184. procedure write_byte(b : byte);
  185. procedure write_word(w : word);
  186. procedure write_long(l : longint);
  187. procedure write_double(d : double);
  188. { writes any data }
  189. procedure write_data(var data;count : longint);
  190. { reads any data }
  191. procedure read_data(var data;bytes : longint;var count : longint);
  192. { closes the file and releases the buffer }
  193. procedure close;
  194. { temporary closing }
  195. procedure tempclose;
  196. procedure tempreopen;
  197. { goto the given position }
  198. procedure seek(l : longint);
  199. { installes an user defined buffer }
  200. { and releases the old one, but be }
  201. { careful, if the old buffer contains }
  202. { data, this data is lost }
  203. procedure setbuf(p : pchar;s : longint);
  204. { reads the file time stamp of the file, }
  205. { the file must be opened }
  206. function getftime : longint;
  207. { returns filesize }
  208. function getsize : longint;
  209. { returns the path }
  210. function getpath : string;
  211. { resets the crc }
  212. procedure clear_crc;
  213. { returns the crc }
  214. function getcrc : longint;
  215. end;
  216. {$endif BUFFEREDFILE}
  217. { releases the string p and assignes nil to p }
  218. { if p=nil then freemem isn't called }
  219. procedure stringdispose(var p : pstring);
  220. { idem for ansistrings }
  221. procedure ansistringdispose(var p : pchar;length : longint);
  222. { allocates mem for a copy of s, copies s to this mem and returns }
  223. { a pointer to this mem }
  224. function stringdup(const s : string) : pstring;
  225. { allocates memory for s and copies s as zero terminated string
  226. to that mem and returns a pointer to that mem }
  227. function strpnew(const s : string) : pchar;
  228. { makes a char lowercase, with spanish, french and german char set }
  229. function lowercase(c : char) : char;
  230. { makes zero terminated string to a pascal string }
  231. { the data in p is modified and p is returned }
  232. function pchar2pstring(p : pchar) : pstring;
  233. { ambivalent to pchar2pstring }
  234. function pstring2pchar(p : pstring) : pchar;
  235. implementation
  236. {$ifdef FPC}
  237. function getspeedvalue(const s : string) : longint;
  238. var
  239. p1,p2:^byte;
  240. begin
  241. p1:=@s;
  242. longint(p2):=longint(p1)+p1^+1;
  243. inc(longint(p1));
  244. getspeedvalue:=0;
  245. while p1<>p2 do
  246. begin
  247. inc(getspeedvalue,p1^);
  248. inc(longint(p1));
  249. end;
  250. end;
  251. {$else}
  252. function getspeedvalue(const s : string) : longint;
  253. type
  254. ptrrec=record
  255. ofs,seg:word;
  256. end;
  257. var
  258. l,w : longint;
  259. p1,p2 : ^byte;
  260. begin
  261. p1:=@s;
  262. ptrrec(p2).seg:=ptrrec(p1).seg;
  263. ptrrec(p2).ofs:=ptrrec(p1).ofs+p1^+1;
  264. inc(p1);
  265. l:=0;
  266. while p1<>p2 do
  267. begin
  268. l:=l+p1^;
  269. inc(p1);
  270. end;
  271. getspeedvalue:=l;
  272. end;
  273. {$endif}
  274. function pchar2pstring(p : pchar) : pstring;
  275. var
  276. w,i : longint;
  277. begin
  278. w:=strlen(p);
  279. for i:=w-1 downto 0 do
  280. p[i+1]:=p[i];
  281. p[0]:=chr(w);
  282. pchar2pstring:=pstring(p);
  283. end;
  284. function pstring2pchar(p : pstring) : pchar;
  285. var
  286. w,i : longint;
  287. begin
  288. w:=length(p^);
  289. for i:=1 to w do
  290. p^[i-1]:=p^[i];
  291. p^[w]:=#0;
  292. pstring2pchar:=pchar(p);
  293. end;
  294. function lowercase(c : char) : char;
  295. begin
  296. case c of
  297. #65..#90 : c := chr(ord (c) + 32);
  298. #154 : c:=#129; { german }
  299. #142 : c:=#132; { german }
  300. #153 : c:=#148; { german }
  301. #144 : c:=#130; { french }
  302. #128 : c:=#135; { french }
  303. #143 : c:=#134; { swedish/norge (?) }
  304. #165 : c:=#164; { spanish }
  305. #228 : c:=#229; { greek }
  306. #226 : c:=#231; { greek }
  307. #232 : c:=#227; { greek }
  308. end;
  309. lowercase := c;
  310. end;
  311. function strpnew(const s : string) : pchar;
  312. var
  313. p : pchar;
  314. begin
  315. getmem(p,length(s)+1);
  316. strpcopy(p,s);
  317. strpnew:=p;
  318. end;
  319. procedure stringdispose(var p : pstring);
  320. begin
  321. if assigned(p) then
  322. freemem(p,length(p^)+1);
  323. p:=nil;
  324. end;
  325. procedure ansistringdispose(var p : pchar;length : longint);
  326. begin
  327. if assigned(p) then
  328. freemem(p,length+1);
  329. p:=nil;
  330. end;
  331. function stringdup(const s : string) : pstring;
  332. var
  333. p : pstring;
  334. begin
  335. getmem(p,length(s)+1);
  336. p^:=s;
  337. stringdup:=p;
  338. end;
  339. {****************************************************************************
  340. TStringQueue
  341. ****************************************************************************}
  342. constructor TStringQueue.Init;
  343. begin
  344. first:=nil;
  345. end;
  346. function TStringQueue.Empty:boolean;
  347. begin
  348. Empty:=(first=nil);
  349. end;
  350. function TStringQueue.Get:string;
  351. var
  352. hp : pstringitem;
  353. begin
  354. if first=nil then
  355. begin
  356. Get:='';
  357. exit;
  358. end;
  359. Get:=first^.data^;
  360. stringdispose(first^.data);
  361. hp:=first;
  362. first:=first^.next;
  363. dispose(hp);
  364. end;
  365. procedure TStringQueue.Insert(const s:string);
  366. var
  367. hp : pstringitem;
  368. begin
  369. new(hp);
  370. hp^.next:=first;
  371. hp^.data:=stringdup(s);
  372. first:=hp;
  373. if last=nil then
  374. last:=hp;
  375. end;
  376. procedure TStringQueue.Concat(const s:string);
  377. var
  378. hp : pstringitem;
  379. begin
  380. new(hp);
  381. hp^.next:=nil;
  382. hp^.data:=stringdup(s);
  383. if first=nil then
  384. first:=hp
  385. else
  386. last^.next:=hp;
  387. last:=hp;
  388. end;
  389. procedure TStringQueue.Clear;
  390. var
  391. hp : pstringitem;
  392. begin
  393. while (first<>nil) do
  394. begin
  395. hp:=first;
  396. stringdispose(first^.data);
  397. first:=first^.next;
  398. dispose(hp);
  399. end;
  400. end;
  401. destructor TStringQueue.Done;
  402. begin
  403. Clear;
  404. end;
  405. {****************************************************************************
  406. TSTRINGCONTAINER
  407. ****************************************************************************}
  408. constructor tstringcontainer.init;
  409. begin
  410. root:=nil;
  411. last:=nil;
  412. doubles:=true;
  413. end;
  414. constructor tstringcontainer.init_no_double;
  415. begin
  416. root:=nil;
  417. last:=nil;
  418. doubles:=false;
  419. end;
  420. destructor tstringcontainer.done;
  421. begin
  422. clear;
  423. end;
  424. function tstringcontainer.empty:boolean;
  425. begin
  426. empty:=(root=nil);
  427. end;
  428. procedure tstringcontainer.insert(const s : string);
  429. var
  430. hp : pstringitem;
  431. begin
  432. if not(doubles) then
  433. begin
  434. hp:=root;
  435. while assigned(hp) do
  436. begin
  437. if hp^.data^=s then exit;
  438. hp:=hp^.next;
  439. end;
  440. end;
  441. new(hp);
  442. hp^.next:=nil;
  443. hp^.data:=stringdup(s);
  444. if root=nil then root:=hp
  445. else last^.next:=hp;
  446. last:=hp;
  447. end;
  448. procedure tstringcontainer.insert_with_tokeninfo(const s : string; const file_info : tfileposinfo);
  449. var
  450. hp : pstringitem;
  451. begin
  452. if not(doubles) then
  453. begin
  454. hp:=root;
  455. while assigned(hp) do
  456. begin
  457. if hp^.data^=s then exit;
  458. hp:=hp^.next;
  459. end;
  460. end;
  461. new(hp);
  462. hp^.next:=nil;
  463. hp^.data:=stringdup(s);
  464. hp^.fileinfo:=file_info;
  465. if root=nil then root:=hp
  466. else last^.next:=hp;
  467. last:=hp;
  468. end;
  469. procedure tstringcontainer.clear;
  470. var
  471. hp : pstringitem;
  472. begin
  473. hp:=root;
  474. while assigned(hp) do
  475. begin
  476. stringdispose(hp^.data);
  477. root:=hp^.next;
  478. dispose(hp);
  479. hp:=root;
  480. end;
  481. last:=nil;
  482. root:=nil;
  483. end;
  484. function tstringcontainer.get : string;
  485. var
  486. hp : pstringitem;
  487. begin
  488. if root=nil then
  489. get:=''
  490. else
  491. begin
  492. get:=root^.data^;
  493. hp:=root;
  494. root:=root^.next;
  495. stringdispose(hp^.data);
  496. dispose(hp);
  497. end;
  498. end;
  499. function tstringcontainer.get_with_tokeninfo(var file_info : tfileposinfo) : string;
  500. var
  501. hp : pstringitem;
  502. begin
  503. if root=nil then
  504. begin
  505. get_with_tokeninfo:='';
  506. file_info.fileindex:=0;
  507. file_info.line:=0;
  508. file_info.column:=0;
  509. end
  510. else
  511. begin
  512. get_with_tokeninfo:=root^.data^;
  513. hp:=root;
  514. root:=root^.next;
  515. stringdispose(hp^.data);
  516. file_info:=hp^.fileinfo;
  517. dispose(hp);
  518. end;
  519. end;
  520. function tstringcontainer.find(const s:string):boolean;
  521. var
  522. hp : pstringitem;
  523. begin
  524. find:=false;
  525. hp:=root;
  526. while assigned(hp) do
  527. begin
  528. if hp^.data^=s then
  529. begin
  530. find:=true;
  531. exit;
  532. end;
  533. hp:=hp^.next;
  534. end;
  535. end;
  536. {****************************************************************************
  537. TLINKEDLIST_ITEM
  538. ****************************************************************************}
  539. constructor tlinkedlist_item.init;
  540. begin
  541. previous:=nil;
  542. next:=nil;
  543. end;
  544. destructor tlinkedlist_item.done;
  545. begin
  546. end;
  547. {****************************************************************************
  548. TSTRING_ITEM
  549. ****************************************************************************}
  550. constructor tstring_item.init(const s : string);
  551. begin
  552. str:=stringdup(s);
  553. end;
  554. destructor tstring_item.done;
  555. begin
  556. stringdispose(str);
  557. inherited done;
  558. end;
  559. {****************************************************************************
  560. TLINKEDLIST
  561. ****************************************************************************}
  562. constructor tlinkedlist.init;
  563. begin
  564. first:=nil;
  565. last:=nil;
  566. end;
  567. destructor tlinkedlist.done;
  568. begin
  569. clear;
  570. end;
  571. procedure tlinkedlist.clear;
  572. var
  573. hp : plinkedlist_item;
  574. begin
  575. hp:=first;
  576. while assigned(hp) do
  577. begin
  578. first:=hp^.next;
  579. dispose(hp,done);
  580. hp:=first;
  581. end;
  582. end;
  583. procedure tlinkedlist.insertlist(p : plinkedlist);
  584. begin
  585. { empty list ? }
  586. if not(assigned(p^.first)) then
  587. exit;
  588. p^.last^.next:=first;
  589. { we have a double linked list }
  590. if assigned(first) then
  591. first^.previous:=p^.last;
  592. first:=p^.first;
  593. if not(assigned(last)) then
  594. last:=p^.last;
  595. { p becomes empty }
  596. p^.first:=nil;
  597. p^.last:=nil;
  598. end;
  599. procedure tlinkedlist.concat(p : plinkedlist_item);
  600. begin
  601. p^.previous:=nil;
  602. p^.next:=nil;
  603. if not(assigned(first)) then
  604. first:=p
  605. else
  606. begin
  607. last^.next:=p;
  608. p^.previous:=last;
  609. end;
  610. last:=p;
  611. end;
  612. procedure tlinkedlist.insert(p : plinkedlist_item);
  613. begin
  614. p^.previous:=nil;
  615. p^.next:=nil;
  616. if not(assigned(first)) then
  617. last:=p
  618. else
  619. begin
  620. first^.previous:=p;
  621. p^.next:=first;
  622. first:=p;
  623. end;
  624. first:=p;
  625. end;
  626. procedure tlinkedlist.remove(p : plinkedlist_item);
  627. begin
  628. if not(assigned(p)) then
  629. exit;
  630. if (first=p) and (last=p) then
  631. begin
  632. first:=nil;
  633. last:=nil;
  634. end
  635. else if first=p then
  636. begin
  637. first:=p^.next;
  638. if assigned(first) then
  639. first^.previous:=nil;
  640. end
  641. else if last=p then
  642. begin
  643. last:=last^.previous;
  644. if assigned(last) then
  645. last^.next:=nil;
  646. end
  647. else
  648. begin
  649. p^.previous^.next:=p^.next;
  650. p^.next^.previous:=p^.previous;
  651. end;
  652. p^.next:=nil;
  653. p^.previous:=nil;
  654. end;
  655. procedure tlinkedlist.concatlist(p : plinkedlist);
  656. begin
  657. if not(assigned(p^.first)) then
  658. exit;
  659. if not(assigned(first)) then
  660. first:=p^.first
  661. else
  662. begin
  663. last^.next:=p^.first;
  664. p^.first^.previous:=last;
  665. end;
  666. last:=p^.last;
  667. { make p empty }
  668. p^.last:=nil;
  669. p^.first:=nil;
  670. end;
  671. function tlinkedlist.empty:boolean;
  672. begin
  673. empty:=(first=nil);
  674. end;
  675. {****************************************************************************
  676. TNAMED_OBJECT
  677. ****************************************************************************}
  678. constructor Tnamed_object.init(const n:string);
  679. begin
  680. left:=nil;
  681. right:=nil;
  682. _name:=stringdup(n);
  683. speedvalue:=getspeedvalue(n);
  684. end;
  685. destructor Tnamed_object.done;
  686. begin
  687. stringdispose(_name);
  688. end;
  689. function Tnamed_object.name:string;
  690. begin
  691. name:=_name^;
  692. end;
  693. {****************************************************************************
  694. TDICTIONARY
  695. ****************************************************************************}
  696. constructor Tdictionary.init(usehash:boolean);
  697. begin
  698. root:=nil;
  699. hasharray:=nil;
  700. replace_existing:=false;
  701. if usehash then
  702. begin
  703. new(hasharray);
  704. fillchar(hasharray^,sizeof(hasharray^),0);
  705. end;
  706. end;
  707. procedure Tdictionary.clear;
  708. var w:longint;
  709. begin
  710. {remove no entry from a withsymtable as it is only a pointer to the
  711. recorddef or objectdef symtable }
  712. { remove all entry from a symbol table }
  713. if assigned(root) then
  714. dispose(root,done);
  715. if assigned(hasharray) then
  716. for w:=0 to hasharraysize-1 do
  717. if assigned(hasharray^[w]) then
  718. begin
  719. dispose(hasharray^[w],done);
  720. hasharray^[w]:=nil;
  721. end;
  722. end;
  723. procedure Tdictionary.foreach(proc2call:Tcallback);
  724. procedure a(p:Pnamed_object);
  725. { must be preorder, because it's used by reading in }
  726. { a PPU file }
  727. begin
  728. proc2call(p);
  729. if assigned(p^.left) then
  730. a(p^.left);
  731. if assigned(p^.right) then
  732. a(p^.right);
  733. end;
  734. var i:longint;
  735. begin
  736. if assigned(hasharray) then
  737. begin
  738. for i:=0 to hasharraysize-1 do
  739. if assigned(hasharray^[i]) then
  740. a(hasharray^[i]);
  741. end
  742. else
  743. if assigned(root) then
  744. a(root);
  745. end;
  746. function Tdictionary.insert(obj:Pnamed_object):Pnamed_object;
  747. function _insert(var osym:Pnamed_object):Pnamed_object;
  748. {To prevent TP from allocating temp space for temp strings, we allocate
  749. some temp strings manually. We can use two temp strings, plus a third
  750. one that TP adds, where TP alone needs five temp strings!. Storing
  751. these on the heap saves even more, totally 1016 bytes per recursion!}
  752. var s1,s2:^string;
  753. begin
  754. if osym=nil then
  755. begin
  756. osym:=obj;
  757. _insert:=osym;
  758. end
  759. { first check speedvalue, to allow a fast insert }
  760. else
  761. if osym^.speedvalue>obj^.speedvalue then
  762. _insert:=_insert(osym^.right)
  763. else
  764. if osym^.speedvalue<obj^.speedvalue then
  765. _insert:=_insert(osym^.left)
  766. else
  767. begin
  768. new(s1);
  769. new(s2);
  770. s1^:=osym^._name^;
  771. s2^:=obj^._name^;
  772. if s1^>s2^ then
  773. begin
  774. dispose(s2);
  775. dispose(s1);
  776. _insert:=_insert(osym^.right);
  777. end
  778. else
  779. if s1^<s2^ then
  780. begin
  781. dispose(s2);
  782. dispose(s1);
  783. _insert:=_insert(osym^.left);
  784. end
  785. else
  786. begin
  787. dispose(s2);
  788. dispose(s1);
  789. if replace_existing and
  790. assigned(osym) then
  791. begin
  792. obj^.left:=osym^.left;
  793. obj^.right:=osym^.right;
  794. osym:=obj;
  795. _insert:=obj;
  796. end
  797. else
  798. _insert:=osym;
  799. end;
  800. end;
  801. end;
  802. begin
  803. obj^.owner:=@self;
  804. obj^.speedvalue:=getspeedvalue(obj^._name^);
  805. if assigned(hasharray) then
  806. insert:=_insert(hasharray^[obj^.speedvalue mod hasharraysize])
  807. else
  808. insert:=_insert(root);
  809. end;
  810. function Tdictionary.search(const s:string):Pnamed_object;
  811. begin
  812. search:=speedsearch(s,getspeedvalue(s));
  813. end;
  814. function Tdictionary.speedsearch(const s:string;
  815. speedvalue:longint):Pnamed_object;
  816. var hp:Pnamed_object;
  817. begin
  818. if assigned(hasharray) then
  819. hp:=hasharray^[speedvalue mod hasharraysize]
  820. else
  821. hp:=root;
  822. while assigned(hp) do
  823. begin
  824. if speedvalue>hp^.speedvalue then
  825. hp:=hp^.left
  826. else
  827. if speedvalue<hp^.speedvalue then
  828. hp:=hp^.right
  829. else
  830. begin
  831. if (hp^._name^=s) then
  832. begin
  833. speedsearch:=hp;
  834. exit;
  835. end
  836. else
  837. if s>hp^._name^ then
  838. hp:=hp^.left
  839. else
  840. hp:=hp^.right;
  841. end;
  842. end;
  843. speedsearch:=nil;
  844. end;
  845. destructor Tdictionary.done;
  846. begin
  847. clear;
  848. if assigned(hasharray) then
  849. dispose(hasharray);
  850. end;
  851. {$ifdef BUFFEREDFILE}
  852. {****************************************************************************
  853. TBUFFEREDFILE
  854. ****************************************************************************}
  855. Const
  856. crcseed = $ffffffff;
  857. crctable : array[0..255] of longint = (
  858. $00000000,$77073096,$ee0e612c,$990951ba,$076dc419,$706af48f,
  859. $e963a535,$9e6495a3,$0edb8832,$79dcb8a4,$e0d5e91e,$97d2d988,
  860. $09b64c2b,$7eb17cbd,$e7b82d07,$90bf1d91,$1db71064,$6ab020f2,
  861. $f3b97148,$84be41de,$1adad47d,$6ddde4eb,$f4d4b551,$83d385c7,
  862. $136c9856,$646ba8c0,$fd62f97a,$8a65c9ec,$14015c4f,$63066cd9,
  863. $fa0f3d63,$8d080df5,$3b6e20c8,$4c69105e,$d56041e4,$a2677172,
  864. $3c03e4d1,$4b04d447,$d20d85fd,$a50ab56b,$35b5a8fa,$42b2986c,
  865. $dbbbc9d6,$acbcf940,$32d86ce3,$45df5c75,$dcd60dcf,$abd13d59,
  866. $26d930ac,$51de003a,$c8d75180,$bfd06116,$21b4f4b5,$56b3c423,
  867. $cfba9599,$b8bda50f,$2802b89e,$5f058808,$c60cd9b2,$b10be924,
  868. $2f6f7c87,$58684c11,$c1611dab,$b6662d3d,$76dc4190,$01db7106,
  869. $98d220bc,$efd5102a,$71b18589,$06b6b51f,$9fbfe4a5,$e8b8d433,
  870. $7807c9a2,$0f00f934,$9609a88e,$e10e9818,$7f6a0dbb,$086d3d2d,
  871. $91646c97,$e6635c01,$6b6b51f4,$1c6c6162,$856530d8,$f262004e,
  872. $6c0695ed,$1b01a57b,$8208f4c1,$f50fc457,$65b0d9c6,$12b7e950,
  873. $8bbeb8ea,$fcb9887c,$62dd1ddf,$15da2d49,$8cd37cf3,$fbd44c65,
  874. $4db26158,$3ab551ce,$a3bc0074,$d4bb30e2,$4adfa541,$3dd895d7,
  875. $a4d1c46d,$d3d6f4fb,$4369e96a,$346ed9fc,$ad678846,$da60b8d0,
  876. $44042d73,$33031de5,$aa0a4c5f,$dd0d7cc9,$5005713c,$270241aa,
  877. $be0b1010,$c90c2086,$5768b525,$206f85b3,$b966d409,$ce61e49f,
  878. $5edef90e,$29d9c998,$b0d09822,$c7d7a8b4,$59b33d17,$2eb40d81,
  879. $b7bd5c3b,$c0ba6cad,$edb88320,$9abfb3b6,$03b6e20c,$74b1d29a,
  880. $ead54739,$9dd277af,$04db2615,$73dc1683,$e3630b12,$94643b84,
  881. $0d6d6a3e,$7a6a5aa8,$e40ecf0b,$9309ff9d,$0a00ae27,$7d079eb1,
  882. $f00f9344,$8708a3d2,$1e01f268,$6906c2fe,$f762575d,$806567cb,
  883. $196c3671,$6e6b06e7,$fed41b76,$89d32be0,$10da7a5a,$67dd4acc,
  884. $f9b9df6f,$8ebeeff9,$17b7be43,$60b08ed5,$d6d6a3e8,$a1d1937e,
  885. $38d8c2c4,$4fdff252,$d1bb67f1,$a6bc5767,$3fb506dd,$48b2364b,
  886. $d80d2bda,$af0a1b4c,$36034af6,$41047a60,$df60efc3,$a867df55,
  887. $316e8eef,$4669be79,$cb61b38c,$bc66831a,$256fd2a0,$5268e236,
  888. $cc0c7795,$bb0b4703,$220216b9,$5505262f,$c5ba3bbe,$b2bd0b28,
  889. $2bb45a92,$5cb36a04,$c2d7ffa7,$b5d0cf31,$2cd99e8b,$5bdeae1d,
  890. $9b64c2b0,$ec63f226,$756aa39c,$026d930a,$9c0906a9,$eb0e363f,
  891. $72076785,$05005713,$95bf4a82,$e2b87a14,$7bb12bae,$0cb61b38,
  892. $92d28e9b,$e5d5be0d,$7cdcefb7,$0bdbdf21,$86d3d2d4,$f1d4e242,
  893. $68ddb3f8,$1fda836e,$81be16cd,$f6b9265b,$6fb077e1,$18b74777,
  894. $88085ae6,$ff0f6a70,$66063bca,$11010b5c,$8f659eff,$f862ae69,
  895. $616bffd3,$166ccf45,$a00ae278,$d70dd2ee,$4e048354,$3903b3c2,
  896. $a7672661,$d06016f7,$4969474d,$3e6e77db,$aed16a4a,$d9d65adc,
  897. $40df0b66,$37d83bf0,$a9bcae53,$debb9ec5,$47b2cf7f,$30b5ffe9,
  898. $bdbdf21c,$cabac28a,$53b39330,$24b4a3a6,$bad03605,$cdd70693,
  899. $54de5729,$23d967bf,$b3667a2e,$c4614ab8,$5d681b02,$2a6f2b94,
  900. $b40bbe37,$c30c8ea1,$5a05df1b,$2d02ef8d);
  901. constructor tbufferedfile.init(const filename : string;_bufsize : longint);
  902. begin
  903. assign(f,filename);
  904. bufsize:=_bufsize;
  905. bufpos:=0;
  906. buflast:=0;
  907. do_crc:=false;
  908. iomode:=0;
  909. tempclosed:=false;
  910. change_endian:=false;
  911. clear_crc;
  912. end;
  913. destructor tbufferedfile.done;
  914. begin
  915. close;
  916. end;
  917. procedure tbufferedfile.clear_crc;
  918. begin
  919. crc:=crcseed;
  920. end;
  921. procedure tbufferedfile.setbuf(p : pchar;s : longint);
  922. begin
  923. flush;
  924. freemem(buf,bufsize);
  925. bufsize:=s;
  926. buf:=p;
  927. end;
  928. function tbufferedfile.reset:boolean;
  929. var
  930. ofm : byte;
  931. begin
  932. ofm:=filemode;
  933. iomode:=1;
  934. getmem(buf,bufsize);
  935. filemode:=0;
  936. {$I-}
  937. system.reset(f,1);
  938. {$I+}
  939. reset:=(ioresult=0);
  940. filemode:=ofm;
  941. end;
  942. procedure tbufferedfile.rewrite;
  943. begin
  944. iomode:=2;
  945. getmem(buf,bufsize);
  946. system.rewrite(f,1);
  947. end;
  948. procedure tbufferedfile.flush;
  949. var
  950. {$ifdef FPC}
  951. count : longint;
  952. {$else}
  953. count : integer;
  954. {$endif}
  955. begin
  956. if iomode=2 then
  957. begin
  958. if bufpos=0 then
  959. exit;
  960. blockwrite(f,buf^,bufpos)
  961. end
  962. else if iomode=1 then
  963. if buflast=bufpos then
  964. begin
  965. blockread(f,buf^,bufsize,count);
  966. buflast:=count;
  967. end;
  968. bufpos:=0;
  969. end;
  970. function tbufferedfile.getftime : longint;
  971. var
  972. l : longint;
  973. {$ifdef linux}
  974. Info : Stat;
  975. {$endif}
  976. begin
  977. {$ifndef linux}
  978. { this only works if the file is open !! }
  979. dos.getftime(f,l);
  980. {$else}
  981. Fstat(f,Info);
  982. l:=info.mtime;
  983. {$endif}
  984. getftime:=l;
  985. end;
  986. function tbufferedfile.getsize : longint;
  987. begin
  988. getsize:=filesize(f);
  989. end;
  990. procedure tbufferedfile.seek(l : longint);
  991. begin
  992. if iomode=2 then
  993. begin
  994. flush;
  995. system.seek(f,l);
  996. end
  997. else if iomode=1 then
  998. begin
  999. { forces a reload }
  1000. bufpos:=buflast;
  1001. system.seek(f,l);
  1002. flush;
  1003. end;
  1004. end;
  1005. type
  1006. {$ifdef tp}
  1007. bytearray1 = array [1..65535] of byte;
  1008. {$else}
  1009. bytearray1 = array [1..10000000] of byte;
  1010. {$endif}
  1011. procedure tbufferedfile.read_data(var data;bytes : longint;var count : longint);
  1012. var
  1013. p : pchar;
  1014. c,i : longint;
  1015. begin
  1016. p:=pchar(@data);
  1017. count:=0;
  1018. while bytes-count>0 do
  1019. begin
  1020. if bytes-count>buflast-bufpos then
  1021. begin
  1022. move((buf+bufpos)^,(p+count)^,buflast-bufpos);
  1023. inc(count,buflast-bufpos);
  1024. bufpos:=buflast;
  1025. flush;
  1026. { can't we read anything ? }
  1027. if bufpos=buflast then
  1028. break;
  1029. end
  1030. else
  1031. begin
  1032. move((buf+bufpos)^,(p+count)^,bytes-count);
  1033. inc(bufpos,bytes-count);
  1034. count:=bytes;
  1035. break;
  1036. end;
  1037. end;
  1038. if do_crc then
  1039. begin
  1040. c:=crc;
  1041. for i:=1 to bytes do
  1042. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1043. crc:=c;
  1044. end;
  1045. end;
  1046. procedure tbufferedfile.write_data(var data;count : longint);
  1047. var
  1048. c,i : longint;
  1049. begin
  1050. if bufpos+count>bufsize then
  1051. flush;
  1052. move(data,(buf+bufpos)^,count);
  1053. inc(bufpos,count);
  1054. if do_crc then
  1055. begin
  1056. c:=crc;
  1057. for i:=1 to count do
  1058. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1059. crc:=c;
  1060. end;
  1061. end;
  1062. function tbufferedfile.getcrc : longint;
  1063. begin
  1064. getcrc:=crc xor crcseed;
  1065. end;
  1066. procedure tbufferedfile.write_string(const s : string);
  1067. begin
  1068. if bufpos+length(s)>bufsize then
  1069. flush;
  1070. { why is there not CRC here ??? }
  1071. move(s[1],(buf+bufpos)^,length(s));
  1072. inc(bufpos,length(s));
  1073. { should be
  1074. write_data(s[1],length(s)); }
  1075. end;
  1076. procedure tbufferedfile.write_pchar(p : pchar);
  1077. var
  1078. l : longint;
  1079. begin
  1080. l:=strlen(p);
  1081. if l>=bufsize then
  1082. runerror(222);
  1083. { why is there not CRC here ???}
  1084. if bufpos+l>bufsize then
  1085. flush;
  1086. move(p^,(buf+bufpos)^,l);
  1087. inc(bufpos,l);
  1088. { should be
  1089. write_data(p^,l); }
  1090. end;
  1091. procedure tbufferedfile.write_byte(b : byte);
  1092. begin
  1093. write_data(b,sizeof(byte));
  1094. end;
  1095. procedure tbufferedfile.write_long(l : longint);
  1096. var
  1097. w1,w2 : word;
  1098. begin
  1099. if change_endian then
  1100. begin
  1101. w1:=l and $ffff;
  1102. w2:=l shr 16;
  1103. l:=swap(w2)+(longint(swap(w1)) shl 16);
  1104. write_data(l,sizeof(longint))
  1105. end
  1106. else
  1107. write_data(l,sizeof(longint))
  1108. end;
  1109. procedure tbufferedfile.write_word(w : word);
  1110. begin
  1111. if change_endian then
  1112. begin
  1113. w:=swap(w);
  1114. write_data(w,sizeof(word))
  1115. end
  1116. else
  1117. write_data(w,sizeof(word));
  1118. end;
  1119. procedure tbufferedfile.write_double(d : double);
  1120. begin
  1121. write_data(d,sizeof(double));
  1122. end;
  1123. function tbufferedfile.getpath : string;
  1124. begin
  1125. {$ifdef dummy}
  1126. getpath:=strpas(filerec(f).name);
  1127. {$endif}
  1128. getpath:='';
  1129. end;
  1130. procedure tbufferedfile.close;
  1131. begin
  1132. if iomode<>0 then
  1133. begin
  1134. flush;
  1135. system.close(f);
  1136. freemem(buf,bufsize);
  1137. buf:=nil;
  1138. iomode:=0;
  1139. end;
  1140. end;
  1141. procedure tbufferedfile.tempclose;
  1142. begin
  1143. if iomode<>0 then
  1144. begin
  1145. temppos:=system.filepos(f);
  1146. tempmode:=iomode;
  1147. tempclosed:=true;
  1148. system.close(f);
  1149. iomode:=0;
  1150. end
  1151. else
  1152. tempclosed:=false;
  1153. end;
  1154. procedure tbufferedfile.tempreopen;
  1155. var
  1156. ofm : byte;
  1157. begin
  1158. if tempclosed then
  1159. begin
  1160. case tempmode of
  1161. 1 : begin
  1162. ofm:=filemode;
  1163. iomode:=1;
  1164. filemode:=0;
  1165. system.reset(f,1);
  1166. filemode:=ofm;
  1167. end;
  1168. 2 : begin
  1169. iomode:=2;
  1170. system.rewrite(f,1);
  1171. end;
  1172. end;
  1173. system.seek(f,temppos);
  1174. tempclosed:=false;
  1175. end;
  1176. end;
  1177. {$endif BUFFEREDFILE}
  1178. end.
  1179. {
  1180. $Log$
  1181. Revision 1.19 1999-03-01 13:32:00 pierre
  1182. * external used before implemented problem fixed
  1183. Revision 1.18 1999/02/24 00:59:13 peter
  1184. * small updates for ag386bin
  1185. Revision 1.17 1999/01/19 11:00:33 daniel
  1186. + Tdictionary object: Tsymtable will become object(TTdictionary) in the
  1187. future
  1188. + Tnamed_item object: Tsym will become object(Tnamed_item) in the future
  1189. Revision 1.16 1998/11/04 10:11:37 peter
  1190. * ansistring fixes
  1191. Revision 1.15 1998/10/19 18:04:40 peter
  1192. + tstringcontainer.init_no_doubles
  1193. Revision 1.14 1998/09/18 16:03:37 florian
  1194. * some changes to compile with Delphi
  1195. Revision 1.13 1998/08/12 19:28:16 peter
  1196. * better libc support
  1197. Revision 1.12 1998/07/14 14:46:47 peter
  1198. * released NEWINPUT
  1199. Revision 1.11 1998/07/07 11:19:54 peter
  1200. + NEWINPUT for a better inputfile and scanner object
  1201. Revision 1.10 1998/07/01 15:26:59 peter
  1202. * better bufferfile.reset error handling
  1203. Revision 1.9 1998/06/03 23:40:37 peter
  1204. + unlimited file support, release tempclose
  1205. Revision 1.8 1998/05/20 09:42:33 pierre
  1206. + UseTokenInfo now default
  1207. * unit in interface uses and implementation uses gives error now
  1208. * only one error for unknown symbol (uses lastsymknown boolean)
  1209. the problem came from the label code !
  1210. + first inlined procedures and function work
  1211. (warning there might be allowed cases were the result is still wrong !!)
  1212. * UseBrower updated gives a global list of all position of all used symbols
  1213. with switch -gb
  1214. Revision 1.7 1998/05/06 18:36:53 peter
  1215. * tai_section extended with code,data,bss sections and enumerated type
  1216. * ident 'compiled by FPC' moved to pmodules
  1217. * small fix for smartlink
  1218. Revision 1.6 1998/05/06 08:38:37 pierre
  1219. * better position info with UseTokenInfo
  1220. UseTokenInfo greatly simplified
  1221. + added check for changed tree after first time firstpass
  1222. (if we could remove all the cases were it happen
  1223. we could skip all firstpass if firstpasscount > 1)
  1224. Only with ExtDebug
  1225. Revision 1.5 1998/04/30 15:59:40 pierre
  1226. * GDB works again better :
  1227. correct type info in one pass
  1228. + UseTokenInfo for better source position
  1229. * fixed one remaining bug in scanner for line counts
  1230. * several little fixes
  1231. Revision 1.4 1998/04/29 10:33:50 pierre
  1232. + added some code for ansistring (not complete nor working yet)
  1233. * corrected operator overloading
  1234. * corrected nasm output
  1235. + started inline procedures
  1236. + added starstarn : use ** for exponentiation (^ gave problems)
  1237. + started UseTokenInfo cond to get accurate positions
  1238. Revision 1.3 1998/04/27 23:10:28 peter
  1239. + new scanner
  1240. * $makelib -> if smartlink
  1241. * small filename fixes pmodule.setfilename
  1242. * moved import from files.pas -> import.pas
  1243. Revision 1.2 1998/04/07 11:09:04 peter
  1244. + filemode is set correct in tbufferedfile.reset
  1245. }