#ifndef __pxx_text_stream_ih__ #define __pxx_text_stream_ih__ #include "pxx_text_stream.hh" #include "pxx_stream.ih" #include "pxx_sys_error.ih" #include "pxx_exception.hh" #include #include #include namespace pxx { inline TextStream::TextStream ( io_mode_t _mode ) : Stream (_mode) {} inline TextStream& TextStream::operator << (char _c) { put(_c); return self; } inline TextStream& TextStream::operator >> (char& _c) { get(_c); return self; } inline TextStream& TextStream::operator << (wchar_t _wc) { int n = wctomb(buf, _wc); if (n != -1) { buf[n] = 0; write(buf, n); } else { throw InvalidCharConv(); } return self; } inline TextStream& TextStream::operator >> (wchar_t& _wc) { int n = 0; mbstate_t ps; memset(&ps, '\0', sizeof(ps)); while (true) { get(buf[n++]); int res = mbrtowc(&_wc, buf, n, &ps); if (res == -2) continue; else if (res == -1) throw InvalidCharConv(); else break; } return self; } inline TextStream& TextStream::operator << (int8_t _n) { int res = sprintf(buf, "%" PRId8, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (int8_t& _n) { long l = read_long(); if (l > INT8_MAX || l < INT8_MIN) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (uint8_t _n) { int res = sprintf(buf, "%" PRIu8, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (uint8_t& _n) { unsigned long l = read_ulong(); if (l > UINT8_MAX) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (uint16_t _n) { int res = sprintf(buf, "%" PRIu16, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (uint16_t& _n) { unsigned long l = read_ulong(); if (l > UINT16_MAX) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (int16_t _n) { int res = sprintf(buf, "%" PRId16, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (int16_t& _n) { long l = read_long(); if (l > INT16_MAX || l < INT16_MIN) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (uint32_t _n) { int res = sprintf(buf, "%" PRIu32, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (uint32_t& _n) { unsigned long l = read_ulong(); if (l > UINT32_MAX) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (int32_t _n) { int res = sprintf(buf, "%" PRId32, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (int32_t& _n) { long l = read_long(); if (l > INT32_MAX || l < INT32_MIN) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (uint64_t _n) { int res = sprintf(buf, "%" PRIu64, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (uint64_t& _n) { unsigned long long l = read_ullong(); if (l > UINT64_MAX) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::operator << (int64_t _n) { int res = sprintf(buf, "%" PRId64, _n); write(buf, res); return self; } inline TextStream& TextStream::operator >> (int64_t& _n) { long long l = read_llong(); if (l > INT64_MAX || l < INT64_MIN) throw_sys_error(ERANGE); else _n = l; return self; } inline TextStream& TextStream::endl () { return self << L'\n'; } } #endif // __pxx_text_stream_ih__