finput.pas 19 KB

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