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