owbase.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. Contains the base stuff for writing for object files to disk
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit owbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cstreams,
  23. cclasses;
  24. type
  25. tobjectwriter=class
  26. private
  27. f : TCFileStream;
  28. opened : boolean;
  29. buf : pchar;
  30. bufidx : longint;
  31. size : longint;
  32. procedure writebuf;
  33. public
  34. constructor create;
  35. destructor destroy;override;
  36. function createfile(const fn:string):boolean;virtual;
  37. procedure closefile;virtual;
  38. procedure writesym(const sym:string);virtual;
  39. procedure write(const b;len:longint);virtual;
  40. procedure WriteZeros(l:longint);
  41. end;
  42. tobjectreader=class
  43. private
  44. f : TCFileStream;
  45. opened : boolean;
  46. buf : pchar;
  47. bufidx,
  48. bufmax : longint;
  49. function readbuf:boolean;
  50. public
  51. constructor create;
  52. destructor destroy;override;
  53. function openfile(const fn:string):boolean;virtual;
  54. procedure closefile;virtual;
  55. procedure seek(len:longint);
  56. function read(var b;len:longint):boolean;virtual;
  57. function readarray(a:TDynamicArray;len:longint):boolean;
  58. end;
  59. implementation
  60. uses
  61. cutils,
  62. verbose;
  63. const
  64. bufsize = 32768;
  65. {****************************************************************************
  66. TObjectWriter
  67. ****************************************************************************}
  68. constructor tobjectwriter.create;
  69. begin
  70. getmem(buf,bufsize);
  71. bufidx:=0;
  72. opened:=false;
  73. size:=0;
  74. end;
  75. destructor tobjectwriter.destroy;
  76. begin
  77. if opened then
  78. closefile;
  79. freemem(buf,bufsize);
  80. end;
  81. function tobjectwriter.createfile(const fn:string):boolean;
  82. begin
  83. createfile:=false;
  84. f:=TCFileStream.Create(fn,fmCreate);
  85. if CStreamError<>0 then
  86. begin
  87. Message1(exec_e_cant_create_objectfile,fn);
  88. exit;
  89. end;
  90. bufidx:=0;
  91. size:=0;
  92. opened:=true;
  93. createfile:=true;
  94. end;
  95. procedure tobjectwriter.closefile;
  96. var
  97. fn : string;
  98. begin
  99. if bufidx>0 then
  100. writebuf;
  101. fn:=f.filename;
  102. f.free;
  103. { Remove if size is 0 }
  104. if size=0 then
  105. DeleteFile(fn);
  106. opened:=false;
  107. size:=0;
  108. end;
  109. procedure tobjectwriter.writebuf;
  110. begin
  111. f.write(buf^,bufidx);
  112. bufidx:=0;
  113. end;
  114. procedure tobjectwriter.writesym(const sym:string);
  115. begin
  116. end;
  117. procedure tobjectwriter.write(const b;len:longint);
  118. var
  119. p : pchar;
  120. left,
  121. idx : longint;
  122. begin
  123. inc(size,len);
  124. p:=pchar(@b);
  125. idx:=0;
  126. while len>0 do
  127. begin
  128. left:=bufsize-bufidx;
  129. if len>left then
  130. begin
  131. move(p[idx],buf[bufidx],left);
  132. dec(len,left);
  133. inc(idx,left);
  134. inc(bufidx,left);
  135. writebuf;
  136. end
  137. else
  138. begin
  139. move(p[idx],buf[bufidx],len);
  140. inc(bufidx,len);
  141. exit;
  142. end;
  143. end;
  144. end;
  145. procedure tobjectwriter.WriteZeros(l:longint);
  146. var
  147. empty : array[0..255] of byte;
  148. begin
  149. if l>sizeof(empty) then
  150. internalerror(200404081);
  151. if l>0 then
  152. begin
  153. fillchar(empty,l,0);
  154. Write(empty,l);
  155. end;
  156. end;
  157. {****************************************************************************
  158. TObjectReader
  159. ****************************************************************************}
  160. constructor tobjectreader.create;
  161. begin
  162. getmem(buf,bufsize);
  163. bufidx:=0;
  164. bufmax:=0;
  165. opened:=false;
  166. end;
  167. destructor tobjectreader.destroy;
  168. begin
  169. if opened then
  170. closefile;
  171. freemem(buf,bufsize);
  172. end;
  173. function tobjectreader.openfile(const fn:string):boolean;
  174. begin
  175. openfile:=false;
  176. f:=TCFileStream.Create(fn,fmOpenRead);
  177. if CStreamError<>0 then
  178. begin
  179. Message1(exec_e_cant_create_objectfile,fn);
  180. exit;
  181. end;
  182. bufidx:=0;
  183. bufmax:=0;
  184. opened:=true;
  185. openfile:=true;
  186. end;
  187. procedure tobjectreader.closefile;
  188. begin
  189. f.free;
  190. opened:=false;
  191. bufidx:=0;
  192. bufmax:=0;
  193. end;
  194. function tobjectreader.readbuf:boolean;
  195. begin
  196. bufmax:=f.read(buf^,bufsize);
  197. bufidx:=0;
  198. readbuf:=(bufmax>0);
  199. end;
  200. procedure tobjectreader.seek(len:longint);
  201. begin
  202. f.seek(len,soFromBeginning);
  203. bufidx:=0;
  204. bufmax:=0;
  205. end;
  206. function tobjectreader.read(var b;len:longint):boolean;
  207. var
  208. p : pchar;
  209. left,
  210. idx : longint;
  211. begin
  212. read:=false;
  213. if bufmax=0 then
  214. if not readbuf then
  215. exit;
  216. p:=pchar(@b);
  217. idx:=0;
  218. while len>0 do
  219. begin
  220. left:=bufmax-bufidx;
  221. if len>left then
  222. begin
  223. move(buf[bufidx],p[idx],left);
  224. dec(len,left);
  225. inc(idx,left);
  226. inc(bufidx,left);
  227. if not readbuf then
  228. exit;
  229. end
  230. else
  231. begin
  232. move(buf[bufidx],p[idx],len);
  233. inc(bufidx,len);
  234. inc(idx,len);
  235. break;
  236. end;
  237. end;
  238. read:=(idx=len);
  239. end;
  240. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  241. var
  242. orglen,
  243. left,
  244. idx : longint;
  245. begin
  246. readarray:=false;
  247. if bufmax=0 then
  248. if not readbuf then
  249. exit;
  250. orglen:=len;
  251. idx:=0;
  252. while len>0 do
  253. begin
  254. left:=bufmax-bufidx;
  255. if len>left then
  256. begin
  257. a.Write(buf[bufidx],left);
  258. dec(len,left);
  259. inc(idx,left);
  260. inc(bufidx,left);
  261. if not readbuf then
  262. exit;
  263. end
  264. else
  265. begin
  266. a.Write(buf[bufidx],len);
  267. inc(bufidx,len);
  268. inc(idx,len);
  269. break;
  270. end;
  271. end;
  272. readarray:=(idx=orglen);
  273. end;
  274. end.
  275. {
  276. $Log$
  277. Revision 1.14 2005-02-14 17:13:07 peter
  278. * truncate log
  279. }