finput.pas 20 KB

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