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