cobjects.pas 27 KB

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