cobjects.pas 28 KB

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