2
0

finput.pas 19 KB

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