finput.pas 19 KB

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