owbase.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. interface
  21. uses
  22. cstreams;
  23. type
  24. tobjectwriter=class
  25. constructor create;
  26. destructor destroy;override;
  27. procedure createfile(const fn:string);virtual;
  28. procedure closefile;virtual;
  29. procedure writesym(const sym:string);virtual;
  30. procedure write(const b;len:longint);virtual;
  31. private
  32. f : TCFileStream;
  33. opened : boolean;
  34. buf : pchar;
  35. bufidx : longint;
  36. size : longint;
  37. procedure writebuf;
  38. end;
  39. implementation
  40. uses
  41. cutils,
  42. verbose;
  43. const
  44. bufsize = 32768;
  45. constructor tobjectwriter.create;
  46. begin
  47. getmem(buf,bufsize);
  48. bufidx:=0;
  49. opened:=false;
  50. size:=0;
  51. end;
  52. destructor tobjectwriter.destroy;
  53. begin
  54. if opened then
  55. closefile;
  56. freemem(buf,bufsize);
  57. end;
  58. procedure tobjectwriter.createfile(const fn:string);
  59. begin
  60. f:=TCFileStream.Create(fn,fmCreate);
  61. if CStreamError<>0 then
  62. begin
  63. Message1(exec_e_cant_create_objectfile,fn);
  64. exit;
  65. end;
  66. bufidx:=0;
  67. size:=0;
  68. opened:=true;
  69. end;
  70. procedure tobjectwriter.closefile;
  71. var
  72. fn : string;
  73. begin
  74. if bufidx>0 then
  75. writebuf;
  76. fn:=f.filename;
  77. f.free;
  78. { Remove if size is 0 }
  79. if size=0 then
  80. DeleteFile(fn);
  81. opened:=false;
  82. size:=0;
  83. end;
  84. procedure tobjectwriter.writebuf;
  85. begin
  86. f.write(buf^,bufidx);
  87. bufidx:=0;
  88. end;
  89. procedure tobjectwriter.writesym(const sym:string);
  90. begin
  91. end;
  92. procedure tobjectwriter.write(const b;len:longint);
  93. var
  94. p : pchar;
  95. left,
  96. idx : longint;
  97. begin
  98. inc(size,len);
  99. p:=pchar(@b);
  100. idx:=0;
  101. while len>0 do
  102. begin
  103. left:=bufsize-bufidx;
  104. if len>left then
  105. begin
  106. move(p[idx],buf[bufidx],left);
  107. dec(len,left);
  108. inc(idx,left);
  109. inc(bufidx,left);
  110. writebuf;
  111. end
  112. else
  113. begin
  114. move(p[idx],buf[bufidx],len);
  115. inc(bufidx,len);
  116. exit;
  117. end;
  118. end;
  119. end;
  120. end.
  121. {
  122. $Log$
  123. Revision 1.6 2000-12-24 12:25:32 peter
  124. + cstreams unit
  125. * dynamicarray object to class
  126. Revision 1.4 2000/09/24 15:06:20 peter
  127. * use defines.inc
  128. Revision 1.3 2000/08/19 18:44:27 peter
  129. * new tdynamicarray implementation using blocks instead of
  130. reallocmem (merged)
  131. Revision 1.2 2000/07/13 11:32:44 michael
  132. + removed logs
  133. }