finput.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit implements an extended file management
  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. unit finput;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cutils;
  23. const
  24. InputFileBufSize=32*1024;
  25. linebufincrease=512;
  26. type
  27. tlongintarr = array[0..1000000] of longint;
  28. plongintarr = ^tlongintarr;
  29. pinputfile = ^tinputfile;
  30. tinputfile = object
  31. path,name : pstring; { path and filename }
  32. next : pinputfile; { next file for reading }
  33. is_macro,
  34. endoffile, { still bytes left to read }
  35. closed : boolean; { is the file closed }
  36. buf : pchar; { buffer }
  37. bufstart, { buffer start position in the file }
  38. bufsize, { amount of bytes in the buffer }
  39. maxbufsize : longint; { size in memory for the buffer }
  40. saveinputpointer : pchar; { save fields for scanner variables }
  41. savelastlinepos,
  42. saveline_no : longint;
  43. linebuf : plongintarr; { line buffer to retrieve lines }
  44. maxlinebuf : longint;
  45. ref_count : longint; { to handle the browser refs }
  46. ref_index : longint;
  47. ref_next : pinputfile;
  48. constructor init(const fn:string);
  49. destructor done;
  50. procedure setpos(l:longint);
  51. procedure seekbuf(fpos:longint);
  52. procedure readbuf;
  53. function open:boolean;
  54. procedure close;
  55. procedure tempclose;
  56. function tempopen:boolean;
  57. procedure setmacro(p:pchar;len:longint);
  58. procedure setline(line,linepos:longint);
  59. function getlinestr(l:longint):string;
  60. {$ifdef FPC}protected{$else}public{$endif}
  61. function fileopen(const filename: string): boolean; virtual;
  62. function fileseek(pos: longint): boolean; virtual;
  63. function fileread(var databuf; maxsize: longint): longint; virtual;
  64. function fileeof: boolean; virtual;
  65. function fileclose: boolean; virtual;
  66. end;
  67. pdosinputfile = ^tdosinputfile;
  68. tdosinputfile = object(tinputfile)
  69. {$ifdef FPC}protected{$else}public{$endif}
  70. function fileopen(const filename: string): boolean; virtual;
  71. function fileseek(pos: longint): boolean; virtual;
  72. function fileread(var databuf; maxsize: longint): longint; virtual;
  73. function fileeof: boolean; virtual;
  74. function fileclose: boolean; virtual;
  75. private
  76. f : file; { current file handle }
  77. end;
  78. pinputfilemanager = ^tinputfilemanager;
  79. tinputfilemanager = object
  80. files : pinputfile;
  81. last_ref_index : longint;
  82. cacheindex : longint;
  83. cacheinputfile : pinputfile;
  84. constructor init;
  85. destructor done;
  86. procedure register_file(f : pinputfile);
  87. procedure inverse_register_indexes;
  88. function get_file(l:longint) : pinputfile;
  89. function get_file_name(l :longint):string;
  90. function get_file_path(l :longint):string;
  91. end;
  92. implementation
  93. uses
  94. {$ifdef Delphi}
  95. dmisc,
  96. {$else Delphi}
  97. dos,
  98. {$endif Delphi}
  99. cobjects,globals
  100. {$ifdef heaptrc}
  101. ,fmodule
  102. {$endif heaptrc}
  103. ;
  104. {****************************************************************************
  105. TINPUTFILE
  106. ****************************************************************************}
  107. constructor tinputfile.init(const fn:string);
  108. var
  109. p:dirstr;
  110. n:namestr;
  111. e:extstr;
  112. begin
  113. FSplit(fn,p,n,e);
  114. name:=stringdup(n+e);
  115. path:=stringdup(p);
  116. next:=nil;
  117. { file info }
  118. is_macro:=false;
  119. endoffile:=false;
  120. closed:=true;
  121. buf:=nil;
  122. bufstart:=0;
  123. bufsize:=0;
  124. maxbufsize:=InputFileBufSize;
  125. { save fields }
  126. saveinputpointer:=nil;
  127. saveline_no:=0;
  128. savelastlinepos:=0;
  129. { indexing refs }
  130. ref_next:=nil;
  131. ref_count:=0;
  132. ref_index:=0;
  133. { line buffer }
  134. linebuf:=nil;
  135. maxlinebuf:=0;
  136. end;
  137. destructor tinputfile.done;
  138. begin
  139. if not closed then
  140. close;
  141. stringdispose(path);
  142. stringdispose(name);
  143. { free memory }
  144. if assigned(linebuf) then
  145. freemem(linebuf,maxlinebuf shl 2);
  146. end;
  147. procedure tinputfile.setpos(l:longint);
  148. begin
  149. bufstart:=l;
  150. end;
  151. procedure tinputfile.seekbuf(fpos:longint);
  152. begin
  153. if closed then
  154. exit;
  155. fileseek(fpos);
  156. bufstart:=fpos;
  157. bufsize:=0;
  158. end;
  159. procedure tinputfile.readbuf;
  160. begin
  161. if is_macro then
  162. endoffile:=true;
  163. if closed then
  164. exit;
  165. inc(bufstart,bufsize);
  166. bufsize:=fileread(buf^,maxbufsize-1);
  167. buf[bufsize]:=#0;
  168. endoffile:=fileeof;
  169. end;
  170. function tinputfile.open:boolean;
  171. begin
  172. open:=false;
  173. if not closed then
  174. Close;
  175. if not fileopen(path^+name^) then
  176. exit;
  177. { file }
  178. endoffile:=false;
  179. closed:=false;
  180. Getmem(buf,MaxBufsize);
  181. bufstart:=0;
  182. bufsize:=0;
  183. open:=true;
  184. end;
  185. procedure tinputfile.close;
  186. begin
  187. if is_macro then
  188. begin
  189. if assigned(buf) then
  190. Freemem(buf,maxbufsize);
  191. buf:=nil;
  192. {is_macro:=false;
  193. still needed for dispose in scanner PM }
  194. closed:=true;
  195. exit;
  196. end;
  197. if not closed then
  198. begin
  199. if fileclose then;
  200. closed:=true;
  201. end;
  202. if assigned(buf) then
  203. begin
  204. Freemem(buf,maxbufsize);
  205. buf:=nil;
  206. end;
  207. bufstart:=0;
  208. end;
  209. procedure tinputfile.tempclose;
  210. begin
  211. if is_macro then
  212. exit;
  213. if not closed then
  214. begin
  215. if fileclose then;
  216. Freemem(buf,maxbufsize);
  217. buf:=nil;
  218. closed:=true;
  219. end;
  220. end;
  221. function tinputfile.tempopen:boolean;
  222. begin
  223. tempopen:=false;
  224. if is_macro then
  225. begin
  226. { seek buffer postion to bufstart }
  227. if bufstart>0 then
  228. begin
  229. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  230. bufstart:=0;
  231. end;
  232. tempopen:=true;
  233. exit;
  234. end;
  235. if not closed then
  236. exit;
  237. if not fileopen(path^+name^) then
  238. exit;
  239. closed:=false;
  240. { get new mem }
  241. Getmem(buf,maxbufsize);
  242. { restore state }
  243. fileseek(BufStart);
  244. bufsize:=0;
  245. readbuf;
  246. tempopen:=true;
  247. end;
  248. procedure tinputfile.setmacro(p:pchar;len:longint);
  249. begin
  250. { create new buffer }
  251. getmem(buf,len+1);
  252. move(p^,buf^,len);
  253. buf[len]:=#0;
  254. { reset }
  255. bufstart:=0;
  256. bufsize:=len;
  257. maxbufsize:=len+1;
  258. is_macro:=true;
  259. endoffile:=true;
  260. closed:=true;
  261. end;
  262. procedure tinputfile.setline(line,linepos:longint);
  263. var
  264. oldlinebuf : plongintarr;
  265. begin
  266. if line<1 then
  267. exit;
  268. while (line>=maxlinebuf) do
  269. begin
  270. oldlinebuf:=linebuf;
  271. { create new linebuf and move old info }
  272. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  273. if assigned(oldlinebuf) then
  274. begin
  275. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  276. freemem(oldlinebuf,maxlinebuf shl 2);
  277. end;
  278. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  279. inc(maxlinebuf,linebufincrease);
  280. end;
  281. linebuf^[line]:=linepos;
  282. end;
  283. function tinputfile.getlinestr(l:longint):string;
  284. var
  285. c : char;
  286. i,
  287. fpos : longint;
  288. p : pchar;
  289. begin
  290. getlinestr:='';
  291. if l<maxlinebuf then
  292. begin
  293. fpos:=linebuf^[l];
  294. { fpos is set negativ if the line was already written }
  295. { but we still know the correct value }
  296. if fpos<0 then
  297. fpos:=-fpos+1;
  298. if closed then
  299. open;
  300. { in current buf ? }
  301. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  302. begin
  303. seekbuf(fpos);
  304. readbuf;
  305. end;
  306. { the begin is in the buf now simply read until #13,#10 }
  307. i:=0;
  308. p:=@buf[fpos-bufstart];
  309. repeat
  310. c:=p^;
  311. if c=#0 then
  312. begin
  313. if endoffile then
  314. break;
  315. readbuf;
  316. p:=buf;
  317. c:=p^;
  318. end;
  319. if c in [#10,#13] then
  320. break;
  321. inc(i);
  322. getlinestr[i]:=c;
  323. inc(longint(p));
  324. until (i=255);
  325. getlinestr[0]:=chr(i);
  326. end;
  327. end;
  328. function tinputfile.fileopen(const filename: string): boolean;
  329. begin
  330. abstract;
  331. fileopen:=false;
  332. end;
  333. function tinputfile.fileseek(pos: longint): boolean;
  334. begin
  335. abstract;
  336. fileseek:=false;
  337. end;
  338. function tinputfile.fileread(var databuf; maxsize: longint): longint;
  339. begin
  340. abstract;
  341. fileread:=0;
  342. end;
  343. function tinputfile.fileeof: boolean;
  344. begin
  345. abstract;
  346. fileeof:=false;
  347. end;
  348. function tinputfile.fileclose: boolean;
  349. begin
  350. abstract;
  351. fileclose:=false;
  352. end;
  353. {****************************************************************************
  354. TDOSINPUTFILE
  355. ****************************************************************************}
  356. function tdosinputfile.fileopen(const filename: string): boolean;
  357. var
  358. ofm : byte;
  359. begin
  360. ofm:=filemode;
  361. filemode:=0;
  362. Assign(f,filename);
  363. {$I-}
  364. reset(f,1);
  365. {$I+}
  366. filemode:=ofm;
  367. fileopen:=(ioresult=0);
  368. end;
  369. function tdosinputfile.fileseek(pos: longint): boolean;
  370. begin
  371. {$I-}
  372. seek(f,Pos);
  373. {$I+}
  374. fileseek:=(ioresult=0);
  375. end;
  376. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  377. var
  378. w : longint;
  379. begin
  380. blockread(f,databuf,maxsize,w);
  381. fileread:=w;
  382. end;
  383. function tdosinputfile.fileeof: boolean;
  384. begin
  385. fileeof:=eof(f);
  386. end;
  387. function tdosinputfile.fileclose: boolean;
  388. begin
  389. {$I-}
  390. system.close(f);
  391. {$I+}
  392. fileclose:=(ioresult=0);
  393. end;
  394. {****************************************************************************
  395. Tinputfilemanager
  396. ****************************************************************************}
  397. constructor tinputfilemanager.init;
  398. begin
  399. files:=nil;
  400. last_ref_index:=0;
  401. cacheindex:=0;
  402. cacheinputfile:=nil;
  403. end;
  404. destructor tinputfilemanager.done;
  405. var
  406. hp : pinputfile;
  407. begin
  408. hp:=files;
  409. while assigned(hp) do
  410. begin
  411. files:=files^.ref_next;
  412. dispose(hp,done);
  413. hp:=files;
  414. end;
  415. last_ref_index:=0;
  416. end;
  417. procedure tinputfilemanager.register_file(f : pinputfile);
  418. begin
  419. { don't register macro's }
  420. if f^.is_macro then
  421. exit;
  422. inc(last_ref_index);
  423. f^.ref_next:=files;
  424. f^.ref_index:=last_ref_index;
  425. files:=f;
  426. { update cache }
  427. cacheindex:=last_ref_index;
  428. cacheinputfile:=f;
  429. {$ifdef heaptrc}
  430. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  431. {$endif heaptrc}
  432. end;
  433. { this procedure is necessary after loading the
  434. sources files from a PPU file PM }
  435. procedure tinputfilemanager.inverse_register_indexes;
  436. var
  437. f : pinputfile;
  438. begin
  439. f:=files;
  440. while assigned(f) do
  441. begin
  442. f^.ref_index:=last_ref_index-f^.ref_index+1;
  443. f:=f^.ref_next;
  444. end;
  445. { reset cache }
  446. cacheindex:=0;
  447. cacheinputfile:=nil;
  448. end;
  449. function tinputfilemanager.get_file(l :longint) : pinputfile;
  450. var
  451. ff : pinputfile;
  452. begin
  453. { check cache }
  454. if (l=cacheindex) and assigned(cacheinputfile) then
  455. begin
  456. get_file:=cacheinputfile;
  457. exit;
  458. end;
  459. ff:=files;
  460. while assigned(ff) and (ff^.ref_index<>l) do
  461. ff:=ff^.ref_next;
  462. get_file:=ff;
  463. end;
  464. function tinputfilemanager.get_file_name(l :longint):string;
  465. var
  466. hp : pinputfile;
  467. begin
  468. hp:=get_file(l);
  469. if assigned(hp) then
  470. get_file_name:=hp^.name^
  471. else
  472. get_file_name:='';
  473. end;
  474. function tinputfilemanager.get_file_path(l :longint):string;
  475. var
  476. hp : pinputfile;
  477. begin
  478. hp:=get_file(l);
  479. if assigned(hp) then
  480. get_file_path:=hp^.path^
  481. else
  482. get_file_path:='';
  483. end;
  484. end.
  485. {
  486. $Log$
  487. Revision 1.3 2000-10-14 21:52:54 peter
  488. * fixed memory leaks
  489. Revision 1.2 2000/09/24 15:06:16 peter
  490. * use defines.inc
  491. Revision 1.1 2000/08/27 16:11:50 peter
  492. * moved some util functions from globals,cobjects to cutils
  493. * splitted files into finput,fmodule
  494. }