owbase.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. uses
  39. verbose;
  40. const
  41. {$ifdef TP}
  42. bufsize = 256;
  43. {$else}
  44. bufsize = 32768;
  45. {$endif}
  46. constructor tobjectwriter.init;
  47. begin
  48. getmem(buf,bufsize);
  49. bufidx:=0;
  50. opened:=false;
  51. size:=0;
  52. end;
  53. destructor tobjectwriter.done;
  54. begin
  55. if opened then
  56. close;
  57. freemem(buf,bufsize);
  58. end;
  59. procedure tobjectwriter.create(const fn:string);
  60. begin
  61. assign(f,fn);
  62. {$I-}
  63. rewrite(f,1);
  64. {$I+}
  65. if ioresult<>0 then
  66. begin
  67. Message1(exec_e_cant_create_objectfile,fn);
  68. exit;
  69. end;
  70. bufidx:=0;
  71. size:=0;
  72. opened:=true;
  73. end;
  74. procedure tobjectwriter.close;
  75. begin
  76. if bufidx>0 then
  77. writebuf;
  78. system.close(f);
  79. { Remove if size is 0 }
  80. if size=0 then
  81. begin
  82. {$I-}
  83. system.erase(f);
  84. {$I+}
  85. if ioresult<>0 then;
  86. end;
  87. opened:=false;
  88. size:=0;
  89. end;
  90. procedure tobjectwriter.writebuf;
  91. begin
  92. blockwrite(f,buf^,bufidx);
  93. bufidx:=0;
  94. end;
  95. procedure tobjectwriter.writesym(sym:string);
  96. begin
  97. end;
  98. procedure tobjectwriter.write(var b;len:longint);
  99. var
  100. p : pchar;
  101. left,
  102. idx : longint;
  103. begin
  104. inc(size,len);
  105. p:=pchar(@b);
  106. idx:=0;
  107. while len>0 do
  108. begin
  109. left:=bufsize-bufidx;
  110. if len>left then
  111. begin
  112. move(p[idx],buf[bufidx],left);
  113. dec(len,left);
  114. inc(idx,left);
  115. inc(bufidx,left);
  116. writebuf;
  117. end
  118. else
  119. begin
  120. move(p[idx],buf[bufidx],len);
  121. inc(bufidx,len);
  122. exit;
  123. end;
  124. end;
  125. end;
  126. end.
  127. {
  128. $Log$
  129. Revision 1.6 2000-04-02 15:22:19 florian
  130. * fixed bug 903: the compiler gives now a nice message if it can't create
  131. the .o file, (same for future .ar)
  132. Revision 1.5 2000/02/24 18:41:39 peter
  133. * removed warnings/notes
  134. Revision 1.4 2000/02/09 13:22:55 peter
  135. * log truncated
  136. Revision 1.3 2000/01/07 01:14:28 peter
  137. * updated copyright to 2000
  138. }