finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an extended file management
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit finput;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,globtype,cclasses,cstreams;
  22. const
  23. InputFileBufSize=32*1024+1;
  24. linebufincrease=512;
  25. type
  26. tlongintarr = array[0..1000000] of longint;
  27. plongintarr = ^tlongintarr;
  28. tinputfile = class
  29. path,name : TPathStr; { path and filename }
  30. inc_path : TPathStr; { path if file was included with $I directive }
  31. next : tinputfile; { next file for reading }
  32. is_macro,
  33. endoffile, { still bytes left to read }
  34. closed : boolean; { is the file closed }
  35. buf : pchar; { buffer }
  36. bufstart, { buffer start position in the file }
  37. bufsize, { amount of bytes in the buffer }
  38. maxbufsize : longint; { size in memory for the buffer }
  39. saveinputpointer : pchar; { save fields for scanner variables }
  40. savelastlinepos,
  41. saveline_no : longint;
  42. linebuf : plongintarr; { line buffer to retrieve lines }
  43. maxlinebuf : longint;
  44. ref_index : longint;
  45. ref_next : tinputfile;
  46. constructor create(const fn:TPathStr);
  47. destructor destroy;override;
  48. procedure setpos(l:longint);
  49. procedure seekbuf(fpos:longint);
  50. procedure readbuf;
  51. function open:boolean;
  52. procedure close;
  53. procedure tempclose;
  54. function tempopen:boolean;
  55. procedure setmacro(p:pchar;len:longint);
  56. procedure setline(line,linepos:longint);
  57. function getlinestr(l:longint):string;
  58. function getfiletime:longint;
  59. protected
  60. filetime : longint;
  61. function fileopen(const filename: TPathStr): boolean; virtual; abstract;
  62. function fileseek(pos: longint): boolean; virtual; abstract;
  63. function fileread(var databuf; maxsize: longint): longint; virtual; abstract;
  64. function fileeof: boolean; virtual; abstract;
  65. function fileclose: boolean; virtual; abstract;
  66. procedure filegettime; virtual; abstract;
  67. end;
  68. tdosinputfile = class(tinputfile)
  69. protected
  70. function fileopen(const filename: TPathStr): boolean; override;
  71. function fileseek(pos: longint): boolean; override;
  72. function fileread(var databuf; maxsize: longint): longint; override;
  73. function fileeof: boolean; override;
  74. function fileclose: boolean; override;
  75. procedure filegettime; override;
  76. private
  77. f : TCCustomFileStream; { current file handle }
  78. end;
  79. tinputfilemanager = class
  80. files : tinputfile;
  81. last_ref_index : longint;
  82. cacheindex : longint;
  83. cacheinputfile : tinputfile;
  84. constructor create;
  85. destructor destroy;override;
  86. procedure register_file(f : tinputfile);
  87. function get_file(l:longint) : tinputfile;
  88. function get_file_name(l :longint):TPathStr;
  89. function get_file_path(l :longint):TPathStr;
  90. end;
  91. {****************************************************************************
  92. TModuleBase
  93. ****************************************************************************}
  94. type
  95. tmodulestate = (ms_unknown,
  96. ms_registered,
  97. ms_load,ms_compile,
  98. ms_second_load,ms_second_compile,
  99. ms_compiled
  100. );
  101. const
  102. ModuleStateStr : array[TModuleState] of string[20] = (
  103. 'Unknown',
  104. 'Registered',
  105. 'Load','Compile',
  106. 'Second_Load','Second_Compile',
  107. 'Compiled'
  108. );
  109. type
  110. tmodulebase = class(TLinkedListItem)
  111. { index }
  112. unit_index : longint; { global counter for browser }
  113. { status }
  114. state : tmodulestate;
  115. { sources }
  116. sourcefiles : tinputfilemanager;
  117. { paths and filenames }
  118. paramallowoutput : boolean; { original allowoutput parameter }
  119. modulename, { name of the module in uppercase }
  120. realmodulename: pshortstring; { name of the module in the orignal case }
  121. paramfn, { original filename }
  122. mainsource, { name of the main sourcefile }
  123. objfilename, { fullname of the objectfile }
  124. asmfilename, { fullname of the assemblerfile }
  125. ppufilename, { fullname of the ppufile }
  126. {$ifdef DEBUG_NODE_XML}
  127. ppxfilename, { fullname of the intermediate node XML file }
  128. {$endif DEBUG_NODE_XML}
  129. importlibfilename, { fullname of the import libraryfile }
  130. staticlibfilename, { fullname of the static libraryfile }
  131. sharedlibfilename, { fullname of the shared libraryfile }
  132. exportfilename, { fullname of the export file }
  133. mapfilename, { fullname of the mapfile }
  134. exefilename, { fullname of the exefile }
  135. dbgfilename, { fullname of the debug info file }
  136. path, { path where the module is find/created }
  137. outputpath : TPathStr; { path where the .s / .o / exe are created }
  138. {$ifdef DEBUG_NODE_XML}
  139. ppxfilefail: Boolean; { If the ppxfile could not be accessed, flag it }
  140. {$endif DEBUG_NODE_XML}
  141. constructor create(const s:string);
  142. destructor destroy;override;
  143. procedure setfilename(const fn:TPathStr;allowoutput:boolean);
  144. end;
  145. Function GetNamedFileTime (Const F : TPathStr) : Longint;
  146. implementation
  147. uses
  148. SysUtils,
  149. Comphook,
  150. {$ifndef GENERIC_CPU}
  151. {$ifdef heaptrc}
  152. fmodule,
  153. ppheap,
  154. {$endif heaptrc}
  155. {$endif not GENERIC_CPU}
  156. cfileutl,
  157. Globals,Systems
  158. ;
  159. {****************************************************************************
  160. Utils
  161. ****************************************************************************}
  162. Function GetNamedFileTime (Const F : TPathStr) : Longint;
  163. begin
  164. GetNamedFileTime:=do_getnamedfiletime(F);
  165. end;
  166. {****************************************************************************
  167. TINPUTFILE
  168. ****************************************************************************}
  169. constructor tinputfile.create(const fn:TPathStr);
  170. begin
  171. name:=ExtractFileName(fn);
  172. path:=ExtractFilePath(fn);
  173. inc_path:='';
  174. next:=nil;
  175. filetime:=-1;
  176. { file info }
  177. is_macro:=false;
  178. endoffile:=false;
  179. closed:=true;
  180. buf:=nil;
  181. bufstart:=0;
  182. bufsize:=0;
  183. maxbufsize:=InputFileBufSize;
  184. { save fields }
  185. saveinputpointer:=nil;
  186. saveline_no:=0;
  187. savelastlinepos:=0;
  188. { indexing refs }
  189. ref_next:=nil;
  190. ref_index:=0;
  191. { line buffer }
  192. linebuf:=nil;
  193. maxlinebuf:=0;
  194. end;
  195. destructor tinputfile.destroy;
  196. begin
  197. if not closed then
  198. close;
  199. { free memory }
  200. if assigned(linebuf) then
  201. freemem(linebuf,maxlinebuf*sizeof(linebuf^[0]));
  202. end;
  203. procedure tinputfile.setpos(l:longint);
  204. begin
  205. bufstart:=l;
  206. end;
  207. procedure tinputfile.seekbuf(fpos:longint);
  208. begin
  209. if closed then
  210. exit;
  211. fileseek(fpos);
  212. bufstart:=fpos;
  213. bufsize:=0;
  214. end;
  215. procedure tinputfile.readbuf;
  216. begin
  217. if is_macro then
  218. endoffile:=true;
  219. if closed then
  220. exit;
  221. inc(bufstart,bufsize);
  222. bufsize:=fileread(buf^,maxbufsize-1);
  223. buf[bufsize]:=#0;
  224. endoffile:=fileeof;
  225. end;
  226. function tinputfile.open:boolean;
  227. begin
  228. open:=false;
  229. if not closed then
  230. Close;
  231. if not fileopen(path+name) then
  232. exit;
  233. { file }
  234. endoffile:=false;
  235. closed:=false;
  236. Getmem(buf,MaxBufsize);
  237. buf[0]:=#0;
  238. bufstart:=0;
  239. bufsize:=0;
  240. open:=true;
  241. end;
  242. procedure tinputfile.close;
  243. begin
  244. if is_macro then
  245. begin
  246. if assigned(buf) then
  247. begin
  248. Freemem(buf,maxbufsize);
  249. buf:=nil;
  250. end;
  251. name:='';
  252. path:='';
  253. closed:=true;
  254. exit;
  255. end;
  256. if not closed then
  257. begin
  258. fileclose;
  259. closed:=true;
  260. end;
  261. if assigned(buf) then
  262. begin
  263. Freemem(buf,maxbufsize);
  264. buf:=nil;
  265. end;
  266. bufstart:=0;
  267. end;
  268. procedure tinputfile.tempclose;
  269. begin
  270. if is_macro then
  271. exit;
  272. if not closed then
  273. begin
  274. fileclose;
  275. if assigned(buf) then
  276. begin
  277. Freemem(buf,maxbufsize);
  278. buf:=nil;
  279. end;
  280. closed:=true;
  281. end;
  282. end;
  283. function tinputfile.tempopen:boolean;
  284. begin
  285. tempopen:=false;
  286. if is_macro then
  287. begin
  288. { seek buffer postion to bufstart }
  289. if bufstart>0 then
  290. begin
  291. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  292. bufstart:=0;
  293. end;
  294. tempopen:=true;
  295. exit;
  296. end;
  297. if not closed then
  298. exit;
  299. if not fileopen(path+name) then
  300. exit;
  301. closed:=false;
  302. { get new mem }
  303. Getmem(buf,maxbufsize);
  304. { restore state }
  305. fileseek(BufStart);
  306. bufsize:=0;
  307. readbuf;
  308. tempopen:=true;
  309. end;
  310. procedure tinputfile.setmacro(p:pchar;len:longint);
  311. begin
  312. { create new buffer }
  313. getmem(buf,len+1);
  314. move(p^,buf^,len);
  315. buf[len]:=#0;
  316. { reset }
  317. bufstart:=0;
  318. bufsize:=len;
  319. maxbufsize:=len+1;
  320. is_macro:=true;
  321. endoffile:=true;
  322. closed:=true;
  323. end;
  324. procedure tinputfile.setline(line,linepos:longint);
  325. begin
  326. if line<1 then
  327. exit;
  328. while (line>=maxlinebuf) do
  329. begin
  330. { create new linebuf and move old info }
  331. linebuf:=reallocmem(linebuf,(maxlinebuf+linebufincrease)*sizeof(linebuf^[0]));
  332. fillchar(linebuf^[maxlinebuf],linebufincrease*sizeof(linebuf^[0]),0);
  333. inc(maxlinebuf,linebufincrease);
  334. end;
  335. linebuf^[line]:=linepos;
  336. end;
  337. function tinputfile.getlinestr(l:longint):string;
  338. var
  339. c : char;
  340. i,
  341. fpos : longint;
  342. p : pchar;
  343. begin
  344. getlinestr:='';
  345. if l<maxlinebuf then
  346. begin
  347. fpos:=linebuf^[l];
  348. { fpos is set negativ if the line was already written }
  349. { but we still know the correct value }
  350. if fpos<0 then
  351. fpos:=-fpos+1;
  352. if closed then
  353. open;
  354. { in current buf ? }
  355. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  356. begin
  357. seekbuf(fpos);
  358. readbuf;
  359. end;
  360. { the begin is in the buf now simply read until #13,#10 }
  361. i:=0;
  362. p:=@buf[fpos-bufstart];
  363. repeat
  364. c:=p^;
  365. if c=#0 then
  366. begin
  367. if endoffile then
  368. break;
  369. readbuf;
  370. p:=buf;
  371. c:=p^;
  372. end;
  373. if c in [#10,#13] then
  374. break;
  375. inc(i);
  376. getlinestr[i]:=c;
  377. inc(p);
  378. until (i=255);
  379. getlinestr[0]:=chr(i);
  380. end;
  381. end;
  382. function tinputfile.getfiletime:longint;
  383. begin
  384. if filetime=-1 then
  385. filegettime;
  386. getfiletime:=filetime;
  387. end;
  388. {****************************************************************************
  389. TDOSINPUTFILE
  390. ****************************************************************************}
  391. function tdosinputfile.fileopen(const filename: TPathStr): boolean;
  392. begin
  393. { Check if file exists, this will also check if it is
  394. a real file and not a directory }
  395. if not fileexists(filename,false) then
  396. begin
  397. result:=false;
  398. exit;
  399. end;
  400. { Open file }
  401. fileopen:=false;
  402. try
  403. f:=CFileStreamClass.Create(filename,fmOpenRead);
  404. fileopen:=CStreamError=0;
  405. except
  406. end;
  407. end;
  408. function tdosinputfile.fileseek(pos: longint): boolean;
  409. begin
  410. fileseek:=false;
  411. try
  412. f.position:=Pos;
  413. fileseek:=true;
  414. except
  415. end;
  416. end;
  417. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  418. begin
  419. fileread:=f.Read(databuf,maxsize);
  420. end;
  421. function tdosinputfile.fileeof: boolean;
  422. begin
  423. fileeof:=f.eof();
  424. end;
  425. function tdosinputfile.fileclose: boolean;
  426. begin
  427. fileclose:=false;
  428. try
  429. f.Free;
  430. fileclose:=true;
  431. except
  432. end;
  433. end;
  434. procedure tdosinputfile.filegettime;
  435. begin
  436. filetime:=getnamedfiletime(path+name);
  437. end;
  438. {****************************************************************************
  439. Tinputfilemanager
  440. ****************************************************************************}
  441. constructor tinputfilemanager.create;
  442. begin
  443. files:=nil;
  444. last_ref_index:=0;
  445. cacheindex:=0;
  446. cacheinputfile:=nil;
  447. end;
  448. destructor tinputfilemanager.destroy;
  449. var
  450. hp : tinputfile;
  451. begin
  452. hp:=files;
  453. while assigned(hp) do
  454. begin
  455. files:=files.ref_next;
  456. hp.free;
  457. hp:=files;
  458. end;
  459. last_ref_index:=0;
  460. end;
  461. procedure tinputfilemanager.register_file(f : tinputfile);
  462. begin
  463. { don't register macro's }
  464. if f.is_macro then
  465. exit;
  466. inc(last_ref_index);
  467. f.ref_next:=files;
  468. f.ref_index:=last_ref_index;
  469. files:=f;
  470. { update cache }
  471. cacheindex:=last_ref_index;
  472. cacheinputfile:=f;
  473. {$ifndef GENERIC_CPU}
  474. {$ifdef heaptrc}
  475. ppheap_register_file(f.path+f.name,current_module.unit_index*100000+f.ref_index);
  476. {$endif heaptrc}
  477. {$endif not GENERIC_CPU}
  478. end;
  479. function tinputfilemanager.get_file(l :longint) : tinputfile;
  480. var
  481. ff : tinputfile;
  482. begin
  483. { check cache }
  484. if (l=cacheindex) and assigned(cacheinputfile) then
  485. begin
  486. get_file:=cacheinputfile;
  487. exit;
  488. end;
  489. ff:=files;
  490. while assigned(ff) and (ff.ref_index<>l) do
  491. ff:=ff.ref_next;
  492. if assigned(ff) then
  493. begin
  494. cacheindex:=ff.ref_index;
  495. cacheinputfile:=ff;
  496. end;
  497. get_file:=ff;
  498. end;
  499. function tinputfilemanager.get_file_name(l :longint):TPathStr;
  500. var
  501. hp : tinputfile;
  502. begin
  503. hp:=get_file(l);
  504. if assigned(hp) then
  505. get_file_name:=hp.name
  506. else
  507. get_file_name:='';
  508. end;
  509. function tinputfilemanager.get_file_path(l :longint):TPathStr;
  510. var
  511. hp : tinputfile;
  512. begin
  513. hp:=get_file(l);
  514. if assigned(hp) then
  515. get_file_path:=hp.path
  516. else
  517. get_file_path:='';
  518. end;
  519. {****************************************************************************
  520. TModuleBase
  521. ****************************************************************************}
  522. procedure tmodulebase.setfilename(const fn:TPathStr;allowoutput:boolean);
  523. var
  524. p, n,
  525. prefix,
  526. suffix : TPathStr;
  527. begin
  528. { Create names }
  529. paramfn := fn;
  530. paramallowoutput := allowoutput;
  531. p := FixPath(ExtractFilePath(fn),false);
  532. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  533. { set path }
  534. path:=p;
  535. { obj,asm,ppu names }
  536. if AllowOutput then
  537. begin
  538. if (OutputUnitDir<>'') then
  539. p:=OutputUnitDir
  540. else
  541. if (OutputExeDir<>'') then
  542. p:=OutputExeDir;
  543. end;
  544. outputpath:=p;
  545. asmfilename:=p+n+target_info.asmext;
  546. objfilename:=p+n+target_info.objext;
  547. ppufilename:=p+n+target_info.unitext;
  548. {$ifdef DEBUG_NODE_XML}
  549. ppxfilename:=p+n+'-node-dump.xml';
  550. {$endif DEBUG_NODE_XML}
  551. importlibfilename:=p+target_info.importlibprefix+n+target_info.importlibext;
  552. staticlibfilename:=p+target_info.staticlibprefix+n+target_info.staticlibext;
  553. exportfilename:=p+'exp'+n+target_info.objext;
  554. { output dir of exe can be specified separatly }
  555. if AllowOutput and (OutputExeDir<>'') then
  556. p:=OutputExeDir
  557. else
  558. p:=path;
  559. { lib and exe could be loaded with a file specified with -o }
  560. if AllowOutput and
  561. (compile_level=1) and
  562. (OutputFileName<>'')then
  563. begin
  564. exefilename:=p+OutputFileName;
  565. sharedlibfilename:=p+OutputFileName;
  566. n:=ChangeFileExt(OutputFileName,''); { for mapfilename and dbgfilename }
  567. end
  568. else
  569. begin
  570. exefilename:=p+n+target_info.exeext;
  571. if Assigned(OutputPrefix) then
  572. prefix := OutputPrefix^
  573. else
  574. prefix := target_info.sharedlibprefix;
  575. if Assigned(OutputSuffix) then
  576. suffix := OutputSuffix^
  577. else
  578. suffix := '';
  579. sharedlibfilename:=p+prefix+n+suffix+target_info.sharedlibext;
  580. end;
  581. mapfilename:=p+n+'.map';
  582. dbgfilename:=p+n+'.dbg';
  583. end;
  584. constructor tmodulebase.create(const s:string);
  585. begin
  586. modulename:=stringdup(Upper(s));
  587. realmodulename:=stringdup(s);
  588. mainsource:='';
  589. ppufilename:='';
  590. {$ifdef DEBUG_NODE_XML}
  591. ppxfilename:='';
  592. {$endif DEBUG_NODE_XML}
  593. objfilename:='';
  594. asmfilename:='';
  595. importlibfilename:='';
  596. staticlibfilename:='';
  597. sharedlibfilename:='';
  598. exefilename:='';
  599. dbgfilename:='';
  600. mapfilename:='';
  601. outputpath:='';
  602. paramfn:='';
  603. path:='';
  604. {$ifdef DEBUG_NODE_XML}
  605. { Setting ppxfilefail to true will stop it from being written to if it
  606. was never initialised, which happens if a module doesn't need
  607. recompiling. }
  608. ppxfilefail := True;
  609. {$endif DEBUG_NODE_XML}
  610. { status }
  611. state:=ms_registered;
  612. { unit index }
  613. inc(global_unit_count);
  614. unit_index:=global_unit_count;
  615. { sources }
  616. sourcefiles:=TInputFileManager.Create;
  617. end;
  618. destructor tmodulebase.destroy;
  619. begin
  620. if assigned(sourcefiles) then
  621. sourcefiles.free;
  622. sourcefiles:=nil;
  623. stringdispose(modulename);
  624. stringdispose(realmodulename);
  625. inherited destroy;
  626. end;
  627. end.