owbase.pas 7.1 KB

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