owbase.pas 6.4 KB

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