owbase.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. Contains the base stuff for writing for object files to disk
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit owbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cstreams,
  22. cclasses;
  23. type
  24. tobjectwriter=class
  25. private
  26. f : TCCustomFileStream;
  27. opened : boolean;
  28. buf : pchar;
  29. bufidx : longword;
  30. procedure writebuf;
  31. protected
  32. fsize,
  33. fobjsize : longword;
  34. public
  35. constructor create;
  36. constructor createAr(const Aarfn:string);virtual;
  37. destructor destroy;override;
  38. function createfile(const fn:string):boolean;virtual;
  39. procedure closefile;virtual;
  40. procedure writesym(const sym:string);virtual;
  41. procedure write(const b;len:longword);virtual;
  42. procedure WriteZeros(l:longword);
  43. procedure writearray(a:TDynamicArray);
  44. property Size:longword read FSize;
  45. property ObjSize:longword read FObjSize;
  46. end;
  47. tobjectwriterclass = class of tobjectwriter;
  48. tobjectreader=class
  49. private
  50. f : TCCustomFileStream;
  51. opened : boolean;
  52. buf : pchar;
  53. ffilename : string;
  54. bufidx,
  55. bufmax : longint;
  56. function readbuf:boolean;
  57. protected
  58. function getfilename : string;virtual;
  59. function GetPos: longint;virtual;
  60. function GetIsArchive: boolean;virtual;
  61. public
  62. constructor create;
  63. constructor createAr(const Aarfn:string;allow_nonar:boolean=false);virtual;
  64. destructor destroy;override;
  65. function openfile(const fn:string):boolean;virtual;
  66. procedure closefile;virtual;
  67. procedure seek(len:longint);virtual;
  68. function read(out b;len:longint):boolean;virtual;
  69. function readarray(a:TDynamicArray;len:longint):boolean;
  70. property filename : string read getfilename;
  71. property size:longint read bufmax;
  72. property Pos:longint read GetPos;
  73. property IsArchive: boolean read GetIsArchive;
  74. end;
  75. tobjectreaderclass = class of tobjectreader;
  76. implementation
  77. uses
  78. SysUtils,
  79. verbose, globals;
  80. const
  81. bufsize = 32768;
  82. {****************************************************************************
  83. TObjectWriter
  84. ****************************************************************************}
  85. constructor tobjectwriter.create;
  86. begin
  87. getmem(buf,bufsize);
  88. bufidx:=0;
  89. opened:=false;
  90. fsize:=0;
  91. end;
  92. destructor tobjectwriter.destroy;
  93. begin
  94. if opened then
  95. closefile;
  96. freemem(buf,bufsize);
  97. end;
  98. constructor tobjectwriter.createAr(const Aarfn:string);
  99. begin
  100. InternalError(2015041901);
  101. end;
  102. function tobjectwriter.createfile(const fn:string):boolean;
  103. begin
  104. createfile:=false;
  105. f:=CFileStreamClass.Create(fn,fmCreate);
  106. if CStreamError<>0 then
  107. begin
  108. Message2(exec_e_cant_create_objectfile,fn,IntToStr(CStreamError));
  109. exit;
  110. end;
  111. bufidx:=0;
  112. fsize:=0;
  113. fobjsize:=0;
  114. opened:=true;
  115. createfile:=true;
  116. end;
  117. procedure tobjectwriter.closefile;
  118. var
  119. fn : string;
  120. begin
  121. if bufidx>0 then
  122. writebuf;
  123. fn:=f.filename;
  124. f.free;
  125. { Remove if size is 0 }
  126. if size=0 then
  127. DeleteFile(fn);
  128. opened:=false;
  129. fsize:=0;
  130. fobjsize:=0;
  131. end;
  132. procedure tobjectwriter.writebuf;
  133. begin
  134. f.write(buf^,bufidx);
  135. bufidx:=0;
  136. end;
  137. procedure tobjectwriter.writesym(const sym:string);
  138. begin
  139. end;
  140. procedure tobjectwriter.write(const b;len:longword);
  141. var
  142. p : pchar;
  143. bufleft,
  144. idx : longword;
  145. begin
  146. inc(fsize,len);
  147. inc(fobjsize,len);
  148. p:=pchar(@b);
  149. idx:=0;
  150. while len>0 do
  151. begin
  152. bufleft:=bufsize-bufidx;
  153. if len>bufleft then
  154. begin
  155. move(p[idx],buf[bufidx],bufleft);
  156. dec(len,bufleft);
  157. inc(idx,bufleft);
  158. inc(bufidx,bufleft);
  159. writebuf;
  160. end
  161. else
  162. begin
  163. move(p[idx],buf[bufidx],len);
  164. inc(bufidx,len);
  165. exit;
  166. end;
  167. end;
  168. end;
  169. procedure tobjectwriter.WriteZeros(l:longword);
  170. var
  171. empty : array[0..1023] of byte;
  172. begin
  173. if l>sizeof(empty) then
  174. internalerror(200404081);
  175. if l>0 then
  176. begin
  177. fillchar(empty,l,0);
  178. Write(empty,l);
  179. end;
  180. end;
  181. procedure tobjectwriter.writearray(a:TDynamicArray);
  182. var
  183. hp : pdynamicblock;
  184. begin
  185. hp:=a.firstblock;
  186. while assigned(hp) do
  187. begin
  188. write(hp^.data,hp^.used);
  189. hp:=hp^.next;
  190. end;
  191. end;
  192. {****************************************************************************
  193. TObjectReader
  194. ****************************************************************************}
  195. constructor tobjectreader.create;
  196. begin
  197. buf:=nil;
  198. bufidx:=0;
  199. bufmax:=0;
  200. ffilename:='';
  201. opened:=false;
  202. end;
  203. destructor tobjectreader.destroy;
  204. begin
  205. if opened then
  206. closefile;
  207. end;
  208. constructor tobjectreader.createAr(const Aarfn:string;allow_nonar:boolean=false);
  209. begin
  210. InternalError(2015081401);
  211. end;
  212. function tobjectreader.openfile(const fn:string):boolean;
  213. begin
  214. openfile:=false;
  215. f:=CFileStreamClass.Create(fn,fmOpenRead);
  216. if CStreamError<>0 then
  217. begin
  218. Comment(V_Error,'Can''t open object file: '+fn);
  219. exit;
  220. end;
  221. ffilename:=fn;
  222. bufmax:=f.Size;
  223. getmem(buf,bufmax);
  224. f.read(buf^,bufmax);
  225. f.free;
  226. bufidx:=0;
  227. opened:=true;
  228. openfile:=true;
  229. end;
  230. procedure tobjectreader.closefile;
  231. begin
  232. opened:=false;
  233. bufidx:=0;
  234. bufmax:=0;
  235. freemem(buf);
  236. end;
  237. function tobjectreader.readbuf:boolean;
  238. begin
  239. result:=bufidx<bufmax;
  240. end;
  241. procedure tobjectreader.seek(len:longint);
  242. begin
  243. bufidx:=len;
  244. end;
  245. function tobjectreader.read(out b;len:longint):boolean;
  246. begin
  247. result:=true;
  248. if bufidx+len>bufmax then
  249. begin
  250. result:=false;
  251. len:=bufmax-bufidx;
  252. end;
  253. move(buf[bufidx],b,len);
  254. inc(bufidx,len);
  255. end;
  256. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  257. begin
  258. result:=true;
  259. if bufidx+len>bufmax then
  260. begin
  261. result:=false;
  262. len:=bufmax-bufidx;
  263. end;
  264. a.write(buf[bufidx],len);
  265. inc(bufidx,len);
  266. end;
  267. function tobjectreader.getfilename : string;
  268. begin
  269. result:=ffilename;
  270. end;
  271. function tobjectreader.GetPos: longint;
  272. begin
  273. Result:=bufidx;
  274. end;
  275. function tobjectreader.GetIsArchive: boolean;
  276. begin
  277. Result:=false;
  278. end;
  279. end.