owbase.pas 2.9 KB

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