From: Christian P. <cp...@us...> - 2005-07-06 13:01:45
|
Update of /cvsroot/pclasses/pclasses2/src/IO In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27612/src/IO Modified Files: TextStream.cpp Log Message: - More work on TextStream Index: TextStream.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/IO/TextStream.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- TextStream.cpp 4 Jul 2005 23:36:14 -0000 1.1 +++ TextStream.cpp 6 Jul 2005 13:01:05 -0000 1.2 @@ -18,6 +18,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "pclasses/ScopedArrayPtr.h" #include "pclasses/IO/TextStream.h" #include "pclasses/IO/IODeviceStreamBuffer.h" @@ -28,133 +29,334 @@ TextStream::TextStream(IO::IODevice& dev, size_t outBufferSize, size_t inBufferSize) : StreamBase(dev, outBufferSize, inBufferSize), - _fmt(Dec), _width(0), _prec(0), _fillCh(' '), - _converter(0) + _numFormat(Unicode::NumberFormat::Decimal), + _width(0), _padPos(Before), _padChar(' '), + _locale(0), _codePage(0), + _converter(0), _numFormater(0) { } TextStream::TextStream(IO::StreamBuffer& buffer) throw() : StreamBase(buffer), - _fmt(Dec), _width(0), _prec(0), _fillCh(' '), - _converter(0) + _numFormat(Unicode::NumberFormat::Decimal), + _width(0), _padPos(Before), _padChar(' '), + _locale(0), _codePage(0), + _converter(0), _numFormater(0) { } TextStream::~TextStream() throw() { + if(_locale) + delete[] _locale; + + if(_codePage) + delete[] _codePage; + + if(_converter) + delete _converter; + + if(_numFormater) + delete _numFormater; } -void TextStream::setFormat(Format fmt) throw() +Unicode::NumberFormat::PadPosition + PadPosition2NumberFormatPadPosition(TextStream::PadPosition pos) { - _fmt = fmt; + Unicode::NumberFormat::PadPosition numFmtPadPos; + + switch(pos) + { + case TextStream::BeforePrefix: + numFmtPadPos = Unicode::NumberFormat::BeforePrefix; + break; + + case TextStream::AfterPrefix: + numFmtPadPos = Unicode::NumberFormat::AfterPrefix; + break; + + case TextStream::BeforeSuffix: + numFmtPadPos = Unicode::NumberFormat::BeforeSuffix; + break; + + case TextStream::AfterSuffix: + default: + numFmtPadPos = Unicode::NumberFormat::AfterSuffix; + break; + } + + return numFmtPadPos; +} + + +Unicode::NumberFormat* TextStream::createNumFormater( + NumberFormat fmt, const char* locale) throw(Unicode::UnicodeError) +{ + Unicode::NumberFormat* ret = new Unicode::NumberFormat(fmt, locale); + ret->setWidth(_width); + ret->setPadChar(_padChar); + ret->setPadPosition(PadPosition2NumberFormatPadPosition(_padPos)); + return ret; +} + +void TextStream::setCodePage(const char* cp) throw(Unicode::UnicodeError) +{ + // copy the given codepage ... + ScopedArrayPtr<char> newCodePage; + if(cp) + { + newCodePage = new char[std::strlen(cp)+1]; + strcpy(newCodePage.get(), cp); + } + + // create the new converter ... + if(_converter) + { + Unicode::Converter* newConverter = new Unicode::Converter(cp); + delete _converter; + _converter = newConverter; + } + + // delete the old codepage ... + if(_codePage) + delete[] _codePage; + + _codePage = newCodePage.release(); +} + +const char* TextStream::codePage() const throw() +{ + return _codePage; +} + +void TextStream::setLocale(const char* loc) throw(Unicode::UnicodeError) +{ + // copy the given locale ... + ScopedArrayPtr<char> newLocale; + if(loc) + { + newLocale = new char[std::strlen(loc)+1]; + strcpy(newLocale.get(), loc); + } + + // create the new number formater ... + if(_numFormater) + { + Unicode::NumberFormat* newNumFormater = createNumFormater(_numFormat, loc); + delete _numFormater; + _numFormater = newNumFormater; + } + + // delete the old locale ... + if(_locale) + delete[] _locale; + + _locale = newLocale.release(); +} + +const char* TextStream::locale() const throw() +{ + return _locale; +} + +void TextStream::setNumberFormat(NumberFormat fmt) throw(Unicode::UnicodeError) +{ + if(_numFormat != fmt) + { + if(_numFormater) + { + Unicode::NumberFormat* newNumFormater = createNumFormater(fmt, _locale); + delete _numFormater; + _numFormater = newNumFormater; + } + + _numFormat = fmt; + } +} + +TextStream::NumberFormat TextStream::numberFormat() const throw() +{ + return _numFormat; } void TextStream::setWidth(unsigned int width) throw() { + if(_numFormater) + _numFormater->setWidth(width); + _width = width; } -void TextStream::setPrecision(unsigned int prec) throw() +unsigned int TextStream::width() const throw() { - _prec = prec; + return _width; } -void TextStream::setFill(Unicode::Char ch) throw() +void TextStream::setPadPosition(PadPosition pos) throw() { - _fillCh = ch; + if(_numFormater) + _numFormater->setPadPosition(PadPosition2NumberFormatPadPosition(pos)); + + _padPos = pos; } -void TextStream::setCodepage(const char* cp) +TextStream::PadPosition TextStream::padPosition() const throw() { - if(_converter) - delete _converter; + return _padPos; +} - _converter = new Unicode::Converter(cp); +void TextStream::setPadChar(Unicode::Char ch) throw() +{ + if(_numFormater) + _numFormater->setPadChar(ch); + + _padChar = ch; +} + +Unicode::Char TextStream::padChar() const throw() +{ + return _padChar; } TextStream& TextStream::operator<<(Int8 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(Int32(val))); } TextStream& TextStream::operator<<(UInt8 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(Int32(val))); } TextStream& TextStream::operator<<(Int16 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(Int32(val))); } TextStream& TextStream::operator<<(UInt16 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(Int32(val))); } TextStream& TextStream::operator<<(Int32 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(val)); } TextStream& TextStream::operator<<(UInt32 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(Int64(val))); } #ifdef PCLASSES_HAVE_64BIT_INT TextStream& TextStream::operator<<(Int64 val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(val)); } TextStream& TextStream::operator<<(UInt64 val) { + //@fixme return *this; } #endif TextStream& TextStream::operator<<(float val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format((double)val)); } TextStream& TextStream::operator<<(double val) { - return *this; + if(!_numFormater) + _numFormater = createNumFormater(_numFormat, _locale); + + return write(_numFormater->format(val)); } #ifdef PCLASSES_HAVE_LONG_DOUBLE TextStream& TextStream::operator<<(long double val) { + //@fixme return *this; } #endif TextStream& TextStream::operator<<(const Unicode::Char& ch) { + if(!_converter) + _converter = new Unicode::Converter(_codePage); + + std::string cpStr = _converter->fromUnicode(ch); + writeRaw(cpStr.c_str(), cpStr.size()); return *this; } +void writePadString(TextStream& strm, Unicode::Char padChar, size_t count) +{ + Unicode::String fillStr(count); + fillStr.insert(0, count, padChar); + strm.write(fillStr); +} + TextStream& TextStream::operator<<(const Unicode::String& str) { + bool smaller = (str.size() < _width); + + if(smaller && (_padPos == BeforePrefix || _padPos == AfterPrefix)) + writePadString(*this, _padChar, _width - str.size()); + + write(str); + + if(smaller && (_padPos == AfterSuffix || _padPos == BeforeSuffix)) + writePadString(*this, _padChar, _width - str.size()); + + return *this; +} + +TextStream& TextStream::write(const Unicode::String& str) +{ if(!_converter) - _converter = new Unicode::Converter(0); + _converter = new Unicode::Converter(_codePage); std::string cpStr = _converter->fromUnicode(str); - (*this) << cpStr; + writeRaw(cpStr.c_str(), cpStr.size()); return *this; } TextStream& TextStream::operator<<(const char* str) { - writeRaw(str, std::strlen(str)); + (*this) << Unicode::String(str); return *this; } TextStream& TextStream::operator<<(const std::string& str) { - writeRaw(str.c_str(), str.size()); + (*this) << Unicode::String(str); return *this; } @@ -169,25 +371,43 @@ #ifdef PCLASSES_WITH_CR_LINEFEED strm << Unicode::Char::cr(); #endif - strm << Unicode::Char::nl(); + strm << Unicode::Char::lf(); return strm; } -TextStream& dec(TextStream& strm) +TextStream& decimal(TextStream& strm) { - strm.setFormat(TextStream::Dec); + strm.setNumberFormat(Unicode::NumberFormat::Decimal); return strm; } -TextStream& hex(TextStream& strm) +TextStream& currency(TextStream& strm) { - strm.setFormat(TextStream::Hex); + strm.setNumberFormat(Unicode::NumberFormat::Currency); return strm; } -TextStream& oct(TextStream& strm) +TextStream& percent(TextStream& strm) { - strm.setFormat(TextStream::Oct); + strm.setNumberFormat(Unicode::NumberFormat::Percent); + return strm; +} + +TextStream& scientific(TextStream& strm) +{ + strm.setNumberFormat(Unicode::NumberFormat::Scientific); + return strm; +} + +TextStream& spellout(TextStream& strm) +{ + strm.setNumberFormat(Unicode::NumberFormat::Spellout); + return strm; +} + +TextStream& ordinal(TextStream& strm) +{ + strm.setNumberFormat(Unicode::NumberFormat::Ordinal); return strm; } |