2
0

finput.pas 20 KB

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