streamer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ** Command & Conquer Generals(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. ///////// calling sync seems to cause crashes here on Win32
  30. //sync();
  31. /////////
  32. delete[](base());
  33. }
  34. int Streamer::setOutputDevice(OutputDevice *device)
  35. {
  36. Output_Device=device;
  37. return(0);
  38. }
  39. // put n chars from string into buffer
  40. int Streamer::xsputn(const char* buf, int size) //implementation of sputn
  41. {
  42. if (size<=0) // Nothing to do
  43. return(0);
  44. const unsigned char *ptr=(const unsigned char *)buf;
  45. for (int i=0; i<size; i++, ptr++)
  46. {
  47. if(*ptr=='\n')
  48. {
  49. if (overflow(*ptr)==EOF)
  50. return(i);
  51. }
  52. else if (sputc(*ptr)==EOF)
  53. return(i);
  54. }
  55. return(size);
  56. }
  57. // Flush the buffer and make more room if needed
  58. int Streamer::overflow(int c)
  59. {
  60. if (c==EOF)
  61. return(sync());
  62. if ((pbase()==0) && (doallocate()==0))
  63. return(EOF);
  64. if((pptr() >= epptr()) && (sync()==EOF))
  65. return(EOF);
  66. else {
  67. sputc(c);
  68. if ((unbuffered() && c=='\n' || pptr() >= epptr())
  69. && sync()==EOF) {
  70. return(EOF);
  71. }
  72. return(c);
  73. }
  74. }
  75. // This is a write only stream, this should never happen
  76. int Streamer::underflow(void)
  77. {
  78. return(EOF);
  79. }
  80. int Streamer::doallocate()
  81. {
  82. if (base()==NULL)
  83. {
  84. char *buf=new char[(2*STREAMER_BUFSIZ)]; // deleted by destructor
  85. memset(buf,0,2*STREAMER_BUFSIZ);
  86. // Buffer
  87. setb(
  88. buf, // base pointer
  89. buf+STREAMER_BUFSIZ, // ebuf pointer (end of buffer);
  90. 0); // 0 = manual deletion of buff
  91. // Get area
  92. setg(
  93. buf, // eback
  94. buf, // gptr
  95. buf); // egptr
  96. buf+=STREAMER_BUFSIZ;
  97. // Put area
  98. setp(buf,buf+STREAMER_BUFSIZ);
  99. return(1);
  100. }
  101. else
  102. return(0);
  103. }
  104. int Streamer::sync()
  105. {
  106. if (pptr()<=pbase()) {
  107. return(0);
  108. }
  109. int wlen=pptr()-pbase();
  110. if (Output_Device)
  111. {
  112. Output_Device->print(pbase(),wlen);
  113. }
  114. if (unbuffered()) {
  115. setp(pbase(),pbase());
  116. }
  117. else {
  118. setp(pbase(),pbase()+STREAMER_BUFSIZ);
  119. }
  120. return(0);
  121. }