2
0

owbase.pas 6.7 KB

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