cobjects.pas 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. var
  592. ofm : byte;
  593. begin
  594. ofm:=filemode;
  595. iomode:=1;
  596. getmem(buf,bufsize);
  597. filemode:=0;
  598. system.reset(f,1);
  599. filemode:=ofm;
  600. end;
  601. procedure tbufferedfile.rewrite;
  602. begin
  603. iomode:=2;
  604. getmem(buf,bufsize);
  605. system.rewrite(f,1);
  606. end;
  607. procedure tbufferedfile.flush;
  608. var
  609. {$ifdef FPC}
  610. count : longint;
  611. {$else}
  612. count : integer;
  613. {$endif}
  614. begin
  615. if iomode=2 then
  616. begin
  617. if bufpos=0 then
  618. exit;
  619. blockwrite(f,buf^,bufpos)
  620. end
  621. else if iomode=1 then
  622. if buflast=bufpos then
  623. begin
  624. blockread(f,buf^,bufsize,count);
  625. buflast:=count;
  626. end;
  627. bufpos:=0;
  628. end;
  629. function tbufferedfile.getftime : longint;
  630. var
  631. l : longint;
  632. {$ifdef linux}
  633. Info : Stat;
  634. {$endif}
  635. begin
  636. {$ifndef linux}
  637. { this only works if the file is open !! }
  638. dos.getftime(f,l);
  639. {$else}
  640. Fstat(f,Info);
  641. l:=info.mtime;
  642. {$endif}
  643. getftime:=l;
  644. end;
  645. function tbufferedfile.getsize : longint;
  646. begin
  647. getsize:=filesize(f);
  648. end;
  649. procedure tbufferedfile.seek(l : longint);
  650. begin
  651. if iomode=2 then
  652. begin
  653. flush;
  654. system.seek(f,l);
  655. end
  656. else if iomode=1 then
  657. begin
  658. { forces a reload }
  659. bufpos:=buflast;
  660. system.seek(f,l);
  661. flush;
  662. end;
  663. end;
  664. type
  665. {$ifdef tp}
  666. bytearray1 = array [1..65535] of byte;
  667. {$else}
  668. bytearray1 = array [1..10000000] of byte;
  669. {$endif}
  670. procedure tbufferedfile.read_data(var data;bytes : longint;var count : longint);
  671. var
  672. p : pchar;
  673. c,i : longint;
  674. begin
  675. p:=pchar(@data);
  676. count:=0;
  677. while bytes-count>0 do
  678. begin
  679. if bytes-count>buflast-bufpos then
  680. begin
  681. move((buf+bufpos)^,(p+count)^,buflast-bufpos);
  682. inc(count,buflast-bufpos);
  683. bufpos:=buflast;
  684. flush;
  685. { can't we read anything ? }
  686. if bufpos=buflast then
  687. break;
  688. end
  689. else
  690. begin
  691. move((buf+bufpos)^,(p+count)^,bytes-count);
  692. inc(bufpos,bytes-count);
  693. count:=bytes;
  694. break;
  695. end;
  696. end;
  697. if do_crc then
  698. begin
  699. c:=crc;
  700. for i:=1 to bytes do
  701. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  702. crc:=c;
  703. end;
  704. end;
  705. procedure tbufferedfile.write_data(var data;count : longint);
  706. var
  707. c,i : longint;
  708. begin
  709. if bufpos+count>bufsize then
  710. flush;
  711. move(data,(buf+bufpos)^,count);
  712. inc(bufpos,count);
  713. if do_crc then
  714. begin
  715. c:=crc;
  716. for i:=1 to count do
  717. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  718. crc:=c;
  719. end;
  720. end;
  721. function tbufferedfile.getcrc : longint;
  722. begin
  723. getcrc:=crc xor crcseed;
  724. end;
  725. procedure tbufferedfile.write_string(const s : string);
  726. begin
  727. if bufpos+length(s)>bufsize then
  728. flush;
  729. move(s[1],(buf+bufpos)^,length(s));
  730. inc(bufpos,length(s));
  731. end;
  732. procedure tbufferedfile.write_pchar(p : pchar);
  733. var
  734. l : longint;
  735. begin
  736. l:=strlen(p);
  737. if l>=bufsize then
  738. runerror(222);
  739. if bufpos+l>bufsize then
  740. flush;
  741. move(p^,(buf+bufpos)^,l);
  742. inc(bufpos,l);
  743. end;
  744. procedure tbufferedfile.write_byte(b : byte);
  745. begin
  746. write_data(b,sizeof(byte));
  747. end;
  748. procedure tbufferedfile.write_long(l : longint);
  749. var
  750. w1,w2 : word;
  751. begin
  752. if change_endian then
  753. begin
  754. w1:=l and $ffff;
  755. w2:=l shr 16;
  756. l:=swap(w2)+(longint(swap(w1)) shl 16);
  757. write_data(l,sizeof(longint))
  758. end
  759. else
  760. write_data(l,sizeof(longint))
  761. end;
  762. procedure tbufferedfile.write_word(w : word);
  763. begin
  764. if change_endian then
  765. begin
  766. w:=swap(w);
  767. write_data(w,sizeof(word))
  768. end
  769. else
  770. write_data(w,sizeof(word));
  771. end;
  772. procedure tbufferedfile.write_double(d : double);
  773. begin
  774. write_data(d,sizeof(double));
  775. end;
  776. function tbufferedfile.getpath : string;
  777. begin
  778. {$ifdef dummy}
  779. getpath:=strpas(filerec(f).name);
  780. {$endif}
  781. getpath:='';
  782. end;
  783. procedure tbufferedfile.close;
  784. begin
  785. if iomode<>0 then
  786. begin
  787. flush;
  788. system.close(f);
  789. freemem(buf,bufsize);
  790. iomode:=0;
  791. end;
  792. end;
  793. {$ifdef MAKELIB}
  794. procedure tbufferedfile.changename(filename : string);
  795. begin
  796. close;
  797. assign(f,filename);
  798. end;
  799. {$endif MAKELIB}
  800. end.
  801. {
  802. $Log$
  803. Revision 1.2 1998-04-07 11:09:04 peter
  804. + filemode is set correct in tbufferedfile.reset
  805. Revision 1.1.1.1 1998/03/25 11:18:15 root
  806. * Restored version
  807. Revision 1.15 1998/03/10 16:27:38 pierre
  808. * better line info in stabs debug
  809. * symtabletype and lexlevel separated into two fields of tsymtable
  810. + ifdef MAKELIB for direct library output, not complete
  811. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  812. working
  813. + ifdef TESTFUNCRET for setting func result in underfunction, not
  814. working
  815. Revision 1.14 1998/03/10 01:17:18 peter
  816. * all files have the same header
  817. * messages are fully implemented, EXTDEBUG uses Comment()
  818. + AG... files for the Assembler generation
  819. Revision 1.13 1998/03/04 17:33:42 michael
  820. + Changed ifdef FPK to ifdef FPC
  821. Revision 1.12 1998/03/02 01:48:31 peter
  822. * renamed target_DOS to target_GO32V1
  823. + new verbose system, merged old errors and verbose units into one new
  824. verbose.pas, so errors.pas is obsolete
  825. Revision 1.11 1998/02/28 00:20:22 florian
  826. * more changes to get import libs for Win32 working
  827. Revision 1.10 1998/02/24 14:20:50 peter
  828. + tstringcontainer.empty
  829. * ld -T option restored for linux
  830. * libraries are placed before the objectfiles in a .PPU file
  831. * removed 'uses link' from files.pas
  832. Revision 1.9 1998/02/18 13:48:17 michael
  833. + Implemented an OS independent AsmRes object.
  834. Revision 1.8 1998/02/17 21:20:45 peter
  835. + Script unit
  836. + __EXIT is called again to exit a program
  837. - target_info.link/assembler calls
  838. * linking works again for dos
  839. * optimized a few filehandling functions
  840. * fixed stabs generation for procedures
  841. Revision 1.7 1998/02/13 10:34:55 daniel
  842. * Made Motorola version compilable.
  843. * Fixed optimizer
  844. Revision 1.6 1998/02/12 11:50:01 daniel
  845. Yes! Finally! After three retries, my patch!
  846. Changes:
  847. Complete rewrite of psub.pas.
  848. Added support for DLL's.
  849. Compiler requires less memory.
  850. Platform units for each platform.
  851. Revision 1.5 1998/02/06 23:08:32 florian
  852. + endian to targetinfo and sourceinfo added
  853. + endian independed writing of ppu file (reading missed), a PPU file
  854. is written with the target endian
  855. Revision 1.4 1998/01/13 17:11:34 michael
  856. * Changed getftime method to work faster under linux.
  857. Revision 1.3 1997/12/05 13:45:34 daniel
  858. - Removed overlay init. This is done by PPOVIN.PAS.
  859. Revision 1.2 1997/11/28 18:14:28 pierre
  860. working version with several bug fixes
  861. Revision 1.1.1.1 1997/11/27 08:32:55 michael
  862. FPC Compiler CVS start
  863. Pre-CVS log:
  864. History:
  865. 30th september 1996:
  866. + english comments (FK)
  867. + _2pchar renamed to pstring2pchar (FK)
  868. + _2pstring renamed to pchar2pstring (FK)
  869. 15th october 1996:
  870. + tstringcontainer is compilable (FK)
  871. + full compilable (FK)
  872. 4th january 1996:
  873. + tstring_item added (FK)
  874. 19th november 1997:
  875. + call of overlay init (FK)
  876. }