Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/modules/uClibcxx/fstream.cpp

1/*Copyright (C) 2004 Garrett A. Kajmowicz
2
3This file is part of the uClibc++ Library.
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#define __UCLIBCXX_COMPILE_FSTREAM__ 1
21
22#include <fstream>
23
24namespace std{
25
26#ifdef __UCLIBCXX_EXPAND_FSTREAM_CHAR__
27
28#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
29
30template _UCXXEXPORT filebuf::basic_filebuf();
31template _UCXXEXPORT filebuf::~basic_filebuf();
32
33#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
34
35template _UCXXEXPORT filebuf::int_type filebuf::pbackfail(filebuf::int_type c);
36template _UCXXEXPORT filebuf * filebuf::open(const char* s, ios_base::openmode mode);
37template _UCXXEXPORT filebuf * filebuf::close();
38template _UCXXEXPORT filebuf::int_type filebuf::overflow(filebuf::int_type);
39template _UCXXEXPORT filebuf::int_type filebuf::underflow ();
40template _UCXXEXPORT streamsize filebuf::xsputn(const char* s, streamsize n);
41
42template _UCXXEXPORT basic_streambuf<char, char_traits<char> >*
43filebuf::setbuf(char * s, streamsize n);
44
45
46#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
47
48template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream();
49template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream(const char* s, ios_base::openmode mode);
50template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::~basic_ofstream();
51
52template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream();
53template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream(const char* s, ios_base::openmode mode);
54template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::~basic_ifstream();
55
56#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
57
58
59#endif
60
61
62
63#ifdef __UCLIBCXX_HAS_WCHAR__
64
65template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
66basic_filebuf<wchar_t, char_traits<wchar_t> >::overflow(int_type c)
67{
68typedef basic_streambuf<wchar_t, char_traits<wchar_t> > wstreambuf;
69typedef char_traits<wchar_t> wtraits;
70
71if(is_open() == false){
72//Can't do much
73return wtraits::eof();
74}
75
76mbstate_t ps = { 0 };
77char out_array[8];
78size_t out_size;
79
80
81if( wstreambuf::pbase() != 0 ){
82
83//Write all possible character from the existing array first
84size_t chars_written = 0;
85while(wstreambuf::pbase() && (wstreambuf::pbase() + chars_written !=wstreambuf::pptr()) ){
86out_size = wcrtomb(out_array, wstreambuf::pbase()[chars_written], &ps);
87if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){
88break;
89}
90++chars_written;
91}
92
93if( wstreambuf::pbase() + chars_written == wstreambuf::pptr() ){
94wstreambuf::pbump(-chars_written);
95}else{
96//Shuffle data back into order
97size_t chars_left = wstreambuf::pptr() - wstreambuf::pbase() - chars_written;
98for(size_t i = 0; i < chars_left; ++i){
99wstreambuf::pbase()[i] = (wstreambuf::pptr() - chars_written)[i];
100}
101return wtraits::eof();
102}
103}
104
105if( !wtraits::eq_int_type(c, wtraits::eof()) ){
106out_size = wcrtomb(out_array, c, &ps);
107if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){
108return wtraits::eof();
109}
110return c;
111}
112
113return wtraits::not_eof(c);
114}
115
116
117template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
118 basic_filebuf<wchar_t, char_traits<wchar_t> >::underflow()
119{
120/*Some variables used internally:
121Buffer pointers:
122
123charT * mgbeg;
124charT * mgnext;
125charT * mgend;
126
127eback() returns mgbeg
128gptr() returns mgnext
129egptr() returns mgend
130
131gbump(int n) mgnext+=n
132*/
133
134typedef char_traits<wchar_t> traits;
135typedef basic_streambuf<wchar_t, traits> wstreambuf;
136
137
138if(wstreambuf::eback() == wstreambuf::gptr() && 0 != wstreambuf::eback()){//Buffer is full
139return traits::to_int_type(*wstreambuf::gptr());
140}
141
142 size_t in_size;
143
144wchar_t c = 0;
145wint_t wi = 0;
146in_size = 0;
147
148wi = fgetwc(fp);
149if(WEOF == wi){
150fprintf(stderr, "WEOF returned by fgetwc\n");
151return traits::eof();
152}
153
154c = traits::to_char_type(wi);
155
156if(wstreambuf::eback() == 0){
157return traits::to_int_type(c);
158}
159
160for(wchar_t * i = wstreambuf::gptr(); i < wstreambuf::egptr(); ++i){
161*(i-1) = *i;
162}
163
164*(wstreambuf::egptr()-1) = c;
165
166wstreambuf::mgnext -= 1;
167
168return traits::to_int_type(*wstreambuf::gptr());
169}
170
171#endif // __UCLIBCXX_HAS_WCHAR__
172
173
174}
175

Archive Download this file

Revision: 2225