owbase.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. end;
  41. tobjectreader=class
  42. private
  43. f : TCFileStream;
  44. opened : boolean;
  45. buf : pchar;
  46. bufidx,
  47. bufmax : longint;
  48. function readbuf:boolean;
  49. public
  50. constructor create;
  51. destructor destroy;override;
  52. function openfile(const fn:string):boolean;virtual;
  53. procedure closefile;virtual;
  54. procedure seek(len:longint);
  55. function read(var b;len:longint):boolean;virtual;
  56. function readarray(a:TDynamicArray;len:longint):boolean;
  57. end;
  58. implementation
  59. uses
  60. cutils,
  61. verbose;
  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. DeleteFile(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. {****************************************************************************
  145. TObjectReader
  146. ****************************************************************************}
  147. constructor tobjectreader.create;
  148. begin
  149. getmem(buf,bufsize);
  150. bufidx:=0;
  151. bufmax:=0;
  152. opened:=false;
  153. end;
  154. destructor tobjectreader.destroy;
  155. begin
  156. if opened then
  157. closefile;
  158. freemem(buf,bufsize);
  159. end;
  160. function tobjectreader.openfile(const fn:string):boolean;
  161. begin
  162. openfile:=false;
  163. f:=TCFileStream.Create(fn,fmOpenRead);
  164. if CStreamError<>0 then
  165. begin
  166. Message1(exec_e_cant_create_objectfile,fn);
  167. exit;
  168. end;
  169. bufidx:=0;
  170. bufmax:=0;
  171. opened:=true;
  172. openfile:=true;
  173. end;
  174. procedure tobjectreader.closefile;
  175. begin
  176. f.free;
  177. opened:=false;
  178. bufidx:=0;
  179. bufmax:=0;
  180. end;
  181. function tobjectreader.readbuf:boolean;
  182. begin
  183. bufmax:=f.read(buf^,bufsize);
  184. bufidx:=0;
  185. readbuf:=(bufmax>0);
  186. end;
  187. procedure tobjectreader.seek(len:longint);
  188. begin
  189. f.seek(len,soFromBeginning);
  190. bufidx:=0;
  191. bufmax:=0;
  192. end;
  193. function tobjectreader.read(var b;len:longint):boolean;
  194. var
  195. p : pchar;
  196. left,
  197. idx : longint;
  198. begin
  199. read:=false;
  200. if bufmax=0 then
  201. if not readbuf then
  202. exit;
  203. p:=pchar(@b);
  204. idx:=0;
  205. while len>0 do
  206. begin
  207. left:=bufmax-bufidx;
  208. if len>left then
  209. begin
  210. move(buf[bufidx],p[idx],left);
  211. dec(len,left);
  212. inc(idx,left);
  213. inc(bufidx,left);
  214. if not readbuf then
  215. exit;
  216. end
  217. else
  218. begin
  219. move(buf[bufidx],p[idx],len);
  220. inc(bufidx,len);
  221. inc(idx,len);
  222. break;
  223. end;
  224. end;
  225. read:=(idx=len);
  226. end;
  227. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  228. var
  229. orglen,
  230. left,
  231. idx : longint;
  232. begin
  233. readarray:=false;
  234. if bufmax=0 then
  235. if not readbuf then
  236. exit;
  237. orglen:=len;
  238. idx:=0;
  239. while len>0 do
  240. begin
  241. left:=bufmax-bufidx;
  242. if len>left then
  243. begin
  244. a.Write(buf[bufidx],left);
  245. dec(len,left);
  246. inc(idx,left);
  247. inc(bufidx,left);
  248. if not readbuf then
  249. exit;
  250. end
  251. else
  252. begin
  253. a.Write(buf[bufidx],len);
  254. inc(bufidx,len);
  255. inc(idx,len);
  256. break;
  257. end;
  258. end;
  259. readarray:=(idx=orglen);
  260. end;
  261. end.
  262. {
  263. $Log$
  264. Revision 1.11 2002-07-01 18:46:24 peter
  265. * internal linker
  266. * reorganized aasm layer
  267. Revision 1.10 2002/05/18 13:34:11 peter
  268. * readded missing revisions
  269. Revision 1.9 2002/05/16 19:46:42 carl
  270. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  271. + try to fix temp allocation (still in ifdef)
  272. + generic constructor calls
  273. + start of tassembler / tmodulebase class cleanup
  274. }