finput.pas 14 KB

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