finput.pas 20 KB

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