finput.pas 14 KB

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