owbase.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. $Id$
  3. Copyright (c) 1999 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. var
  71. i : longint;
  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. i:=ioresult;
  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(sym:string);
  93. begin
  94. end;
  95. procedure tobjectwriter.write(var 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.2 1999-05-09 11:38:07 peter
  127. * don't write .o and link if errors occure during assembling
  128. Revision 1.1 1999/05/01 13:24:26 peter
  129. * merged nasm compiler
  130. * old asm moved to oldasm/
  131. Revision 1.2 1999/03/18 20:30:51 peter
  132. + .a writer
  133. Revision 1.1 1999/03/08 14:51:11 peter
  134. + smartlinking for ag386bin
  135. }