-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaliased_buf.h
128 lines (98 loc) · 2.69 KB
/
aliased_buf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* aliased_buf.h
*
* Created on: 2012-4-6
* Author: gxl
*/
#ifndef ALIASED_BUF_H_
#define ALIASED_BUF_H_
#include <streambuf>
class aliased_buf : public std::basic_streambuf<char>
{
public:
typedef std::basic_streambuf<char> parent_type ;
public:
aliased_buf(char* buf,int size)
{
this->setp(buf,buf + size) ;
this->setg(buf,buf,buf + size ) ;
}
protected:
virtual int_type overflow(int_type c )
{
if(this->pptr() < this->epptr() )
{
return traits_type::to_int_type(*this->pptr());
}
return traits_type::eof() ;
}
virtual int_type underflow()
{
if (this->gptr() < this->egptr())
{
return traits_type::to_int_type(*this->gptr());
}
return traits_type::eof();
}
virtual int sync()
{
return 0 ;
}
virtual parent_type* setbuf( char * s, std::streamsize n )
{
return this ;
}
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode)
{
pos_type ret = pos_type(off_type(-1));
off_type newoffi = 0 , newoffo = 0 ;
if(way == std::ios_base::cur)
{
if(mode & std::ios_base::in)
{
newoffi = this->gptr() - this->eback() + off ;
}
if(mode & std::ios_base::out)
{
newoffi = this->pptr() - this->pbase() + off ;
}
}
else if(way == std::ios_base::end)
{
if(mode & std::ios_base::in)
{
newoffi = this->egptr() - this->eback() + off ;
}
if(mode & std::ios_base::out)
{
newoffo = this->epptr() - this->pbase() + off ;
}
}
if( (mode & std::ios_base::in) && newoffi >= 0 && newoffi < this->egptr() - this->eback() )
{
this->gbump( this->eback() + newoffi - this->gptr()) ;
ret = pos_type(newoffi) ;
}
if((mode & std::ios_base::out) && newoffo >=0 && newoffo < this->epptr() - this->pbase() )
{
this->pbump( this->pbase() + newoffi - this->pptr()) ;
ret = pos_type(newoffi) ;
}
return ret ;
}
virtual pos_type seekpos(pos_type, std::ios_base::openmode )
{
return -1 ;
}
virtual std::streamsize showmanyc()
{
return 0 ;
}
/*
virtual pos_type seekpos(pos_type, ios_base::openmode)
{
return -1 ;
}
*/
} ;
#endif /* ALIASED_BUF_H_ */