cobjects.pas 30 KB

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