Root/
Source at commit 1274 created 12 years 11 months ago. By meklort, Module changes, makefile changes. | |
---|---|
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 | #define __UCLIBCXX_COMPILE_FSTREAM__ 1␊ |
21 | ␊ |
22 | #include <fstream>␊ |
23 | ␊ |
24 | namespace std{␊ |
25 | ␊ |
26 | #ifdef __UCLIBCXX_EXPAND_FSTREAM_CHAR__␊ |
27 | ␊ |
28 | #ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__␊ |
29 | ␊ |
30 | ␉template _UCXXEXPORT filebuf::basic_filebuf();␊ |
31 | ␉template _UCXXEXPORT filebuf::~basic_filebuf();␊ |
32 | ␊ |
33 | #endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__␊ |
34 | ␊ |
35 | ␉template _UCXXEXPORT filebuf::int_type filebuf::pbackfail(filebuf::int_type c);␊ |
36 | ␉template _UCXXEXPORT filebuf * filebuf::open(const char* s, ios_base::openmode mode);␊ |
37 | ␉template _UCXXEXPORT filebuf * filebuf::close();␊ |
38 | ␉template _UCXXEXPORT filebuf::int_type filebuf::overflow(filebuf::int_type);␊ |
39 | ␉template _UCXXEXPORT filebuf::int_type filebuf::underflow ();␊ |
40 | ␉template _UCXXEXPORT streamsize filebuf::xsputn(const char* s, streamsize n);␊ |
41 | ␊ |
42 | ␉template _UCXXEXPORT basic_streambuf<char, char_traits<char> >*␊ |
43 | ␉␉filebuf::setbuf(char * s, streamsize n);␊ |
44 | ␊ |
45 | ␊ |
46 | #ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__␊ |
47 | ␊ |
48 | ␉template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream();␊ |
49 | ␉template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream(const char* s, ios_base::openmode mode);␊ |
50 | ␉template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::~basic_ofstream();␊ |
51 | ␊ |
52 | ␉template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream();␊ |
53 | ␉template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream(const char* s, ios_base::openmode mode);␊ |
54 | ␉template _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 | ␊ |
65 | template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type␊ |
66 | ␉basic_filebuf<wchar_t, char_traits<wchar_t> >::overflow(int_type c)␊ |
67 | {␊ |
68 | ␉typedef basic_streambuf<wchar_t, char_traits<wchar_t> > wstreambuf;␊ |
69 | ␉typedef char_traits<wchar_t> wtraits;␊ |
70 | ␊ |
71 | ␉if(is_open() == false){␊ |
72 | ␉␉//Can't do much␊ |
73 | ␉␉return wtraits::eof();␊ |
74 | ␉}␊ |
75 | ␊ |
76 | ␉mbstate_t ps = { 0 };␊ |
77 | ␉char out_array[8];␊ |
78 | ␉size_t out_size;␊ |
79 | ␊ |
80 | ␊ |
81 | ␉if( wstreambuf::pbase() != 0 ){␊ |
82 | ␊ |
83 | ␉␉//Write all possible character from the existing array first␊ |
84 | ␉␉size_t chars_written = 0;␊ |
85 | ␉␉while(wstreambuf::pbase() && (wstreambuf::pbase() + chars_written !=wstreambuf::pptr()) ){␊ |
86 | ␉␉␉out_size = wcrtomb(out_array, wstreambuf::pbase()[chars_written], &ps);␊ |
87 | ␉␉␉if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){␊ |
88 | ␉␉␉␉break;␊ |
89 | ␉␉␉}␊ |
90 | ␉␉␉++chars_written;␊ |
91 | ␉␉}␊ |
92 | ␊ |
93 | ␉␉if( wstreambuf::pbase() + chars_written == wstreambuf::pptr() ){␊ |
94 | ␉␉␉wstreambuf::pbump(-chars_written);␊ |
95 | ␉␉}else{␊ |
96 | ␉␉␉//Shuffle data back into order␊ |
97 | ␉␉␉size_t chars_left = wstreambuf::pptr() - wstreambuf::pbase() - chars_written;␊ |
98 | ␉␉␉for(size_t i = 0; i < chars_left; ++i){␊ |
99 | ␉␉␉␉wstreambuf::pbase()[i] = (wstreambuf::pptr() - chars_written)[i];␊ |
100 | ␉␉␉}␊ |
101 | ␉␉␉return wtraits::eof();␊ |
102 | ␉␉}␊ |
103 | ␉}␊ |
104 | ␊ |
105 | ␉if( !wtraits::eq_int_type(c, wtraits::eof()) ){␊ |
106 | ␉␉out_size = wcrtomb(out_array, c, &ps);␊ |
107 | ␉␉if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){␊ |
108 | ␉␉␉return wtraits::eof();␊ |
109 | ␉␉}␊ |
110 | ␉␉return c;␊ |
111 | ␉}␊ |
112 | ␊ |
113 | ␉return wtraits::not_eof(c);␊ |
114 | }␊ |
115 | ␊ |
116 | ␊ |
117 | template <> _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:␊ |
121 | ␉Buffer pointers:␊ |
122 | ␊ |
123 | ␉charT * mgbeg;␊ |
124 | ␉charT * mgnext;␊ |
125 | ␉charT * mgend;␊ |
126 | ␊ |
127 | ␉eback() returns mgbeg␊ |
128 | ␉gptr() returns mgnext␊ |
129 | ␉egptr() returns mgend␊ |
130 | ␊ |
131 | ␉gbump(int n) mgnext+=n␊ |
132 | ␉*/␊ |
133 | ␊ |
134 | ␉typedef char_traits<wchar_t> traits;␊ |
135 | ␉typedef basic_streambuf<wchar_t, traits> wstreambuf;␊ |
136 | ␊ |
137 | ␊ |
138 | ␉if(wstreambuf::eback() == wstreambuf::gptr() && 0 != wstreambuf::eback()){␉//Buffer is full␊ |
139 | ␉␉return traits::to_int_type(*wstreambuf::gptr());␊ |
140 | ␉}␊ |
141 | ␊ |
142 | size_t in_size;␊ |
143 | ␊ |
144 | ␉wchar_t c = 0;␊ |
145 | ␉wint_t wi = 0;␊ |
146 | ␉in_size = 0;␊ |
147 | ␊ |
148 | ␉wi = fgetwc(fp);␊ |
149 | ␉if(WEOF == wi){␊ |
150 | ␉␉fprintf(stderr, "WEOF returned by fgetwc\n");␊ |
151 | ␉␉return traits::eof();␊ |
152 | ␉}␊ |
153 | ␊ |
154 | ␉c = traits::to_char_type(wi);␊ |
155 | ␊ |
156 | ␉if(wstreambuf::eback() == 0){␊ |
157 | ␉␉return traits::to_int_type(c);␊ |
158 | ␉}␊ |
159 | ␊ |
160 | ␉for(wchar_t * i = wstreambuf::gptr(); i < wstreambuf::egptr(); ++i){␊ |
161 | ␉␉*(i-1) = *i;␊ |
162 | ␉}␊ |
163 | ␊ |
164 | ␉*(wstreambuf::egptr()-1) = c;␊ |
165 | ␉␊ |
166 | ␉wstreambuf::mgnext -= 1;␊ |
167 | ␊ |
168 | ␉return traits::to_int_type(*wstreambuf::gptr());␊ |
169 | }␊ |
170 | ␊ |
171 | #endif // __UCLIBCXX_HAS_WCHAR__␊ |
172 | ␊ |
173 | ␊ |
174 | }␊ |
175 |