1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
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
;
43 std::string
IntoStr(mpd_tag_type tag
) // this is only for left column's title in media library
59 case MPD_TAG_COMPOSER
:
61 case MPD_TAG_PERFORMER
:
72 std::string
IntoStr(NCurses::Color color
)
76 if (color
== NCurses::clDefault
)
78 else if (color
== NCurses::clBlack
)
80 else if (color
== NCurses::clRed
)
82 else if (color
== NCurses::clGreen
)
84 else if (color
== NCurses::clYellow
)
86 else if (color
== NCurses::clBlue
)
88 else if (color
== NCurses::clMagenta
)
90 else if (color
== NCurses::clCyan
)
92 else if (color
== NCurses::clWhite
)
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
;
122 mpd_tag_type
IntoTagItem(char c
)
127 return MPD_TAG_ARTIST
;
129 return MPD_TAG_ALBUM
;
133 return MPD_TAG_GENRE
;
135 return MPD_TAG_COMPOSER
;
137 return MPD_TAG_PERFORMER
;
139 return MPD_TAG_ARTIST
;
144 MPD::Song::SetFunction
IntoSetFunction(mpd_tag_type tag
)
149 return &MPD::Song::SetArtist
;
151 return &MPD::Song::SetAlbum
;
153 return &MPD::Song::SetTitle
;
155 return &MPD::Song::SetTrack
;
157 return &MPD::Song::SetGenre
;
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
;
167 return &MPD::Song::SetDisc
;
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
)
180 std::basic_string
<my_char_t
> result(s
, 0, max_length
/2-1);
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
)
196 s
.erase(s
.begin()+i
);
203 void EscapeHtml(std::string
&s
)
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
, "'", "'");
212 Replace(s
, """, "\"");
213 Replace(s
, "&", "&");
214 for (size_t i
= 0; i
< s
.length(); ++i
)
218 s
.erase(s
.begin()+i
);
221 if (s
[i
] == 13) // ascii code for windows line ending, get rid of this shit
226 else if (s
[i
] == '\t')
231 void Trim(std::string
&s
)
237 size_t e
= s
.length()-1;
239 while (s
[b
] == ' ' || s
[b
] == '\n')
241 while (s
[e
] == ' ' || s
[e
] == '\n')
245 if (b
!= 0 || e
!= s
.length()-1)
246 s
= s
.substr(b
, e
-b
);