finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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;
  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. 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. mainsource : pshortstring; { name of the main sourcefile }
  132. constructor create(const s:string);
  133. destructor destroy;override;
  134. procedure setfilename(const fn:string;allowoutput:boolean);
  135. end;
  136. Function GetNamedFileTime (Const F : String) : Longint;
  137. implementation
  138. uses
  139. SysUtils,
  140. GlobType,Comphook,
  141. {$ifdef heaptrc}
  142. fmodule,
  143. ppheap,
  144. {$endif heaptrc}
  145. CFileUtils,
  146. Globals,Systems
  147. ;
  148. {****************************************************************************
  149. Utils
  150. ****************************************************************************}
  151. Function GetNamedFileTime (Const F : String) : Longint;
  152. begin
  153. GetNamedFileTime:=do_getnamedfiletime(F);
  154. end;
  155. {****************************************************************************
  156. TINPUTFILE
  157. ****************************************************************************}
  158. constructor tinputfile.create(const fn:string);
  159. begin
  160. name:=stringdup(ExtractFileName(fn));
  161. path:=stringdup(ExtractFilePath(fn));
  162. next:=nil;
  163. filetime:=-1;
  164. { file info }
  165. is_macro:=false;
  166. endoffile:=false;
  167. closed:=true;
  168. buf:=nil;
  169. bufstart:=0;
  170. bufsize:=0;
  171. maxbufsize:=InputFileBufSize;
  172. { save fields }
  173. saveinputpointer:=nil;
  174. saveline_no:=0;
  175. savelastlinepos:=0;
  176. { indexing refs }
  177. ref_next:=nil;
  178. ref_index:=0;
  179. { line buffer }
  180. linebuf:=nil;
  181. maxlinebuf:=0;
  182. end;
  183. destructor tinputfile.destroy;
  184. begin
  185. if not closed then
  186. close;
  187. stringdispose(path);
  188. stringdispose(name);
  189. { free memory }
  190. if assigned(linebuf) then
  191. freemem(linebuf,maxlinebuf shl 2);
  192. end;
  193. procedure tinputfile.setpos(l:longint);
  194. begin
  195. bufstart:=l;
  196. end;
  197. procedure tinputfile.seekbuf(fpos:longint);
  198. begin
  199. if closed then
  200. exit;
  201. fileseek(fpos);
  202. bufstart:=fpos;
  203. bufsize:=0;
  204. end;
  205. procedure tinputfile.readbuf;
  206. begin
  207. if is_macro then
  208. endoffile:=true;
  209. if closed then
  210. exit;
  211. inc(bufstart,bufsize);
  212. bufsize:=fileread(buf^,maxbufsize-1);
  213. buf[bufsize]:=#0;
  214. endoffile:=fileeof;
  215. end;
  216. function tinputfile.open:boolean;
  217. begin
  218. open:=false;
  219. if not closed then
  220. Close;
  221. if not fileopen(path^+name^) then
  222. exit;
  223. { file }
  224. endoffile:=false;
  225. closed:=false;
  226. Getmem(buf,MaxBufsize);
  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. closed:=true;
  241. exit;
  242. end;
  243. if not closed then
  244. begin
  245. fileclose;
  246. closed:=true;
  247. end;
  248. if assigned(buf) then
  249. begin
  250. Freemem(buf,maxbufsize);
  251. buf:=nil;
  252. end;
  253. bufstart:=0;
  254. end;
  255. procedure tinputfile.tempclose;
  256. begin
  257. if is_macro then
  258. exit;
  259. if not closed then
  260. begin
  261. fileclose;
  262. if assigned(buf) then
  263. begin
  264. Freemem(buf,maxbufsize);
  265. buf:=nil;
  266. end;
  267. closed:=true;
  268. end;
  269. end;
  270. function tinputfile.tempopen:boolean;
  271. begin
  272. tempopen:=false;
  273. if is_macro then
  274. begin
  275. { seek buffer postion to bufstart }
  276. if bufstart>0 then
  277. begin
  278. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  279. bufstart:=0;
  280. end;
  281. tempopen:=true;
  282. exit;
  283. end;
  284. if not closed then
  285. exit;
  286. if not fileopen(path^+name^) then
  287. exit;
  288. closed:=false;
  289. { get new mem }
  290. Getmem(buf,maxbufsize);
  291. { restore state }
  292. fileseek(BufStart);
  293. bufsize:=0;
  294. readbuf;
  295. tempopen:=true;
  296. end;
  297. procedure tinputfile.setmacro(p:pchar;len:longint);
  298. begin
  299. { create new buffer }
  300. getmem(buf,len+1);
  301. move(p^,buf^,len);
  302. buf[len]:=#0;
  303. { reset }
  304. bufstart:=0;
  305. bufsize:=len;
  306. maxbufsize:=len+1;
  307. is_macro:=true;
  308. endoffile:=true;
  309. closed:=true;
  310. end;
  311. procedure tinputfile.setline(line,linepos:longint);
  312. var
  313. oldlinebuf : plongintarr;
  314. begin
  315. if line<1 then
  316. exit;
  317. while (line>=maxlinebuf) do
  318. begin
  319. oldlinebuf:=linebuf;
  320. { create new linebuf and move old info }
  321. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  322. if assigned(oldlinebuf) then
  323. begin
  324. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  325. freemem(oldlinebuf,maxlinebuf shl 2);
  326. end;
  327. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  328. inc(maxlinebuf,linebufincrease);
  329. end;
  330. linebuf^[line]:=linepos;
  331. end;
  332. function tinputfile.getlinestr(l:longint):string;
  333. var
  334. c : char;
  335. i,
  336. fpos : longint;
  337. p : pchar;
  338. begin
  339. getlinestr:='';
  340. if l<maxlinebuf then
  341. begin
  342. fpos:=linebuf^[l];
  343. { fpos is set negativ if the line was already written }
  344. { but we still know the correct value }
  345. if fpos<0 then
  346. fpos:=-fpos+1;
  347. if closed then
  348. open;
  349. { in current buf ? }
  350. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  351. begin
  352. seekbuf(fpos);
  353. readbuf;
  354. end;
  355. { the begin is in the buf now simply read until #13,#10 }
  356. i:=0;
  357. p:=@buf[fpos-bufstart];
  358. repeat
  359. c:=p^;
  360. if c=#0 then
  361. begin
  362. if endoffile then
  363. break;
  364. readbuf;
  365. p:=buf;
  366. c:=p^;
  367. end;
  368. if c in [#10,#13] then
  369. break;
  370. inc(i);
  371. getlinestr[i]:=c;
  372. inc(p);
  373. until (i=255);
  374. getlinestr[0]:=chr(i);
  375. end;
  376. end;
  377. function tinputfile.getfiletime:longint;
  378. begin
  379. if filetime=-1 then
  380. filegettime;
  381. getfiletime:=filetime;
  382. end;
  383. {****************************************************************************
  384. TDOSINPUTFILE
  385. ****************************************************************************}
  386. function tdosinputfile.fileopen(const filename: string): boolean;
  387. var
  388. ofm : byte;
  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. ofm:=filemode;
  399. filemode:=0;
  400. Assign(f,filename);
  401. {$I-}
  402. reset(f,1);
  403. {$I+}
  404. filemode:=ofm;
  405. fileopen:=(ioresult=0);
  406. end;
  407. function tdosinputfile.fileseek(pos: longint): boolean;
  408. begin
  409. {$I-}
  410. seek(f,Pos);
  411. {$I+}
  412. fileseek:=(ioresult=0);
  413. end;
  414. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  415. var
  416. w : longint;
  417. begin
  418. blockread(f,databuf,maxsize,w);
  419. fileread:=w;
  420. end;
  421. function tdosinputfile.fileeof: boolean;
  422. begin
  423. fileeof:=eof(f);
  424. end;
  425. function tdosinputfile.fileclose: boolean;
  426. begin
  427. {$I-}
  428. system.close(f);
  429. {$I+}
  430. fileclose:=(ioresult=0);
  431. end;
  432. procedure tdosinputfile.filegettime;
  433. begin
  434. filetime:=getnamedfiletime(path^+name^);
  435. end;
  436. {****************************************************************************
  437. Tinputfilemanager
  438. ****************************************************************************}
  439. constructor tinputfilemanager.create;
  440. begin
  441. files:=nil;
  442. last_ref_index:=0;
  443. cacheindex:=0;
  444. cacheinputfile:=nil;
  445. end;
  446. destructor tinputfilemanager.destroy;
  447. var
  448. hp : tinputfile;
  449. begin
  450. hp:=files;
  451. while assigned(hp) do
  452. begin
  453. files:=files.ref_next;
  454. hp.free;
  455. hp:=files;
  456. end;
  457. last_ref_index:=0;
  458. end;
  459. procedure tinputfilemanager.register_file(f : tinputfile);
  460. begin
  461. { don't register macro's }
  462. if f.is_macro then
  463. exit;
  464. inc(last_ref_index);
  465. f.ref_next:=files;
  466. f.ref_index:=last_ref_index;
  467. files:=f;
  468. { update cache }
  469. cacheindex:=last_ref_index;
  470. cacheinputfile:=f;
  471. {$ifdef heaptrc}
  472. ppheap_register_file(f.name^,current_module.unit_index*100000+f.ref_index);
  473. {$endif heaptrc}
  474. end;
  475. function tinputfilemanager.get_file(l :longint) : tinputfile;
  476. var
  477. ff : tinputfile;
  478. begin
  479. { check cache }
  480. if (l=cacheindex) and assigned(cacheinputfile) then
  481. begin
  482. get_file:=cacheinputfile;
  483. exit;
  484. end;
  485. ff:=files;
  486. while assigned(ff) and (ff.ref_index<>l) do
  487. ff:=ff.ref_next;
  488. if assigned(ff) then
  489. begin
  490. cacheindex:=ff.ref_index;
  491. cacheinputfile:=ff;
  492. end;
  493. get_file:=ff;
  494. end;
  495. function tinputfilemanager.get_file_name(l :longint):string;
  496. var
  497. hp : tinputfile;
  498. begin
  499. hp:=get_file(l);
  500. if assigned(hp) then
  501. get_file_name:=hp.name^
  502. else
  503. get_file_name:='';
  504. end;
  505. function tinputfilemanager.get_file_path(l :longint):string;
  506. var
  507. hp : tinputfile;
  508. begin
  509. hp:=get_file(l);
  510. if assigned(hp) then
  511. get_file_path:=hp.path^
  512. else
  513. get_file_path:='';
  514. end;
  515. {****************************************************************************
  516. TModuleBase
  517. ****************************************************************************}
  518. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  519. var
  520. p,n,
  521. prefix,
  522. suffix : string;
  523. begin
  524. stringdispose(objfilename);
  525. stringdispose(asmfilename);
  526. stringdispose(ppufilename);
  527. stringdispose(importlibfilename);
  528. stringdispose(staticlibfilename);
  529. stringdispose(sharedlibfilename);
  530. stringdispose(mapfilename);
  531. stringdispose(exefilename);
  532. stringdispose(outputpath);
  533. stringdispose(path);
  534. stringdispose(paramfn);
  535. { Create names }
  536. paramfn := stringdup(fn);
  537. paramallowoutput := allowoutput;
  538. p := FixPath(ExtractFilePath(fn),false);
  539. n := FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  540. { set path }
  541. path:=stringdup(p);
  542. { obj,asm,ppu names }
  543. if AllowOutput then
  544. begin
  545. if (OutputUnitDir<>'') then
  546. p:=OutputUnitDir
  547. else
  548. if (OutputExeDir<>'') then
  549. p:=OutputExeDir;
  550. end;
  551. outputpath:=stringdup(p);
  552. asmfilename:=stringdup(p+n+target_info.asmext);
  553. objfilename:=stringdup(p+n+target_info.objext);
  554. ppufilename:=stringdup(p+n+target_info.unitext);
  555. importlibfilename:=stringdup(p+target_info.staticClibprefix+'imp'+n+target_info.staticlibext);
  556. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  557. { output dir of exe can be specified separatly }
  558. if AllowOutput and (OutputExeDir<>'') then
  559. p:=OutputExeDir
  560. else
  561. p:=path^;
  562. { lib and exe could be loaded with a file specified with -o }
  563. if AllowOutput and
  564. (compile_level=1) and
  565. (OutputFileName<>'')then
  566. begin
  567. exefilename:=stringdup(p+OutputFileName);
  568. sharedlibfilename:=stringdup(p+OutputFileName);
  569. end
  570. else
  571. begin
  572. exefilename:=stringdup(p+n+target_info.exeext);
  573. if Assigned(OutputPrefix) then
  574. prefix := OutputPrefix^
  575. else
  576. prefix := target_info.sharedlibprefix;
  577. if Assigned(OutputSuffix) then
  578. suffix := OutputSuffix^
  579. else
  580. suffix := '';
  581. sharedlibfilename:=stringdup(p+prefix+n+suffix+target_info.sharedlibext);
  582. end;
  583. mapfilename:=stringdup(p+n+'.map');
  584. end;
  585. constructor tmodulebase.create(const s:string);
  586. begin
  587. modulename:=stringdup(Upper(s));
  588. realmodulename:=stringdup(s);
  589. mainsource:=nil;
  590. ppufilename:=nil;
  591. objfilename:=nil;
  592. asmfilename:=nil;
  593. importlibfilename:=nil;
  594. staticlibfilename:=nil;
  595. sharedlibfilename:=nil;
  596. exefilename:=nil;
  597. mapfilename:=nil;
  598. outputpath:=nil;
  599. paramfn:=nil;
  600. path:=nil;
  601. { status }
  602. state:=ms_registered;
  603. { unit index }
  604. inc(global_unit_count);
  605. unit_index:=global_unit_count;
  606. { sources }
  607. sourcefiles:=TInputFileManager.Create;
  608. end;
  609. destructor tmodulebase.destroy;
  610. begin
  611. if assigned(sourcefiles) then
  612. sourcefiles.free;
  613. sourcefiles:=nil;
  614. stringdispose(objfilename);
  615. stringdispose(asmfilename);
  616. stringdispose(ppufilename);
  617. stringdispose(importlibfilename);
  618. stringdispose(staticlibfilename);
  619. stringdispose(sharedlibfilename);
  620. stringdispose(exefilename);
  621. stringdispose(mapfilename);
  622. stringdispose(outputpath);
  623. stringdispose(path);
  624. stringdispose(modulename);
  625. stringdispose(realmodulename);
  626. stringdispose(mainsource);
  627. stringdispose(paramfn);
  628. inherited destroy;
  629. end;
  630. end.