/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/streambuf

  • Committer: edam
  • Date: 2012-02-25 01:31:43 UTC
  • Revision ID: tim@ed.am-20120225013143-9fet2y2d3fjlrwez
added ulibc

Show diffs side-by-side

added added

removed removed

 
1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
2
 
 
3
        This file is part of the uClibc++ Library.
 
4
 
 
5
        This library is free software; you can redistribute it and/or
 
6
        modify it under the terms of the GNU Lesser General Public
 
7
        License as published by the Free Software Foundation; either
 
8
        version 2.1 of the License, or (at your option) any later version.
 
9
 
 
10
        This library 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 GNU
 
13
        Lesser General Public License for more details.
 
14
 
 
15
        You should have received a copy of the GNU Lesser General Public
 
16
        License along with this library; if not, write to the Free Software
 
17
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
*/
 
19
 
 
20
#include <basic_definitions>
 
21
#include <locale>
 
22
#include <string>
 
23
#include <iosfwd>
 
24
 
 
25
#ifndef HEADER_STD_STREAMBUF
 
26
#define HEADER_STD_STREAMBUF 1
 
27
 
 
28
#include <ios>
 
29
 
 
30
#pragma GCC visibility push(default)
 
31
 
 
32
namespace std{
 
33
 
 
34
        template <class charT, class traits> class _UCXXEXPORT basic_streambuf{
 
35
        public:
 
36
#ifdef __UCLIBCXX_SUPPORT_CDIR__
 
37
                friend ios_base::Init::Init();
 
38
#endif
 
39
        // Types:
 
40
                typedef charT                           char_type;
 
41
                typedef typename traits::int_type       int_type;
 
42
                typedef typename traits::pos_type       pos_type;
 
43
                typedef typename traits::off_type       off_type;
 
44
                typedef traits                          traits_type;
 
45
 
 
46
                virtual ~basic_streambuf();
 
47
 
 
48
                locale pubimbue(const locale &loc);
 
49
 
 
50
                locale getloc() const{
 
51
                        return myLocale;
 
52
                }
 
53
 
 
54
                basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n){
 
55
                        return setbuf(s,n);
 
56
                }
 
57
                pos_type pubseekoff(off_type off, 
 
58
                        typename ios_base::seekdir way, 
 
59
                        ios_base::openmode which = ios_base::in |
 
60
                        ios_base::out
 
61
                        )
 
62
                {
 
63
                        return seekoff(off,way,which);
 
64
                }
 
65
                pos_type pubseekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out){
 
66
                        return seekpos(sp,which);
 
67
                }
 
68
                int pubsync(){
 
69
                        return sync();
 
70
                }
 
71
 
 
72
                streamsize in_avail();
 
73
 
 
74
                int_type snextc();
 
75
 
 
76
                int_type sbumpc();
 
77
 
 
78
                int_type sgetc();
 
79
 
 
80
                streamsize sgetn(char_type* s, streamsize n){
 
81
                        return xsgetn(s,n);
 
82
                }
 
83
 
 
84
                int_type sputbackc(char_type c);
 
85
 
 
86
                int_type sungetc();
 
87
 
 
88
                int_type sputc(char_type c);
 
89
 
 
90
                streamsize sputn(const char_type* s, streamsize n){
 
91
                        if(openedFor & ios_base::app){
 
92
                                seekoff(0, ios_base::end, ios_base::out);
 
93
                        }
 
94
                        return xsputn(s, n);
 
95
                }
 
96
 
 
97
        protected:
 
98
                locale myLocale;
 
99
                //Pointers for the "get" buffers
 
100
                charT * mgbeg;
 
101
                charT * mgnext;
 
102
                charT * mgend;
 
103
 
 
104
                //Pointers for the "put" buffers        
 
105
                charT * mpbeg;
 
106
                charT * mpnext;
 
107
                charT * mpend;
 
108
 
 
109
                //In the event of null buffers Lets us know what the buffer is opened for
 
110
                ios_base::openmode openedFor;
 
111
                
 
112
                basic_streambuf();
 
113
 
 
114
                basic_streambuf(const basic_streambuf<char, char_traits<char> > &)
 
115
                        : myLocale(),
 
116
                        mgbeg(0), mgnext(0), mgend(0), mpbeg(0), mpnext(0), mpend(0),
 
117
                        openedFor(0)
 
118
                { }
 
119
                basic_streambuf<char, char_traits<char> > & operator=(const basic_streambuf<char, char_traits<char> > &){
 
120
                        return *this;
 
121
                }
 
122
                
 
123
                char_type* eback() const{
 
124
                        return mgbeg;
 
125
                }
 
126
                char_type* gptr()  const{
 
127
                        return mgnext;
 
128
                }
 
129
                char_type* egptr() const{
 
130
                        return mgend;
 
131
                }
 
132
                void gbump(int n){
 
133
                        mgnext+=n;
 
134
                }
 
135
                void setg(char_type* gbeg, char_type* gnext, char_type* gend){
 
136
                        mgbeg = gbeg;
 
137
                        mgnext = gnext;
 
138
                        mgend = gend;
 
139
                }
 
140
 
 
141
                char_type* pbase() const{
 
142
                        return mpbeg;
 
143
                }
 
144
                char_type* pptr() const{
 
145
                        return mpnext;
 
146
                }
 
147
                char_type* epptr() const{
 
148
                        return mpend;
 
149
                }
 
150
                void pbump(int n){
 
151
                        mpnext+=n;
 
152
                }
 
153
                void setp(char_type* pbeg, char_type* pend){
 
154
                        mpbeg = pbeg;
 
155
                        mpnext  = pbeg;
 
156
                        mpend = pend;
 
157
                }
 
158
 
 
159
                virtual void imbue(const locale &loc){
 
160
                        myLocale = loc;
 
161
                }
 
162
 
 
163
                //Virtual functions which we will not implement
 
164
 
 
165
                virtual basic_streambuf<char_type,traits>* setbuf(char_type* , streamsize){
 
166
                        return 0;
 
167
                }
 
168
                virtual pos_type seekoff(off_type , ios_base::seekdir, 
 
169
                        ios_base::openmode = ios_base::in | ios_base::out)
 
170
                {
 
171
                        return 0;
 
172
                }
 
173
                virtual pos_type seekpos(pos_type , ios_base::openmode = ios_base::in | ios_base::out){
 
174
                        return 0;
 
175
                }
 
176
                virtual int sync(){
 
177
                        return 0;
 
178
                }
 
179
 
 
180
                virtual int showmanyc(){
 
181
                        return 0;
 
182
                }
 
183
                virtual streamsize xsgetn(char_type* , streamsize ){
 
184
                        return 0;
 
185
                }
 
186
                virtual int_type underflow(){
 
187
                        return traits_type::eof();
 
188
                }
 
189
                virtual int_type uflow(){
 
190
                        int_type ret = underflow();
 
191
                        if (!traits_type::eq_int_type(ret, traits_type::eof()))
 
192
                                gbump(1);
 
193
                        return ret;
 
194
                }
 
195
 
 
196
                virtual int_type pbackfail(int_type c = traits::eof()){
 
197
                        return c;
 
198
                }
 
199
                virtual streamsize xsputn(const char_type* c, streamsize n){
 
200
                        //This function is designed to be replaced by subclasses
 
201
                        for(streamsize i = 0; i< n; ++i){
 
202
                                if(sputc(c[i]) == traits::eof()){
 
203
                                        return i;
 
204
                                }
 
205
                        }
 
206
                        return n;
 
207
                }
 
208
                virtual int_type overflow (int_type c = traits::eof()){
 
209
                        return c;
 
210
                }
 
211
        };
 
212
 
 
213
        typedef basic_streambuf<char>     streambuf;
 
214
#ifdef __UCLIBCXX_HAS_WCHAR__
 
215
        typedef basic_streambuf<wchar_t> wstreambuf;
 
216
#endif
 
217
 
 
218
 
 
219
//Definitions put below to allow for easy expansion of code
 
220
 
 
221
        template <class C, class T> basic_streambuf<C, T>::~basic_streambuf(){  }
 
222
 
 
223
        template <class C, class T> locale basic_streambuf<C, T>::pubimbue(const locale &loc){
 
224
                locale temp = myLocale;
 
225
                myLocale = loc;
 
226
                return temp;
 
227
        }
 
228
 
 
229
        template <class C, class T> streamsize basic_streambuf<C, T>::in_avail(){
 
230
                if(mgend !=0 && mgnext !=0){
 
231
                        return mgend - mgnext;
 
232
                }
 
233
                return showmanyc();
 
234
        }
 
235
 
 
236
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::sbumpc(){
 
237
                if(mgbeg == 0 || mgnext == mgend){
 
238
                        return uflow();
 
239
                }
 
240
                int_type retval = T::to_int_type(*gptr());
 
241
                gbump(1);
 
242
                return retval;
 
243
        }
 
244
 
 
245
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::snextc(){
 
246
                if(sbumpc() == T::eof() ){
 
247
                        return T::eof() ;
 
248
                }
 
249
                return sgetc();
 
250
        }
 
251
 
 
252
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::sgetc(){
 
253
                if(mgbeg == 0 || mgnext == mgend){
 
254
                        return underflow();
 
255
                }
 
256
                return T::to_int_type(*gptr());
 
257
        }
 
258
 
 
259
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::sputbackc(char_type c){
 
260
                if(mgbeg == 0 || mgnext == mgbeg || !T::eq(c, gptr()[-1] )){
 
261
                        return pbackfail(T::to_int_type(c));
 
262
                }
 
263
                gbump(-1);
 
264
                return T::to_int_type(*gptr());
 
265
        }
 
266
 
 
267
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::sungetc(){
 
268
                if(mgbeg == 0 || mgnext == mgbeg){
 
269
                        return ios_base::failbit;
 
270
                }
 
271
                gbump(-1);
 
272
                return T::to_int_type(*gptr());
 
273
        }
 
274
 
 
275
        template <class C, class T> typename basic_streambuf<C, T>::int_type basic_streambuf<C, T>::sputc(char_type c){
 
276
                if(openedFor & ios_base::app){
 
277
                        seekoff(0, ios_base::end, ios_base::out);
 
278
                }
 
279
                if(mpnext < mpend){
 
280
                        *mpnext = c;
 
281
                        ++mpnext;
 
282
                }else{
 
283
                        return overflow( T::to_int_type(c) );
 
284
                }
 
285
                return T::to_int_type(c);
 
286
        }
 
287
 
 
288
        template <class C, class T> basic_streambuf<C, T>::basic_streambuf() 
 
289
                : myLocale(),
 
290
                mgbeg(0), mgnext(0), mgend(0), mpbeg(0), mpnext(0), mpend(0),
 
291
                openedFor(0)
 
292
        { }
 
293
 
 
294
 
 
295
 
 
296
 
 
297
 
 
298
 
 
299
#ifdef __UCLIBCXX_EXPAND_STREAMBUF_CHAR__
 
300
#ifndef __UCLIBCXX_COMPILE_STREAMBUF__
 
301
 
 
302
#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
 
303
 
 
304
        template <> _UCXXEXPORT streambuf::basic_streambuf();
 
305
        template <> _UCXXEXPORT streambuf::~basic_streambuf();
 
306
 
 
307
#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
 
308
 
 
309
        template <> _UCXXEXPORT locale streambuf::pubimbue(const locale &loc);
 
310
        template <> _UCXXEXPORT streamsize streambuf::in_avail();
 
311
        template <> _UCXXEXPORT streambuf::int_type streambuf::sbumpc();
 
312
        template <> _UCXXEXPORT streambuf::int_type streambuf::snextc();
 
313
        template <> _UCXXEXPORT streambuf::int_type streambuf::sgetc();
 
314
        template <> _UCXXEXPORT streambuf::int_type streambuf::sputbackc(char_type c);
 
315
        template <> _UCXXEXPORT streambuf::int_type streambuf::sungetc();
 
316
        template <> _UCXXEXPORT streambuf::int_type streambuf::sputc(char_type c);
 
317
 
 
318
#endif
 
319
#endif
 
320
 
 
321
 
 
322
 
 
323
 
 
324
 
 
325
}
 
326
 
 
327
#pragma GCC visibility pop
 
328
 
 
329
#endif