2
0

finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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,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 : pshortstring; { 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:string);
  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: string): 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: string): 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):string;
  88. function get_file_path(l :longint):string;
  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. paramfn, { original filename }
  119. path, { path where the module is find/created }
  120. outputpath, { path where the .s / .o / exe are created }
  121. modulename, { name of the module in uppercase }
  122. realmodulename, { name of the module in the orignal case }
  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. mainsource : pshortstring; { name of the main sourcefile }
  133. constructor create(const s:string);
  134. destructor destroy;override;
  135. procedure setfilename(const fn:string;allowoutput:boolean);
  136. end;
  137. Function GetNamedFileTime (Const F : String) : Longint;
  138. implementation
  139. uses
  140. SysUtils,
  141. GlobType,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 : String) : Longint;
  153. begin
  154. GetNamedFileTime:=do_getnamedfiletime(F);
  155. end;
  156. {****************************************************************************
  157. TINPUTFILE
  158. ****************************************************************************}
  159. constructor tinputfile.create(const fn:string);
  160. begin
  161. name:=stringdup(ExtractFileName(fn));
  162. path:=stringdup(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. stringdispose(path);
  189. stringdispose(name);
  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. 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: string): boolean;
  389. var
  390. ofm : byte;
  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. {$ifdef heaptrc}
  473. ppheap_register_file(f.name^,current_module.unit_index*100000+f.ref_index);
  474. {$endif heaptrc}
  475. end;
  476. function tinputfilemanager.get_file(l :longint) : tinputfile;
  477. var
  478. ff : tinputfile;
  479. begin
  480. { check cache }
  481. if (l=cacheindex) and assigned(cacheinputfile) then
  482. begin
  483. get_file:=cacheinputfile;
  484. exit;
  485. end;
  486. ff:=files;
  487. while assigned(ff) and (ff.ref_index<>l) do
  488. ff:=ff.ref_next;
  489. if assigned(ff) then
  490. begin
  491. cacheindex:=ff.ref_index;
  492. cacheinputfile:=ff;
  493. end;
  494. get_file:=ff;
  495. end;
  496. function tinputfilemanager.get_file_name(l :longint):string;
  497. var
  498. hp : tinputfile;
  499. begin
  500. hp:=get_file(l);
  501. if assigned(hp) then
  502. get_file_name:=hp.name^
  503. else
  504. get_file_name:='';
  505. end;
  506. function tinputfilemanager.get_file_path(l :longint):string;
  507. var
  508. hp : tinputfile;
  509. begin
  510. hp:=get_file(l);
  511. if assigned(hp) then
  512. get_file_path:=hp.path^
  513. else
  514. get_file_path:='';
  515. end;
  516. {****************************************************************************
  517. TModuleBase
  518. ****************************************************************************}
  519. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  520. var
  521. p,n,
  522. prefix,
  523. suffix : string;
  524. begin
  525. stringdispose(objfilename);
  526. stringdispose(asmfilename);
  527. stringdispose(ppufilename);
  528. stringdispose(importlibfilename);
  529. stringdispose(staticlibfilename);
  530. stringdispose(sharedlibfilename);
  531. stringdispose(mapfilename);
  532. stringdispose(exefilename);
  533. stringdispose(dbgfilename);
  534. stringdispose(outputpath);
  535. stringdispose(path);
  536. stringdispose(paramfn);
  537. { Create names }
  538. paramfn := stringdup(fn);
  539. paramallowoutput := allowoutput;
  540. p := FixPath(ExtractFilePath(fn),false);
  541. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  542. { set path }
  543. path:=stringdup(p);
  544. { obj,asm,ppu names }
  545. if AllowOutput then
  546. begin
  547. if (OutputUnitDir<>'') then
  548. p:=OutputUnitDir
  549. else
  550. if (OutputExeDir<>'') then
  551. p:=OutputExeDir;
  552. end;
  553. outputpath:=stringdup(p);
  554. asmfilename:=stringdup(p+n+target_info.asmext);
  555. objfilename:=stringdup(p+n+target_info.objext);
  556. ppufilename:=stringdup(p+n+target_info.unitext);
  557. importlibfilename:=stringdup(p+target_info.importlibprefix+n+target_info.importlibext);
  558. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  559. { output dir of exe can be specified separatly }
  560. if AllowOutput and (OutputExeDir<>'') then
  561. p:=OutputExeDir
  562. else
  563. p:=path^;
  564. { lib and exe could be loaded with a file specified with -o }
  565. if AllowOutput and
  566. (compile_level=1) and
  567. (OutputFileName<>'')then
  568. begin
  569. exefilename:=stringdup(p+OutputFileName);
  570. sharedlibfilename:=stringdup(p+OutputFileName);
  571. end
  572. else
  573. begin
  574. exefilename:=stringdup(p+n+target_info.exeext);
  575. if Assigned(OutputPrefix) then
  576. prefix := OutputPrefix^
  577. else
  578. prefix := target_info.sharedlibprefix;
  579. if Assigned(OutputSuffix) then
  580. suffix := OutputSuffix^
  581. else
  582. suffix := '';
  583. sharedlibfilename:=stringdup(p+prefix+n+suffix+target_info.sharedlibext);
  584. end;
  585. mapfilename:=stringdup(p+n+'.map');
  586. dbgfilename:=stringdup(p+n+'.dbg');
  587. end;
  588. constructor tmodulebase.create(const s:string);
  589. begin
  590. modulename:=stringdup(Upper(s));
  591. realmodulename:=stringdup(s);
  592. mainsource:=nil;
  593. ppufilename:=nil;
  594. objfilename:=nil;
  595. asmfilename:=nil;
  596. importlibfilename:=nil;
  597. staticlibfilename:=nil;
  598. sharedlibfilename:=nil;
  599. exefilename:=nil;
  600. dbgfilename:=nil;
  601. mapfilename:=nil;
  602. outputpath:=nil;
  603. paramfn:=nil;
  604. path:=nil;
  605. { status }
  606. state:=ms_registered;
  607. { unit index }
  608. inc(global_unit_count);
  609. unit_index:=global_unit_count;
  610. { sources }
  611. sourcefiles:=TInputFileManager.Create;
  612. end;
  613. destructor tmodulebase.destroy;
  614. begin
  615. if assigned(sourcefiles) then
  616. sourcefiles.free;
  617. sourcefiles:=nil;
  618. stringdispose(objfilename);
  619. stringdispose(asmfilename);
  620. stringdispose(ppufilename);
  621. stringdispose(importlibfilename);
  622. stringdispose(staticlibfilename);
  623. stringdispose(sharedlibfilename);
  624. stringdispose(exefilename);
  625. stringdispose(dbgfilename);
  626. stringdispose(mapfilename);
  627. stringdispose(outputpath);
  628. stringdispose(path);
  629. stringdispose(modulename);
  630. stringdispose(realmodulename);
  631. stringdispose(mainsource);
  632. stringdispose(paramfn);
  633. inherited destroy;
  634. end;
  635. end.