finput.pas 19 KB

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