owbase.pas 6.1 KB

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