finput.pas 19 KB

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