readlinebuf.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*******************************************************************************
  2. * $Revision$
  3. * $Date$
  4. * $Author$
  5. *
  6. * Contents: A streambuf which uses the GNU readline library for line I/O
  7. * (c) 2001 by Dimitris Vyzovitis [vyzo@media.mit.edu]
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program; if not, write to the Free
  21. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. ******************************************************************************/
  25. #ifndef _READLINEBUF_H_
  26. #define _READLINEBUF_H_
  27. #include <iostream>
  28. #include <cstring>
  29. #include <cassert>
  30. #include <cstdlib>
  31. #include <cstdio>
  32. #include <readline/readline.h>
  33. #include <readline/history.h>
  34. #if (defined __GNUC__) && (__GNUC__ < 3)
  35. #include <streambuf.h>
  36. #else
  37. #include <streambuf>
  38. using std::streamsize;
  39. using std::streambuf;
  40. #endif
  41. class readlinebuf : public streambuf {
  42. public:
  43. #if (defined __GNUC__) && (__GNUC__ < 3)
  44. typedef char char_type;
  45. typedef int int_type;
  46. typedef streampos pos_type;
  47. typedef streamoff off_type;
  48. #endif
  49. static const int_type eof = EOF; // this is -1
  50. static const int_type not_eof = 0;
  51. private:
  52. const char* prompt_;
  53. bool history_;
  54. char* line_;
  55. int low_;
  56. int high_;
  57. protected:
  58. virtual int_type showmanyc() const { return high_ - low_; }
  59. virtual streamsize xsgetn( char_type* buf, streamsize n ) {
  60. int rd = n > (high_ - low_)? (high_ - low_) : n;
  61. memcpy( buf, line_, rd );
  62. low_ += rd;
  63. if ( rd < n ) {
  64. low_ = high_ = 0;
  65. free( line_ ); // free( NULL ) is a noop
  66. line_ = readline( prompt_ );
  67. if ( line_ ) {
  68. high_ = strlen( line_ );
  69. if ( history_ && high_ ) add_history( line_ );
  70. rd += xsgetn( buf + rd, n - rd );
  71. }
  72. }
  73. return rd;
  74. }
  75. virtual int_type underflow() {
  76. if ( high_ == low_ ) {
  77. low_ = high_ = 0;
  78. free( line_ ); // free( NULL ) is a noop
  79. line_ = readline( prompt_ );
  80. if ( line_ ) {
  81. high_ = strlen( line_ );
  82. if ( history_ && high_ ) add_history( line_ );
  83. }
  84. }
  85. if ( low_ < high_ ) return line_[low_];
  86. else return eof;
  87. }
  88. virtual int_type uflow() {
  89. int_type c = underflow();
  90. if ( c != eof ) ++low_;
  91. return c;
  92. }
  93. virtual int_type pbackfail( int_type c = eof ) {
  94. if ( low_ > 0 ) --low_;
  95. else if ( c != eof ) {
  96. if ( high_ > 0 ) {
  97. char* nl = (char*)realloc( line_, high_ + 1 );
  98. if ( nl ) {
  99. line_ = (char*)memcpy( nl + 1, line_, high_ );
  100. high_ += 1;
  101. line_[0] = char( c );
  102. } else return eof;
  103. } else {
  104. assert( !line_ );
  105. line_ = (char*)malloc( sizeof( char ) );
  106. *line_ = char( c );
  107. high_ = 1;
  108. }
  109. } else return eof;
  110. return not_eof;
  111. }
  112. public:
  113. readlinebuf( const char* prompt = NULL, bool history = true )
  114. : prompt_( prompt ), history_( history ),
  115. line_( NULL ), low_( 0 ), high_( 0 ) {
  116. setbuf( 0, 0 );
  117. }
  118. };
  119. #endif