strbuffer: get rid of ostringstream

This commit is contained in:
Andrzej Rybczak
2012-08-28 08:10:16 +02:00
parent b910ce38be
commit eaabbee189
10 changed files with 128 additions and 48 deletions

View File

@@ -61,6 +61,7 @@ noinst_HEADERS = \
media_library.h \
menu.h \
mpdpp.h \
numeric_conversions.h \
outputs.h \
playlist_editor.h \
screen.h \

View File

@@ -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<std::string>::apply(Config.locked_screen_width_part*100));
UnlockStatusbar();
if (str_part.empty())
return;

View File

@@ -19,8 +19,6 @@
***************************************************************************/
#include <algorithm>
#include <sstream>
#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<std::string>::apply(key.getChar()-264);
}
else if ((key == Action::Key(KEY_BACKSPACE, ctNCurses) || key == Action::Key(KEY_BACKSPACE_2, ctStandard)))
{

View File

@@ -24,6 +24,7 @@
#include <cstring>
#include <string>
#include "numeric_conversions.h"
#include "actions.h"
#include "window.h"
#include "song.h"
@@ -43,7 +44,6 @@ template <size_t N> 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);

92
src/numeric_conversions.h Normal file
View File

@@ -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 <cstdio>
#include <cwchar>
#include <string>
#ifndef _NUMERIC_CONVERSIONS_H
#define _NUMERIC_CONVERSIONS_H
template <typename R> struct intTo { };
template <> struct intTo<std::string> {
static std::string apply(int n) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", n);
return buf;
}
};
template <> struct intTo<std::wstring> {
static std::wstring apply(int n) {
wchar_t buf[32];
swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%d", n);
return buf;
}
};
template <typename R> struct longIntTo { };
template <> struct longIntTo<std::string> {
static std::string apply(long int n) {
char buf[32];
snprintf(buf, sizeof(buf), "%ld", n);
return buf;
}
};
template <> struct longIntTo<std::wstring> {
static std::wstring apply(long int n) {
wchar_t buf[32];
swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%ld", n);
return buf;
}
};
template <typename R> struct unsignedIntTo { };
template <> struct unsignedIntTo<std::string> {
static std::string apply(unsigned int n) {
char buf[32];
snprintf(buf, sizeof(buf), "%u", n);
return buf;
}
};
template <> struct unsignedIntTo<std::wstring> {
static std::wstring apply(unsigned int n) {
wchar_t buf[32];
swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%u", n);
return buf;
}
};
template <typename R> struct unsignedLongIntTo { };
template <> struct unsignedLongIntTo<std::string> {
static std::string apply(unsigned long int n) {
char buf[32];
snprintf(buf, sizeof(buf), "%lu", n);
return buf;
}
};
template <> struct unsignedLongIntTo<std::wstring> {
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

View File

@@ -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<std::string>::apply(plsize) + " item" + (plsize == 1 ? ")" : "s)") : "";
title.resize(Content->GetWidth());
Content->SetTitle(title);
}

View File

@@ -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");

View File

@@ -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<std::string>::apply(i+1) << ": '" << fmt[i] << "'\n";
return false;
}
}

View File

@@ -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<std::string>::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<std::string>::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<std::string>::apply(volume);
VolumeState += "%";
}
*wHeader << Config.volume_color;

View File

@@ -21,10 +21,10 @@
#ifndef _STRBUFFER_H
#define _STRBUFFER_H
#include "numeric_conversions.h"
#include "tolower.h"
#include "window.h"
#include <sstream>
#include <list>
namespace NCurses
@@ -56,7 +56,7 @@ namespace NCurses
/// Internal buffer for storing raw text
///
std::basic_ostringstream<C> itsString;
std::basic_string<C> 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<C> Str() const;
const std::basic_string<C> &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<C> &operator<<(int n)
{
itsString << n;
itsString += intTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(long int n)
{
itsString << n;
itsString += longIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(unsigned int n)
{
itsString << n;
itsString += unsignedIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(long unsigned int n)
basic_buffer<C> &operator<<(unsigned long int n)
{
itsString << n;
itsString += unsignedLongIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(char c)
{
itsString << c;
itsString += c;
return *this;
}
basic_buffer<C> &operator<<(wchar_t c)
basic_buffer<C> &operator<<(wchar_t wc)
{
itsString << c;
itsString += wc;
return *this;
}
basic_buffer<C> &operator<<(const C *s)
{
itsString << s;
itsString += s;
return *this;
}
basic_buffer<C> &operator<<(const std::basic_string<C> &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<C> &buf)
{
const std::basic_string<C> &s = buf.itsTempString ? *buf.itsTempString : buf.itsString.str();
const std::basic_string<C> &s = buf.itsTempString ? *buf.itsTempString : buf.itsString;
if (buf.itsFormat.empty())
w << s;
else
@@ -249,15 +249,11 @@ namespace NCurses
typedef basic_buffer<wchar_t> WBuffer;
}
template <typename C> NCurses::basic_buffer<C>::basic_buffer(const basic_buffer &b) : itsFormat(b.itsFormat),
itsTempString(b.itsTempString)
{
itsString << b.itsString.str();
}
template <typename C> NCurses::basic_buffer<C>::basic_buffer(const basic_buffer &b) : itsString(b.itsString), itsFormat(b.itsFormat), itsTempString(b.itsTempString) { }
template <typename C> std::basic_string<C> NCurses::basic_buffer<C>::Str() const
template <typename C> const std::basic_string<C> &NCurses::basic_buffer<C>::Str() const
{
return itsString.str();
return itsString;
}
template <typename C> bool NCurses::basic_buffer<C>::SetFormatting( short val_b,
@@ -270,7 +266,7 @@ template <typename C> bool NCurses::basic_buffer<C>::SetFormatting( short val_b,
if (s.empty())
return false;
bool result = false;
std::basic_string<C> base = itsString.str();
std::basic_string<C> base = itsString;
if (!case_sensitive)
{
ToLower(s);
@@ -303,7 +299,7 @@ template <typename C> void NCurses::basic_buffer<C>::RemoveFormatting( short val
{
if (pattern.empty())
return;
std::basic_string<C> base = itsString.str();
std::basic_string<C> base = itsString;
if (!case_sensitive)
{
ToLower(pattern);
@@ -340,7 +336,7 @@ template <typename C> void NCurses::basic_buffer<C>::Write( Window &w,
const std::basic_string<C> &separator
) const
{
std::basic_string<C> s = itsString.str();
std::basic_string<C> s = itsString;
size_t len = Window::Length(s);
if (len > width)
@@ -393,7 +389,7 @@ template <typename C> void NCurses::basic_buffer<C>::Write( Window &w,
template <typename C> void NCurses::basic_buffer<C>::Clear()
{
itsString.str(std::basic_string<C>());
itsString.clear();
itsFormat.clear();
}
@@ -408,7 +404,7 @@ template <typename C> void NCurses::basic_buffer<C>::LoadAttribute(Window &w, sh
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::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 <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operat
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const NCurses::basic_buffer<C> &buf)
{
size_t len = itsString.str().length();
itsString << buf.itsString.str();
size_t length = itsString.length();
itsString += buf.itsString;
std::list<FormatPos> 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;
}