finput.pas 19 KB

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