owbase.pas 6.7 KB

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