finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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;
  22. const
  23. InputFileBufSize=32*1024;
  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; { to handle the browser refs }
  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 : file; { 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. procedure inverse_register_indexes;
  87. function get_file(l:longint) : tinputfile;
  88. function get_file_name(l :longint):string;
  89. function get_file_path(l :longint):string;
  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. paramfn, { original filename }
  120. path, { path where the module is find/created }
  121. outputpath, { path where the .s / .o / exe are created }
  122. modulename, { name of the module in uppercase }
  123. realmodulename, { name of the module in the orignal case }
  124. objfilename, { fullname of the objectfile }
  125. asmfilename, { fullname of the assemblerfile }
  126. ppufilename, { fullname of the ppufile }
  127. importlibfilename, { fullname of the import libraryfile }
  128. staticlibfilename, { fullname of the static libraryfile }
  129. sharedlibfilename, { fullname of the shared libraryfile }
  130. mapfilename, { fullname of the mapfile }
  131. exefilename, { fullname of the exefile }
  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. CFileUtils,
  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. bufstart:=0;
  229. bufsize:=0;
  230. open:=true;
  231. end;
  232. procedure tinputfile.close;
  233. begin
  234. if is_macro then
  235. begin
  236. if assigned(buf) then
  237. begin
  238. Freemem(buf,maxbufsize);
  239. buf:=nil;
  240. end;
  241. closed:=true;
  242. exit;
  243. end;
  244. if not closed then
  245. begin
  246. fileclose;
  247. closed:=true;
  248. end;
  249. if assigned(buf) then
  250. begin
  251. Freemem(buf,maxbufsize);
  252. buf:=nil;
  253. end;
  254. bufstart:=0;
  255. end;
  256. procedure tinputfile.tempclose;
  257. begin
  258. if is_macro then
  259. exit;
  260. if not closed then
  261. begin
  262. fileclose;
  263. if assigned(buf) then
  264. begin
  265. Freemem(buf,maxbufsize);
  266. buf:=nil;
  267. end;
  268. closed:=true;
  269. end;
  270. end;
  271. function tinputfile.tempopen:boolean;
  272. begin
  273. tempopen:=false;
  274. if is_macro then
  275. begin
  276. { seek buffer postion to bufstart }
  277. if bufstart>0 then
  278. begin
  279. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  280. bufstart:=0;
  281. end;
  282. tempopen:=true;
  283. exit;
  284. end;
  285. if not closed then
  286. exit;
  287. if not fileopen(path^+name^) then
  288. exit;
  289. closed:=false;
  290. { get new mem }
  291. Getmem(buf,maxbufsize);
  292. { restore state }
  293. fileseek(BufStart);
  294. bufsize:=0;
  295. readbuf;
  296. tempopen:=true;
  297. end;
  298. procedure tinputfile.setmacro(p:pchar;len:longint);
  299. begin
  300. { create new buffer }
  301. getmem(buf,len+1);
  302. move(p^,buf^,len);
  303. buf[len]:=#0;
  304. { reset }
  305. bufstart:=0;
  306. bufsize:=len;
  307. maxbufsize:=len+1;
  308. is_macro:=true;
  309. endoffile:=true;
  310. closed:=true;
  311. end;
  312. procedure tinputfile.setline(line,linepos:longint);
  313. var
  314. oldlinebuf : plongintarr;
  315. begin
  316. if line<1 then
  317. exit;
  318. while (line>=maxlinebuf) do
  319. begin
  320. oldlinebuf:=linebuf;
  321. { create new linebuf and move old info }
  322. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  323. if assigned(oldlinebuf) then
  324. begin
  325. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  326. freemem(oldlinebuf,maxlinebuf shl 2);
  327. end;
  328. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  329. inc(maxlinebuf,linebufincrease);
  330. end;
  331. linebuf^[line]:=linepos;
  332. end;
  333. function tinputfile.getlinestr(l:longint):string;
  334. var
  335. c : char;
  336. i,
  337. fpos : longint;
  338. p : pchar;
  339. begin
  340. getlinestr:='';
  341. if l<maxlinebuf then
  342. begin
  343. fpos:=linebuf^[l];
  344. { fpos is set negativ if the line was already written }
  345. { but we still know the correct value }
  346. if fpos<0 then
  347. fpos:=-fpos+1;
  348. if closed then
  349. open;
  350. { in current buf ? }
  351. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  352. begin
  353. seekbuf(fpos);
  354. readbuf;
  355. end;
  356. { the begin is in the buf now simply read until #13,#10 }
  357. i:=0;
  358. p:=@buf[fpos-bufstart];
  359. repeat
  360. c:=p^;
  361. if c=#0 then
  362. begin
  363. if endoffile then
  364. break;
  365. readbuf;
  366. p:=buf;
  367. c:=p^;
  368. end;
  369. if c in [#10,#13] then
  370. break;
  371. inc(i);
  372. getlinestr[i]:=c;
  373. inc(p);
  374. until (i=255);
  375. getlinestr[0]:=chr(i);
  376. end;
  377. end;
  378. function tinputfile.getfiletime:longint;
  379. begin
  380. if filetime=-1 then
  381. filegettime;
  382. getfiletime:=filetime;
  383. end;
  384. {****************************************************************************
  385. TDOSINPUTFILE
  386. ****************************************************************************}
  387. function tdosinputfile.fileopen(const filename: string): boolean;
  388. var
  389. ofm : byte;
  390. begin
  391. { Check if file exists, this will also check if it is
  392. a real file and not a directory }
  393. if not fileexists(filename,false) then
  394. begin
  395. result:=false;
  396. exit;
  397. end;
  398. { Open file }
  399. ofm:=filemode;
  400. filemode:=0;
  401. Assign(f,filename);
  402. {$I-}
  403. reset(f,1);
  404. {$I+}
  405. filemode:=ofm;
  406. fileopen:=(ioresult=0);
  407. end;
  408. function tdosinputfile.fileseek(pos: longint): boolean;
  409. begin
  410. {$I-}
  411. seek(f,Pos);
  412. {$I+}
  413. fileseek:=(ioresult=0);
  414. end;
  415. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  416. var
  417. w : longint;
  418. begin
  419. blockread(f,databuf,maxsize,w);
  420. fileread:=w;
  421. end;
  422. function tdosinputfile.fileeof: boolean;
  423. begin
  424. fileeof:=eof(f);
  425. end;
  426. function tdosinputfile.fileclose: boolean;
  427. begin
  428. {$I-}
  429. system.close(f);
  430. {$I+}
  431. fileclose:=(ioresult=0);
  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. { this procedure is necessary after loading the
  477. sources files from a PPU file PM }
  478. procedure tinputfilemanager.inverse_register_indexes;
  479. var
  480. f : tinputfile;
  481. begin
  482. f:=files;
  483. while assigned(f) do
  484. begin
  485. f.ref_index:=last_ref_index-f.ref_index+1;
  486. f:=f.ref_next;
  487. end;
  488. { reset cache }
  489. cacheindex:=0;
  490. cacheinputfile:=nil;
  491. end;
  492. function tinputfilemanager.get_file(l :longint) : tinputfile;
  493. var
  494. ff : tinputfile;
  495. begin
  496. { check cache }
  497. if (l=cacheindex) and assigned(cacheinputfile) then
  498. begin
  499. get_file:=cacheinputfile;
  500. exit;
  501. end;
  502. ff:=files;
  503. while assigned(ff) and (ff.ref_index<>l) do
  504. ff:=ff.ref_next;
  505. get_file:=ff;
  506. end;
  507. function tinputfilemanager.get_file_name(l :longint):string;
  508. var
  509. hp : tinputfile;
  510. begin
  511. hp:=get_file(l);
  512. if assigned(hp) then
  513. get_file_name:=hp.name^
  514. else
  515. get_file_name:='';
  516. end;
  517. function tinputfilemanager.get_file_path(l :longint):string;
  518. var
  519. hp : tinputfile;
  520. begin
  521. hp:=get_file(l);
  522. if assigned(hp) then
  523. get_file_path:=hp.path^
  524. else
  525. get_file_path:='';
  526. end;
  527. {****************************************************************************
  528. TModuleBase
  529. ****************************************************************************}
  530. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  531. var
  532. p,n,e,
  533. extension,
  534. prefix,
  535. suffix : string;
  536. begin
  537. stringdispose(objfilename);
  538. stringdispose(asmfilename);
  539. stringdispose(ppufilename);
  540. stringdispose(importlibfilename);
  541. stringdispose(staticlibfilename);
  542. stringdispose(sharedlibfilename);
  543. stringdispose(mapfilename);
  544. stringdispose(exefilename);
  545. stringdispose(outputpath);
  546. stringdispose(path);
  547. stringdispose(paramfn);
  548. { Create names }
  549. paramfn := stringdup(fn);
  550. paramallowoutput := allowoutput;
  551. p := FixPath(ExtractFilePath(fn),false);
  552. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  553. e := ExtractFileExt(fn);
  554. { set path }
  555. path:=stringdup(p);
  556. { obj,asm,ppu names }
  557. if AllowOutput then
  558. begin
  559. if (OutputUnitDir<>'') then
  560. p:=OutputUnitDir
  561. else
  562. if (OutputExeDir<>'') then
  563. p:=OutputExeDir;
  564. end;
  565. outputpath:=stringdup(p);
  566. asmfilename:=stringdup(p+n+target_info.asmext);
  567. objfilename:=stringdup(p+n+target_info.objext);
  568. ppufilename:=stringdup(p+n+target_info.unitext);
  569. { lib and exe could be loaded with a file specified with -o }
  570. prefix := target_info.sharedlibprefix;
  571. suffix := '';
  572. extension := target_info.sharedlibext;
  573. if AllowOutput and (compile_level=1) then
  574. begin
  575. if OutputFileName <> '' then
  576. n:=OutputFileName;
  577. if Assigned(OutputPrefix) then
  578. prefix := OutputPrefix^;
  579. if Assigned(OutputSuffix) then
  580. suffix := OutputSuffix^;
  581. if ExtractFileExt(OutputFileName) <> '' then
  582. extension := ExtractFileExt(OutputFileName);
  583. end;
  584. importlibfilename:=stringdup(p+target_info.staticClibprefix+'imp'+n+target_info.staticlibext);
  585. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  586. { output dir of exe can be specified separatly }
  587. if AllowOutput and (OutputExeDir<>'') then
  588. p:=OutputExeDir
  589. else
  590. p:=path^;
  591. sharedlibfilename:=stringdup(p+prefix+n+suffix+extension);
  592. { don't use extension alone to check, it can be empty !! }
  593. if (OutputFileName<>'')then
  594. exefilename:=stringdup(p+OutputFileName)
  595. else
  596. exefilename:=stringdup(p+n+target_info.exeext);
  597. mapfilename:=stringdup(p+n+'.map');
  598. end;
  599. constructor tmodulebase.create(const s:string);
  600. begin
  601. modulename:=stringdup(Upper(s));
  602. realmodulename:=stringdup(s);
  603. mainsource:=nil;
  604. ppufilename:=nil;
  605. objfilename:=nil;
  606. asmfilename:=nil;
  607. importlibfilename:=nil;
  608. staticlibfilename:=nil;
  609. sharedlibfilename:=nil;
  610. exefilename:=nil;
  611. mapfilename:=nil;
  612. outputpath:=nil;
  613. paramfn:=nil;
  614. path:=nil;
  615. { status }
  616. state:=ms_registered;
  617. { unit index }
  618. inc(global_unit_count);
  619. unit_index:=global_unit_count;
  620. { sources }
  621. sourcefiles:=TInputFileManager.Create;
  622. end;
  623. destructor tmodulebase.destroy;
  624. begin
  625. if assigned(sourcefiles) then
  626. sourcefiles.free;
  627. sourcefiles:=nil;
  628. stringdispose(objfilename);
  629. stringdispose(asmfilename);
  630. stringdispose(ppufilename);
  631. stringdispose(importlibfilename);
  632. stringdispose(staticlibfilename);
  633. stringdispose(sharedlibfilename);
  634. stringdispose(exefilename);
  635. stringdispose(mapfilename);
  636. stringdispose(outputpath);
  637. stringdispose(path);
  638. stringdispose(modulename);
  639. stringdispose(realmodulename);
  640. stringdispose(mainsource);
  641. stringdispose(paramfn);
  642. inherited destroy;
  643. end;
  644. end.