finput.pas 20 KB

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