/elec/propeller-clock

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