finput.pas 19 KB

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