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