finput.pas 20 KB

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