more cleanup and grouping functions logically together
This commit is contained in:
75
src/utility/numeric_conversions.h
Normal file
75
src/utility/numeric_conversions.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/***************************************************************************
|
||||
* 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 <string>
|
||||
#include "string.h"
|
||||
|
||||
#ifndef _UTILITY_NUMERIC_CONVERSIONS_H
|
||||
#define _UTILITY_NUMERIC_CONVERSIONS_H
|
||||
|
||||
template <typename R> struct intTo { };
|
||||
template <> struct intTo<std::string> {
|
||||
static std::string apply(int n) {
|
||||
return print<32, std::string>::apply("%d", n);
|
||||
}
|
||||
};
|
||||
template <> struct intTo<std::wstring> {
|
||||
static std::wstring apply(int n) {
|
||||
return print<32, std::wstring>::apply(L"%d", n);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename R> struct longIntTo { };
|
||||
template <> struct longIntTo<std::string> {
|
||||
static std::string apply(long int n) {
|
||||
return print<32, std::string>::apply("%ld", n);
|
||||
}
|
||||
};
|
||||
template <> struct longIntTo<std::wstring> {
|
||||
static std::wstring apply(long int n) {
|
||||
return print<32, std::wstring>::apply(L"%ld", n);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename R> struct unsignedIntTo { };
|
||||
template <> struct unsignedIntTo<std::string> {
|
||||
static std::string apply(unsigned int n) {
|
||||
return print<32, std::string>::apply("%u", n);
|
||||
}
|
||||
};
|
||||
template <> struct unsignedIntTo<std::wstring> {
|
||||
static std::wstring apply(unsigned int n) {
|
||||
return print<32, std::wstring>::apply(L"%u", n);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename R> struct unsignedLongIntTo { };
|
||||
template <> struct unsignedLongIntTo<std::string> {
|
||||
static std::string apply(unsigned long int n) {
|
||||
return print<32, std::string>::apply("%lu", n);
|
||||
}
|
||||
};
|
||||
template <> struct unsignedLongIntTo<std::wstring> {
|
||||
static std::wstring apply(unsigned long int n) {
|
||||
return print<32, std::wstring>::apply(L"%lu", n);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _UTILITY_NUMERIC_CONVERSIONS_H
|
||||
@@ -23,6 +23,52 @@
|
||||
#include <algorithm>
|
||||
#include "utility/string.h"
|
||||
|
||||
int stringToInt(const std::string &s)
|
||||
{
|
||||
return atoi(s.c_str());
|
||||
}
|
||||
|
||||
long stringToLongInt(const std::string &s)
|
||||
{
|
||||
return atol(s.c_str());
|
||||
}
|
||||
|
||||
bool isInteger(const char *s)
|
||||
{
|
||||
assert(s);
|
||||
if (*s == '\0')
|
||||
return false;
|
||||
for (const char *it = s; *it != '\0'; ++it)
|
||||
if (!isdigit(*it) && (it != s || *it != '-'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string ToString(const std::wstring &ws)
|
||||
{
|
||||
std::string result;
|
||||
char s[MB_CUR_MAX];
|
||||
for (size_t i = 0; i < ws.length(); ++i)
|
||||
{
|
||||
int n = wcrtomb(s, ws[i], 0);
|
||||
if (n > 0)
|
||||
result.append(s, n);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring ToWString(const std::string &s)
|
||||
{
|
||||
std::wstring result;
|
||||
wchar_t *ws = new wchar_t[s.length()];
|
||||
const char *c_s = s.c_str();
|
||||
int n = mbsrtowcs(ws, &c_s, s.length(), 0);
|
||||
if (n > 0)
|
||||
result.append(ws, n);
|
||||
delete [] ws;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> split(const std::string &s, const std::string &delimiter)
|
||||
{
|
||||
if (delimiter.empty())
|
||||
@@ -134,13 +180,18 @@ std::string getEnclosedString(const std::string &s, char a, char b, size_t *pos)
|
||||
return result;
|
||||
}
|
||||
|
||||
bool isInteger(const char *s)
|
||||
void removeInvalidCharsFromFilename(std::string &filename)
|
||||
{
|
||||
assert(s);
|
||||
if (*s == '\0')
|
||||
return false;
|
||||
for (const char *it = s; *it != '\0'; ++it)
|
||||
if (!isdigit(*it) && (it != s || *it != '-'))
|
||||
return false;
|
||||
return true;
|
||||
const char *unallowed_chars = "\"*/:<>?\\|";
|
||||
for (const char *c = unallowed_chars; *c; ++c)
|
||||
{
|
||||
for (size_t i = 0; i < filename.length(); ++i)
|
||||
{
|
||||
if (filename[i] == *c)
|
||||
{
|
||||
filename.erase(filename.begin()+i);
|
||||
--i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,43 @@
|
||||
#ifndef _UTILITY_STRING
|
||||
#define _UTILITY_STRING
|
||||
|
||||
#include <cstdarg>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "gcc.h"
|
||||
|
||||
template <size_t N> size_t const_strlen(const char (&)[N]) {
|
||||
return N-1;
|
||||
}
|
||||
|
||||
template <size_t N, typename T> struct print { };
|
||||
template <size_t N> struct print<N, std::string> {
|
||||
static std::string apply(const char *format, ...) GNUC_PRINTF(1, 2) {
|
||||
char buf[N];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf)/sizeof(char), format, args);
|
||||
va_end(args);
|
||||
return buf;
|
||||
}
|
||||
};
|
||||
template <size_t N> struct print<N, std::wstring> {
|
||||
static std::wstring apply(const wchar_t *format, ...) {
|
||||
wchar_t buf[N];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vswprintf(buf, sizeof(buf)/sizeof(wchar_t), format, args);
|
||||
va_end(args);
|
||||
return buf;
|
||||
}
|
||||
};
|
||||
|
||||
int stringToInt(const std::string &s);
|
||||
long stringToLongInt(const std::string &s);
|
||||
bool isInteger(const char *s);
|
||||
|
||||
std::string ToString(const std::wstring &ws);
|
||||
std::wstring ToWString(const std::string &s);
|
||||
|
||||
std::vector<std::string> split(const std::string &s, const std::string &delimiter);
|
||||
void replace(std::string &s, const std::string &from, const std::string &to);
|
||||
@@ -38,6 +73,6 @@ std::string getSharedDirectory(const std::string &dir1, const std::string &dir2)
|
||||
|
||||
std::string getEnclosedString(const std::string &s, char a, char b, size_t *pos);
|
||||
|
||||
bool isInteger(const char *s);
|
||||
void removeInvalidCharsFromFilename(std::string &filename);
|
||||
|
||||
#endif // _UTILITY_STRING
|
||||
|
||||
157
src/utility/type_conversions.cpp
Normal file
157
src/utility/type_conversions.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/***************************************************************************
|
||||
* 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 <cassert>
|
||||
#include "utility/type_conversions.h"
|
||||
|
||||
std::string tagTypeToString(mpd_tag_type tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case MPD_TAG_ARTIST:
|
||||
return "Artist";
|
||||
case MPD_TAG_ALBUM:
|
||||
return "Album";
|
||||
case MPD_TAG_ALBUM_ARTIST:
|
||||
return "Album Artist";
|
||||
case MPD_TAG_TITLE:
|
||||
return "Title";
|
||||
case MPD_TAG_TRACK:
|
||||
return "Track";
|
||||
case MPD_TAG_GENRE:
|
||||
return "Genre";
|
||||
case MPD_TAG_DATE:
|
||||
return "Date";
|
||||
case MPD_TAG_COMPOSER:
|
||||
return "Composer";
|
||||
case MPD_TAG_PERFORMER:
|
||||
return "Performer";
|
||||
case MPD_TAG_COMMENT:
|
||||
return "Comment";
|
||||
case MPD_TAG_DISC:
|
||||
return "Disc";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
MPD::MutableSong::SetFunction tagTypeToSetFunction(mpd_tag_type tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case MPD_TAG_ARTIST:
|
||||
return &MPD::MutableSong::setArtist;
|
||||
case MPD_TAG_ALBUM:
|
||||
return &MPD::MutableSong::setAlbum;
|
||||
case MPD_TAG_ALBUM_ARTIST:
|
||||
return &MPD::MutableSong::setAlbumArtist;
|
||||
case MPD_TAG_TITLE:
|
||||
return &MPD::MutableSong::setTitle;
|
||||
case MPD_TAG_TRACK:
|
||||
return &MPD::MutableSong::setTrack;
|
||||
case MPD_TAG_GENRE:
|
||||
return &MPD::MutableSong::setGenre;
|
||||
case MPD_TAG_DATE:
|
||||
return &MPD::MutableSong::setDate;
|
||||
case MPD_TAG_COMPOSER:
|
||||
return &MPD::MutableSong::setComposer;
|
||||
case MPD_TAG_PERFORMER:
|
||||
return &MPD::MutableSong::setPerformer;
|
||||
case MPD_TAG_COMMENT:
|
||||
return &MPD::MutableSong::setComment;
|
||||
case MPD_TAG_DISC:
|
||||
return &MPD::MutableSong::setDisc;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
mpd_tag_type charToTagType(char c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'a':
|
||||
return MPD_TAG_ARTIST;
|
||||
case 'A':
|
||||
return MPD_TAG_ALBUM_ARTIST;
|
||||
case 't':
|
||||
return MPD_TAG_TITLE;
|
||||
case 'b':
|
||||
return MPD_TAG_ALBUM;
|
||||
case 'y':
|
||||
return MPD_TAG_DATE;
|
||||
case 'n':
|
||||
return MPD_TAG_TRACK;
|
||||
case 'g':
|
||||
return MPD_TAG_GENRE;
|
||||
case 'c':
|
||||
return MPD_TAG_COMPOSER;
|
||||
case 'p':
|
||||
return MPD_TAG_PERFORMER;
|
||||
case 'd':
|
||||
return MPD_TAG_DISC;
|
||||
case 'C':
|
||||
return MPD_TAG_COMMENT;
|
||||
default:
|
||||
assert(false);
|
||||
return MPD_TAG_ARTIST;
|
||||
}
|
||||
}
|
||||
|
||||
MPD::Song::GetFunction charToGetFunction(char c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'l':
|
||||
return &MPD::Song::getLength;
|
||||
case 'D':
|
||||
return &MPD::Song::getDirectory;
|
||||
case 'f':
|
||||
return &MPD::Song::getName;
|
||||
case 'a':
|
||||
return &MPD::Song::getArtist;
|
||||
case 'A':
|
||||
return &MPD::Song::getAlbumArtist;
|
||||
case 't':
|
||||
return &MPD::Song::getTitle;
|
||||
case 'b':
|
||||
return &MPD::Song::getAlbum;
|
||||
case 'y':
|
||||
return &MPD::Song::getDate;
|
||||
case 'n':
|
||||
return &MPD::Song::getTrackNumber;
|
||||
case 'N':
|
||||
return &MPD::Song::getTrack;
|
||||
case 'g':
|
||||
return &MPD::Song::getGenre;
|
||||
case 'c':
|
||||
return &MPD::Song::getComposer;
|
||||
case 'p':
|
||||
return &MPD::Song::getPerformer;
|
||||
case 'd':
|
||||
return &MPD::Song::getDisc;
|
||||
case 'C':
|
||||
return &MPD::Song::getComment;
|
||||
case 'P':
|
||||
return &MPD::Song::getPriority;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
32
src/utility/type_conversions.h
Normal file
32
src/utility/type_conversions.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/***************************************************************************
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _UTILITY_TYPE_CONVERSIONS
|
||||
#define _UTILITY_TYPE_CONVERSIONS
|
||||
|
||||
#include "mutable_song.h"
|
||||
|
||||
std::string tagTypeToString(mpd_tag_type tag);
|
||||
MPD::MutableSong::SetFunction tagTypeToSetFunction(mpd_tag_type tag);
|
||||
|
||||
mpd_tag_type charToTagType(char c);
|
||||
MPD::Song::GetFunction charToGetFunction(char c);
|
||||
|
||||
#endif // _UTILITY_TYPE_CONVERSIONS
|
||||
Reference in New Issue
Block a user