finput.pas 19 KB

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