cobjects.pas 30 KB

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