finput.pas 20 KB

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