cobjects.pas 28 KB

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