owbase.pas 6.3 KB

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