strbuffer: get rid of ostringstream
This commit is contained in:
@@ -61,6 +61,7 @@ noinst_HEADERS = \
|
|||||||
media_library.h \
|
media_library.h \
|
||||||
menu.h \
|
menu.h \
|
||||||
mpdpp.h \
|
mpdpp.h \
|
||||||
|
numeric_conversions.h \
|
||||||
outputs.h \
|
outputs.h \
|
||||||
playlist_editor.h \
|
playlist_editor.h \
|
||||||
screen.h \
|
screen.h \
|
||||||
|
|||||||
@@ -1677,7 +1677,7 @@ void ToggleScreenLock::Run()
|
|||||||
{
|
{
|
||||||
LockStatusbar();
|
LockStatusbar();
|
||||||
Statusbar() << "% of the locked screen's width to be reserved (20-80): ";
|
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();
|
UnlockStatusbar();
|
||||||
if (str_part.empty())
|
if (str_part.empty())
|
||||||
return;
|
return;
|
||||||
|
|||||||
11
src/conv.cpp
11
src/conv.cpp
@@ -19,8 +19,6 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "conv.h"
|
#include "conv.h"
|
||||||
|
|
||||||
int StrToInt(const std::string &str)
|
int StrToInt(const std::string &str)
|
||||||
@@ -33,13 +31,6 @@ long StrToLong(const std::string &str)
|
|||||||
return atol(str.c_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
|
std::string IntoStr(mpd_tag_type tag) // this is only for left column's title in media library
|
||||||
{
|
{
|
||||||
switch (tag)
|
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))
|
else if (key >= Action::Key(KEY_F1, ctNCurses) && key <= Action::Key(KEY_F12, ctNCurses))
|
||||||
{
|
{
|
||||||
result += "F";
|
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)))
|
else if ((key == Action::Key(KEY_BACKSPACE, ctNCurses) || key == Action::Key(KEY_BACKSPACE_2, ctStandard)))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "numeric_conversions.h"
|
||||||
#include "actions.h"
|
#include "actions.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "song.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 &);
|
int StrToInt(const std::string &);
|
||||||
long StrToLong(const std::string &);
|
long StrToLong(const std::string &);
|
||||||
|
|
||||||
std::string IntoStr(int);
|
|
||||||
std::string IntoStr(mpd_tag_type);
|
std::string IntoStr(mpd_tag_type);
|
||||||
std::string IntoStr(NCurses::Color);
|
std::string IntoStr(NCurses::Color);
|
||||||
std::string IntoStr(const Action::Key &key, bool *print_backspace = 0);
|
std::string IntoStr(const Action::Key &key, bool *print_backspace = 0);
|
||||||
|
|||||||
92
src/numeric_conversions.h
Normal file
92
src/numeric_conversions.h
Normal 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
|
||||||
@@ -153,7 +153,7 @@ void PlaylistEditor::Update()
|
|||||||
});
|
});
|
||||||
if (plsize > 0)
|
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());
|
title.resize(Content->GetWidth());
|
||||||
Content->SetTitle(title);
|
Content->SetTitle(title);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ void SearchEngine::EnterPressed()
|
|||||||
found += 3; // don't count options inserted below
|
found += 3; // don't count options inserted below
|
||||||
w->InsertSeparator(ResetButton+1);
|
w->InsertSeparator(ResetButton+1);
|
||||||
w->InsertOption(ResetButton+2, SEItem(), 1, 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);
|
w->InsertSeparator(ResetButton+3);
|
||||||
UpdateFoundList();
|
UpdateFoundList();
|
||||||
ShowMessage("Searching finished");
|
ShowMessage("Searching finished");
|
||||||
|
|||||||
@@ -317,7 +317,7 @@ bool MPD::Song::isFormatOk(const std::string &type, const std::string &fmt)
|
|||||||
while (isdigit(fmt[++i])) { }
|
while (isdigit(fmt[++i])) { }
|
||||||
if (!toGetFunction(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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -421,7 +421,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
|
|||||||
if (Config.display_bitrate && Mpd.GetBitrate())
|
if (Config.display_bitrate && Mpd.GetBitrate())
|
||||||
{
|
{
|
||||||
tracklength += " ";
|
tracklength += " ";
|
||||||
tracklength += IntoStr(Mpd.GetBitrate());
|
tracklength += intTo<std::string>::apply(Mpd.GetBitrate());
|
||||||
tracklength += " kbps";
|
tracklength += " kbps";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,7 +455,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
|
|||||||
if (Config.display_bitrate && Mpd.GetBitrate())
|
if (Config.display_bitrate && Mpd.GetBitrate())
|
||||||
{
|
{
|
||||||
tracklength += " [";
|
tracklength += " [";
|
||||||
tracklength += IntoStr(Mpd.GetBitrate());
|
tracklength += intTo<std::string>::apply(Mpd.GetBitrate());
|
||||||
tracklength += " kbps]";
|
tracklength += " kbps]";
|
||||||
}
|
}
|
||||||
tracklength += " [";
|
tracklength += " [";
|
||||||
@@ -604,7 +604,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
|
|||||||
VolumeState += "n/a";
|
VolumeState += "n/a";
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
VolumeState += IntoStr(volume);
|
VolumeState += intTo<std::string>::apply(volume);
|
||||||
VolumeState += "%";
|
VolumeState += "%";
|
||||||
}
|
}
|
||||||
*wHeader << Config.volume_color;
|
*wHeader << Config.volume_color;
|
||||||
|
|||||||
@@ -21,10 +21,10 @@
|
|||||||
#ifndef _STRBUFFER_H
|
#ifndef _STRBUFFER_H
|
||||||
#define _STRBUFFER_H
|
#define _STRBUFFER_H
|
||||||
|
|
||||||
|
#include "numeric_conversions.h"
|
||||||
#include "tolower.h"
|
#include "tolower.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
namespace NCurses
|
namespace NCurses
|
||||||
@@ -56,7 +56,7 @@ namespace NCurses
|
|||||||
|
|
||||||
/// Internal buffer for storing raw text
|
/// Internal buffer for storing raw text
|
||||||
///
|
///
|
||||||
std::basic_ostringstream<C> itsString;
|
std::basic_string<C> itsString;
|
||||||
|
|
||||||
/// List used for storing formatting informations
|
/// List used for storing formatting informations
|
||||||
///
|
///
|
||||||
@@ -79,7 +79,7 @@ namespace NCurses
|
|||||||
|
|
||||||
/// @return raw content of the buffer without formatting informations
|
/// @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
|
/// 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
|
/// 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)
|
basic_buffer<C> &operator<<(int n)
|
||||||
{
|
{
|
||||||
itsString << n;
|
itsString += intTo< std::basic_string<C> >::apply(n);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(long int n)
|
basic_buffer<C> &operator<<(long int n)
|
||||||
{
|
{
|
||||||
itsString << n;
|
itsString += longIntTo< std::basic_string<C> >::apply(n);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(unsigned int n)
|
basic_buffer<C> &operator<<(unsigned int n)
|
||||||
{
|
{
|
||||||
itsString << n;
|
itsString += unsignedIntTo< std::basic_string<C> >::apply(n);
|
||||||
return *this;
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(char c)
|
basic_buffer<C> &operator<<(char c)
|
||||||
{
|
{
|
||||||
itsString << c;
|
itsString += c;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(wchar_t c)
|
basic_buffer<C> &operator<<(wchar_t wc)
|
||||||
{
|
{
|
||||||
itsString << c;
|
itsString += wc;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(const C *s)
|
basic_buffer<C> &operator<<(const C *s)
|
||||||
{
|
{
|
||||||
itsString << s;
|
itsString += s;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
basic_buffer<C> &operator<<(const std::basic_string<C> &s)
|
basic_buffer<C> &operator<<(const std::basic_string<C> &s)
|
||||||
{
|
{
|
||||||
itsString << s;
|
itsString += s;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +204,7 @@ namespace NCurses
|
|||||||
/// the content of buffer to window object
|
/// the content of buffer to window object
|
||||||
friend Window &operator<<(Window &w, const basic_buffer<C> &buf)
|
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())
|
if (buf.itsFormat.empty())
|
||||||
w << s;
|
w << s;
|
||||||
else
|
else
|
||||||
@@ -249,15 +249,11 @@ namespace NCurses
|
|||||||
typedef basic_buffer<wchar_t> WBuffer;
|
typedef basic_buffer<wchar_t> WBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename C> NCurses::basic_buffer<C>::basic_buffer(const basic_buffer &b) : itsFormat(b.itsFormat),
|
template <typename C> NCurses::basic_buffer<C>::basic_buffer(const basic_buffer &b) : itsString(b.itsString), itsFormat(b.itsFormat), itsTempString(b.itsTempString) { }
|
||||||
itsTempString(b.itsTempString)
|
|
||||||
{
|
|
||||||
itsString << b.itsString.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
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())
|
if (s.empty())
|
||||||
return false;
|
return false;
|
||||||
bool result = false;
|
bool result = false;
|
||||||
std::basic_string<C> base = itsString.str();
|
std::basic_string<C> base = itsString;
|
||||||
if (!case_sensitive)
|
if (!case_sensitive)
|
||||||
{
|
{
|
||||||
ToLower(s);
|
ToLower(s);
|
||||||
@@ -303,7 +299,7 @@ template <typename C> void NCurses::basic_buffer<C>::RemoveFormatting( short val
|
|||||||
{
|
{
|
||||||
if (pattern.empty())
|
if (pattern.empty())
|
||||||
return;
|
return;
|
||||||
std::basic_string<C> base = itsString.str();
|
std::basic_string<C> base = itsString;
|
||||||
if (!case_sensitive)
|
if (!case_sensitive)
|
||||||
{
|
{
|
||||||
ToLower(pattern);
|
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> &separator
|
||||||
) const
|
) const
|
||||||
{
|
{
|
||||||
std::basic_string<C> s = itsString.str();
|
std::basic_string<C> s = itsString;
|
||||||
size_t len = Window::Length(s);
|
size_t len = Window::Length(s);
|
||||||
|
|
||||||
if (len > width)
|
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()
|
template <typename C> void NCurses::basic_buffer<C>::Clear()
|
||||||
{
|
{
|
||||||
itsString.str(std::basic_string<C>());
|
itsString.clear();
|
||||||
itsFormat.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)
|
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(Color color)
|
||||||
{
|
{
|
||||||
FormatPos f;
|
FormatPos f;
|
||||||
f.Position = itsString.str().length();
|
f.Position = itsString.length();
|
||||||
f.Value = color;
|
f.Value = color;
|
||||||
itsFormat.push_back(f);
|
itsFormat.push_back(f);
|
||||||
return *this;
|
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)
|
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const NCurses::basic_buffer<C> &buf)
|
||||||
{
|
{
|
||||||
size_t len = itsString.str().length();
|
size_t length = itsString.length();
|
||||||
itsString << buf.itsString.str();
|
itsString += buf.itsString;
|
||||||
std::list<FormatPos> tmp = buf.itsFormat;
|
std::list<FormatPos> tmp = buf.itsFormat;
|
||||||
if (len)
|
if (length)
|
||||||
for (auto it = tmp.begin(); it != tmp.end(); ++it)
|
for (auto it = tmp.begin(); it != tmp.end(); ++it)
|
||||||
it->Position += len;
|
it->Position += length;
|
||||||
itsFormat.merge(tmp);
|
itsFormat.merge(tmp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user