finput.pas 20 KB

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