streambuf.C 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: streambuf.C /main/8 1996/08/21 15:55:14 drk $ */
  24. #include "utility/c_streambuf.h"
  25. #include <stdlib.h>
  26. #define DEF_BUF_SIZ 4096
  27. streambuf::streambuf() : _size(0), _capacity(DEF_BUF_SIZ), _alloc(1),
  28. _pcount(0), _gcount(0)
  29. {
  30. base = (char*)malloc(DEF_BUF_SIZ);
  31. end = base + DEF_BUF_SIZ;
  32. get_ptr = put_ptr = base;
  33. }
  34. streambuf::streambuf(char* p, int l, int bufferFull) :
  35. base(p), end(p+l), put_ptr(p), get_ptr(p), _size(0), _capacity(l),
  36. _alloc(0), _pcount(0), _gcount(0)
  37. {
  38. if (bufferFull) {
  39. _size = l;
  40. }
  41. }
  42. streambuf::~streambuf()
  43. {
  44. if ( _alloc )
  45. free(base);
  46. }
  47. int streambuf::examine()
  48. {
  49. notify(GET);
  50. if ( empty() && underflow() == EOF )
  51. return EOF;
  52. return (unsigned char)(*get_ptr);
  53. }
  54. int streambuf::get()
  55. {
  56. notify(GET);
  57. if ( empty() && underflow() == EOF )
  58. return EOF;
  59. int x = (unsigned char)(*get_ptr);
  60. move_get_ptr(+1);
  61. _size--;
  62. _gcount++;
  63. return x;
  64. }
  65. int streambuf::putback(char c)
  66. {
  67. if ( full() )
  68. return EOF;
  69. move_get_ptr(-1);
  70. _size++;
  71. *get_ptr = c;
  72. return 0;
  73. }
  74. int streambuf::put(char c)
  75. {
  76. notify(PUT);
  77. if ( full() && overflow() == EOF )
  78. return EOF;
  79. *put_ptr = c;
  80. move_put_ptr(1);
  81. _size++;
  82. _pcount++;
  83. return 0;
  84. }
  85. int streambuf::move_get_ptr(int one)
  86. {
  87. switch (one) {
  88. case 1:
  89. get_ptr++;
  90. if ( get_ptr == end )
  91. get_ptr = base;
  92. return 0;
  93. case -1:
  94. get_ptr--;
  95. if ( get_ptr == base-1 )
  96. get_ptr = end-1;
  97. return 0;
  98. default:
  99. return EOF;
  100. }
  101. }
  102. int streambuf::move_put_ptr(int one)
  103. {
  104. switch (one) {
  105. case 1:
  106. if ( get_ptr == 0 )
  107. get_ptr = put_ptr;
  108. put_ptr++;
  109. if ( put_ptr == end )
  110. put_ptr = base;
  111. return 0;
  112. default:
  113. return EOF;
  114. }
  115. }