owbase.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. begin
  175. fillchar(empty,sizeof(empty),0);
  176. while l>sizeof(empty) do
  177. begin
  178. Write(empty,sizeof(empty));
  179. Dec(l,sizeof(empty));
  180. end;
  181. if l>0 then
  182. Write(empty,l);
  183. end
  184. else if l>0 then
  185. begin
  186. fillchar(empty,l,0);
  187. Write(empty,l);
  188. end;
  189. end;
  190. procedure tobjectwriter.writearray(a:TDynamicArray);
  191. var
  192. hp : pdynamicblock;
  193. begin
  194. hp:=a.firstblock;
  195. while assigned(hp) do
  196. begin
  197. write(hp^.data,hp^.used);
  198. hp:=hp^.next;
  199. end;
  200. end;
  201. {****************************************************************************
  202. TObjectReader
  203. ****************************************************************************}
  204. constructor tobjectreader.create;
  205. begin
  206. buf:=nil;
  207. bufidx:=0;
  208. bufmax:=0;
  209. ffilename:='';
  210. opened:=false;
  211. end;
  212. destructor tobjectreader.destroy;
  213. begin
  214. if opened then
  215. closefile;
  216. end;
  217. constructor tobjectreader.createAr(const Aarfn:string;allow_nonar:boolean=false);
  218. begin
  219. InternalError(2015081401);
  220. end;
  221. function tobjectreader.openfile(const fn:string):boolean;
  222. begin
  223. openfile:=false;
  224. f:=CFileStreamClass.Create(fn,fmOpenRead);
  225. if CStreamError<>0 then
  226. begin
  227. Comment(V_Error,'Can''t open object file: '+fn);
  228. exit;
  229. end;
  230. ffilename:=fn;
  231. bufmax:=f.Size;
  232. getmem(buf,bufmax);
  233. f.read(buf^,bufmax);
  234. f.free;
  235. bufidx:=0;
  236. opened:=true;
  237. openfile:=true;
  238. end;
  239. procedure tobjectreader.closefile;
  240. begin
  241. opened:=false;
  242. bufidx:=0;
  243. bufmax:=0;
  244. freemem(buf);
  245. end;
  246. function tobjectreader.readbuf:boolean;
  247. begin
  248. result:=bufidx<bufmax;
  249. end;
  250. procedure tobjectreader.seek(len:longint);
  251. begin
  252. bufidx:=len;
  253. end;
  254. function tobjectreader.read(out b;len:longint):boolean;
  255. begin
  256. result:=true;
  257. if bufidx+len>bufmax then
  258. begin
  259. result:=false;
  260. len:=bufmax-bufidx;
  261. end;
  262. move(buf[bufidx],b,len);
  263. inc(bufidx,len);
  264. end;
  265. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  266. begin
  267. result:=true;
  268. if bufidx+len>bufmax then
  269. begin
  270. result:=false;
  271. len:=bufmax-bufidx;
  272. end;
  273. a.write(buf[bufidx],len);
  274. inc(bufidx,len);
  275. end;
  276. function tobjectreader.getfilename : string;
  277. begin
  278. result:=ffilename;
  279. end;
  280. function tobjectreader.GetPos: longint;
  281. begin
  282. Result:=bufidx;
  283. end;
  284. function tobjectreader.GetIsArchive: boolean;
  285. begin
  286. Result:=false;
  287. end;
  288. end.