finput.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 USE_SYSUTILS}
  141. SysUtils,
  142. GlobType,
  143. {$ELSE USE_SYSUTILS}
  144. dos,
  145. {$ENDIF USE_SYSUTILS}
  146. {$ifdef heaptrc}
  147. fmodule,
  148. ppheap,
  149. {$endif heaptrc}
  150. globals,systems
  151. ;
  152. {****************************************************************************
  153. TINPUTFILE
  154. ****************************************************************************}
  155. constructor tinputfile.create(const fn:string);
  156. {$IFDEF USE_SYSUTILS}
  157. {$ELSE USE_SYSUTILS}
  158. var
  159. p:dirstr;
  160. n:namestr;
  161. e:extstr;
  162. {$ENDIF USE_SYSUTILS}
  163. begin
  164. {$IFDEF USE_SYSUTILS}
  165. name:=stringdup(SplitFileName(fn));
  166. path:=stringdup(SplitPath(fn));
  167. {$ELSE USE_SYSUTILS}
  168. FSplit(fn,p,n,e);
  169. name:=stringdup(n+e);
  170. path:=stringdup(p);
  171. {$ENDIF USE_SYSUTILS}
  172. next:=nil;
  173. filetime:=-1;
  174. { file info }
  175. is_macro:=false;
  176. endoffile:=false;
  177. closed:=true;
  178. buf:=nil;
  179. bufstart:=0;
  180. bufsize:=0;
  181. maxbufsize:=InputFileBufSize;
  182. { save fields }
  183. saveinputpointer:=nil;
  184. saveline_no:=0;
  185. savelastlinepos:=0;
  186. { indexing refs }
  187. ref_next:=nil;
  188. ref_index:=0;
  189. { line buffer }
  190. linebuf:=nil;
  191. maxlinebuf:=0;
  192. end;
  193. destructor tinputfile.destroy;
  194. begin
  195. if not closed then
  196. close;
  197. stringdispose(path);
  198. stringdispose(name);
  199. { free memory }
  200. if assigned(linebuf) then
  201. freemem(linebuf,maxlinebuf shl 2);
  202. end;
  203. procedure tinputfile.setpos(l:longint);
  204. begin
  205. bufstart:=l;
  206. end;
  207. procedure tinputfile.seekbuf(fpos:longint);
  208. begin
  209. if closed then
  210. exit;
  211. fileseek(fpos);
  212. bufstart:=fpos;
  213. bufsize:=0;
  214. end;
  215. procedure tinputfile.readbuf;
  216. begin
  217. if is_macro then
  218. endoffile:=true;
  219. if closed then
  220. exit;
  221. inc(bufstart,bufsize);
  222. bufsize:=fileread(buf^,maxbufsize-1);
  223. buf[bufsize]:=#0;
  224. endoffile:=fileeof;
  225. end;
  226. function tinputfile.open:boolean;
  227. begin
  228. open:=false;
  229. if not closed then
  230. Close;
  231. if not fileopen(path^+name^) then
  232. exit;
  233. { file }
  234. endoffile:=false;
  235. closed:=false;
  236. Getmem(buf,MaxBufsize);
  237. bufstart:=0;
  238. bufsize:=0;
  239. open:=true;
  240. end;
  241. procedure tinputfile.close;
  242. begin
  243. if is_macro then
  244. begin
  245. if assigned(buf) then
  246. begin
  247. Freemem(buf,maxbufsize);
  248. buf:=nil;
  249. end;
  250. closed:=true;
  251. exit;
  252. end;
  253. if not closed then
  254. begin
  255. fileclose;
  256. closed:=true;
  257. end;
  258. if assigned(buf) then
  259. begin
  260. Freemem(buf,maxbufsize);
  261. buf:=nil;
  262. end;
  263. bufstart:=0;
  264. end;
  265. procedure tinputfile.tempclose;
  266. begin
  267. if is_macro then
  268. exit;
  269. if not closed then
  270. begin
  271. fileclose;
  272. if assigned(buf) then
  273. begin
  274. Freemem(buf,maxbufsize);
  275. buf:=nil;
  276. end;
  277. closed:=true;
  278. end;
  279. end;
  280. function tinputfile.tempopen:boolean;
  281. begin
  282. tempopen:=false;
  283. if is_macro then
  284. begin
  285. { seek buffer postion to bufstart }
  286. if bufstart>0 then
  287. begin
  288. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  289. bufstart:=0;
  290. end;
  291. tempopen:=true;
  292. exit;
  293. end;
  294. if not closed then
  295. exit;
  296. if not fileopen(path^+name^) then
  297. exit;
  298. closed:=false;
  299. { get new mem }
  300. Getmem(buf,maxbufsize);
  301. { restore state }
  302. fileseek(BufStart);
  303. bufsize:=0;
  304. readbuf;
  305. tempopen:=true;
  306. end;
  307. procedure tinputfile.setmacro(p:pchar;len:longint);
  308. begin
  309. { create new buffer }
  310. getmem(buf,len+1);
  311. move(p^,buf^,len);
  312. buf[len]:=#0;
  313. { reset }
  314. bufstart:=0;
  315. bufsize:=len;
  316. maxbufsize:=len+1;
  317. is_macro:=true;
  318. endoffile:=true;
  319. closed:=true;
  320. end;
  321. procedure tinputfile.setline(line,linepos:longint);
  322. var
  323. oldlinebuf : plongintarr;
  324. begin
  325. if line<1 then
  326. exit;
  327. while (line>=maxlinebuf) do
  328. begin
  329. oldlinebuf:=linebuf;
  330. { create new linebuf and move old info }
  331. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  332. if assigned(oldlinebuf) then
  333. begin
  334. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  335. freemem(oldlinebuf,maxlinebuf shl 2);
  336. end;
  337. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  338. inc(maxlinebuf,linebufincrease);
  339. end;
  340. linebuf^[line]:=linepos;
  341. end;
  342. function tinputfile.getlinestr(l:longint):string;
  343. var
  344. c : char;
  345. i,
  346. fpos : longint;
  347. p : pchar;
  348. begin
  349. getlinestr:='';
  350. if l<maxlinebuf then
  351. begin
  352. fpos:=linebuf^[l];
  353. { fpos is set negativ if the line was already written }
  354. { but we still know the correct value }
  355. if fpos<0 then
  356. fpos:=-fpos+1;
  357. if closed then
  358. open;
  359. { in current buf ? }
  360. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  361. begin
  362. seekbuf(fpos);
  363. readbuf;
  364. end;
  365. { the begin is in the buf now simply read until #13,#10 }
  366. i:=0;
  367. p:=@buf[fpos-bufstart];
  368. repeat
  369. c:=p^;
  370. if c=#0 then
  371. begin
  372. if endoffile then
  373. break;
  374. readbuf;
  375. p:=buf;
  376. c:=p^;
  377. end;
  378. if c in [#10,#13] then
  379. break;
  380. inc(i);
  381. getlinestr[i]:=c;
  382. inc(p);
  383. until (i=255);
  384. getlinestr[0]:=chr(i);
  385. end;
  386. end;
  387. function tinputfile.getfiletime:longint;
  388. begin
  389. if filetime=-1 then
  390. filegettime;
  391. getfiletime:=filetime;
  392. end;
  393. {****************************************************************************
  394. TDOSINPUTFILE
  395. ****************************************************************************}
  396. function tdosinputfile.fileopen(const filename: string): boolean;
  397. var
  398. ofm : byte;
  399. begin
  400. { Check if file exists, this will also check if it is
  401. a real file and not a directory }
  402. if not fileexists(filename) then
  403. begin
  404. result:=false;
  405. exit;
  406. end;
  407. { Open file }
  408. ofm:=filemode;
  409. filemode:=0;
  410. Assign(f,filename);
  411. {$I-}
  412. reset(f,1);
  413. {$I+}
  414. filemode:=ofm;
  415. fileopen:=(ioresult=0);
  416. end;
  417. function tdosinputfile.fileseek(pos: longint): boolean;
  418. begin
  419. {$I-}
  420. seek(f,Pos);
  421. {$I+}
  422. fileseek:=(ioresult=0);
  423. end;
  424. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  425. var
  426. w : longint;
  427. begin
  428. blockread(f,databuf,maxsize,w);
  429. fileread:=w;
  430. end;
  431. function tdosinputfile.fileeof: boolean;
  432. begin
  433. fileeof:=eof(f);
  434. end;
  435. function tdosinputfile.fileclose: boolean;
  436. begin
  437. {$I-}
  438. system.close(f);
  439. {$I+}
  440. fileclose:=(ioresult=0);
  441. end;
  442. procedure tdosinputfile.filegettime;
  443. begin
  444. filetime:=getnamedfiletime(path^+name^);
  445. end;
  446. {****************************************************************************
  447. Tinputfilemanager
  448. ****************************************************************************}
  449. constructor tinputfilemanager.create;
  450. begin
  451. files:=nil;
  452. last_ref_index:=0;
  453. cacheindex:=0;
  454. cacheinputfile:=nil;
  455. end;
  456. destructor tinputfilemanager.destroy;
  457. var
  458. hp : tinputfile;
  459. begin
  460. hp:=files;
  461. while assigned(hp) do
  462. begin
  463. files:=files.ref_next;
  464. hp.free;
  465. hp:=files;
  466. end;
  467. last_ref_index:=0;
  468. end;
  469. procedure tinputfilemanager.register_file(f : tinputfile);
  470. begin
  471. { don't register macro's }
  472. if f.is_macro then
  473. exit;
  474. inc(last_ref_index);
  475. f.ref_next:=files;
  476. f.ref_index:=last_ref_index;
  477. files:=f;
  478. { update cache }
  479. cacheindex:=last_ref_index;
  480. cacheinputfile:=f;
  481. {$ifdef heaptrc}
  482. ppheap_register_file(f.name^,current_module.unit_index*100000+f.ref_index);
  483. {$endif heaptrc}
  484. end;
  485. { this procedure is necessary after loading the
  486. sources files from a PPU file PM }
  487. procedure tinputfilemanager.inverse_register_indexes;
  488. var
  489. f : tinputfile;
  490. begin
  491. f:=files;
  492. while assigned(f) do
  493. begin
  494. f.ref_index:=last_ref_index-f.ref_index+1;
  495. f:=f.ref_next;
  496. end;
  497. { reset cache }
  498. cacheindex:=0;
  499. cacheinputfile:=nil;
  500. end;
  501. function tinputfilemanager.get_file(l :longint) : tinputfile;
  502. var
  503. ff : tinputfile;
  504. begin
  505. { check cache }
  506. if (l=cacheindex) and assigned(cacheinputfile) then
  507. begin
  508. get_file:=cacheinputfile;
  509. exit;
  510. end;
  511. ff:=files;
  512. while assigned(ff) and (ff.ref_index<>l) do
  513. ff:=ff.ref_next;
  514. get_file:=ff;
  515. end;
  516. function tinputfilemanager.get_file_name(l :longint):string;
  517. var
  518. hp : tinputfile;
  519. begin
  520. hp:=get_file(l);
  521. if assigned(hp) then
  522. get_file_name:=hp.name^
  523. else
  524. get_file_name:='';
  525. end;
  526. function tinputfilemanager.get_file_path(l :longint):string;
  527. var
  528. hp : tinputfile;
  529. begin
  530. hp:=get_file(l);
  531. if assigned(hp) then
  532. get_file_path:=hp.path^
  533. else
  534. get_file_path:='';
  535. end;
  536. {****************************************************************************
  537. TModuleBase
  538. ****************************************************************************}
  539. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  540. var
  541. p : dirstr;
  542. n : NameStr;
  543. e : ExtStr;
  544. prefix,
  545. suffix,
  546. extension : NameStr;
  547. begin
  548. stringdispose(objfilename);
  549. stringdispose(newfilename);
  550. stringdispose(ppufilename);
  551. stringdispose(staticlibfilename);
  552. stringdispose(sharedlibfilename);
  553. stringdispose(mapfilename);
  554. stringdispose(exefilename);
  555. stringdispose(outputpath);
  556. stringdispose(path);
  557. { Create names }
  558. paramfn := stringdup(fn);
  559. paramallowoutput := allowoutput;
  560. {$IFDEF USE_SYSUTILS}
  561. p := SplitPath(fn);
  562. n := SplitName(fn);
  563. e := SplitExtension(fn);
  564. {$ELSE USE_SYSUTILS}
  565. fsplit(fn,p,n,e);
  566. {$ENDIF USE_SYSUTILS}
  567. n:=FixFileName(n);
  568. { set path }
  569. path:=stringdup(FixPath(p,false));
  570. { obj,asm,ppu names }
  571. p:=path^;
  572. if AllowOutput then
  573. begin
  574. if (OutputUnitDir<>'') then
  575. p:=OutputUnitDir
  576. else
  577. if (OutputExeDir<>'') then
  578. p:=OutputExeDir;
  579. end;
  580. outputpath:=stringdup(p);
  581. newfilename := stringdup(n);
  582. objfilename:=stringdup(p+n+target_info.objext);
  583. ppufilename:=stringdup(p+n+target_info.unitext);
  584. { lib and exe could be loaded with a file specified with -o }
  585. prefix := target_info.sharedlibprefix;
  586. suffix := '';
  587. extension := target_info.sharedlibext;
  588. if AllowOutput and (compile_level=1) then
  589. begin
  590. if OutputFile <> '' then n:=OutputFile;
  591. if Assigned(OutputPrefix) then prefix := OutputPrefix^;
  592. if Assigned(OutputSuffix) then suffix := OutputSuffix^;
  593. if OutputExtension <> '' then extension := OutputExtension;
  594. end;
  595. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  596. { output dir of exe can be specified separatly }
  597. if AllowOutput and (OutputExeDir<>'') then
  598. p:=OutputExeDir
  599. else
  600. p:=path^;
  601. sharedlibfilename:=stringdup(p+prefix+n+suffix+extension);
  602. exefilename:=stringdup(p+n+target_info.exeext);
  603. mapfilename:=stringdup(p+n+'.map');
  604. end;
  605. constructor tmodulebase.create(const s:string);
  606. begin
  607. modulename:=stringdup(Upper(s));
  608. realmodulename:=stringdup(s);
  609. mainsource:=nil;
  610. ppufilename:=nil;
  611. objfilename:=nil;
  612. newfilename:=nil;
  613. staticlibfilename:=nil;
  614. sharedlibfilename:=nil;
  615. exefilename:=nil;
  616. mapfilename:=nil;
  617. outputpath:=nil;
  618. path:=nil;
  619. { status }
  620. state:=ms_registered;
  621. { unit index }
  622. inc(global_unit_count);
  623. unit_index:=global_unit_count;
  624. { sources }
  625. sourcefiles:=TInputFileManager.Create;
  626. end;
  627. function tmodulebase.get_asmfilename : string;
  628. begin
  629. get_asmfilename:=outputpath^+newfilename^+target_info.asmext;
  630. end;
  631. destructor tmodulebase.destroy;
  632. begin
  633. if assigned(sourcefiles) then
  634. sourcefiles.free;
  635. sourcefiles:=nil;
  636. stringdispose(objfilename);
  637. stringdispose(newfilename);
  638. stringdispose(ppufilename);
  639. stringdispose(staticlibfilename);
  640. stringdispose(sharedlibfilename);
  641. stringdispose(exefilename);
  642. stringdispose(mapfilename);
  643. stringdispose(outputpath);
  644. stringdispose(path);
  645. stringdispose(modulename);
  646. stringdispose(realmodulename);
  647. stringdispose(mainsource);
  648. inherited destroy;
  649. end;
  650. end.
  651. {
  652. $Log$
  653. Revision 1.28 2004-10-14 18:16:17 mazen
  654. * USE_SYSUTILS merged successfully : cycles with and without defines
  655. * Need to be optimized in performance
  656. Revision 1.27 2004/10/14 17:26:04 mazen
  657. * use SysUtils unit instead of Dos Unit
  658. Revision 1.26 2004/08/02 07:15:54 michael
  659. + Patch from Christian Iversen to implement LIBPREFIX/SUFFIX/EXTENSION directives
  660. Revision 1.25 2004/06/20 08:55:29 florian
  661. * logs truncated
  662. Revision 1.24 2004/06/16 20:07:07 florian
  663. * dwarf branch merged
  664. Revision 1.23.2.1 2004/04/28 21:46:56 peter
  665. * compile fixes for x86-64
  666. }