owbase.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. type
  22. pobjectwriter=^tobjectwriter;
  23. tobjectwriter=object
  24. constructor Init;
  25. destructor Done;virtual;
  26. procedure create(const fn:string);virtual;
  27. procedure close;virtual;
  28. procedure writesym(const sym:string);virtual;
  29. procedure write(const b;len:longint);virtual;
  30. private
  31. f : file;
  32. opened : boolean;
  33. buf : pchar;
  34. bufidx : longint;
  35. size : longint;
  36. procedure writebuf;
  37. end;
  38. implementation
  39. uses
  40. verbose;
  41. const
  42. bufsize = 32768;
  43. constructor tobjectwriter.init;
  44. begin
  45. getmem(buf,bufsize);
  46. bufidx:=0;
  47. opened:=false;
  48. size:=0;
  49. end;
  50. destructor tobjectwriter.done;
  51. begin
  52. if opened then
  53. close;
  54. freemem(buf,bufsize);
  55. end;
  56. procedure tobjectwriter.create(const fn:string);
  57. begin
  58. assign(f,fn);
  59. {$I-}
  60. rewrite(f,1);
  61. {$I+}
  62. if ioresult<>0 then
  63. begin
  64. Message1(exec_e_cant_create_objectfile,fn);
  65. exit;
  66. end;
  67. bufidx:=0;
  68. size:=0;
  69. opened:=true;
  70. end;
  71. procedure tobjectwriter.close;
  72. begin
  73. if bufidx>0 then
  74. writebuf;
  75. system.close(f);
  76. { Remove if size is 0 }
  77. if size=0 then
  78. begin
  79. {$I-}
  80. system.erase(f);
  81. {$I+}
  82. if ioresult<>0 then;
  83. end;
  84. opened:=false;
  85. size:=0;
  86. end;
  87. procedure tobjectwriter.writebuf;
  88. begin
  89. blockwrite(f,buf^,bufidx);
  90. bufidx:=0;
  91. end;
  92. procedure tobjectwriter.writesym(const sym:string);
  93. begin
  94. end;
  95. procedure tobjectwriter.write(const b;len:longint);
  96. var
  97. p : pchar;
  98. left,
  99. idx : longint;
  100. begin
  101. inc(size,len);
  102. p:=pchar(@b);
  103. idx:=0;
  104. while len>0 do
  105. begin
  106. left:=bufsize-bufidx;
  107. if len>left then
  108. begin
  109. move(p[idx],buf[bufidx],left);
  110. dec(len,left);
  111. inc(idx,left);
  112. inc(bufidx,left);
  113. writebuf;
  114. end
  115. else
  116. begin
  117. move(p[idx],buf[bufidx],len);
  118. inc(bufidx,len);
  119. exit;
  120. end;
  121. end;
  122. end;
  123. end.
  124. {
  125. $Log$
  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. }