/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*	Copyright (C) 2004 Garrett A. Kajmowicz

	This file is part of the uClibc++ Library.

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#if ! defined(__AVR__)
 
#define __UCLIBCXX_COMPILE_FSTREAM__ 1

#include <fstream>

namespace std{

#ifdef __UCLIBCXX_EXPAND_FSTREAM_CHAR__

#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

	template _UCXXEXPORT filebuf::basic_filebuf();
	template _UCXXEXPORT filebuf::~basic_filebuf();

#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

	template _UCXXEXPORT filebuf::int_type filebuf::pbackfail(filebuf::int_type c);
	template _UCXXEXPORT filebuf * filebuf::open(const char* s, ios_base::openmode mode);
	template _UCXXEXPORT filebuf * filebuf::close();
	template _UCXXEXPORT filebuf::int_type filebuf::overflow(filebuf::int_type);
	template _UCXXEXPORT filebuf::int_type filebuf::underflow ();
	template _UCXXEXPORT streamsize filebuf::xsputn(const char* s, streamsize n);

	template _UCXXEXPORT basic_streambuf<char, char_traits<char> >*
		filebuf::setbuf(char * s, streamsize n);


#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

	template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream();
	template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream(const char* s, ios_base::openmode mode);
	template _UCXXEXPORT basic_ofstream<char, char_traits<char> >::~basic_ofstream();

	template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream();
	template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream(const char* s, ios_base::openmode mode);
	template _UCXXEXPORT basic_ifstream<char, char_traits<char> >::~basic_ifstream();

#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__


#endif



#ifdef __UCLIBCXX_HAS_WCHAR__

template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
	basic_filebuf<wchar_t, char_traits<wchar_t> >::overflow(int_type c)
{
	typedef basic_streambuf<wchar_t, char_traits<wchar_t> > wstreambuf;
	typedef char_traits<wchar_t> wtraits;

	if(is_open() == false){
		//Can't do much
		return wtraits::eof();
	}

	mbstate_t ps = { 0 };
	char out_array[8];
	size_t out_size;


	if( wstreambuf::pbase() != 0 ){

		//Write all possible character from the existing array first
		size_t chars_written = 0;
		while(wstreambuf::pbase() && (wstreambuf::pbase() + chars_written !=wstreambuf::pptr()) ){
			out_size = wcrtomb(out_array, wstreambuf::pbase()[chars_written], &ps);
			if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){
				break;
			}
			++chars_written;
		}

		if( wstreambuf::pbase() + chars_written == wstreambuf::pptr() ){
			wstreambuf::pbump(-chars_written);
		}else{
			//Shuffle data back into order
			size_t chars_left = wstreambuf::pptr() - wstreambuf::pbase() - chars_written;
			for(size_t i = 0; i < chars_left; ++i){
				wstreambuf::pbase()[i] = (wstreambuf::pptr() - chars_written)[i];
			}
			return wtraits::eof();
		}
	}

	if( !wtraits::eq_int_type(c, wtraits::eof()) ){
		out_size = wcrtomb(out_array, c, &ps);
		if(out_size == (size_t)(-1) || fwrite(out_array, out_size, 1, fp) == 0){
			return wtraits::eof();
		}
		return c;
	}

	return wtraits::not_eof(c);
}


template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
        basic_filebuf<wchar_t, char_traits<wchar_t> >::underflow()
{
	/*Some variables used internally:
	Buffer pointers:

	charT * mgbeg;
	charT * mgnext;
	charT * mgend;

	eback() returns mgbeg
	gptr()  returns mgnext
	egptr() returns mgend

	gbump(int n) mgnext+=n
	*/

	typedef char_traits<wchar_t> traits;
	typedef basic_streambuf<wchar_t, traits> wstreambuf;


	if(wstreambuf::eback() == wstreambuf::gptr() && 0 != wstreambuf::eback()){	//Buffer is full
		return traits::to_int_type(*wstreambuf::gptr());
	}

        size_t in_size;

	wchar_t c = 0;
	wint_t wi = 0;
	in_size = 0;

	wi = fgetwc(fp);
	if(WEOF == wi){
		fprintf(stderr, "WEOF returned by fgetwc\n");
		return traits::eof();
	}

	c = traits::to_char_type(wi);

	if(wstreambuf::eback() == 0){
		return traits::to_int_type(c);
	}

	for(wchar_t * i = wstreambuf::gptr(); i < wstreambuf::egptr(); ++i){
		*(i-1) = *i;
	}

	*(wstreambuf::egptr()-1) = c;
	
	wstreambuf::mgnext -= 1;

	return traits::to_int_type(*wstreambuf::gptr());
}

#endif // __UCLIBCXX_HAS_WCHAR__


}

#endif // not ARDUINO