move code responsible for screen resize to SIGWINCH handler
[ncmpcpp.git] / src / conv.cpp
blobf48bbe1495fc9c0286b6c10ef8d0db5787bf4d86
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 void ToLower(std::string &s)
28 transform(s.begin(), s.end(), s.begin(), tolower);
31 int StrToInt(const std::string &str)
33 return atoi(str.c_str());
36 long StrToLong(const std::string &str)
38 return atol(str.c_str());
41 std::string IntoStr(int l)
43 std::ostringstream ss;
44 ss << l;
45 return ss.str();
48 std::string IntoStr(mpd_tag_type tag) // this is only for left column's title in media library
50 switch (tag)
52 case MPD_TAG_ARTIST:
53 return "Artist";
54 case MPD_TAG_ALBUM:
55 return "Album";
56 case MPD_TAG_TITLE:
57 return "Title";
58 case MPD_TAG_TRACK:
59 return "Track";
60 case MPD_TAG_GENRE:
61 return "Genre";
62 case MPD_TAG_DATE:
63 return "Year";
64 case MPD_TAG_COMPOSER:
65 return "Composer";
66 case MPD_TAG_PERFORMER:
67 return "Performer";
68 case MPD_TAG_COMMENT:
69 return "Comment";
70 case MPD_TAG_DISC:
71 return "Disc";
72 default:
73 return "";
77 std::string IntoStr(NCurses::Color color)
79 std::string result;
81 if (color == NCurses::clDefault)
82 result = "default";
83 else if (color == NCurses::clBlack)
84 result = "black";
85 else if (color == NCurses::clRed)
86 result = "red";
87 else if (color == NCurses::clGreen)
88 result = "green";
89 else if (color == NCurses::clYellow)
90 result = "yellow";
91 else if (color == NCurses::clBlue)
92 result = "blue";
93 else if (color == NCurses::clMagenta)
94 result = "magenta";
95 else if (color == NCurses::clCyan)
96 result = "cyan";
97 else if (color == NCurses::clWhite)
98 result = "white";
100 return result;
103 NCurses::Color IntoColor(const std::string &color)
105 NCurses::Color result = NCurses::clDefault;
107 if (color == "black")
108 result = NCurses::clBlack;
109 else if (color == "red")
110 result = NCurses::clRed;
111 else if (color == "green")
112 result = NCurses::clGreen;
113 else if (color == "yellow")
114 result = NCurses::clYellow;
115 else if (color == "blue")
116 result = NCurses::clBlue;
117 else if (color == "magenta")
118 result = NCurses::clMagenta;
119 else if (color == "cyan")
120 result = NCurses::clCyan;
121 else if (color == "white")
122 result = NCurses::clWhite;
124 return result;
127 mpd_tag_type IntoTagItem(char c)
129 switch (c)
131 case 'a':
132 return MPD_TAG_ARTIST;
133 case 'y':
134 return MPD_TAG_DATE;
135 case 'g':
136 return MPD_TAG_GENRE;
137 case 'c':
138 return MPD_TAG_COMPOSER;
139 case 'p':
140 return MPD_TAG_PERFORMER;
141 default:
142 return MPD_TAG_ARTIST;
146 #ifdef HAVE_TAGLIB_H
147 MPD::Song::SetFunction IntoSetFunction(mpd_tag_type tag)
149 switch (tag)
151 case MPD_TAG_ARTIST:
152 return &MPD::Song::SetArtist;
153 case MPD_TAG_ALBUM:
154 return &MPD::Song::SetAlbum;
155 case MPD_TAG_TITLE:
156 return &MPD::Song::SetTitle;
157 case MPD_TAG_TRACK:
158 return &MPD::Song::SetTrack;
159 case MPD_TAG_GENRE:
160 return &MPD::Song::SetGenre;
161 case MPD_TAG_DATE:
162 return &MPD::Song::SetDate;
163 case MPD_TAG_COMPOSER:
164 return &MPD::Song::SetComposer;
165 case MPD_TAG_PERFORMER:
166 return &MPD::Song::SetPerformer;
167 case MPD_TAG_COMMENT:
168 return &MPD::Song::SetComment;
169 case MPD_TAG_DISC:
170 return &MPD::Song::SetDisc;
171 default:
172 return 0;
175 #endif // HAVE_TAGLIB_H
177 std::string Shorten(const std::basic_string<my_char_t> &s, size_t max_length)
179 if (s.length() <= max_length)
180 return TO_STRING(s);
181 if (max_length < 2)
182 return "";
183 std::basic_string<my_char_t> result(s, 0, max_length/2-1);
184 result += U("..");
185 result += s.substr(s.length()-max_length/2+1);
186 return TO_STRING(result);
189 void EscapeUnallowedChars(std::string &s)
191 static const std::string unallowed_chars = "\"*/:<>?\\|";
193 for (std::string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); ++it)
195 for (size_t i = 0; i < s.length(); ++i)
197 if (s[i] == *it)
199 s.erase(s.begin()+i);
200 i--;
206 void EscapeHtml(std::string &s)
208 bool erase = 0;
209 for (size_t i = s.find("<"); i != std::string::npos; i = s.find("<"))
211 size_t j = s.find(">")+1;
212 s.replace(i, j-i, "");
214 Replace(s, "&#039;", "'");
215 Replace(s, "&quot;", "\"");
216 Replace(s, "&amp;", "&");
217 for (size_t i = 0; i < s.length(); ++i)
219 if (erase)
221 s.erase(s.begin()+i);
222 erase = 0;
224 if (s[i] == 13) // ascii code for windows line ending, get rid of this shit
226 s[i] = '\n';
227 erase = 1;
229 else if (s[i] == '\t')
230 s[i] = ' ';
234 void Trim(std::string &s)
236 if (s.empty())
237 return;
239 size_t b = 0;
240 size_t e = s.length()-1;
242 while (s[b] == ' ' || s[b] == '\n')
243 ++b;
244 while (s[e] == ' ' || s[e] == '\n')
245 --e;
246 ++e;
248 if (b != 0 || e != s.length()-1)
249 s = s.substr(b, e-b);