owbase.pas 6.7 KB

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