owbase.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 }
  49. tobjectreader=class
  50. private
  51. f : TCCustomFileStream;
  52. opened : boolean;
  53. buf : pchar;
  54. ffilename : string;
  55. bufidx,
  56. bufmax : longint;
  57. function readbuf:boolean;
  58. protected
  59. function getfilename : string;virtual;
  60. function GetSize: longint;virtual;
  61. function GetPos: longint;virtual;
  62. function GetIsArchive: boolean;virtual;
  63. public
  64. constructor create;
  65. constructor createAr(const Aarfn:string;allow_nonar:boolean=false);virtual;
  66. destructor destroy;override;
  67. function openfile(const fn:string):boolean;virtual;
  68. procedure closefile;virtual;
  69. procedure seek(len:longint);virtual;
  70. function read(out b;len:longint):boolean;virtual;
  71. function readarray(a:TDynamicArray;len:longint):boolean;
  72. property filename : string read getfilename;
  73. property size:longint read GetSize;
  74. property Pos:longint read GetPos;
  75. property IsArchive: boolean read GetIsArchive;
  76. end;
  77. tobjectreaderclass = class of tobjectreader;
  78. implementation
  79. uses
  80. SysUtils,
  81. verbose, globals;
  82. const
  83. bufsize = 32768;
  84. {****************************************************************************
  85. TObjectWriter
  86. ****************************************************************************}
  87. constructor tobjectwriter.create;
  88. begin
  89. getmem(buf,bufsize);
  90. bufidx:=0;
  91. opened:=false;
  92. fsize:=0;
  93. end;
  94. destructor tobjectwriter.destroy;
  95. begin
  96. if opened then
  97. closefile;
  98. freemem(buf,bufsize);
  99. end;
  100. constructor tobjectwriter.createAr(const Aarfn:string);
  101. begin
  102. InternalError(2015041901);
  103. end;
  104. function tobjectwriter.createfile(const fn:string):boolean;
  105. begin
  106. createfile:=false;
  107. f:=CFileStreamClass.Create(fn,fmCreate);
  108. if CStreamError<>0 then
  109. begin
  110. Message2(exec_e_cant_create_objectfile,fn,IntToStr(CStreamError));
  111. exit;
  112. end;
  113. bufidx:=0;
  114. fsize:=0;
  115. fobjsize:=0;
  116. opened:=true;
  117. createfile:=true;
  118. end;
  119. procedure tobjectwriter.closefile;
  120. var
  121. fn : string;
  122. begin
  123. if bufidx>0 then
  124. writebuf;
  125. fn:=f.filename;
  126. f.free;
  127. { Remove if size is 0 }
  128. if size=0 then
  129. DeleteFile(fn);
  130. opened:=false;
  131. fsize:=0;
  132. fobjsize:=0;
  133. end;
  134. procedure tobjectwriter.writebuf;
  135. begin
  136. f.write(buf^,bufidx);
  137. bufidx:=0;
  138. end;
  139. procedure tobjectwriter.writesym(const sym:string);
  140. begin
  141. end;
  142. procedure tobjectwriter.write(const b;len:longword);
  143. var
  144. p : pchar;
  145. bufleft,
  146. idx : longword;
  147. begin
  148. inc(fsize,len);
  149. inc(fobjsize,len);
  150. p:=pchar(@b);
  151. idx:=0;
  152. while len>0 do
  153. begin
  154. bufleft:=bufsize-bufidx;
  155. if len>bufleft then
  156. begin
  157. move(p[idx],buf[bufidx],bufleft);
  158. dec(len,bufleft);
  159. inc(idx,bufleft);
  160. inc(bufidx,bufleft);
  161. writebuf;
  162. end
  163. else
  164. begin
  165. move(p[idx],buf[bufidx],len);
  166. inc(bufidx,len);
  167. exit;
  168. end;
  169. end;
  170. end;
  171. procedure tobjectwriter.WriteZeros(l:longword);
  172. var
  173. empty : array[0..1023] of byte;
  174. begin
  175. if l>sizeof(empty) then
  176. begin
  177. fillchar(empty,sizeof(empty),0);
  178. while l>sizeof(empty) do
  179. begin
  180. Write(empty,sizeof(empty));
  181. Dec(l,sizeof(empty));
  182. end;
  183. if l>0 then
  184. Write(empty,l);
  185. end
  186. else if l>0 then
  187. begin
  188. fillchar(empty,l,0);
  189. Write(empty,l);
  190. end;
  191. end;
  192. procedure tobjectwriter.writearray(a:TDynamicArray);
  193. var
  194. hp : pdynamicblock;
  195. begin
  196. hp:=a.firstblock;
  197. while assigned(hp) do
  198. begin
  199. write(hp^.data,hp^.used);
  200. hp:=hp^.next;
  201. end;
  202. end;
  203. {****************************************************************************
  204. TObjectReader
  205. ****************************************************************************}
  206. constructor tobjectreader.create;
  207. begin
  208. buf:=nil;
  209. bufidx:=0;
  210. bufmax:=0;
  211. ffilename:='';
  212. opened:=false;
  213. end;
  214. destructor tobjectreader.destroy;
  215. begin
  216. if opened then
  217. closefile;
  218. end;
  219. constructor tobjectreader.createAr(const Aarfn:string;allow_nonar:boolean=false);
  220. begin
  221. InternalError(2015081401);
  222. end;
  223. function tobjectreader.openfile(const fn:string):boolean;
  224. begin
  225. openfile:=false;
  226. f:=CFileStreamClass.Create(fn,fmOpenRead);
  227. if CStreamError<>0 then
  228. begin
  229. Comment(V_Error,'Can''t open object file: '+fn);
  230. exit;
  231. end;
  232. ffilename:=fn;
  233. bufmax:=f.Size;
  234. getmem(buf,bufmax);
  235. f.read(buf^,bufmax);
  236. f.free;
  237. bufidx:=0;
  238. opened:=true;
  239. openfile:=true;
  240. end;
  241. procedure tobjectreader.closefile;
  242. begin
  243. opened:=false;
  244. bufidx:=0;
  245. bufmax:=0;
  246. freemem(buf);
  247. end;
  248. function tobjectreader.readbuf:boolean;
  249. begin
  250. result:=bufidx<bufmax;
  251. end;
  252. procedure tobjectreader.seek(len:longint);
  253. begin
  254. bufidx:=len;
  255. end;
  256. function tobjectreader.read(out b;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. move(buf[bufidx],b,len);
  265. inc(bufidx,len);
  266. end;
  267. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  268. begin
  269. result:=true;
  270. if bufidx+len>bufmax then
  271. begin
  272. result:=false;
  273. len:=bufmax-bufidx;
  274. end;
  275. a.write(buf[bufidx],len);
  276. inc(bufidx,len);
  277. end;
  278. function tobjectreader.getfilename : string;
  279. begin
  280. result:=ffilename;
  281. end;
  282. function tobjectreader.GetSize: longint;
  283. begin
  284. result:=bufmax;
  285. end;
  286. function tobjectreader.GetPos: longint;
  287. begin
  288. Result:=bufidx;
  289. end;
  290. function tobjectreader.GetIsArchive: boolean;
  291. begin
  292. Result:=false;
  293. end;
  294. end.