From 5a63a8c0ec6ce13d985c70de00d1848c61bd9853 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 28 Jan 2019 18:06:18 +0000 Subject: [PATCH] added: to Log: IntMinWidth(size_t min_width) minimal width for integers added: to Log: operator<<(long long s) added: to TextStreamBase: operator<<(long long); operator<<(unsigned long long); int_min_width(size_t min_width); // minimal width for integers git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1167 e52654a7-88a9-db11-a3e9-0013d4bc506e --- log/log.cpp | 22 ++++++++++++- log/log.h | 4 ++- textstream/textstream.h | 69 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/log/log.cpp b/log/log.cpp index 0e28e6b..19b98db 100644 --- a/log/log.cpp +++ b/log/log.cpp @@ -99,6 +99,17 @@ FileLog * Log::GetFileLog() } +Log & Log::IntMinWidth(size_t min_width) +{ + if( buffer ) + { + buffer->int_min_width(min_width); + } + + return *this; +} + + Log & Log::operator<<(const void * s) { if( buffer && file_log && current_level <= file_log->get_log_level() ) @@ -211,6 +222,16 @@ Log & Log::operator<<(long s) } +Log & Log::operator<<(long long s) +{ + if( buffer && file_log && current_level <= file_log->get_log_level() ) + { + (*buffer) << s; + } + + return *this; +} + Log & Log::operator<<(char s) @@ -442,7 +463,6 @@ void Log::save_log() } - } // namespace diff --git a/log/log.h b/log/log.h index 5b44b8a..ec1011f 100644 --- a/log/log.h +++ b/log/log.h @@ -96,6 +96,8 @@ public: void SetMaxBufferLength(size_t max_buffer_length); size_t GetMaxBufferLength(); + virtual Log & IntMinWidth(size_t min_width); + virtual Log & operator<<(const void * s); virtual Log & operator<<(const char * s); @@ -111,7 +113,7 @@ public: virtual Log & operator<<(int s); virtual Log & operator<<(long s); - //virtual Log & operator<<(long long s); // added + virtual Log & operator<<(long long s); // add unsigned long, unsigned int diff --git a/textstream/textstream.h b/textstream/textstream.h index b488951..523f638 100644 --- a/textstream/textstream.h +++ b/textstream/textstream.h @@ -100,13 +100,20 @@ public: TextStreamBase & operator<<(wchar_t); TextStreamBase & operator<<(int); TextStreamBase & operator<<(long); + TextStreamBase & operator<<(long long); TextStreamBase & operator<<(unsigned int); TextStreamBase & operator<<(unsigned long); + TextStreamBase & operator<<(unsigned long long); TextStreamBase & operator<<(double); TextStreamBase & operator<<(const void *); // printing a pointer TextStreamBase & operator<<(const PT::Space & space); TextStreamBase & operator<<(const PT::Date & date); + // min width for integer output + // if the output value has less digits then first zeroes are added + // (0 turn off) + TextStreamBase & int_min_width(size_t min_width); + template TextStreamBase & operator<<(const TextStreamBase & arg); @@ -118,10 +125,13 @@ public: TextStreamBase & write(const char * format, double val); TextStreamBase & write(const wchar_t * format, double val); + TextStreamBase & fill_up_if_needed(wchar_t fill_up_char, size_t existing_length); + /* raw access */ int radix; + size_t min_width_for_integers; buffer_type buffer; }; @@ -133,13 +143,15 @@ public: template TextStreamBase::TextStreamBase() { - radix = 10; + clear(); } template void TextStreamBase::clear() { + radix = 10; + min_width_for_integers = 0; buffer.clear(); } @@ -323,20 +335,31 @@ template TextStreamBase & TextStreamBase::operator<<(int v) { - return operator<<(static_cast(v)); + return operator<<(static_cast(v)); } template TextStreamBase & TextStreamBase::operator<<(long v) +{ + return operator<<(static_cast(v)); +} + + +template +TextStreamBase & +TextStreamBase::operator<<(long long v) { char_type buf[50]; size_t len = sizeof(buf) / sizeof(char_type); size_t lenout; if( Toa(v, buf, len, radix, &lenout) ) + { + fill_up_if_needed('0', lenout); buffer.append(buf, lenout); + } return *this; } @@ -346,20 +369,31 @@ template TextStreamBase & TextStreamBase::operator<<(unsigned int v) { - return operator<<(static_cast(v)); + return operator<<(static_cast(v)); } template TextStreamBase & TextStreamBase::operator<<(unsigned long v) +{ + return operator<<(static_cast(v)); +} + + +template +TextStreamBase & +TextStreamBase::operator<<(unsigned long long v) { char_type buf[50]; size_t len = sizeof(buf) / sizeof(char_type); size_t lenout; if( Toa(v, buf, len, radix, &lenout) ) + { + fill_up_if_needed('0', lenout); buffer.append(buf, lenout); + } return *this; } @@ -387,7 +421,8 @@ size_t lenout; buf[0] = '0'; buf[1] = 'x'; - if( Toa(reinterpret_cast(v), buf+2, len-2, 16, &lenout) ) + // IMPROVE ME add some minimal width? + if( Toa(reinterpret_cast(v), buf+2, len-2, 16, &lenout) ) buffer.append(buf, lenout+2); return *this; @@ -464,6 +499,32 @@ return *this; +template +TextStreamBase & +TextStreamBase::int_min_width(size_t min_width) +{ + min_width_for_integers = min_width; + + return *this; +} + + +template +TextStreamBase & +TextStreamBase::fill_up_if_needed(wchar_t fill_up_char, size_t existing_length) +{ + if( min_width_for_integers > 0 && min_width_for_integers > existing_length ) + { + for(size_t i = existing_length ; i < min_width_for_integers ; ++i) + { + buffer.append(fill_up_char); + } + } + + return *this; +} + + } // namespace