owbase.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. f := nil;
  129. { Remove if size is 0 }
  130. if size=0 then
  131. DeleteFile(fn);
  132. opened:=false;
  133. fsize:=0;
  134. fobjsize:=0;
  135. end;
  136. procedure tobjectwriter.writebuf;
  137. begin
  138. f.write(buf[0],bufidx);
  139. bufidx:=0;
  140. end;
  141. procedure tobjectwriter.writesym(const sym:string);
  142. begin
  143. end;
  144. procedure tobjectwriter.write(const b;len:longword);
  145. var
  146. p : pchar;
  147. bufleft,
  148. idx : longword;
  149. begin
  150. inc(fsize,len);
  151. inc(fobjsize,len);
  152. p:=pchar(@b);
  153. idx:=0;
  154. while len>0 do
  155. begin
  156. bufleft:=bufsize-bufidx;
  157. if bufleft=0 then
  158. Writebuf;
  159. if len>bufleft then
  160. begin
  161. move(p[idx],buf[bufidx],bufleft);
  162. dec(len,bufleft);
  163. inc(idx,bufleft);
  164. inc(bufidx,bufleft);
  165. writebuf;
  166. end
  167. else
  168. begin
  169. move(p[idx],buf[bufidx],len);
  170. inc(bufidx,len);
  171. exit;
  172. end;
  173. end;
  174. end;
  175. procedure tobjectwriter.WriteZeros(l:longword);
  176. var
  177. empty : array[0..1023] of byte;
  178. begin
  179. if l>sizeof(empty) then
  180. begin
  181. fillchar(empty,sizeof(empty),0);
  182. while l>sizeof(empty) do
  183. begin
  184. Write(empty,sizeof(empty));
  185. Dec(l,sizeof(empty));
  186. end;
  187. if l>0 then
  188. Write(empty,l);
  189. end
  190. else if l>0 then
  191. begin
  192. fillchar(empty,l,0);
  193. Write(empty,l);
  194. end;
  195. end;
  196. procedure tobjectwriter.writearray(a:TDynamicArray);
  197. var
  198. hp : pdynamicblock;
  199. begin
  200. hp:=a.firstblock;
  201. while assigned(hp) do
  202. begin
  203. write(hp^.data,hp^.used);
  204. hp:=hp^.next;
  205. end;
  206. end;
  207. {****************************************************************************
  208. TObjectReader
  209. ****************************************************************************}
  210. constructor tobjectreader.create;
  211. begin
  212. buf:=nil;
  213. bufidx:=0;
  214. bufmax:=0;
  215. ffilename:='';
  216. opened:=false;
  217. end;
  218. destructor tobjectreader.destroy;
  219. begin
  220. if opened then
  221. closefile;
  222. end;
  223. constructor tobjectreader.createAr(const Aarfn:string;allow_nonar:boolean=false);
  224. begin
  225. InternalError(2015081401);
  226. end;
  227. function tobjectreader.openfile(const fn:string):boolean;
  228. begin
  229. openfile:=false;
  230. f:=CFileStreamClass.Create(fn,fmOpenRead);
  231. if CStreamError<>0 then
  232. begin
  233. Comment(V_Error,'Can''t open object file: '+fn);
  234. exit;
  235. end;
  236. ffilename:=fn;
  237. bufmax:=f.Size;
  238. setlength(buf,bufmax);
  239. f.read(buf[0],bufmax);
  240. f.free;
  241. f := nil;
  242. bufidx:=0;
  243. opened:=true;
  244. openfile:=true;
  245. end;
  246. procedure tobjectreader.closefile;
  247. begin
  248. opened:=false;
  249. bufidx:=0;
  250. bufmax:=0;
  251. buf:=Nil;
  252. end;
  253. function tobjectreader.readbuf:boolean;
  254. begin
  255. result:=bufidx<bufmax;
  256. end;
  257. procedure tobjectreader.seek(len:longint);
  258. begin
  259. bufidx:=len;
  260. end;
  261. function tobjectreader.read(out b;len:longint):boolean;
  262. begin
  263. result:=true;
  264. if bufidx+len>bufmax then
  265. begin
  266. result:=false;
  267. len:=bufmax-bufidx;
  268. end;
  269. move(buf[bufidx],b,len);
  270. inc(bufidx,len);
  271. end;
  272. function tobjectreader.readarray(a:TDynamicArray;len:longint):boolean;
  273. begin
  274. result:=true;
  275. if bufidx+len>bufmax then
  276. begin
  277. result:=false;
  278. len:=bufmax-bufidx;
  279. end;
  280. a.write(buf[bufidx],len);
  281. inc(bufidx,len);
  282. end;
  283. function tobjectreader.getfilename : string;
  284. begin
  285. result:=ffilename;
  286. end;
  287. function tobjectreader.GetSize: longint;
  288. begin
  289. result:=bufmax;
  290. end;
  291. function tobjectreader.GetPos: longint;
  292. begin
  293. Result:=bufidx;
  294. end;
  295. function tobjectreader.GetIsArchive: boolean;
  296. begin
  297. Result:=false;
  298. end;
  299. end.