cobjects.pas 31 KB

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