owbase.pas 6.2 KB

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