finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an extended file management
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit finput;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses;
  23. const
  24. InputFileBufSize=32*1024;
  25. linebufincrease=512;
  26. type
  27. tlongintarr = array[0..1000000] of longint;
  28. plongintarr = ^tlongintarr;
  29. tinputfile = class
  30. path,name : pstring; { path and filename }
  31. next : tinputfile; { next file for reading }
  32. is_macro,
  33. endoffile, { still bytes left to read }
  34. closed : boolean; { is the file closed }
  35. buf : pchar; { buffer }
  36. bufstart, { buffer start position in the file }
  37. bufsize, { amount of bytes in the buffer }
  38. maxbufsize : longint; { size in memory for the buffer }
  39. saveinputpointer : pchar; { save fields for scanner variables }
  40. savelastlinepos,
  41. saveline_no : longint;
  42. linebuf : plongintarr; { line buffer to retrieve lines }
  43. maxlinebuf : longint;
  44. ref_index : longint; { to handle the browser refs }
  45. ref_next : tinputfile;
  46. constructor create(const fn:string);
  47. destructor destroy;override;
  48. procedure setpos(l:longint);
  49. procedure seekbuf(fpos:longint);
  50. procedure readbuf;
  51. function open:boolean;
  52. procedure close;
  53. procedure tempclose;
  54. function tempopen:boolean;
  55. procedure setmacro(p:pchar;len:longint);
  56. procedure setline(line,linepos:longint);
  57. function getlinestr(l:longint):string;
  58. function getfiletime:longint;
  59. protected
  60. filetime : longint;
  61. function fileopen(const filename: string): boolean; virtual; abstract;
  62. function fileseek(pos: longint): boolean; virtual; abstract;
  63. function fileread(var databuf; maxsize: longint): longint; virtual; abstract;
  64. function fileeof: boolean; virtual; abstract;
  65. function fileclose: boolean; virtual; abstract;
  66. procedure filegettime; virtual; abstract;
  67. end;
  68. tdosinputfile = class(tinputfile)
  69. protected
  70. function fileopen(const filename: string): boolean; override;
  71. function fileseek(pos: longint): boolean; override;
  72. function fileread(var databuf; maxsize: longint): longint; override;
  73. function fileeof: boolean; override;
  74. function fileclose: boolean; override;
  75. procedure filegettime; override;
  76. private
  77. f : file; { current file handle }
  78. end;
  79. tinputfilemanager = class
  80. files : tinputfile;
  81. last_ref_index : longint;
  82. cacheindex : longint;
  83. cacheinputfile : tinputfile;
  84. constructor create;
  85. destructor destroy;override;
  86. procedure register_file(f : tinputfile);
  87. procedure inverse_register_indexes;
  88. function get_file(l:longint) : tinputfile;
  89. function get_file_name(l :longint):string;
  90. function get_file_path(l :longint):string;
  91. end;
  92. {****************************************************************************
  93. TModuleBase
  94. ****************************************************************************}
  95. type
  96. tmodulestate = (ms_unknown,
  97. ms_registered,
  98. ms_load,ms_compile,
  99. ms_second_load,ms_second_compile,
  100. ms_compiled
  101. );
  102. const
  103. ModuleStateStr : array[TModuleState] of string[20] = (
  104. 'Unknown',
  105. 'Registered',
  106. 'Load','Compile',
  107. 'Second_Load','Second_Compile',
  108. 'Compiled'
  109. );
  110. type
  111. tmodulebase = class(TLinkedListItem)
  112. { index }
  113. unit_index : longint; { global counter for browser }
  114. { status }
  115. state : tmodulestate;
  116. { sources }
  117. sourcefiles : tinputfilemanager;
  118. { paths and filenames }
  119. paramallowoutput : boolean; { original allowoutput parameter }
  120. paramfn, { original filename }
  121. path, { path where the module is find/created }
  122. outputpath, { path where the .s / .o / exe are created }
  123. modulename, { name of the module in uppercase }
  124. realmodulename, { name of the module in the orignal case }
  125. objfilename, { fullname of the objectfile }
  126. newfilename, { fullname of the assemblerfile }
  127. ppufilename, { fullname of the ppufile }
  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 : pstring; { name of the main sourcefile }
  133. constructor create(const s:string);
  134. destructor destroy;override;
  135. procedure setfilename(const fn:string;allowoutput:boolean);
  136. function get_asmfilename : string;
  137. end;
  138. implementation
  139. uses
  140. {$ifdef Delphi}
  141. dmisc,
  142. {$else Delphi}
  143. dos,
  144. {$endif Delphi}
  145. {$ifdef heaptrc}
  146. fmodule,
  147. ppheap,
  148. {$endif heaptrc}
  149. globals,systems
  150. ;
  151. {****************************************************************************
  152. TINPUTFILE
  153. ****************************************************************************}
  154. constructor tinputfile.create(const fn:string);
  155. var
  156. p:dirstr;
  157. n:namestr;
  158. e:extstr;
  159. begin
  160. FSplit(fn,p,n,e);
  161. name:=stringdup(n+e);
  162. path:=stringdup(p);
  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) 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 : dirstr;
  533. n : NameStr;
  534. e : ExtStr;
  535. prefix,
  536. suffix,
  537. extension : NameStr;
  538. begin
  539. stringdispose(objfilename);
  540. stringdispose(newfilename);
  541. stringdispose(ppufilename);
  542. stringdispose(staticlibfilename);
  543. stringdispose(sharedlibfilename);
  544. stringdispose(mapfilename);
  545. stringdispose(exefilename);
  546. stringdispose(outputpath);
  547. stringdispose(path);
  548. { Create names }
  549. paramfn := stringdup(fn);
  550. paramallowoutput := allowoutput;
  551. fsplit(fn,p,n,e);
  552. n:=FixFileName(n);
  553. { set path }
  554. path:=stringdup(FixPath(p,false));
  555. { obj,asm,ppu names }
  556. p:=path^;
  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. newfilename := stringdup(n);
  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 OutputFile <> '' then n:=OutputFile;
  576. if Assigned(OutputPrefix) then prefix := OutputPrefix^;
  577. if Assigned(OutputSuffix) then suffix := OutputSuffix^;
  578. if OutputExtension <> '' then extension := OutputExtension;
  579. end;
  580. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  581. { output dir of exe can be specified separatly }
  582. if AllowOutput and (OutputExeDir<>'') then
  583. p:=OutputExeDir
  584. else
  585. p:=path^;
  586. sharedlibfilename:=stringdup(p+prefix+n+suffix+extension);
  587. exefilename:=stringdup(p+n+target_info.exeext);
  588. mapfilename:=stringdup(p+n+'.map');
  589. end;
  590. constructor tmodulebase.create(const s:string);
  591. begin
  592. modulename:=stringdup(Upper(s));
  593. realmodulename:=stringdup(s);
  594. mainsource:=nil;
  595. ppufilename:=nil;
  596. objfilename:=nil;
  597. newfilename:=nil;
  598. staticlibfilename:=nil;
  599. sharedlibfilename:=nil;
  600. exefilename:=nil;
  601. mapfilename:=nil;
  602. outputpath:=nil;
  603. path:=nil;
  604. { status }
  605. state:=ms_registered;
  606. { unit index }
  607. inc(global_unit_count);
  608. unit_index:=global_unit_count;
  609. { sources }
  610. sourcefiles:=TInputFileManager.Create;
  611. end;
  612. function tmodulebase.get_asmfilename : string;
  613. begin
  614. get_asmfilename:=outputpath^+newfilename^+target_info.asmext;
  615. end;
  616. destructor tmodulebase.destroy;
  617. begin
  618. if assigned(sourcefiles) then
  619. sourcefiles.free;
  620. sourcefiles:=nil;
  621. stringdispose(objfilename);
  622. stringdispose(newfilename);
  623. stringdispose(ppufilename);
  624. stringdispose(staticlibfilename);
  625. stringdispose(sharedlibfilename);
  626. stringdispose(exefilename);
  627. stringdispose(mapfilename);
  628. stringdispose(outputpath);
  629. stringdispose(path);
  630. stringdispose(modulename);
  631. stringdispose(realmodulename);
  632. stringdispose(mainsource);
  633. inherited destroy;
  634. end;
  635. end.
  636. {
  637. $Log$
  638. Revision 1.26 2004-08-02 07:15:54 michael
  639. + Patch from Christian Iversen to implement LIBPREFIX/SUFFIX/EXTENSION directives
  640. Revision 1.25 2004/06/20 08:55:29 florian
  641. * logs truncated
  642. Revision 1.24 2004/06/16 20:07:07 florian
  643. * dwarf branch merged
  644. Revision 1.23.2.1 2004/04/28 21:46:56 peter
  645. * compile fixes for x86-64
  646. }