finput.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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,cobjects;
  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_index : longint; { to handle the browser refs }
  46. ref_next : pinputfile;
  47. constructor init(const fn:string);
  48. destructor done;
  49. procedure setpos(l:longint);
  50. procedure seekbuf(fpos:longint);
  51. procedure readbuf;
  52. function open:boolean;
  53. procedure close;
  54. procedure tempclose;
  55. function tempopen:boolean;
  56. procedure setmacro(p:pchar;len:longint);
  57. procedure setline(line,linepos:longint);
  58. function getlinestr(l:longint):string;
  59. {$ifdef FPC}protected{$else}public{$endif}
  60. function fileopen(const filename: string): boolean; virtual;
  61. function fileseek(pos: longint): boolean; virtual;
  62. function fileread(var databuf; maxsize: longint): longint; virtual;
  63. function fileeof: boolean; virtual;
  64. function fileclose: boolean; virtual;
  65. end;
  66. pdosinputfile = ^tdosinputfile;
  67. tdosinputfile = object(tinputfile)
  68. {$ifdef FPC}protected{$else}public{$endif}
  69. function fileopen(const filename: string): boolean; virtual;
  70. function fileseek(pos: longint): boolean; virtual;
  71. function fileread(var databuf; maxsize: longint): longint; virtual;
  72. function fileeof: boolean; virtual;
  73. function fileclose: boolean; virtual;
  74. private
  75. f : file; { current file handle }
  76. end;
  77. pinputfilemanager = ^tinputfilemanager;
  78. tinputfilemanager = object
  79. files : pinputfile;
  80. last_ref_index : longint;
  81. cacheindex : longint;
  82. cacheinputfile : pinputfile;
  83. constructor init;
  84. destructor done;
  85. procedure register_file(f : pinputfile);
  86. procedure inverse_register_indexes;
  87. function get_file(l:longint) : pinputfile;
  88. function get_file_name(l :longint):string;
  89. function get_file_path(l :longint):string;
  90. end;
  91. {****************************************************************************
  92. TModuleBase
  93. ****************************************************************************}
  94. pmodulebase = ^tmodulebase;
  95. tmodulebase = object(tlinkedlist_item)
  96. { index }
  97. unit_index : longint; { global counter for browser }
  98. { sources }
  99. sourcefiles : pinputfilemanager;
  100. { paths and filenames }
  101. path, { path where the module is find/created }
  102. outputpath, { path where the .s / .o / exe are created }
  103. modulename, { name of the module in uppercase }
  104. realmodulename, { name of the module in the orignal case }
  105. objfilename, { fullname of the objectfile }
  106. asmfilename, { fullname of the assemblerfile }
  107. ppufilename, { fullname of the ppufile }
  108. staticlibfilename, { fullname of the static libraryfile }
  109. sharedlibfilename, { fullname of the shared libraryfile }
  110. exefilename, { fullname of the exefile }
  111. mainsource : pstring; { name of the main sourcefile }
  112. constructor init(const s:string);
  113. destructor done;virtual;
  114. procedure setfilename(const fn:string;allowoutput:boolean);
  115. end;
  116. implementation
  117. uses
  118. {$ifdef Delphi}
  119. dmisc,
  120. {$else Delphi}
  121. dos,
  122. {$endif Delphi}
  123. globals,systems
  124. ;
  125. {****************************************************************************
  126. TINPUTFILE
  127. ****************************************************************************}
  128. constructor tinputfile.init(const fn:string);
  129. var
  130. p:dirstr;
  131. n:namestr;
  132. e:extstr;
  133. begin
  134. FSplit(fn,p,n,e);
  135. name:=stringdup(n+e);
  136. path:=stringdup(p);
  137. next:=nil;
  138. { file info }
  139. is_macro:=false;
  140. endoffile:=false;
  141. closed:=true;
  142. buf:=nil;
  143. bufstart:=0;
  144. bufsize:=0;
  145. maxbufsize:=InputFileBufSize;
  146. { save fields }
  147. saveinputpointer:=nil;
  148. saveline_no:=0;
  149. savelastlinepos:=0;
  150. { indexing refs }
  151. ref_next:=nil;
  152. ref_index:=0;
  153. { line buffer }
  154. linebuf:=nil;
  155. maxlinebuf:=0;
  156. end;
  157. destructor tinputfile.done;
  158. begin
  159. if not closed then
  160. close;
  161. stringdispose(path);
  162. stringdispose(name);
  163. { free memory }
  164. if assigned(linebuf) then
  165. freemem(linebuf,maxlinebuf shl 2);
  166. end;
  167. procedure tinputfile.setpos(l:longint);
  168. begin
  169. bufstart:=l;
  170. end;
  171. procedure tinputfile.seekbuf(fpos:longint);
  172. begin
  173. if closed then
  174. exit;
  175. fileseek(fpos);
  176. bufstart:=fpos;
  177. bufsize:=0;
  178. end;
  179. procedure tinputfile.readbuf;
  180. begin
  181. if is_macro then
  182. endoffile:=true;
  183. if closed then
  184. exit;
  185. inc(bufstart,bufsize);
  186. bufsize:=fileread(buf^,maxbufsize-1);
  187. buf[bufsize]:=#0;
  188. endoffile:=fileeof;
  189. end;
  190. function tinputfile.open:boolean;
  191. begin
  192. open:=false;
  193. if not closed then
  194. Close;
  195. if not fileopen(path^+name^) then
  196. exit;
  197. { file }
  198. endoffile:=false;
  199. closed:=false;
  200. Getmem(buf,MaxBufsize);
  201. bufstart:=0;
  202. bufsize:=0;
  203. open:=true;
  204. end;
  205. procedure tinputfile.close;
  206. begin
  207. if is_macro then
  208. begin
  209. if assigned(buf) then
  210. Freemem(buf,maxbufsize);
  211. buf:=nil;
  212. {is_macro:=false;
  213. still needed for dispose in scanner PM }
  214. closed:=true;
  215. exit;
  216. end;
  217. if not closed then
  218. begin
  219. if fileclose then;
  220. closed:=true;
  221. end;
  222. if assigned(buf) then
  223. begin
  224. Freemem(buf,maxbufsize);
  225. buf:=nil;
  226. end;
  227. bufstart:=0;
  228. end;
  229. procedure tinputfile.tempclose;
  230. begin
  231. if is_macro then
  232. exit;
  233. if not closed then
  234. begin
  235. if fileclose then;
  236. Freemem(buf,maxbufsize);
  237. buf:=nil;
  238. closed:=true;
  239. end;
  240. end;
  241. function tinputfile.tempopen:boolean;
  242. begin
  243. tempopen:=false;
  244. if is_macro then
  245. begin
  246. { seek buffer postion to bufstart }
  247. if bufstart>0 then
  248. begin
  249. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  250. bufstart:=0;
  251. end;
  252. tempopen:=true;
  253. exit;
  254. end;
  255. if not closed then
  256. exit;
  257. if not fileopen(path^+name^) then
  258. exit;
  259. closed:=false;
  260. { get new mem }
  261. Getmem(buf,maxbufsize);
  262. { restore state }
  263. fileseek(BufStart);
  264. bufsize:=0;
  265. readbuf;
  266. tempopen:=true;
  267. end;
  268. procedure tinputfile.setmacro(p:pchar;len:longint);
  269. begin
  270. { create new buffer }
  271. getmem(buf,len+1);
  272. move(p^,buf^,len);
  273. buf[len]:=#0;
  274. { reset }
  275. bufstart:=0;
  276. bufsize:=len;
  277. maxbufsize:=len+1;
  278. is_macro:=true;
  279. endoffile:=true;
  280. closed:=true;
  281. end;
  282. procedure tinputfile.setline(line,linepos:longint);
  283. var
  284. oldlinebuf : plongintarr;
  285. begin
  286. if line<1 then
  287. exit;
  288. while (line>=maxlinebuf) do
  289. begin
  290. oldlinebuf:=linebuf;
  291. { create new linebuf and move old info }
  292. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  293. if assigned(oldlinebuf) then
  294. begin
  295. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  296. freemem(oldlinebuf,maxlinebuf shl 2);
  297. end;
  298. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  299. inc(maxlinebuf,linebufincrease);
  300. end;
  301. linebuf^[line]:=linepos;
  302. end;
  303. function tinputfile.getlinestr(l:longint):string;
  304. var
  305. c : char;
  306. i,
  307. fpos : longint;
  308. p : pchar;
  309. begin
  310. getlinestr:='';
  311. if l<maxlinebuf then
  312. begin
  313. fpos:=linebuf^[l];
  314. { fpos is set negativ if the line was already written }
  315. { but we still know the correct value }
  316. if fpos<0 then
  317. fpos:=-fpos+1;
  318. if closed then
  319. open;
  320. { in current buf ? }
  321. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  322. begin
  323. seekbuf(fpos);
  324. readbuf;
  325. end;
  326. { the begin is in the buf now simply read until #13,#10 }
  327. i:=0;
  328. p:=@buf[fpos-bufstart];
  329. repeat
  330. c:=p^;
  331. if c=#0 then
  332. begin
  333. if endoffile then
  334. break;
  335. readbuf;
  336. p:=buf;
  337. c:=p^;
  338. end;
  339. if c in [#10,#13] then
  340. break;
  341. inc(i);
  342. getlinestr[i]:=c;
  343. inc(longint(p));
  344. until (i=255);
  345. getlinestr[0]:=chr(i);
  346. end;
  347. end;
  348. function tinputfile.fileopen(const filename: string): boolean;
  349. begin
  350. abstract;
  351. fileopen:=false;
  352. end;
  353. function tinputfile.fileseek(pos: longint): boolean;
  354. begin
  355. abstract;
  356. fileseek:=false;
  357. end;
  358. function tinputfile.fileread(var databuf; maxsize: longint): longint;
  359. begin
  360. abstract;
  361. fileread:=0;
  362. end;
  363. function tinputfile.fileeof: boolean;
  364. begin
  365. abstract;
  366. fileeof:=false;
  367. end;
  368. function tinputfile.fileclose: boolean;
  369. begin
  370. abstract;
  371. fileclose:=false;
  372. end;
  373. {****************************************************************************
  374. TDOSINPUTFILE
  375. ****************************************************************************}
  376. function tdosinputfile.fileopen(const filename: string): boolean;
  377. var
  378. ofm : byte;
  379. begin
  380. ofm:=filemode;
  381. filemode:=0;
  382. Assign(f,filename);
  383. {$I-}
  384. reset(f,1);
  385. {$I+}
  386. filemode:=ofm;
  387. fileopen:=(ioresult=0);
  388. end;
  389. function tdosinputfile.fileseek(pos: longint): boolean;
  390. begin
  391. {$I-}
  392. seek(f,Pos);
  393. {$I+}
  394. fileseek:=(ioresult=0);
  395. end;
  396. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  397. var
  398. w : longint;
  399. begin
  400. blockread(f,databuf,maxsize,w);
  401. fileread:=w;
  402. end;
  403. function tdosinputfile.fileeof: boolean;
  404. begin
  405. fileeof:=eof(f);
  406. end;
  407. function tdosinputfile.fileclose: boolean;
  408. begin
  409. {$I-}
  410. system.close(f);
  411. {$I+}
  412. fileclose:=(ioresult=0);
  413. end;
  414. {****************************************************************************
  415. Tinputfilemanager
  416. ****************************************************************************}
  417. constructor tinputfilemanager.init;
  418. begin
  419. files:=nil;
  420. last_ref_index:=0;
  421. cacheindex:=0;
  422. cacheinputfile:=nil;
  423. end;
  424. destructor tinputfilemanager.done;
  425. var
  426. hp : pinputfile;
  427. begin
  428. hp:=files;
  429. while assigned(hp) do
  430. begin
  431. files:=files^.ref_next;
  432. dispose(hp,done);
  433. hp:=files;
  434. end;
  435. last_ref_index:=0;
  436. end;
  437. procedure tinputfilemanager.register_file(f : pinputfile);
  438. begin
  439. { don't register macro's }
  440. if f^.is_macro then
  441. exit;
  442. inc(last_ref_index);
  443. f^.ref_next:=files;
  444. f^.ref_index:=last_ref_index;
  445. files:=f;
  446. { update cache }
  447. cacheindex:=last_ref_index;
  448. cacheinputfile:=f;
  449. {$ifdef HEAPTRC}
  450. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  451. {$endif HEAPTRC}
  452. end;
  453. { this procedure is necessary after loading the
  454. sources files from a PPU file PM }
  455. procedure tinputfilemanager.inverse_register_indexes;
  456. var
  457. f : pinputfile;
  458. begin
  459. f:=files;
  460. while assigned(f) do
  461. begin
  462. f^.ref_index:=last_ref_index-f^.ref_index+1;
  463. f:=f^.ref_next;
  464. end;
  465. { reset cache }
  466. cacheindex:=0;
  467. cacheinputfile:=nil;
  468. end;
  469. function tinputfilemanager.get_file(l :longint) : pinputfile;
  470. var
  471. ff : pinputfile;
  472. begin
  473. { check cache }
  474. if (l=cacheindex) and assigned(cacheinputfile) then
  475. begin
  476. get_file:=cacheinputfile;
  477. exit;
  478. end;
  479. ff:=files;
  480. while assigned(ff) and (ff^.ref_index<>l) do
  481. ff:=ff^.ref_next;
  482. get_file:=ff;
  483. end;
  484. function tinputfilemanager.get_file_name(l :longint):string;
  485. var
  486. hp : pinputfile;
  487. begin
  488. hp:=get_file(l);
  489. if assigned(hp) then
  490. get_file_name:=hp^.name^
  491. else
  492. get_file_name:='';
  493. end;
  494. function tinputfilemanager.get_file_path(l :longint):string;
  495. var
  496. hp : pinputfile;
  497. begin
  498. hp:=get_file(l);
  499. if assigned(hp) then
  500. get_file_path:=hp^.path^
  501. else
  502. get_file_path:='';
  503. end;
  504. {****************************************************************************
  505. TModuleBase
  506. ****************************************************************************}
  507. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  508. var
  509. p : dirstr;
  510. n : NameStr;
  511. e : ExtStr;
  512. begin
  513. stringdispose(objfilename);
  514. stringdispose(asmfilename);
  515. stringdispose(ppufilename);
  516. stringdispose(staticlibfilename);
  517. stringdispose(sharedlibfilename);
  518. stringdispose(exefilename);
  519. stringdispose(outputpath);
  520. stringdispose(path);
  521. { Create names }
  522. fsplit(fn,p,n,e);
  523. n:=FixFileName(n);
  524. { set path }
  525. path:=stringdup(FixPath(p,false));
  526. { obj,asm,ppu names }
  527. p:=path^;
  528. if AllowOutput then
  529. begin
  530. if (OutputUnitDir<>'') then
  531. p:=OutputUnitDir
  532. else
  533. if (OutputExeDir<>'') then
  534. p:=OutputExeDir;
  535. end;
  536. outputpath:=stringdup(p);
  537. objfilename:=stringdup(p+n+target_info.objext);
  538. asmfilename:=stringdup(p+n+target_info.asmext);
  539. ppufilename:=stringdup(p+n+target_info.unitext);
  540. { lib and exe could be loaded with a file specified with -o }
  541. if AllowOutput and (OutputFile<>'') and (compile_level=1) then
  542. n:=OutputFile;
  543. staticlibfilename:=stringdup(p+target_os.libprefix+n+target_os.staticlibext);
  544. if target_info.target=target_i386_WIN32 then
  545. sharedlibfilename:=stringdup(p+n+target_os.sharedlibext)
  546. else
  547. sharedlibfilename:=stringdup(p+target_os.libprefix+n+target_os.sharedlibext);
  548. { output dir of exe can be specified separatly }
  549. if AllowOutput and (OutputExeDir<>'') then
  550. p:=OutputExeDir
  551. else
  552. p:=path^;
  553. exefilename:=stringdup(p+n+target_info.exeext);
  554. end;
  555. constructor tmodulebase.init(const s:string);
  556. begin
  557. modulename:=stringdup(Upper(s));
  558. realmodulename:=stringdup(s);
  559. mainsource:=nil;
  560. ppufilename:=nil;
  561. objfilename:=nil;
  562. asmfilename:=nil;
  563. staticlibfilename:=nil;
  564. sharedlibfilename:=nil;
  565. exefilename:=nil;
  566. outputpath:=nil;
  567. path:=nil;
  568. { unit index }
  569. inc(global_unit_count);
  570. unit_index:=global_unit_count;
  571. { sources }
  572. new(sourcefiles,init);
  573. end;
  574. destructor tmodulebase.done;
  575. begin
  576. if assigned(sourcefiles) then
  577. dispose(sourcefiles,done);
  578. sourcefiles:=nil;
  579. stringdispose(objfilename);
  580. stringdispose(asmfilename);
  581. stringdispose(ppufilename);
  582. stringdispose(staticlibfilename);
  583. stringdispose(sharedlibfilename);
  584. stringdispose(exefilename);
  585. stringdispose(outputpath);
  586. stringdispose(path);
  587. stringdispose(modulename);
  588. stringdispose(realmodulename);
  589. stringdispose(mainsource);
  590. inherited done;
  591. end;
  592. end.
  593. {
  594. $Log$
  595. Revision 1.5 2000-11-07 20:48:33 peter
  596. * removed ref_count from pinputfile it's not used
  597. Revision 1.4 2000/10/31 22:02:46 peter
  598. * symtable splitted, no real code changes
  599. Revision 1.3 2000/10/14 21:52:54 peter
  600. * fixed memory leaks
  601. Revision 1.2 2000/09/24 15:06:16 peter
  602. * use defines.inc
  603. Revision 1.1 2000/08/27 16:11:50 peter
  604. * moved some util functions from globals,cobjects to cutils
  605. * splitted files into finput,fmodule
  606. }