finput.pas 20 KB

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