STREAMER.CPP 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  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 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "streamer.h"
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #endif
  22. Streamer::Streamer() : streambuf()
  23. {
  24. int state=unbuffered();
  25. unbuffered(0); // 0 = buffered, 1 = unbuffered
  26. }
  27. Streamer::~Streamer()
  28. {
  29. sync();
  30. delete[](base());
  31. }
  32. int Streamer::setOutputDevice(OutputDevice *device)
  33. {
  34. Output_Device=device;
  35. return(0);
  36. }
  37. // put n chars from string into buffer
  38. int Streamer::xsputn(const char* buf, int size) //implementation of sputn
  39. {
  40. if (size<=0) // Nothing to do
  41. return(0);
  42. const unsigned char *ptr=(const unsigned char *)buf;
  43. for (int i=0; i<size; i++, ptr++)
  44. {
  45. if(*ptr=='\n')
  46. {
  47. if (overflow(*ptr)==EOF)
  48. return(i);
  49. }
  50. else if (sputc(*ptr)==EOF)
  51. return(i);
  52. }
  53. return(size);
  54. }
  55. // Flush the buffer and make more room if needed
  56. int Streamer::overflow(int c)
  57. {
  58. if (c==EOF)
  59. return(sync());
  60. if ((pbase()==0) && (doallocate()==0))
  61. return(EOF);
  62. if((pptr() >= epptr()) && (sync()==EOF))
  63. return(EOF);
  64. else {
  65. sputc(c);
  66. if ((unbuffered() && c=='\n' || pptr() >= epptr())
  67. && sync()==EOF) {
  68. return(EOF);
  69. }
  70. return(c);
  71. }
  72. }
  73. // This is a write only stream, this should never happen
  74. int Streamer::underflow(void)
  75. {
  76. return(EOF);
  77. }
  78. int Streamer::doallocate()
  79. {
  80. if (base()==NULL)
  81. {
  82. char *buf=new char[(2*STREAMER_BUFSIZ)]; // deleted by destructor
  83. memset(buf,0,2*STREAMER_BUFSIZ);
  84. // Buffer
  85. setb(
  86. buf, // base pointer
  87. buf+STREAMER_BUFSIZ, // ebuf pointer (end of buffer);
  88. 0); // 0 = manual deletion of buff
  89. // Get area
  90. setg(
  91. buf, // eback
  92. buf, // gptr
  93. buf); // egptr
  94. buf+=STREAMER_BUFSIZ;
  95. // Put area
  96. setp(buf,buf+STREAMER_BUFSIZ);
  97. return(1);
  98. }
  99. else
  100. return(0);
  101. }
  102. int Streamer::sync()
  103. {
  104. if (pptr()<=pbase()) {
  105. return(0);
  106. }
  107. int wlen=pptr()-pbase();
  108. if (Output_Device)
  109. {
  110. Output_Device->print(pbase(),wlen);
  111. }
  112. if (unbuffered()) {
  113. setp(pbase(),pbase());
  114. }
  115. else {
  116. setp(pbase(),pbase()+STREAMER_BUFSIZ);
  117. }
  118. return(0);
  119. }