cobjects.pas 31 KB

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