finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 : TAnsiCharDynArray; { 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[0],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. SetLength(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. buf:=nil;
  262. name:='';
  263. path:='';
  264. closed:=true;
  265. exit;
  266. end;
  267. if not closed then
  268. begin
  269. fileclose;
  270. closed:=true;
  271. end;
  272. if assigned(buf) then
  273. buf:=nil;
  274. bufstart:=0;
  275. end;
  276. procedure tinputfile.tempclose;
  277. begin
  278. if is_macro then
  279. exit;
  280. if not closed then
  281. begin
  282. fileclose;
  283. buf:=nil;
  284. closed:=true;
  285. end;
  286. end;
  287. function tinputfile.tempopen:boolean;
  288. begin
  289. tempopen:=false;
  290. if is_macro then
  291. begin
  292. { seek buffer postion to bufstart }
  293. if bufstart>0 then
  294. begin
  295. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  296. bufstart:=0;
  297. end;
  298. tempopen:=true;
  299. exit;
  300. end;
  301. if not closed then
  302. exit;
  303. if not fileopen(path+name) then
  304. exit;
  305. closed:=false;
  306. { get new mem }
  307. SetLength(buf,maxbufsize);
  308. { restore state }
  309. fileseek(BufStart);
  310. bufsize:=0;
  311. readbuf;
  312. tempopen:=true;
  313. end;
  314. procedure tinputfile.setmacro(p:pchar;len:longint);
  315. begin
  316. { create new buffer }
  317. SetLength(buf,len+1);
  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. linebuf:=reallocmem(linebuf,(maxlinebuf+linebufincrease)*sizeof(linebuf^[0]));
  336. fillchar(linebuf^[maxlinebuf],linebufincrease*sizeof(linebuf^[0]),0);
  337. inc(maxlinebuf,linebufincrease);
  338. end;
  339. linebuf^[line]:=linepos;
  340. end;
  341. function tinputfile.getlinestr(l:longint):string;
  342. var
  343. c : char;
  344. i,
  345. fpos : longint;
  346. p : pchar;
  347. begin
  348. getlinestr:='';
  349. if l<maxlinebuf then
  350. begin
  351. fpos:=linebuf^[l];
  352. { fpos is set negativ if the line was already written }
  353. { but we still know the correct value }
  354. if fpos<0 then
  355. fpos:=-fpos+1;
  356. if closed then
  357. open;
  358. { in current buf ? }
  359. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  360. begin
  361. seekbuf(fpos);
  362. readbuf;
  363. end;
  364. { the begin is in the buf now simply read until #13,#10 }
  365. i:=0;
  366. p:=@buf[fpos-bufstart];
  367. repeat
  368. c:=p^;
  369. if c=#0 then
  370. begin
  371. if endoffile then
  372. break;
  373. readbuf;
  374. p:=@buf[0];
  375. c:=p^;
  376. end;
  377. if c in [#10,#13] then
  378. break;
  379. inc(i);
  380. getlinestr[i]:=c;
  381. inc(p);
  382. until (i=255);
  383. getlinestr[0]:=chr(i);
  384. end;
  385. end;
  386. function tinputfile.getfiletime:longint;
  387. begin
  388. if filetime=-1 then
  389. filegettime;
  390. getfiletime:=filetime;
  391. end;
  392. {****************************************************************************
  393. TDOSINPUTFILE
  394. ****************************************************************************}
  395. function tdosinputfile.fileopen(const filename: TPathStr): boolean;
  396. begin
  397. { Check if file exists, this will also check if it is
  398. a real file and not a directory }
  399. if not fileexists(filename,false) then
  400. begin
  401. result:=false;
  402. exit;
  403. end;
  404. { Open file }
  405. fileopen:=false;
  406. try
  407. f:=CFileStreamClass.Create(filename,fmOpenRead);
  408. fileopen:=CStreamError=0;
  409. except
  410. end;
  411. end;
  412. function tdosinputfile.fileseek(pos: longint): boolean;
  413. begin
  414. fileseek:=false;
  415. try
  416. f.position:=Pos;
  417. fileseek:=true;
  418. except
  419. end;
  420. end;
  421. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  422. begin
  423. fileread:=f.Read(databuf,maxsize);
  424. end;
  425. function tdosinputfile.fileeof: boolean;
  426. begin
  427. fileeof:=f.eof();
  428. end;
  429. function tdosinputfile.fileclose: boolean;
  430. begin
  431. fileclose:=false;
  432. try
  433. f.Free;
  434. fileclose:=true;
  435. except
  436. end;
  437. end;
  438. procedure tdosinputfile.filegettime;
  439. begin
  440. filetime:=getnamedfiletime(path+name);
  441. end;
  442. {****************************************************************************
  443. Tinputfilemanager
  444. ****************************************************************************}
  445. constructor tinputfilemanager.create;
  446. begin
  447. end;
  448. destructor tinputfilemanager.destroy;
  449. var
  450. ifile : SizeInt;
  451. begin
  452. for ifile:=0 to nfiles-1 do
  453. files[ifile].free;
  454. FreeMem(files);
  455. end;
  456. procedure tinputfilemanager.register_file(f : tinputfile);
  457. begin
  458. { don't register macro's }
  459. if f.is_macro then
  460. exit;
  461. if nfiles=afiles then
  462. begin
  463. afiles:=afiles+4+SizeUint(afiles) div 4+SizeUint(afiles) div 8;
  464. ReallocMem(files,afiles*sizeof(files[0]));
  465. end;
  466. f.ref_index:=1+nfiles;
  467. files[nfiles]:=f;
  468. inc(nfiles);
  469. {$ifndef GENERIC_CPU}
  470. {$ifdef heaptrc}
  471. ppheap_register_file(f.path+f.name,current_module.unit_index*100000+f.ref_index);
  472. {$endif heaptrc}
  473. {$endif not GENERIC_CPU}
  474. end;
  475. function tinputfilemanager.get_file(l :longint) : tinputfile;
  476. begin
  477. if not ((l>=1) and (l<=nfiles)) then
  478. exit(nil);
  479. result:=files[l-1];
  480. end;
  481. function tinputfilemanager.get_file_name(l :longint):TPathStr;
  482. var
  483. hp : tinputfile;
  484. begin
  485. hp:=get_file(l);
  486. if assigned(hp) then
  487. get_file_name:=hp.name
  488. else
  489. get_file_name:='';
  490. end;
  491. function tinputfilemanager.get_file_path(l :longint):TPathStr;
  492. var
  493. hp : tinputfile;
  494. begin
  495. hp:=get_file(l);
  496. if assigned(hp) then
  497. get_file_path:=hp.path
  498. else
  499. get_file_path:='';
  500. end;
  501. {****************************************************************************
  502. TModuleBase
  503. ****************************************************************************}
  504. procedure tmodulebase.setfilename(const fn:TPathStr;allowoutput:boolean);
  505. var
  506. p, n,
  507. prefix,
  508. suffix : TPathStr;
  509. begin
  510. { Create names }
  511. paramfn := fn;
  512. paramallowoutput := allowoutput;
  513. p := FixPath(ExtractFilePath(fn),false);
  514. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  515. { set path }
  516. path:=p;
  517. { obj,asm,ppu names }
  518. if AllowOutput then
  519. begin
  520. if (OutputUnitDir<>'') then
  521. p:=OutputUnitDir
  522. else
  523. if (OutputExeDir<>'') then
  524. p:=OutputExeDir;
  525. end;
  526. outputpath:=p;
  527. asmfilename:=p+n+target_info.asmext;
  528. objfilename:=p+n+target_info.objext;
  529. ppufilename:=p+n+target_info.unitext;
  530. {$ifdef DEBUG_NODE_XML}
  531. ppxfilename:=p+n+'-node-dump.xml';
  532. {$endif DEBUG_NODE_XML}
  533. importlibfilename:=p+target_info.importlibprefix+n+target_info.importlibext;
  534. staticlibfilename:=p+target_info.staticlibprefix+n+target_info.staticlibext;
  535. exportfilename:=p+'exp'+n+target_info.objext;
  536. { output dir of exe can be specified separatly }
  537. if AllowOutput and (OutputExeDir<>'') then
  538. p:=OutputExeDir
  539. else
  540. p:=path;
  541. { lib and exe could be loaded with a file specified with -o }
  542. if AllowOutput and is_initial and
  543. (OutputFileName<>'')then
  544. begin
  545. exefilename:=p+OutputFileName;
  546. sharedlibfilename:=p+OutputFileName;
  547. n:=ChangeFileExt(OutputFileName,''); { for mapfilename and dbgfilename }
  548. end
  549. else
  550. begin
  551. exefilename:=p+n+target_info.exeext;
  552. if Assigned(OutputPrefix) then
  553. prefix := OutputPrefix^
  554. else
  555. prefix := target_info.sharedlibprefix;
  556. if Assigned(OutputSuffix) then
  557. suffix := OutputSuffix^
  558. else
  559. suffix := '';
  560. sharedlibfilename:=p+prefix+n+suffix+target_info.sharedlibext;
  561. end;
  562. mapfilename:=p+n+'.map';
  563. dbgfilename:=p+n+'.dbg';
  564. end;
  565. constructor tmodulebase.create(const s:string);
  566. begin
  567. modulename:=stringdup(Upper(s));
  568. realmodulename:=stringdup(s);
  569. mainsource:='';
  570. ppufilename:='';
  571. {$ifdef DEBUG_NODE_XML}
  572. ppxfilename:='';
  573. {$endif DEBUG_NODE_XML}
  574. objfilename:='';
  575. asmfilename:='';
  576. importlibfilename:='';
  577. staticlibfilename:='';
  578. sharedlibfilename:='';
  579. exefilename:='';
  580. dbgfilename:='';
  581. mapfilename:='';
  582. outputpath:='';
  583. paramfn:='';
  584. path:='';
  585. {$ifdef DEBUG_NODE_XML}
  586. { Setting ppxfilefail to true will stop it from being written to if it
  587. was never initialised, which happens if a module doesn't need
  588. recompiling. }
  589. ppxfilefail := True;
  590. {$endif DEBUG_NODE_XML}
  591. { status }
  592. state:=ms_registered;
  593. { unit index }
  594. inc(global_unit_count);
  595. unit_index:=global_unit_count;
  596. { sources }
  597. sourcefiles:=TInputFileManager.Create;
  598. end;
  599. destructor tmodulebase.destroy;
  600. begin
  601. if assigned(sourcefiles) then
  602. sourcefiles.free;
  603. sourcefiles:=nil;
  604. stringdispose(modulename);
  605. stringdispose(realmodulename);
  606. inherited destroy;
  607. end;
  608. end.