finput.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. is_macro,
  33. endoffile, { still bytes left to read }
  34. closed : boolean; { is the file closed }
  35. buf : pchar; { buffer }
  36. bufstart, { buffer start position in the file }
  37. bufsize, { amount of bytes in the buffer }
  38. maxbufsize : longint; { size in memory for the buffer }
  39. saveinputpointer : pchar; { save fields for scanner variables }
  40. savelastlinepos,
  41. saveline_no : longint;
  42. linebuf : plongintarr; { line buffer to retrieve lines }
  43. maxlinebuf : longint;
  44. ref_index : longint;
  45. ref_next : tinputfile;
  46. constructor create(const fn:TPathStr);
  47. destructor destroy;override;
  48. procedure setpos(l:longint);
  49. procedure seekbuf(fpos:longint);
  50. procedure readbuf;
  51. function open:boolean;
  52. procedure close;
  53. procedure tempclose;
  54. function tempopen:boolean;
  55. procedure setmacro(p:pchar;len:longint);
  56. procedure setline(line,linepos:longint);
  57. function getlinestr(l:longint):string;
  58. function getfiletime:longint;
  59. protected
  60. filetime : longint;
  61. function fileopen(const filename: TPathStr): boolean; virtual; abstract;
  62. function fileseek(pos: longint): boolean; virtual; abstract;
  63. function fileread(var databuf; maxsize: longint): longint; virtual; abstract;
  64. function fileeof: boolean; virtual; abstract;
  65. function fileclose: boolean; virtual; abstract;
  66. procedure filegettime; virtual; abstract;
  67. end;
  68. tdosinputfile = class(tinputfile)
  69. protected
  70. function fileopen(const filename: TPathStr): boolean; override;
  71. function fileseek(pos: longint): boolean; override;
  72. function fileread(var databuf; maxsize: longint): longint; override;
  73. function fileeof: boolean; override;
  74. function fileclose: boolean; override;
  75. procedure filegettime; override;
  76. private
  77. f : TCCustomFileStream; { current file handle }
  78. end;
  79. tinputfilemanager = class
  80. files : tinputfile;
  81. last_ref_index : longint;
  82. cacheindex : longint;
  83. cacheinputfile : tinputfile;
  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,ms_compile,
  98. ms_second_load,ms_second_compile,
  99. ms_compiled
  100. );
  101. const
  102. ModuleStateStr : array[TModuleState] of string[20] = (
  103. 'Unknown',
  104. 'Registered',
  105. 'Load','Compile',
  106. 'Second_Load','Second_Compile',
  107. 'Compiled'
  108. );
  109. type
  110. tmodulebase = class(TLinkedListItem)
  111. { index }
  112. unit_index : longint; { global counter for browser }
  113. { status }
  114. state : tmodulestate;
  115. { sources }
  116. sourcefiles : tinputfilemanager;
  117. { paths and filenames }
  118. paramallowoutput : boolean; { original allowoutput parameter }
  119. modulename, { name of the module in uppercase }
  120. realmodulename: pshortstring; { name of the module in the orignal case }
  121. paramfn, { original filename }
  122. mainsource, { name of the main sourcefile }
  123. objfilename, { fullname of the objectfile }
  124. asmfilename, { fullname of the assemblerfile }
  125. ppufilename, { fullname of the ppufile }
  126. importlibfilename, { fullname of the import libraryfile }
  127. staticlibfilename, { fullname of the static libraryfile }
  128. sharedlibfilename, { fullname of the shared libraryfile }
  129. mapfilename, { fullname of the mapfile }
  130. exefilename, { fullname of the exefile }
  131. dbgfilename, { fullname of the debug info file }
  132. path, { path where the module is find/created }
  133. outputpath : TPathStr; { path where the .s / .o / exe are created }
  134. constructor create(const s:string);
  135. destructor destroy;override;
  136. procedure setfilename(const fn:TPathStr;allowoutput:boolean);
  137. end;
  138. Function GetNamedFileTime (Const F : TPathStr) : Longint;
  139. implementation
  140. uses
  141. SysUtils,
  142. Comphook,
  143. {$ifndef GENERIC_CPU}
  144. {$ifdef heaptrc}
  145. fmodule,
  146. ppheap,
  147. {$endif heaptrc}
  148. {$endif not GENERIC_CPU}
  149. cfileutl,
  150. Globals,Systems
  151. ;
  152. {****************************************************************************
  153. Utils
  154. ****************************************************************************}
  155. Function GetNamedFileTime (Const F : TPathStr) : Longint;
  156. begin
  157. GetNamedFileTime:=do_getnamedfiletime(F);
  158. end;
  159. {****************************************************************************
  160. TINPUTFILE
  161. ****************************************************************************}
  162. constructor tinputfile.create(const fn:TPathStr);
  163. begin
  164. name:=ExtractFileName(fn);
  165. path:=ExtractFilePath(fn);
  166. inc_path:='';
  167. next:=nil;
  168. filetime:=-1;
  169. { file info }
  170. is_macro:=false;
  171. endoffile:=false;
  172. closed:=true;
  173. buf:=nil;
  174. bufstart:=0;
  175. bufsize:=0;
  176. maxbufsize:=InputFileBufSize;
  177. { save fields }
  178. saveinputpointer:=nil;
  179. saveline_no:=0;
  180. savelastlinepos:=0;
  181. { indexing refs }
  182. ref_next:=nil;
  183. ref_index:=0;
  184. { line buffer }
  185. linebuf:=nil;
  186. maxlinebuf:=0;
  187. end;
  188. destructor tinputfile.destroy;
  189. begin
  190. if not closed then
  191. close;
  192. { free memory }
  193. if assigned(linebuf) then
  194. freemem(linebuf,maxlinebuf shl 2);
  195. end;
  196. procedure tinputfile.setpos(l:longint);
  197. begin
  198. bufstart:=l;
  199. end;
  200. procedure tinputfile.seekbuf(fpos:longint);
  201. begin
  202. if closed then
  203. exit;
  204. fileseek(fpos);
  205. bufstart:=fpos;
  206. bufsize:=0;
  207. end;
  208. procedure tinputfile.readbuf;
  209. begin
  210. if is_macro then
  211. endoffile:=true;
  212. if closed then
  213. exit;
  214. inc(bufstart,bufsize);
  215. bufsize:=fileread(buf^,maxbufsize-1);
  216. buf[bufsize]:=#0;
  217. endoffile:=fileeof;
  218. end;
  219. function tinputfile.open:boolean;
  220. begin
  221. open:=false;
  222. if not closed then
  223. Close;
  224. if not fileopen(path+name) then
  225. exit;
  226. { file }
  227. endoffile:=false;
  228. closed:=false;
  229. Getmem(buf,MaxBufsize);
  230. buf[0]:=#0;
  231. bufstart:=0;
  232. bufsize:=0;
  233. open:=true;
  234. end;
  235. procedure tinputfile.close;
  236. begin
  237. if is_macro then
  238. begin
  239. if assigned(buf) then
  240. begin
  241. Freemem(buf,maxbufsize);
  242. buf:=nil;
  243. end;
  244. name:='';
  245. path:='';
  246. closed:=true;
  247. exit;
  248. end;
  249. if not closed then
  250. begin
  251. fileclose;
  252. closed:=true;
  253. end;
  254. if assigned(buf) then
  255. begin
  256. Freemem(buf,maxbufsize);
  257. buf:=nil;
  258. end;
  259. bufstart:=0;
  260. end;
  261. procedure tinputfile.tempclose;
  262. begin
  263. if is_macro then
  264. exit;
  265. if not closed then
  266. begin
  267. fileclose;
  268. if assigned(buf) then
  269. begin
  270. Freemem(buf,maxbufsize);
  271. buf:=nil;
  272. end;
  273. closed:=true;
  274. end;
  275. end;
  276. function tinputfile.tempopen:boolean;
  277. begin
  278. tempopen:=false;
  279. if is_macro then
  280. begin
  281. { seek buffer postion to bufstart }
  282. if bufstart>0 then
  283. begin
  284. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  285. bufstart:=0;
  286. end;
  287. tempopen:=true;
  288. exit;
  289. end;
  290. if not closed then
  291. exit;
  292. if not fileopen(path+name) then
  293. exit;
  294. closed:=false;
  295. { get new mem }
  296. Getmem(buf,maxbufsize);
  297. { restore state }
  298. fileseek(BufStart);
  299. bufsize:=0;
  300. readbuf;
  301. tempopen:=true;
  302. end;
  303. procedure tinputfile.setmacro(p:pchar;len:longint);
  304. begin
  305. { create new buffer }
  306. getmem(buf,len+1);
  307. move(p^,buf^,len);
  308. buf[len]:=#0;
  309. { reset }
  310. bufstart:=0;
  311. bufsize:=len;
  312. maxbufsize:=len+1;
  313. is_macro:=true;
  314. endoffile:=true;
  315. closed:=true;
  316. end;
  317. procedure tinputfile.setline(line,linepos:longint);
  318. var
  319. oldlinebuf : plongintarr;
  320. begin
  321. if line<1 then
  322. exit;
  323. while (line>=maxlinebuf) do
  324. begin
  325. oldlinebuf:=linebuf;
  326. { create new linebuf and move old info }
  327. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  328. if assigned(oldlinebuf) then
  329. begin
  330. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  331. freemem(oldlinebuf,maxlinebuf shl 2);
  332. end;
  333. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,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:=true;
  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. files:=nil;
  445. last_ref_index:=0;
  446. cacheindex:=0;
  447. cacheinputfile:=nil;
  448. end;
  449. destructor tinputfilemanager.destroy;
  450. var
  451. hp : tinputfile;
  452. begin
  453. hp:=files;
  454. while assigned(hp) do
  455. begin
  456. files:=files.ref_next;
  457. hp.free;
  458. hp:=files;
  459. end;
  460. last_ref_index:=0;
  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. inc(last_ref_index);
  468. f.ref_next:=files;
  469. f.ref_index:=last_ref_index;
  470. files:=f;
  471. { update cache }
  472. cacheindex:=last_ref_index;
  473. cacheinputfile:=f;
  474. {$ifndef GENERIC_CPU}
  475. {$ifdef heaptrc}
  476. ppheap_register_file(f.path+f.name,current_module.unit_index*100000+f.ref_index);
  477. {$endif heaptrc}
  478. {$endif not GENERIC_CPU}
  479. end;
  480. function tinputfilemanager.get_file(l :longint) : tinputfile;
  481. var
  482. ff : tinputfile;
  483. begin
  484. { check cache }
  485. if (l=cacheindex) and assigned(cacheinputfile) then
  486. begin
  487. get_file:=cacheinputfile;
  488. exit;
  489. end;
  490. ff:=files;
  491. while assigned(ff) and (ff.ref_index<>l) do
  492. ff:=ff.ref_next;
  493. if assigned(ff) then
  494. begin
  495. cacheindex:=ff.ref_index;
  496. cacheinputfile:=ff;
  497. end;
  498. get_file:=ff;
  499. end;
  500. function tinputfilemanager.get_file_name(l :longint):TPathStr;
  501. var
  502. hp : tinputfile;
  503. begin
  504. hp:=get_file(l);
  505. if assigned(hp) then
  506. get_file_name:=hp.name
  507. else
  508. get_file_name:='';
  509. end;
  510. function tinputfilemanager.get_file_path(l :longint):TPathStr;
  511. var
  512. hp : tinputfile;
  513. begin
  514. hp:=get_file(l);
  515. if assigned(hp) then
  516. get_file_path:=hp.path
  517. else
  518. get_file_path:='';
  519. end;
  520. {****************************************************************************
  521. TModuleBase
  522. ****************************************************************************}
  523. procedure tmodulebase.setfilename(const fn:TPathStr;allowoutput:boolean);
  524. var
  525. p, n,
  526. prefix,
  527. suffix : TPathStr;
  528. begin
  529. { Create names }
  530. paramfn := fn;
  531. paramallowoutput := allowoutput;
  532. p := FixPath(ExtractFilePath(fn),false);
  533. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  534. { set path }
  535. path:=p;
  536. { obj,asm,ppu names }
  537. if AllowOutput then
  538. begin
  539. if (OutputUnitDir<>'') then
  540. p:=OutputUnitDir
  541. else
  542. if (OutputExeDir<>'') then
  543. p:=OutputExeDir;
  544. end;
  545. outputpath:=p;
  546. asmfilename:=p+n+target_info.asmext;
  547. objfilename:=p+n+target_info.objext;
  548. ppufilename:=p+n+target_info.unitext;
  549. importlibfilename:=p+target_info.importlibprefix+n+target_info.importlibext;
  550. staticlibfilename:=p+target_info.staticlibprefix+n+target_info.staticlibext;
  551. { output dir of exe can be specified separatly }
  552. if AllowOutput and (OutputExeDir<>'') then
  553. p:=OutputExeDir
  554. else
  555. p:=path;
  556. { lib and exe could be loaded with a file specified with -o }
  557. if AllowOutput and
  558. (compile_level=1) and
  559. (OutputFileName<>'')then
  560. begin
  561. exefilename:=p+OutputFileName;
  562. sharedlibfilename:=p+OutputFileName;
  563. n:=ChangeFileExt(OutputFileName,''); { for mapfilename and dbgfilename }
  564. end
  565. else
  566. begin
  567. exefilename:=p+n+target_info.exeext;
  568. if Assigned(OutputPrefix) then
  569. prefix := OutputPrefix^
  570. else
  571. prefix := target_info.sharedlibprefix;
  572. if Assigned(OutputSuffix) then
  573. suffix := OutputSuffix^
  574. else
  575. suffix := '';
  576. sharedlibfilename:=p+prefix+n+suffix+target_info.sharedlibext;
  577. end;
  578. mapfilename:=p+n+'.map';
  579. dbgfilename:=p+n+'.dbg';
  580. end;
  581. constructor tmodulebase.create(const s:string);
  582. begin
  583. modulename:=stringdup(Upper(s));
  584. realmodulename:=stringdup(s);
  585. mainsource:='';
  586. ppufilename:='';
  587. objfilename:='';
  588. asmfilename:='';
  589. importlibfilename:='';
  590. staticlibfilename:='';
  591. sharedlibfilename:='';
  592. exefilename:='';
  593. dbgfilename:='';
  594. mapfilename:='';
  595. outputpath:='';
  596. paramfn:='';
  597. path:='';
  598. { status }
  599. state:=ms_registered;
  600. { unit index }
  601. inc(global_unit_count);
  602. unit_index:=global_unit_count;
  603. { sources }
  604. sourcefiles:=TInputFileManager.Create;
  605. end;
  606. destructor tmodulebase.destroy;
  607. begin
  608. if assigned(sourcefiles) then
  609. sourcefiles.free;
  610. sourcefiles:=nil;
  611. stringdispose(modulename);
  612. stringdispose(realmodulename);
  613. inherited destroy;
  614. end;
  615. end.