1 /*******************************************************************************
6 * Contents: A streambuf which uses the GNU readline library for line I/O
7 * (c) 2001 by Dimitris Vyzovitis [vyzo@media.mit.edu]
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.
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.
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,
24 ******************************************************************************/
26 #ifndef _READLINEBUF_H_
27 #define _READLINEBUF_H_
35 #include <readline/readline.h>
36 #include <readline/history.h>
38 #if (defined __GNUC__) && (__GNUC__ < 3)
39 #include <streambuf.h>
42 using std::streamsize
;
46 class readlinebuf
: public streambuf
{
48 #if (defined __GNUC__) && (__GNUC__ < 3)
49 typedef char char_type
;
51 typedef streampos pos_type
;
52 typedef streamoff off_type
;
54 static const int_type eof
= EOF
; // this is -1
55 static const int_type not_eof
= 0;
66 virtual int_type
showmanyc() const { return high_
- low_
; }
68 virtual streamsize
xsgetn( char_type
* buf
, streamsize n
) {
69 int rd
= n
> (high_
- low_
)? (high_
- low_
) : n
;
70 memcpy( buf
, line_
, rd
);
75 free( line_
); // free( NULL ) is a noop
76 line_
= readline( prompt_
);
78 high_
= strlen( line_
);
79 if ( history_
&& high_
) add_history( line_
);
80 rd
+= xsgetn( buf
+ rd
, n
- rd
);
87 virtual int_type
underflow() {
88 if ( high_
== low_
) {
90 free( line_
); // free( NULL ) is a noop
91 line_
= readline( prompt_
);
93 high_
= strlen( line_
);
94 if ( history_
&& high_
) add_history( line_
);
98 if ( low_
< high_
) return line_
[low_
];
102 virtual int_type
uflow() {
103 int_type c
= underflow();
104 if ( c
!= eof
) ++low_
;
108 virtual int_type
pbackfail( int_type c
= eof
) {
109 if ( low_
> 0 ) --low_
;
110 else if ( c
!= eof
) {
112 char* nl
= (char*)realloc( line_
, high_
+ 1 );
114 line_
= (char*)memcpy( nl
+ 1, line_
, high_
);
116 line_
[0] = char( c
);
120 line_
= (char*)malloc( sizeof( char ) );
130 readlinebuf( const char* prompt
= NULL
, bool history
= true )
131 : prompt_( prompt
), history_( history
),
132 line_( NULL
), low_( 0 ), high_( 0 ) {
This page took 0.031435 seconds and 4 git commands to generate.