2
0

cobjects.pas 39 KB

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