From eaabbee189e390d255184b65eaf149d6f01ba403 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Tue, 28 Aug 2012 08:10:16 +0200 Subject: [PATCH] strbuffer: get rid of ostringstream --- src/Makefile.am | 1 + src/actions.cpp | 2 +- src/conv.cpp | 11 +---- src/conv.h | 2 +- src/numeric_conversions.h | 92 +++++++++++++++++++++++++++++++++++++++ src/playlist_editor.cpp | 2 +- src/search_engine.cpp | 2 +- src/song.cpp | 2 +- src/status.cpp | 6 +-- src/strbuffer.h | 56 +++++++++++------------- 10 files changed, 128 insertions(+), 48 deletions(-) create mode 100644 src/numeric_conversions.h diff --git a/src/Makefile.am b/src/Makefile.am index 03ae9fdb..36733300 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -61,6 +61,7 @@ noinst_HEADERS = \ media_library.h \ menu.h \ mpdpp.h \ + numeric_conversions.h \ outputs.h \ playlist_editor.h \ screen.h \ diff --git a/src/actions.cpp b/src/actions.cpp index af711538..8c7cbaba 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -1677,7 +1677,7 @@ void ToggleScreenLock::Run() { LockStatusbar(); Statusbar() << "% of the locked screen's width to be reserved (20-80): "; - std::string str_part = wFooter->GetString(IntoStr(Config.locked_screen_width_part*100)); + std::string str_part = wFooter->GetString(intTo::apply(Config.locked_screen_width_part*100)); UnlockStatusbar(); if (str_part.empty()) return; diff --git a/src/conv.cpp b/src/conv.cpp index a6be4914..4a46ccaf 100644 --- a/src/conv.cpp +++ b/src/conv.cpp @@ -19,8 +19,6 @@ ***************************************************************************/ #include -#include - #include "conv.h" int StrToInt(const std::string &str) @@ -33,13 +31,6 @@ long StrToLong(const std::string &str) return atol(str.c_str()); } -std::string IntoStr(int i) -{ - char buf[32]; - snprintf(buf, sizeof(buf), "%d", i); - return buf; -} - std::string IntoStr(mpd_tag_type tag) // this is only for left column's title in media library { switch (tag) @@ -134,7 +125,7 @@ std::string IntoStr(const Action::Key &key, bool *print_backspace) else if (key >= Action::Key(KEY_F1, ctNCurses) && key <= Action::Key(KEY_F12, ctNCurses)) { result += "F"; - result += IntoStr(key.getChar()-264); + result += intTo::apply(key.getChar()-264); } else if ((key == Action::Key(KEY_BACKSPACE, ctNCurses) || key == Action::Key(KEY_BACKSPACE_2, ctStandard))) { diff --git a/src/conv.h b/src/conv.h index cfcec87a..78c9d2fc 100644 --- a/src/conv.h +++ b/src/conv.h @@ -24,6 +24,7 @@ #include #include +#include "numeric_conversions.h" #include "actions.h" #include "window.h" #include "song.h" @@ -43,7 +44,6 @@ template void Replace(std::string &s, const char (&from)[N], const ch int StrToInt(const std::string &); long StrToLong(const std::string &); -std::string IntoStr(int); std::string IntoStr(mpd_tag_type); std::string IntoStr(NCurses::Color); std::string IntoStr(const Action::Key &key, bool *print_backspace = 0); diff --git a/src/numeric_conversions.h b/src/numeric_conversions.h new file mode 100644 index 00000000..11bff331 --- /dev/null +++ b/src/numeric_conversions.h @@ -0,0 +1,92 @@ +/*************************************************************************** + * Copyright (C) 2008-2012 by Andrzej Rybczak * + * electricityispower@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include + +#ifndef _NUMERIC_CONVERSIONS_H +#define _NUMERIC_CONVERSIONS_H + +template struct intTo { }; +template <> struct intTo { + static std::string apply(int n) { + char buf[32]; + snprintf(buf, sizeof(buf), "%d", n); + return buf; + } +}; +template <> struct intTo { + static std::wstring apply(int n) { + wchar_t buf[32]; + swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%d", n); + return buf; + } +}; + +template struct longIntTo { }; +template <> struct longIntTo { + static std::string apply(long int n) { + char buf[32]; + snprintf(buf, sizeof(buf), "%ld", n); + return buf; + } +}; +template <> struct longIntTo { + static std::wstring apply(long int n) { + wchar_t buf[32]; + swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%ld", n); + return buf; + } +}; + +template struct unsignedIntTo { }; +template <> struct unsignedIntTo { + static std::string apply(unsigned int n) { + char buf[32]; + snprintf(buf, sizeof(buf), "%u", n); + return buf; + } +}; +template <> struct unsignedIntTo { + static std::wstring apply(unsigned int n) { + wchar_t buf[32]; + swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%u", n); + return buf; + } +}; + +template struct unsignedLongIntTo { }; +template <> struct unsignedLongIntTo { + static std::string apply(unsigned long int n) { + char buf[32]; + snprintf(buf, sizeof(buf), "%lu", n); + return buf; + } +}; +template <> struct unsignedLongIntTo { + static std::wstring apply(unsigned long int n) { + wchar_t buf[32]; + swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%lu", n); + return buf; + } +}; + +#endif // _NUMERIC_CONVERSIONS_H diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp index 04d79089..961adb51 100644 --- a/src/playlist_editor.cpp +++ b/src/playlist_editor.cpp @@ -153,7 +153,7 @@ void PlaylistEditor::Update() }); if (plsize > 0) { - std::string title = Config.titles_visibility ? "Playlist content (" + IntoStr(plsize) + " item" + (plsize == 1 ? ")" : "s)") : ""; + std::string title = Config.titles_visibility ? "Playlist content (" + unsignedLongIntTo::apply(plsize) + " item" + (plsize == 1 ? ")" : "s)") : ""; title.resize(Content->GetWidth()); Content->SetTitle(title); } diff --git a/src/search_engine.cpp b/src/search_engine.cpp index 73229e75..1c545ba5 100644 --- a/src/search_engine.cpp +++ b/src/search_engine.cpp @@ -170,7 +170,7 @@ void SearchEngine::EnterPressed() found += 3; // don't count options inserted below w->InsertSeparator(ResetButton+1); w->InsertOption(ResetButton+2, SEItem(), 1, 1); - w->at(ResetButton+2).mkBuffer() << Config.color1 << "Search results: " << Config.color2 << "Found " << found << (found > 1 ? " songs" : " song") << clDefault; + w->at(ResetButton+2).mkBuffer() << Config.color1 << "Search results: " << Config.color2 << "Found " << found << (found > 1 ? " songs" : " song") << clDefault; w->InsertSeparator(ResetButton+3); UpdateFoundList(); ShowMessage("Searching finished"); diff --git a/src/song.cpp b/src/song.cpp index a2364a3e..2b00d317 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -317,7 +317,7 @@ bool MPD::Song::isFormatOk(const std::string &type, const std::string &fmt) while (isdigit(fmt[++i])) { } if (!toGetFunction(fmt[i])) { - std::cerr << type << ": invalid character at position " << IntoStr(i+1) << ": '" << fmt[i] << "'\n"; + std::cerr << type << ": invalid character at position " << unsignedLongIntTo::apply(i+1) << ": '" << fmt[i] << "'\n"; return false; } } diff --git a/src/status.cpp b/src/status.cpp index 2b9bd858..4b70768c 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -421,7 +421,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) if (Config.display_bitrate && Mpd.GetBitrate()) { tracklength += " "; - tracklength += IntoStr(Mpd.GetBitrate()); + tracklength += intTo::apply(Mpd.GetBitrate()); tracklength += " kbps"; } @@ -455,7 +455,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) if (Config.display_bitrate && Mpd.GetBitrate()) { tracklength += " ["; - tracklength += IntoStr(Mpd.GetBitrate()); + tracklength += intTo::apply(Mpd.GetBitrate()); tracklength += " kbps]"; } tracklength += " ["; @@ -604,7 +604,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) VolumeState += "n/a"; else { - VolumeState += IntoStr(volume); + VolumeState += intTo::apply(volume); VolumeState += "%"; } *wHeader << Config.volume_color; diff --git a/src/strbuffer.h b/src/strbuffer.h index 6d31467e..4157842b 100644 --- a/src/strbuffer.h +++ b/src/strbuffer.h @@ -21,10 +21,10 @@ #ifndef _STRBUFFER_H #define _STRBUFFER_H +#include "numeric_conversions.h" #include "tolower.h" #include "window.h" -#include #include namespace NCurses @@ -56,7 +56,7 @@ namespace NCurses /// Internal buffer for storing raw text /// - std::basic_ostringstream itsString; + std::basic_string itsString; /// List used for storing formatting informations /// @@ -79,7 +79,7 @@ namespace NCurses /// @return raw content of the buffer without formatting informations /// - std::basic_string Str() const; + const std::basic_string &Str() const; /// Searches for given string in buffer and sets format/color at the /// beginning and end of it using val_b and val_e flags accordingly @@ -138,49 +138,49 @@ namespace NCurses basic_buffer &operator<<(int n) { - itsString << n; + itsString += intTo< std::basic_string >::apply(n); return *this; } basic_buffer &operator<<(long int n) { - itsString << n; + itsString += longIntTo< std::basic_string >::apply(n); return *this; } basic_buffer &operator<<(unsigned int n) { - itsString << n; + itsString += unsignedIntTo< std::basic_string >::apply(n); return *this; } - basic_buffer &operator<<(long unsigned int n) + basic_buffer &operator<<(unsigned long int n) { - itsString << n; + itsString += unsignedLongIntTo< std::basic_string >::apply(n); return *this; } basic_buffer &operator<<(char c) { - itsString << c; + itsString += c; return *this; } - basic_buffer &operator<<(wchar_t c) + basic_buffer &operator<<(wchar_t wc) { - itsString << c; + itsString += wc; return *this; } basic_buffer &operator<<(const C *s) { - itsString << s; + itsString += s; return *this; } basic_buffer &operator<<(const std::basic_string &s) { - itsString << s; + itsString += s; return *this; } @@ -204,7 +204,7 @@ namespace NCurses /// the content of buffer to window object friend Window &operator<<(Window &w, const basic_buffer &buf) { - const std::basic_string &s = buf.itsTempString ? *buf.itsTempString : buf.itsString.str(); + const std::basic_string &s = buf.itsTempString ? *buf.itsTempString : buf.itsString; if (buf.itsFormat.empty()) w << s; else @@ -249,15 +249,11 @@ namespace NCurses typedef basic_buffer WBuffer; } -template NCurses::basic_buffer::basic_buffer(const basic_buffer &b) : itsFormat(b.itsFormat), - itsTempString(b.itsTempString) -{ - itsString << b.itsString.str(); -} +template NCurses::basic_buffer::basic_buffer(const basic_buffer &b) : itsString(b.itsString), itsFormat(b.itsFormat), itsTempString(b.itsTempString) { } -template std::basic_string NCurses::basic_buffer::Str() const +template const std::basic_string &NCurses::basic_buffer::Str() const { - return itsString.str(); + return itsString; } template bool NCurses::basic_buffer::SetFormatting( short val_b, @@ -270,7 +266,7 @@ template bool NCurses::basic_buffer::SetFormatting( short val_b, if (s.empty()) return false; bool result = false; - std::basic_string base = itsString.str(); + std::basic_string base = itsString; if (!case_sensitive) { ToLower(s); @@ -303,7 +299,7 @@ template void NCurses::basic_buffer::RemoveFormatting( short val { if (pattern.empty()) return; - std::basic_string base = itsString.str(); + std::basic_string base = itsString; if (!case_sensitive) { ToLower(pattern); @@ -340,7 +336,7 @@ template void NCurses::basic_buffer::Write( Window &w, const std::basic_string &separator ) const { - std::basic_string s = itsString.str(); + std::basic_string s = itsString; size_t len = Window::Length(s); if (len > width) @@ -393,7 +389,7 @@ template void NCurses::basic_buffer::Write( Window &w, template void NCurses::basic_buffer::Clear() { - itsString.str(std::basic_string()); + itsString.clear(); itsFormat.clear(); } @@ -408,7 +404,7 @@ template void NCurses::basic_buffer::LoadAttribute(Window &w, sh template NCurses::basic_buffer &NCurses::basic_buffer::operator<<(Color color) { FormatPos f; - f.Position = itsString.str().length(); + f.Position = itsString.length(); f.Value = color; itsFormat.push_back(f); return *this; @@ -421,12 +417,12 @@ template NCurses::basic_buffer &NCurses::basic_buffer::operat template NCurses::basic_buffer &NCurses::basic_buffer::operator<<(const NCurses::basic_buffer &buf) { - size_t len = itsString.str().length(); - itsString << buf.itsString.str(); + size_t length = itsString.length(); + itsString += buf.itsString; std::list tmp = buf.itsFormat; - if (len) + if (length) for (auto it = tmp.begin(); it != tmp.end(); ++it) - it->Position += len; + it->Position += length; itsFormat.merge(tmp); return *this; }