finput.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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. protected
  59. function fileopen(const filename: string): boolean; virtual;
  60. function fileseek(pos: longint): boolean; virtual;
  61. function fileread(var databuf; maxsize: longint): longint; virtual;
  62. function fileeof: boolean; virtual;
  63. function fileclose: boolean; virtual;
  64. end;
  65. tdosinputfile = class(tinputfile)
  66. protected
  67. function fileopen(const filename: string): boolean; override;
  68. function fileseek(pos: longint): boolean; override;
  69. function fileread(var databuf; maxsize: longint): longint; override;
  70. function fileeof: boolean; override;
  71. function fileclose: boolean; override;
  72. private
  73. f : file; { current file handle }
  74. end;
  75. tinputfilemanager = class
  76. files : tinputfile;
  77. last_ref_index : longint;
  78. cacheindex : longint;
  79. cacheinputfile : tinputfile;
  80. constructor create;
  81. destructor destroy;override;
  82. procedure register_file(f : tinputfile);
  83. procedure inverse_register_indexes;
  84. function get_file(l:longint) : tinputfile;
  85. function get_file_name(l :longint):string;
  86. function get_file_path(l :longint):string;
  87. end;
  88. {****************************************************************************
  89. TModuleBase
  90. ****************************************************************************}
  91. tmodulebase = class(TLinkedListItem)
  92. { index }
  93. unit_index : longint; { global counter for browser }
  94. { status }
  95. in_compile : boolean; { is it being compiled ?? }
  96. { sources }
  97. sourcefiles : tinputfilemanager;
  98. { paths and filenames }
  99. path, { path where the module is find/created }
  100. outputpath, { path where the .s / .o / exe are created }
  101. modulename, { name of the module in uppercase }
  102. realmodulename, { name of the module in the orignal case }
  103. objfilename, { fullname of the objectfile }
  104. asmfilename, { fullname of the assemblerfile }
  105. ppufilename, { fullname of the ppufile }
  106. staticlibfilename, { fullname of the static libraryfile }
  107. sharedlibfilename, { fullname of the shared libraryfile }
  108. exefilename, { fullname of the exefile }
  109. mainsource : pstring; { name of the main sourcefile }
  110. constructor create(const s:string);
  111. destructor destroy;override;
  112. procedure setfilename(const fn:string;allowoutput:boolean);
  113. end;
  114. implementation
  115. uses
  116. {$ifdef Delphi}
  117. dmisc,
  118. {$else Delphi}
  119. dos,
  120. {$endif Delphi}
  121. {$ifdef HEAPTRC}
  122. fmodule,
  123. {$endif HEAPTRC}
  124. globals,systems
  125. ;
  126. {****************************************************************************
  127. TINPUTFILE
  128. ****************************************************************************}
  129. constructor tinputfile.create(const fn:string);
  130. var
  131. p:dirstr;
  132. n:namestr;
  133. e:extstr;
  134. begin
  135. FSplit(fn,p,n,e);
  136. name:=stringdup(n+e);
  137. path:=stringdup(p);
  138. next:=nil;
  139. { file info }
  140. is_macro:=false;
  141. endoffile:=false;
  142. closed:=true;
  143. buf:=nil;
  144. bufstart:=0;
  145. bufsize:=0;
  146. maxbufsize:=InputFileBufSize;
  147. { save fields }
  148. saveinputpointer:=nil;
  149. saveline_no:=0;
  150. savelastlinepos:=0;
  151. { indexing refs }
  152. ref_next:=nil;
  153. ref_index:=0;
  154. { line buffer }
  155. linebuf:=nil;
  156. maxlinebuf:=0;
  157. end;
  158. destructor tinputfile.destroy;
  159. begin
  160. if not closed then
  161. close;
  162. stringdispose(path);
  163. stringdispose(name);
  164. { free memory }
  165. if assigned(linebuf) then
  166. freemem(linebuf,maxlinebuf shl 2);
  167. end;
  168. procedure tinputfile.setpos(l:longint);
  169. begin
  170. bufstart:=l;
  171. end;
  172. procedure tinputfile.seekbuf(fpos:longint);
  173. begin
  174. if closed then
  175. exit;
  176. fileseek(fpos);
  177. bufstart:=fpos;
  178. bufsize:=0;
  179. end;
  180. procedure tinputfile.readbuf;
  181. begin
  182. if is_macro then
  183. endoffile:=true;
  184. if closed then
  185. exit;
  186. inc(bufstart,bufsize);
  187. bufsize:=fileread(buf^,maxbufsize-1);
  188. buf[bufsize]:=#0;
  189. endoffile:=fileeof;
  190. end;
  191. function tinputfile.open:boolean;
  192. begin
  193. open:=false;
  194. if not closed then
  195. Close;
  196. if not fileopen(path^+name^) then
  197. exit;
  198. { file }
  199. endoffile:=false;
  200. closed:=false;
  201. Getmem(buf,MaxBufsize);
  202. bufstart:=0;
  203. bufsize:=0;
  204. open:=true;
  205. end;
  206. procedure tinputfile.close;
  207. begin
  208. if is_macro then
  209. begin
  210. if assigned(buf) then
  211. Freemem(buf,maxbufsize);
  212. buf:=nil;
  213. {is_macro:=false;
  214. still needed for dispose in scanner PM }
  215. closed:=true;
  216. exit;
  217. end;
  218. if not closed then
  219. begin
  220. if fileclose then;
  221. closed:=true;
  222. end;
  223. if assigned(buf) then
  224. begin
  225. Freemem(buf,maxbufsize);
  226. buf:=nil;
  227. end;
  228. bufstart:=0;
  229. end;
  230. procedure tinputfile.tempclose;
  231. begin
  232. if is_macro then
  233. exit;
  234. if not closed then
  235. begin
  236. if fileclose then;
  237. Freemem(buf,maxbufsize);
  238. buf:=nil;
  239. closed:=true;
  240. end;
  241. end;
  242. function tinputfile.tempopen:boolean;
  243. begin
  244. tempopen:=false;
  245. if is_macro then
  246. begin
  247. { seek buffer postion to bufstart }
  248. if bufstart>0 then
  249. begin
  250. move(buf[bufstart],buf[0],bufsize-bufstart+1);
  251. bufstart:=0;
  252. end;
  253. tempopen:=true;
  254. exit;
  255. end;
  256. if not closed then
  257. exit;
  258. if not fileopen(path^+name^) then
  259. exit;
  260. closed:=false;
  261. { get new mem }
  262. Getmem(buf,maxbufsize);
  263. { restore state }
  264. fileseek(BufStart);
  265. bufsize:=0;
  266. readbuf;
  267. tempopen:=true;
  268. end;
  269. procedure tinputfile.setmacro(p:pchar;len:longint);
  270. begin
  271. { create new buffer }
  272. getmem(buf,len+1);
  273. move(p^,buf^,len);
  274. buf[len]:=#0;
  275. { reset }
  276. bufstart:=0;
  277. bufsize:=len;
  278. maxbufsize:=len+1;
  279. is_macro:=true;
  280. endoffile:=true;
  281. closed:=true;
  282. end;
  283. procedure tinputfile.setline(line,linepos:longint);
  284. var
  285. oldlinebuf : plongintarr;
  286. begin
  287. if line<1 then
  288. exit;
  289. while (line>=maxlinebuf) do
  290. begin
  291. oldlinebuf:=linebuf;
  292. { create new linebuf and move old info }
  293. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  294. if assigned(oldlinebuf) then
  295. begin
  296. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  297. freemem(oldlinebuf,maxlinebuf shl 2);
  298. end;
  299. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  300. inc(maxlinebuf,linebufincrease);
  301. end;
  302. linebuf^[line]:=linepos;
  303. end;
  304. function tinputfile.getlinestr(l:longint):string;
  305. var
  306. c : char;
  307. i,
  308. fpos : longint;
  309. p : pchar;
  310. begin
  311. getlinestr:='';
  312. if l<maxlinebuf then
  313. begin
  314. fpos:=linebuf^[l];
  315. { fpos is set negativ if the line was already written }
  316. { but we still know the correct value }
  317. if fpos<0 then
  318. fpos:=-fpos+1;
  319. if closed then
  320. open;
  321. { in current buf ? }
  322. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  323. begin
  324. seekbuf(fpos);
  325. readbuf;
  326. end;
  327. { the begin is in the buf now simply read until #13,#10 }
  328. i:=0;
  329. p:=@buf[fpos-bufstart];
  330. repeat
  331. c:=p^;
  332. if c=#0 then
  333. begin
  334. if endoffile then
  335. break;
  336. readbuf;
  337. p:=buf;
  338. c:=p^;
  339. end;
  340. if c in [#10,#13] then
  341. break;
  342. inc(i);
  343. getlinestr[i]:=c;
  344. inc(longint(p));
  345. until (i=255);
  346. getlinestr[0]:=chr(i);
  347. end;
  348. end;
  349. function tinputfile.fileopen(const filename: string): boolean;
  350. begin
  351. abstract;
  352. fileopen:=false;
  353. end;
  354. function tinputfile.fileseek(pos: longint): boolean;
  355. begin
  356. abstract;
  357. fileseek:=false;
  358. end;
  359. function tinputfile.fileread(var databuf; maxsize: longint): longint;
  360. begin
  361. abstract;
  362. fileread:=0;
  363. end;
  364. function tinputfile.fileeof: boolean;
  365. begin
  366. abstract;
  367. fileeof:=false;
  368. end;
  369. function tinputfile.fileclose: boolean;
  370. begin
  371. abstract;
  372. fileclose:=false;
  373. end;
  374. {****************************************************************************
  375. TDOSINPUTFILE
  376. ****************************************************************************}
  377. function tdosinputfile.fileopen(const filename: string): boolean;
  378. var
  379. ofm : byte;
  380. begin
  381. ofm:=filemode;
  382. filemode:=0;
  383. Assign(f,filename);
  384. {$I-}
  385. reset(f,1);
  386. {$I+}
  387. filemode:=ofm;
  388. fileopen:=(ioresult=0);
  389. end;
  390. function tdosinputfile.fileseek(pos: longint): boolean;
  391. begin
  392. {$I-}
  393. seek(f,Pos);
  394. {$I+}
  395. fileseek:=(ioresult=0);
  396. end;
  397. function tdosinputfile.fileread(var databuf; maxsize: longint): longint;
  398. var
  399. w : longint;
  400. begin
  401. blockread(f,databuf,maxsize,w);
  402. fileread:=w;
  403. end;
  404. function tdosinputfile.fileeof: boolean;
  405. begin
  406. fileeof:=eof(f);
  407. end;
  408. function tdosinputfile.fileclose: boolean;
  409. begin
  410. {$I-}
  411. system.close(f);
  412. {$I+}
  413. fileclose:=(ioresult=0);
  414. end;
  415. {****************************************************************************
  416. Tinputfilemanager
  417. ****************************************************************************}
  418. constructor tinputfilemanager.create;
  419. begin
  420. files:=nil;
  421. last_ref_index:=0;
  422. cacheindex:=0;
  423. cacheinputfile:=nil;
  424. end;
  425. destructor tinputfilemanager.destroy;
  426. var
  427. hp : tinputfile;
  428. begin
  429. hp:=files;
  430. while assigned(hp) do
  431. begin
  432. files:=files.ref_next;
  433. hp.free;
  434. hp:=files;
  435. end;
  436. last_ref_index:=0;
  437. end;
  438. procedure tinputfilemanager.register_file(f : tinputfile);
  439. begin
  440. { don't register macro's }
  441. if f.is_macro then
  442. exit;
  443. inc(last_ref_index);
  444. f.ref_next:=files;
  445. f.ref_index:=last_ref_index;
  446. files:=f;
  447. { update cache }
  448. cacheindex:=last_ref_index;
  449. cacheinputfile:=f;
  450. {$ifdef HEAPTRC}
  451. writeln(stderr,f.name^,' index ',current_module.unit_index*100000+f.ref_index);
  452. {$endif HEAPTRC}
  453. end;
  454. { this procedure is necessary after loading the
  455. sources files from a PPU file PM }
  456. procedure tinputfilemanager.inverse_register_indexes;
  457. var
  458. f : tinputfile;
  459. begin
  460. f:=files;
  461. while assigned(f) do
  462. begin
  463. f.ref_index:=last_ref_index-f.ref_index+1;
  464. f:=f.ref_next;
  465. end;
  466. { reset cache }
  467. cacheindex:=0;
  468. cacheinputfile:=nil;
  469. end;
  470. function tinputfilemanager.get_file(l :longint) : tinputfile;
  471. var
  472. ff : tinputfile;
  473. begin
  474. { check cache }
  475. if (l=cacheindex) and assigned(cacheinputfile) then
  476. begin
  477. get_file:=cacheinputfile;
  478. exit;
  479. end;
  480. ff:=files;
  481. while assigned(ff) and (ff.ref_index<>l) do
  482. ff:=ff.ref_next;
  483. get_file:=ff;
  484. end;
  485. function tinputfilemanager.get_file_name(l :longint):string;
  486. var
  487. hp : tinputfile;
  488. begin
  489. hp:=get_file(l);
  490. if assigned(hp) then
  491. get_file_name:=hp.name^
  492. else
  493. get_file_name:='';
  494. end;
  495. function tinputfilemanager.get_file_path(l :longint):string;
  496. var
  497. hp : tinputfile;
  498. begin
  499. hp:=get_file(l);
  500. if assigned(hp) then
  501. get_file_path:=hp.path^
  502. else
  503. get_file_path:='';
  504. end;
  505. {****************************************************************************
  506. TModuleBase
  507. ****************************************************************************}
  508. procedure tmodulebase.setfilename(const fn:string;allowoutput:boolean);
  509. var
  510. p : dirstr;
  511. n : NameStr;
  512. e : ExtStr;
  513. begin
  514. stringdispose(objfilename);
  515. stringdispose(asmfilename);
  516. stringdispose(ppufilename);
  517. stringdispose(staticlibfilename);
  518. stringdispose(sharedlibfilename);
  519. stringdispose(exefilename);
  520. stringdispose(outputpath);
  521. stringdispose(path);
  522. { Create names }
  523. fsplit(fn,p,n,e);
  524. n:=FixFileName(n);
  525. { set path }
  526. path:=stringdup(FixPath(p,false));
  527. { obj,asm,ppu names }
  528. p:=path^;
  529. if AllowOutput then
  530. begin
  531. if (OutputUnitDir<>'') then
  532. p:=OutputUnitDir
  533. else
  534. if (OutputExeDir<>'') then
  535. p:=OutputExeDir;
  536. end;
  537. outputpath:=stringdup(p);
  538. objfilename:=stringdup(p+n+target_info.objext);
  539. asmfilename:=stringdup(p+n+target_info.asmext);
  540. ppufilename:=stringdup(p+n+target_info.unitext);
  541. { lib and exe could be loaded with a file specified with -o }
  542. if AllowOutput and (OutputFile<>'') and (compile_level=1) then
  543. n:=OutputFile;
  544. staticlibfilename:=stringdup(p+target_info.staticlibprefix+n+target_info.staticlibext);
  545. if target_info.target=target_i386_WIN32 then
  546. sharedlibfilename:=stringdup(p+n+target_info.sharedlibext)
  547. else
  548. sharedlibfilename:=stringdup(p+target_info.sharedlibprefix+n+target_info.sharedlibext);
  549. { output dir of exe can be specified separatly }
  550. if AllowOutput and (OutputExeDir<>'') then
  551. p:=OutputExeDir
  552. else
  553. p:=path^;
  554. exefilename:=stringdup(p+n+target_info.exeext);
  555. end;
  556. constructor tmodulebase.create(const s:string);
  557. begin
  558. modulename:=stringdup(Upper(s));
  559. realmodulename:=stringdup(s);
  560. mainsource:=nil;
  561. ppufilename:=nil;
  562. objfilename:=nil;
  563. asmfilename:=nil;
  564. staticlibfilename:=nil;
  565. sharedlibfilename:=nil;
  566. exefilename:=nil;
  567. outputpath:=nil;
  568. path:=nil;
  569. { status }
  570. in_compile:=false;
  571. { unit index }
  572. inc(global_unit_count);
  573. unit_index:=global_unit_count;
  574. { sources }
  575. sourcefiles:=TInputFileManager.Create;
  576. end;
  577. destructor tmodulebase.destroy;
  578. begin
  579. if assigned(sourcefiles) then
  580. sourcefiles.free;
  581. sourcefiles:=nil;
  582. stringdispose(objfilename);
  583. stringdispose(asmfilename);
  584. stringdispose(ppufilename);
  585. stringdispose(staticlibfilename);
  586. stringdispose(sharedlibfilename);
  587. stringdispose(exefilename);
  588. stringdispose(outputpath);
  589. stringdispose(path);
  590. stringdispose(modulename);
  591. stringdispose(realmodulename);
  592. stringdispose(mainsource);
  593. inherited destroy;
  594. end;
  595. end.
  596. {
  597. $Log$
  598. Revision 1.11 2001-08-04 10:23:54 peter
  599. * updates so it works with the ide
  600. Revision 1.10 2001/06/03 15:15:31 peter
  601. * dllprt0 stub for linux shared libs
  602. * pass -init and -fini for linux shared libs
  603. * libprefix splitted into staticlibprefix and sharedlibprefix
  604. Revision 1.9 2001/04/18 22:01:53 peter
  605. * registration of targets and assemblers
  606. Revision 1.8 2001/04/13 01:22:07 peter
  607. * symtable change to classes
  608. * range check generation and errors fixed, make cycle DEBUG=1 works
  609. * memory leaks fixed
  610. Revision 1.7 2001/03/13 18:43:17 peter
  611. * made memdebug and heaptrc compilable again
  612. Revision 1.6 2000/12/25 00:07:25 peter
  613. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  614. tlinkedlist objects)
  615. Revision 1.5 2000/11/07 20:48:33 peter
  616. * removed ref_count from tinputfile it's not used
  617. Revision 1.4 2000/10/31 22:02:46 peter
  618. * symtable splitted, no real code changes
  619. Revision 1.3 2000/10/14 21:52:54 peter
  620. * fixed memory leaks
  621. Revision 1.2 2000/09/24 15:06:16 peter
  622. * use defines.inc
  623. Revision 1.1 2000/08/27 16:11:50 peter
  624. * moved some util functions from globals,cobjects to cutils
  625. * splitted files into finput,fmodule
  626. }