owbase.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 : TCFileStream;
  27. opened : boolean;
  28. buf : pchar;
  29. bufidx : longint;
  30. procedure writebuf;
  31. protected
  32. fsize,
  33. fobjsize : longint;
  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:longint);virtual;
  41. procedure WriteZeros(l:longint);
  42. procedure writearray(a:TDynamicArray);
  43. property Size:longint read FSize;
  44. property ObjSize:longint read FObjSize;
  45. end;
  46. tobjectreader=class
  47. private
  48. f : TCFileStream;
  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. end;
  67. implementation
  68. uses
  69. SysUtils,
  70. verbose, globals;
  71. const
  72. bufsize = 32768;
  73. {****************************************************************************
  74. TObjectWriter
  75. ****************************************************************************}
  76. constructor tobjectwriter.create;
  77. begin
  78. getmem(buf,bufsize);
  79. bufidx:=0;
  80. opened:=false;
  81. fsize:=0;
  82. end;
  83. destructor tobjectwriter.destroy;
  84. begin
  85. if opened then
  86. closefile;
  87. freemem(buf,bufsize);
  88. end;
  89. function tobjectwriter.createfile(const fn:string):boolean;
  90. begin
  91. createfile:=false;
  92. f:=TCFileStream.Create(fn,fmCreate);
  93. if CStreamError<>0 then
  94. begin
  95. Message1(exec_e_cant_create_objectfile,fn);
  96. exit;
  97. end;
  98. bufidx:=0;
  99. fsize:=0;
  100. fobjsize:=0;
  101. opened:=true;
  102. createfile:=true;
  103. end;
  104. procedure tobjectwriter.closefile;
  105. var
  106. fn : string;
  107. begin
  108. if bufidx>0 then
  109. writebuf;
  110. fn:=f.filename;
  111. f.free;
  112. { Remove if size is 0 }
  113. if size=0 then
  114. DeleteFile(fn);
  115. opened:=false;
  116. fsize:=0;
  117. fobjsize:=0;
  118. end;
  119. procedure tobjectwriter.writebuf;
  120. begin
  121. f.write(buf^,bufidx);
  122. bufidx:=0;
  123. end;
  124. procedure tobjectwriter.writesym(const sym:string);
  125. begin
  126. end;
  127. procedure tobjectwriter.write(const b;len:longint);
  128. var
  129. p : pchar;
  130. bufleft,
  131. idx : longint;
  132. begin
  133. inc(fsize,len);
  134. inc(fobjsize,len);
  135. p:=pchar(@b);
  136. idx:=0;
  137. while len>0 do
  138. begin
  139. bufleft:=bufsize-bufidx;
  140. if len>bufleft then
  141. begin
  142. move(p[idx],buf[bufidx],bufleft);
  143. dec(len,bufleft);
  144. inc(idx,bufleft);
  145. inc(bufidx,bufleft);
  146. writebuf;
  147. end
  148. else
  149. begin
  150. move(p[idx],buf[bufidx],len);
  151. inc(bufidx,len);
  152. exit;
  153. end;
  154. end;
  155. end;
  156. procedure tobjectwriter.WriteZeros(l:longint);
  157. var
  158. empty : array[0..1023] of byte;
  159. begin
  160. if l>sizeof(empty) then
  161. internalerror(200404081);
  162. if l>0 then
  163. begin
  164. fillchar(empty,l,0);
  165. Write(empty,l);
  166. end;
  167. end;
  168. procedure tobjectwriter.writearray(a:TDynamicArray);
  169. var
  170. hp : pdynamicblock;
  171. begin
  172. hp:=a.firstblock;
  173. while assigned(hp) do
  174. begin
  175. write(hp^.data,hp^.used);
  176. hp:=hp^.next;
  177. end;
  178. end;
  179. {****************************************************************************
  180. TObjectReader
  181. ****************************************************************************}
  182. constructor tobjectreader.create;
  183. begin
  184. buf:=nil;
  185. bufidx:=0;
  186. bufmax:=0;
  187. ffilename:='';
  188. opened:=false;
  189. end;
  190. destructor tobjectreader.destroy;
  191. begin
  192. if opened then
  193. closefile;
  194. freemem(buf);
  195. end;
  196. function tobjectreader.openfile(const fn:string):boolean;
  197. begin
  198. openfile:=false;
  199. f:=TCFileStream.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. getmem(buf,f.Size);
  207. f.read(buf^,f.Size);
  208. bufmax:=f.Size;
  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. end;
  220. function tobjectreader.readbuf:boolean;
  221. begin
  222. result:=true;
  223. end;
  224. procedure tobjectreader.seek(len:longint);
  225. begin
  226. bufidx:=len;
  227. end;
  228. function tobjectreader.read(out b;len:longint):boolean;
  229. begin
  230. move(buf[bufidx],b,len);
  231. inc(bufidx,len);
  232. result:=true;
  233. end;
  234. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  235. begin
  236. a.write(buf[bufidx],len);
  237. inc(bufidx,len);
  238. result:=true;
  239. end;
  240. function tobjectreader.getfilename : string;
  241. begin
  242. result:=ffilename;
  243. end;
  244. end.