add support for adding random artists/albums to playlist
[ncmpcpp.git] / src / conv.cpp
blobb71c65e637ad83b603e8125d6ed43b3661fa0f64
1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <algorithm>
22 #include <sstream>
24 #include "conv.h"
26 int StrToInt(const std::string &str)
28 return atoi(str.c_str());
31 long StrToLong(const std::string &str)
33 return atol(str.c_str());
36 std::string IntoStr(int l)
38 std::ostringstream ss;
39 ss << l;
40 return ss.str();
43 std::string IntoStr(mpd_tag_type tag) // this is only for left column's title in media library
45 switch (tag)
47 case MPD_TAG_ARTIST:
48 return "Artist";
49 case MPD_TAG_ALBUM:
50 return "Album";
51 case MPD_TAG_TITLE:
52 return "Title";
53 case MPD_TAG_TRACK:
54 return "Track";
55 case MPD_TAG_GENRE:
56 return "Genre";
57 case MPD_TAG_DATE:
58 return "Year";
59 case MPD_TAG_COMPOSER:
60 return "Composer";
61 case MPD_TAG_PERFORMER:
62 return "Performer";
63 case MPD_TAG_COMMENT:
64 return "Comment";
65 case MPD_TAG_DISC:
66 return "Disc";
67 default:
68 return "";
72 std::string IntoStr(NCurses::Color color)
74 std::string result;
76 if (color == NCurses::clDefault)
77 result = "default";
78 else if (color == NCurses::clBlack)
79 result = "black";
80 else if (color == NCurses::clRed)
81 result = "red";
82 else if (color == NCurses::clGreen)
83 result = "green";
84 else if (color == NCurses::clYellow)
85 result = "yellow";
86 else if (color == NCurses::clBlue)
87 result = "blue";
88 else if (color == NCurses::clMagenta)
89 result = "magenta";
90 else if (color == NCurses::clCyan)
91 result = "cyan";
92 else if (color == NCurses::clWhite)
93 result = "white";
95 return result;
98 NCurses::Color IntoColor(const std::string &color)
100 NCurses::Color result = NCurses::clDefault;
102 if (color == "black")
103 result = NCurses::clBlack;
104 else if (color == "red")
105 result = NCurses::clRed;
106 else if (color == "green")
107 result = NCurses::clGreen;
108 else if (color == "yellow")
109 result = NCurses::clYellow;
110 else if (color == "blue")
111 result = NCurses::clBlue;
112 else if (color == "magenta")
113 result = NCurses::clMagenta;
114 else if (color == "cyan")
115 result = NCurses::clCyan;
116 else if (color == "white")
117 result = NCurses::clWhite;
119 return result;
122 mpd_tag_type IntoTagItem(char c)
124 switch (c)
126 case 'a':
127 return MPD_TAG_ARTIST;
128 case 'b':
129 return MPD_TAG_ALBUM;
130 case 'y':
131 return MPD_TAG_DATE;
132 case 'g':
133 return MPD_TAG_GENRE;
134 case 'c':
135 return MPD_TAG_COMPOSER;
136 case 'p':
137 return MPD_TAG_PERFORMER;
138 default:
139 return MPD_TAG_ARTIST;
143 #ifdef HAVE_TAGLIB_H
144 MPD::Song::SetFunction IntoSetFunction(mpd_tag_type tag)
146 switch (tag)
148 case MPD_TAG_ARTIST:
149 return &MPD::Song::SetArtist;
150 case MPD_TAG_ALBUM:
151 return &MPD::Song::SetAlbum;
152 case MPD_TAG_TITLE:
153 return &MPD::Song::SetTitle;
154 case MPD_TAG_TRACK:
155 return &MPD::Song::SetTrack;
156 case MPD_TAG_GENRE:
157 return &MPD::Song::SetGenre;
158 case MPD_TAG_DATE:
159 return &MPD::Song::SetDate;
160 case MPD_TAG_COMPOSER:
161 return &MPD::Song::SetComposer;
162 case MPD_TAG_PERFORMER:
163 return &MPD::Song::SetPerformer;
164 case MPD_TAG_COMMENT:
165 return &MPD::Song::SetComment;
166 case MPD_TAG_DISC:
167 return &MPD::Song::SetDisc;
168 default:
169 return 0;
172 #endif // HAVE_TAGLIB_H
174 std::string Shorten(const std::basic_string<my_char_t> &s, size_t max_length)
176 if (s.length() <= max_length)
177 return TO_STRING(s);
178 if (max_length < 2)
179 return "";
180 std::basic_string<my_char_t> result(s, 0, max_length/2-1);
181 result += U("..");
182 result += s.substr(s.length()-max_length/2+1);
183 return TO_STRING(result);
186 void EscapeUnallowedChars(std::string &s)
188 static const std::string unallowed_chars = "\"*/:<>?\\|";
190 for (std::string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); ++it)
192 for (size_t i = 0; i < s.length(); ++i)
194 if (s[i] == *it)
196 s.erase(s.begin()+i);
197 i--;
203 void EscapeHtml(std::string &s)
205 bool erase = 0;
206 for (size_t i = s.find("<"); i != std::string::npos; i = s.find("<"))
208 size_t j = s.find(">")+1;
209 s.replace(i, j-i, "");
211 Replace(s, "&#039;", "'");
212 Replace(s, "&quot;", "\"");
213 Replace(s, "&amp;", "&");
214 for (size_t i = 0; i < s.length(); ++i)
216 if (erase)
218 s.erase(s.begin()+i);
219 erase = 0;
221 if (s[i] == 13) // ascii code for windows line ending, get rid of this shit
223 s[i] = '\n';
224 erase = 1;
226 else if (s[i] == '\t')
227 s[i] = ' ';
231 void Trim(std::string &s)
233 if (s.empty())
234 return;
236 size_t b = 0;
237 size_t e = s.length()-1;
239 while (s[b] == ' ' || s[b] == '\n')
240 ++b;
241 while (s[e] == ' ' || s[e] == '\n')
242 --e;
243 ++e;
245 if (b != 0 || e != s.length()-1)
246 s = s.substr(b, e-b);