cobjects.pas 38 KB

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