cobjects.pas 30 KB

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