owbase.pas 5.9 KB

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